1 /*
2 * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
3 * Authors:
4 * PutinLee <putin.lee@rock-chips.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19
20 #ifndef _LIBS_RGA_MUTEX_H
21 #define _LIBS_RGA_MUTEX_H
22
23 #include <stdint.h>
24 #include <sys/types.h>
25 #include <time.h>
26
27 #include <pthread.h>
28
29
30 // Enable thread safety attributes only with clang.
31 // The attributes can be safely erased when compiling with other compilers.
32 #if defined(__clang__) && (!defined(SWIG))
33 #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
34 #else
35 #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
36 #endif
37
38 #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
39
40 #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
41
42 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
43
44 #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
45
46 #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
47
48 #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
49
50 #define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
51
52 #define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
53
54 #define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
55
56 #define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
57
58 #define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
59
60 #define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
61
62 #define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
63
64 #define TRY_ACQUIRE_SHARED(...) \
65 THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
66
67 #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
68
69 #define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
70
71 #define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
72
73 #define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
74
75 #define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
76
77 class Condition;
78
79 /*
80 * NOTE: This class is for code that builds on Win32. Its usage is
81 * deprecated for code which doesn't build for Win32. New code which
82 * doesn't build for Win32 should use std::mutex and std::lock_guard instead.
83 *
84 * Simple mutex class. The implementation is system-dependent.
85 *
86 * The mutex must be unlocked by the thread that locked it. They are not
87 * recursive, i.e. the same thread can't lock it multiple times.
88 */
89 class CAPABILITY("mutex") Mutex {
90 public:
91 enum {
92 PRIVATE = 0,
93 SHARED = 1
94 };
95
96 Mutex();
97 explicit Mutex(const char* name);
98 explicit Mutex(int type, const char* name = nullptr);
99 ~Mutex();
100
101 // lock or unlock the mutex
102 int32_t lock() ACQUIRE();
103 void unlock() RELEASE();
104
105 // lock if possible; returns 0 on success, error otherwise
106 int32_t tryLock() TRY_ACQUIRE(0);
107
108 int32_t timedLock(int64_t timeoutNs) TRY_ACQUIRE(0);
109
110 // Manages the mutex automatically. It'll be locked when Autolock is
111 // constructed and released when Autolock goes out of scope.
112 class SCOPED_CAPABILITY Autolock {
113 public:
Autolock(Mutex & mutex)114 inline explicit Autolock(Mutex& mutex) ACQUIRE(mutex) : mLock(mutex)
115 {
116 mLock.lock();
117 }
Autolock(Mutex * mutex)118 inline explicit Autolock(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex)
119 {
120 mLock.lock();
121 }
RELEASE()122 inline ~Autolock() RELEASE()
123 {
124 mLock.unlock();
125 }
126
127 private:
128 Mutex& mLock;
129 // Cannot be copied or moved - declarations only
130 Autolock(const Autolock&);
131 Autolock& operator=(const Autolock&);
132 };
133
134 private:
135 friend class Condition;
136
137 // A mutex cannot be copied
138 Mutex(const Mutex&);
139 Mutex& operator=(const Mutex&);
140
141 pthread_mutex_t mMutex;
142 };
143
144 // ---------------------------------------------------------------------------
Mutex()145 inline Mutex::Mutex()
146 {
147 pthread_mutex_init(&mMutex, nullptr);
148 }
Mutex(const char * name)149 inline Mutex::Mutex(__attribute__((unused)) const char* name)
150 {
151 pthread_mutex_init(&mMutex, nullptr);
152 }
Mutex(int type,const char * name)153 inline Mutex::Mutex(int type, __attribute__((unused)) const char* name)
154 {
155 if (type == SHARED) {
156 pthread_mutexattr_t attr;
157 pthread_mutexattr_init(&attr);
158 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
159 pthread_mutex_init(&mMutex, &attr);
160 pthread_mutexattr_destroy(&attr);
161 } else {
162 pthread_mutex_init(&mMutex, nullptr);
163 }
164 }
~Mutex()165 inline Mutex::~Mutex()
166 {
167 pthread_mutex_destroy(&mMutex);
168 }
lock()169 inline int32_t Mutex::lock()
170 {
171 return -pthread_mutex_lock(&mMutex);
172 }
unlock()173 inline void Mutex::unlock()
174 {
175 pthread_mutex_unlock(&mMutex);
176 }
tryLock()177 inline int32_t Mutex::tryLock()
178 {
179 return -pthread_mutex_trylock(&mMutex);
180 }
timedLock(int64_t timeoutNs)181 inline int32_t Mutex::timedLock(int64_t timeoutNs)
182 {
183 timespec now;
184 clock_gettime(CLOCK_REALTIME, &now);
185 timeoutNs += now.tv_sec * 1000000000 + now.tv_nsec; // 1000000000:unit conversion
186 const struct timespec ts = {
187 static_cast<time_t>(timeoutNs / 1000000000), // 1000000000:unit conversion
188 static_cast<long>(timeoutNs % 1000000000), // 1000000000:unit conversion
189 };
190 return -pthread_mutex_timedlock(&mMutex, &ts);
191 }
192
193 // ---------------------------------------------------------------------------
194
195 /*
196 * Automatic mutex. Declare one of these at the top of a function.
197 * When the function returns, it will go out of scope, and release the
198 * mutex.
199 */
200
201 typedef Mutex::Autolock AutoMutex;
202 #endif // _LIBS_RGA_MUTEX_H
203