• 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 #ifndef ANDROID
24 #include <stdint.h>
25 #include <sys/types.h>
26 #include <time.h>
27 
28 #include <pthread.h>
29 
30 
31 // Enable thread safety attributes only with clang.
32 // The attributes can be safely erased when compiling with other compilers.
33 #if defined(__clang__) && (!defined(SWIG))
34 #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
35 #else
36 #define THREAD_ANNOTATION_ATTRIBUTE__(x)  // no-op
37 #endif
38 
39 #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
40 
41 #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
42 
43 #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
44 
45 #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
46 
47 #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
48 
49 #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
50 
51 #define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
52 
53 #define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
54 
55 #define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
56 
57 #define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
58 
59 #define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
60 
61 #define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
62 
63 #define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
64 
65 #define TRY_ACQUIRE_SHARED(...) \
66     THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
67 
68 #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
69 
70 #define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
71 
72 #define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
73 
74 #define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
75 
76 #define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
77 
78 class Condition;
79 
80 /*
81  * NOTE: This class is for code that builds on Win32.  Its usage is
82  * deprecated for code which doesn't build for Win32.  New code which
83  * doesn't build for Win32 should use std::mutex and std::lock_guard instead.
84  *
85  * Simple mutex class.  The implementation is system-dependent.
86  *
87  * The mutex must be unlocked by the thread that locked it.  They are not
88  * recursive, i.e. the same thread can't lock it multiple times.
89  */
90 class CAPABILITY("mutex") Mutex {
91   public:
92     enum {
93         PRIVATE = 0,
94         SHARED = 1
95     };
96 
97     Mutex();
98     explicit Mutex(const char* name);
99     explicit Mutex(int type, const char* name = nullptr);
100     ~Mutex();
101 
102     // lock or unlock the mutex
103     int32_t lock() ACQUIRE();
104     void unlock() RELEASE();
105 
106     // lock if possible; returns 0 on success, error otherwise
107     int32_t tryLock() TRY_ACQUIRE(0);
108 
109     int32_t timedLock(int64_t timeoutNs) TRY_ACQUIRE(0);
110 
111     // Manages the mutex automatically. It'll be locked when Autolock is
112     // constructed and released when Autolock goes out of scope.
113     class SCOPED_CAPABILITY Autolock {
114       public:
Autolock(Mutex & mutex)115         inline explicit Autolock(Mutex& mutex) ACQUIRE(mutex) : mLock(mutex) {
116             mLock.lock();
117         }
Autolock(Mutex * mutex)118         inline explicit Autolock(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex) {
119             mLock.lock();
120         }
RELEASE()121         inline ~Autolock() RELEASE() {
122             mLock.unlock();
123         }
124 
125       private:
126         Mutex& mLock;
127         // Cannot be copied or moved - declarations only
128         Autolock(const Autolock&);
129         Autolock& operator=(const Autolock&);
130     };
131 
132   private:
133     friend class Condition;
134 
135     // A mutex cannot be copied
136     Mutex(const Mutex&);
137     Mutex& operator=(const Mutex&);
138 
139     pthread_mutex_t mMutex;
140 };
141 
142 // ---------------------------------------------------------------------------
Mutex()143 inline Mutex::Mutex() {
144     pthread_mutex_init(&mMutex, nullptr);
145 }
Mutex(const char * name)146 inline Mutex::Mutex(__attribute__((unused)) const char* name) {
147     pthread_mutex_init(&mMutex, nullptr);
148 }
Mutex(int type,const char * name)149 inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) {
150     if (type == SHARED) {
151         pthread_mutexattr_t attr;
152         pthread_mutexattr_init(&attr);
153         pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
154         pthread_mutex_init(&mMutex, &attr);
155         pthread_mutexattr_destroy(&attr);
156     } else {
157         pthread_mutex_init(&mMutex, nullptr);
158     }
159 }
~Mutex()160 inline Mutex::~Mutex() {
161     pthread_mutex_destroy(&mMutex);
162 }
lock()163 inline int32_t Mutex::lock() {
164     return -pthread_mutex_lock(&mMutex);
165 }
unlock()166 inline void Mutex::unlock() {
167     pthread_mutex_unlock(&mMutex);
168 }
tryLock()169 inline int32_t Mutex::tryLock() {
170     return -pthread_mutex_trylock(&mMutex);
171 }
timedLock(int64_t timeoutNs)172 inline int32_t Mutex::timedLock(int64_t timeoutNs) {
173     timespec now;
174     clock_gettime(CLOCK_REALTIME, &now);
175     timeoutNs += now.tv_sec*1000000000 + now.tv_nsec;
176     const struct timespec ts = {
177         /* .tv_sec = */ static_cast<time_t>(timeoutNs / 1000000000),
178         /* .tv_nsec = */ static_cast<long>(timeoutNs % 1000000000),
179     };
180     return -pthread_mutex_timedlock(&mMutex, &ts);
181 }
182 
183 // ---------------------------------------------------------------------------
184 
185 /*
186  * Automatic mutex.  Declare one of these at the top of a function.
187  * When the function returns, it will go out of scope, and release the
188  * mutex.
189  */
190 
191 typedef Mutex::Autolock AutoMutex;
192 #endif // __ANDROID_VNDK__
193 #endif // _LIBS_RGA_MUTEX_H
194