• 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  * @file mutex.h
18  *
19  * @brief Declares the mutex interfaces in C++.
20  *
21  * @since 10
22  * @version 1.0
23  */
24 #ifndef FFRT_API_CPP_MUTEX_H
25 #define FFRT_API_CPP_MUTEX_H
26 #include "../c/mutex.h"
27 
28 namespace ffrt {
29 class mutex : public ffrt_mutex_t {
30 public:
mutex()31     mutex()
32     {
33         ffrt_mutex_init(this, nullptr);
34     }
35 
~mutex()36     ~mutex()
37     {
38         ffrt_mutex_destroy(this);
39     }
40 
41     mutex(mutex const&) = delete;
42     void operator=(mutex const&) = delete;
43 
try_lock()44     inline bool try_lock()
45     {
46         return ffrt_mutex_trylock(this) == ffrt_success ? true : false;
47     }
48 
lock()49     inline void lock()
50     {
51         ffrt_mutex_lock(this);
52     }
53 
unlock()54     inline void unlock()
55     {
56         ffrt_mutex_unlock(this);
57     }
58 };
59 
60 class recursive_mutex : public ffrt_mutex_t {
61 public:
recursive_mutex()62     recursive_mutex()
63     {
64         ffrt_recursive_mutex_init(this, nullptr);
65     }
66 
~recursive_mutex()67     ~recursive_mutex()
68     {
69         ffrt_recursive_mutex_destroy(this);
70     }
71 
72     recursive_mutex(recursive_mutex const&) = delete;
73     void operator=(recursive_mutex const&) = delete;
74 
try_lock()75     inline bool try_lock()
76     {
77         return ffrt_recursive_mutex_trylock(this) == ffrt_success ? true : false;
78     }
79 
lock()80     inline void lock()
81     {
82         ffrt_recursive_mutex_lock(this);
83     }
84 
unlock()85     inline void unlock()
86     {
87         ffrt_recursive_mutex_unlock(this);
88     }
89 };
90 } // namespace ffrt
91 #endif
92