RgaMutex.h 6.1 KB

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