• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             mLock.lock();
116         }
Autolock(Mutex * mutex)117         inline explicit Autolock(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex) {
118             mLock.lock();
119         }
RELEASE()120         inline ~Autolock() RELEASE() {
121             mLock.unlock();
122         }
123 
124       private:
125         Mutex& mLock;
126         // Cannot be copied or moved - declarations only
127         Autolock(const Autolock&);
128         Autolock& operator=(const Autolock&);
129     };
130 
131   private:
132     friend class Condition;
133 
134     // A mutex cannot be copied
135     Mutex(const Mutex&);
136     Mutex& operator=(const Mutex&);
137 
138     pthread_mutex_t mMutex;
139 };
140 
141 // ---------------------------------------------------------------------------
Mutex()142 inline Mutex::Mutex() {
143     pthread_mutex_init(&mMutex, nullptr);
144 }
Mutex(const char * name)145 inline Mutex::Mutex(__attribute__((unused)) const char* name) {
146     pthread_mutex_init(&mMutex, nullptr);
147 }
Mutex(int type,const char * name)148 inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) {
149     if (type == SHARED) {
150         pthread_mutexattr_t attr;
151         pthread_mutexattr_init(&attr);
152         pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
153         pthread_mutex_init(&mMutex, &attr);
154         pthread_mutexattr_destroy(&attr);
155     } else {
156         pthread_mutex_init(&mMutex, nullptr);
157     }
158 }
~Mutex()159 inline Mutex::~Mutex() {
160     pthread_mutex_destroy(&mMutex);
161 }
lock()162 inline int32_t Mutex::lock() {
163     return -pthread_mutex_lock(&mMutex);
164 }
unlock()165 inline void Mutex::unlock() {
166     pthread_mutex_unlock(&mMutex);
167 }
tryLock()168 inline int32_t Mutex::tryLock() {
169     return -pthread_mutex_trylock(&mMutex);
170 }
timedLock(int64_t timeoutNs)171 inline int32_t Mutex::timedLock(int64_t timeoutNs) {
172     timespec now;
173     clock_gettime(CLOCK_REALTIME, &now);
174     timeoutNs += now.tv_sec*1000000000 + now.tv_nsec; // 1000000000:unit conversion
175     const struct timespec ts = {
176         static_cast<time_t>(timeoutNs / 1000000000), // 1000000000:unit conversion
177         static_cast<long>(timeoutNs % 1000000000), // 1000000000:unit conversion
178     };
179     return -pthread_mutex_timedlock(&mMutex, &ts);
180 }
181 
182 // ---------------------------------------------------------------------------
183 
184 /*
185  * Automatic mutex.  Declare one of these at the top of a function.
186  * When the function returns, it will go out of scope, and release the
187  * mutex.
188  */
189 
190 typedef Mutex::Autolock AutoMutex;
191 #endif // _LIBS_RGA_MUTEX_H