fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 Amlogic Corporation. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #ifndef _TOOS_MUTEX_H_ |
| 17 | #define _TOOS_MUTEX_H_ |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <time.h> |
| 22 | #include <pthread.h> |
| 23 | |
| 24 | namespace Tls { |
| 25 | |
| 26 | // Enable thread safety attributes only with clang. |
| 27 | // The attributes can be safely erased when compiling with other compilers. |
| 28 | #if defined(__clang__) && (!defined(SWIG)) |
| 29 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) |
| 30 | #else |
| 31 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op |
| 32 | #endif |
| 33 | |
| 34 | #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) |
| 35 | |
| 36 | #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) |
| 37 | |
| 38 | #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) |
| 39 | |
| 40 | #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) |
| 41 | |
| 42 | #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) |
| 43 | |
| 44 | #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) |
| 45 | |
| 46 | #define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) |
| 47 | |
| 48 | #define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) |
| 49 | |
| 50 | #define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) |
| 51 | |
| 52 | #define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) |
| 53 | |
| 54 | #define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) |
| 55 | |
| 56 | #define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) |
| 57 | |
| 58 | #define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) |
| 59 | |
| 60 | #define TRY_ACQUIRE_SHARED(...) \ |
| 61 | THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) |
| 62 | |
| 63 | #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) |
| 64 | |
| 65 | #define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) |
| 66 | |
| 67 | #define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) |
| 68 | |
| 69 | #define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) |
| 70 | |
| 71 | #define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) |
| 72 | |
| 73 | class Condition; |
| 74 | |
| 75 | /* |
| 76 | * NOTE: This class is for code that builds on Win32. Its usage is |
| 77 | * deprecated for code which doesn't build for Win32. New code which |
| 78 | * doesn't build for Win32 should use std::mutex and std::lock_guard instead. |
| 79 | * |
| 80 | * Simple mutex class. The implementation is system-dependent. |
| 81 | * |
| 82 | * The mutex must be unlocked by the thread that locked it. They are not |
| 83 | * recursive, i.e. the same thread can't lock it multiple times. |
| 84 | */ |
| 85 | class CAPABILITY("mutex") Mutex { |
| 86 | public: |
| 87 | enum { |
| 88 | PRIVATE = 0, |
| 89 | SHARED = 1 |
| 90 | }; |
| 91 | |
| 92 | Mutex(); |
| 93 | explicit Mutex(const char* name); |
| 94 | explicit Mutex(int type, const char* name = NULL); |
| 95 | ~Mutex(); |
| 96 | |
| 97 | // lock or unlock the mutex |
| 98 | int lock() ACQUIRE(); |
| 99 | void unlock() RELEASE(); |
| 100 | |
| 101 | // lock if possible; returns 0 on success, error otherwise |
| 102 | int tryLock() TRY_ACQUIRE(true); |
| 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: |
| 108 | inline explicit Autolock(Mutex& mutex) ACQUIRE(mutex) : mLock(mutex) { mLock.lock(); } |
| 109 | inline explicit Autolock(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex) { mLock.lock(); } |
| 110 | inline ~Autolock() RELEASE() { mLock.unlock(); } |
| 111 | |
| 112 | private: |
| 113 | Mutex& mLock; |
| 114 | // Cannot be copied or moved - declarations only |
| 115 | Autolock(const Autolock&); |
| 116 | Autolock& operator=(const Autolock&); |
| 117 | }; |
| 118 | |
| 119 | private: |
| 120 | friend class Condition; |
| 121 | |
| 122 | // A mutex cannot be copied |
| 123 | Mutex(const Mutex&); |
| 124 | Mutex& operator=(const Mutex&); |
| 125 | pthread_mutex_t mMutex; |
| 126 | }; |
| 127 | |
| 128 | inline Mutex::Mutex() { |
| 129 | pthread_mutex_init(&mMutex, NULL); |
| 130 | } |
| 131 | inline Mutex::Mutex(__attribute__((unused)) const char* name) { |
| 132 | pthread_mutex_init(&mMutex, NULL); |
| 133 | } |
| 134 | inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) { |
| 135 | if (type == SHARED) { |
| 136 | pthread_mutexattr_t attr; |
| 137 | pthread_mutexattr_init(&attr); |
| 138 | pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
| 139 | pthread_mutex_init(&mMutex, &attr); |
| 140 | pthread_mutexattr_destroy(&attr); |
| 141 | } else { |
| 142 | pthread_mutex_init(&mMutex, NULL); |
| 143 | } |
| 144 | } |
| 145 | inline Mutex::~Mutex() { |
| 146 | pthread_mutex_destroy(&mMutex); |
| 147 | } |
| 148 | inline int Mutex::lock() { |
| 149 | return -pthread_mutex_lock(&mMutex); |
| 150 | } |
| 151 | inline void Mutex::unlock() { |
| 152 | pthread_mutex_unlock(&mMutex); |
| 153 | } |
| 154 | inline int Mutex::tryLock() { |
| 155 | return -pthread_mutex_trylock(&mMutex); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | typedef Mutex::Autolock AutoMutex; |
| 160 | |
| 161 | } |
| 162 | #endif // _TOOS_MUTEX_H_ |
| 163 | |