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