• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup FFRT
18  * @{
19  *
20  * @brief Provides FFRT C++ APIs.
21  *
22  * @since 18
23  */
24 
25 /**
26  * @file shared_mutex.h
27  *
28  * @brief Declares the shared_mutex interfaces in C++.
29  *
30  * @library libffrt.z.so
31  * @kit FunctionFlowRuntimeKit
32  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
33  * @since 18
34  */
35 
36 #ifndef FFRT_API_CPP_SHARED_MUTEX_H
37 #define FFRT_API_CPP_SHARED_MUTEX_H
38 
39 #include "c/shared_mutex.h"
40 
41 namespace ffrt {
42 /**
43  * @class shared_mutex
44  * @brief A class for managing a shared mutex, providing synchronization mechanisms.
45  *
46  * This class offers methods to support:
47  * - Exclusive locking: Ensures only one thread can access critical sections.
48  * - Shared locking: Allows multiple threads to read simultaneously.
49  *
50  * @since 18
51  */
52 class shared_mutex : public ffrt_rwlock_t {
53 public:
54     /**
55      * @brief Constructs a shared_mutex object and initializes the underlying lock.
56      *
57      * @since 18
58      */
shared_mutex()59     shared_mutex()
60     {
61         ffrt_rwlock_init(this, nullptr);
62     }
63 
64     /**
65      * @brief Destroys the shared_mutex object and releases the underlying lock.
66      *
67      * @since 18
68      */
~shared_mutex()69     ~shared_mutex()
70     {
71         ffrt_rwlock_destroy(this);
72     }
73 
74     /**
75      * @brief Deleted copy constructor to prevent copying of the shared_mutex object.
76      */
77     shared_mutex(const shared_mutex&) = delete;
78 
79     /**
80      * @brief Deleted copy assignment operator to prevent assignment of the shared_mutex object.
81      */
82     void operator=(const shared_mutex&) = delete;
83 
84     /**
85      * @brief Acquires an exclusive lock, blocking other threads.
86      *
87      * @since 18
88      */
lock()89     inline void lock()
90     {
91         ffrt_rwlock_wrlock(this);
92     }
93 
94     /**
95      * @brief Attempts to acquire an exclusive lock without blocking.
96      *
97      * @return true if the lock is successfully acquired; false otherwise.
98      * @since 18
99      */
try_lock()100     inline bool try_lock()
101     {
102         return ffrt_rwlock_trywrlock(this) == ffrt_success ? true : false;
103     }
104 
105     /**
106      * @brief Releases the exclusive lock.
107      *
108      * @since 18
109      */
unlock()110     inline void unlock()
111     {
112         ffrt_rwlock_unlock(this);
113     }
114 
115     /**
116      * @brief Acquires a shared lock, allowing concurrent access by multiple threads.
117      *
118      * @since 18
119      */
lock_shared()120     inline void lock_shared()
121     {
122         ffrt_rwlock_rdlock(this);
123     }
124 
125     /**
126      * @brief Attempts to acquire a shared lock without blocking.
127      *
128      * @return true if the lock is successfully acquired; false otherwise.
129      * @since 18
130      */
try_lock_shared()131     inline bool try_lock_shared()
132     {
133         return ffrt_rwlock_tryrdlock(this) == ffrt_success ? true : false;
134     }
135 
136     /**
137      * @brief Releases the shared lock.
138      *
139      * @since 18
140      */
unlock_shared()141     inline void unlock_shared()
142     {
143         ffrt_rwlock_unlock(this);
144     }
145 };
146 } // namespace ffrt
147 
148 #endif // FFRT_API_CPP_SHARED_MUTEX_H
149 /** @} */