• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 10
23  */
24 
25 /**
26  * @file mutex.h
27  *
28  * @brief Declares the mutex interfaces in C++.
29  *
30  * @library libffrt.z.so
31  * @kit FunctionFlowRuntimeKit
32  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
33  * @since 10
34  */
35 
36 #ifndef FFRT_API_CPP_MUTEX_H
37 #define FFRT_API_CPP_MUTEX_H
38 
39 #include "c/mutex.h"
40 
41 namespace ffrt {
42 /**
43  * @class mutex
44  * @brief Provides a standard mutex for thread synchronization.
45  *
46  * The `mutex` class offers basic methods for locking, unlocking, and attempting
47  * to acquire a lock without blocking. It is designed for safe and efficient
48  * synchronization in multithreaded applications.
49  *
50  * @since 10
51  */
52 class mutex : public ffrt_mutex_t {
53 public:
54     /**
55      * @brief Constructs a new mutex object.
56      */
mutex()57     mutex()
58     {
59         ffrt_mutex_init(this, nullptr);
60     }
61 
62     /**
63      * @brief Destroys the mutex object.
64      */
~mutex()65     ~mutex()
66     {
67         ffrt_mutex_destroy(this);
68     }
69 
70     /**
71      * @brief Deleted copy constructor to prevent copying of the mutex object.
72      */
73     mutex(const mutex&) = delete;
74 
75     /**
76      * @brief Deleted copy assignment operator to prevent assignment of the mutex object.
77      */
78     void operator=(const mutex&) = delete;
79 
80     /**
81      * @brief Attempts to lock the mutex without blocking.
82      *
83      * @return true if the lock is successfully acquired, false otherwise.
84      * @since 10
85      */
try_lock()86     inline bool try_lock()
87     {
88         return ffrt_mutex_trylock(this) == ffrt_success ? true : false;
89     }
90 
91     /**
92      * @brief Locks the mutex.
93      *
94      * @since 10
95      */
lock()96     inline void lock()
97     {
98         ffrt_mutex_lock(this);
99     }
100 
101     /**
102      * @brief Unlocks the mutex.
103      *
104      * @since 10
105      */
unlock()106     inline void unlock()
107     {
108         ffrt_mutex_unlock(this);
109     }
110 };
111 
112 /**
113  * @class recursive_mutex
114  * @brief Provides a recursive mutex for thread synchronization.
115  *
116  * The `recursive_mutex` class allows the same thread to acquire the lock
117  * multiple times without causing a deadlock. It is particularly useful in
118  * scenarios where a function that acquires a lock may be called recursively.
119  *
120  * @since 10
121  */
122 class recursive_mutex : public ffrt_mutex_t {
123 public:
124     /**
125      * @brief Constructs a new recursive_mutex object.
126      *
127      * @since 10
128      */
recursive_mutex()129     recursive_mutex()
130     {
131         ffrt_mutexattr_init(&attr);
132         ffrt_mutexattr_settype(&attr, ffrt_mutex_recursive);
133         ffrt_mutex_init(this, &attr);
134     }
135 
136     /**
137      * @brief Destroys the recursive_mutex object.
138      *
139      * @since 10
140      */
~recursive_mutex()141     ~recursive_mutex()
142     {
143         ffrt_mutexattr_destroy(&attr);
144         ffrt_mutex_destroy(this);
145     }
146 
147     /**
148      * @brief Deleted copy constructor to prevent copying of the recursive_mutex object.
149      */
150     recursive_mutex(const recursive_mutex&) = delete;
151 
152     /**
153      * @brief Deleted copy assignment operator to prevent assignment of the recursive_mutex object.
154      */
155     void operator=(const recursive_mutex&) = delete;
156 
157     /**
158      * @brief Attempts to lock the recursive mutex without blocking.
159      *
160      * @return true if the lock is successfully acquired, false otherwise.
161      * @since 10
162      */
try_lock()163     inline bool try_lock()
164     {
165         return ffrt_mutex_trylock(this) == ffrt_success ? true : false;
166     }
167 
168     /**
169      * @brief Locks the recursive mutex.
170      *
171      * @since 10
172      */
lock()173     inline void lock()
174     {
175         ffrt_mutex_lock(this);
176     }
177 
178     /**
179      * @brief Unlocks the recursive mutex.
180      *
181      * @since 10
182      */
unlock()183     inline void unlock()
184     {
185         ffrt_mutex_unlock(this);
186     }
187 
188 private:
189     ffrt_mutexattr_t attr; ///< Mutex attribute object used to configure the recursive behavior of the mutex.
190 };
191 } // namespace ffrt
192 
193 #endif // FFRT_API_CPP_MUTEX_H
194 /** @} */