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  * @file shared_mutex.h
18  *
19  * @brief Declares the shared_mutex interfaces in C++.
20  *
21  * @version 1.0
22  */
23 
24 #ifndef FFRT_API_CPP_SHARED_MUTEX_H
25 #define FFRT_API_CPP_SHARED_MUTEX_H
26 #include "c/shared_mutex.h"
27 
28 namespace ffrt {
29 class shared_mutex : public ffrt_rwlock_t {
30 public:
shared_mutex()31     shared_mutex()
32     {
33         ffrt_rwlock_init(this, nullptr);
34     }
35 
~shared_mutex()36     ~shared_mutex()
37     {
38         ffrt_rwlock_destroy(this);
39     }
40 
41     shared_mutex(shared_mutex const&) = delete;
42     void operator=(shared_mutex const&) = delete;
43 
lock()44     inline void lock()
45     {
46         ffrt_rwlock_wrlock(this);
47     }
48 
try_lock()49     inline bool try_lock()
50     {
51         return ffrt_rwlock_trywrlock(this) == ffrt_success ? true : false;
52     }
53 
unlock()54     inline void unlock()
55     {
56         ffrt_rwlock_unlock(this);
57     }
58 
lock_shared()59     inline void lock_shared()
60     {
61         ffrt_rwlock_rdlock(this);
62     }
63 
try_lock_shared()64     inline bool try_lock_shared()
65     {
66         return ffrt_rwlock_tryrdlock(this) == ffrt_success ? true : false;
67     }
68 
unlock_shared()69     inline void unlock_shared()
70     {
71         ffrt_rwlock_unlock(this);
72     }
73 };
74 } // namespace ffrt
75 #endif
76