TimedMutex.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @file TimedMutex.hpp
  16. */
  17. #ifndef _UTILS_TIMEDMUTEX_HPP_
  18. #define _UTILS_TIMEDMUTEX_HPP_
  19. #include <chrono>
  20. #include <iostream>
  21. #if defined(_WIN32)
  22. #include <thread>
  23. extern int clock_gettime(
  24. int,
  25. struct timespec* tv);
  26. #elif _GTHREAD_USE_MUTEX_TIMEDLOCK
  27. #include <mutex>
  28. #else
  29. #include <pthread.h>
  30. #endif
  31. namespace eprosima {
  32. namespace fastrtps {
  33. #if defined(_WIN32)
  34. class TimedMutex
  35. {
  36. public:
  37. TimedMutex()
  38. {
  39. _Mtx_init(&mutex_, _Mtx_timed);
  40. }
  41. TimedMutex(
  42. const TimedMutex&) = delete;
  43. TimedMutex& operator =(
  44. const TimedMutex&) = delete;
  45. ~TimedMutex()
  46. {
  47. _Mtx_destroy(mutex_);
  48. }
  49. void lock()
  50. {
  51. _Mtx_lock(mutex_);
  52. }
  53. void unlock()
  54. {
  55. _Mtx_unlock(mutex_);
  56. }
  57. template <class Rep, class Period>
  58. bool try_lock_for(
  59. const std::chrono::duration<Rep, Period>& rel_time)
  60. {
  61. return try_lock_until(chrono::steady_clock::now() + rel_time);
  62. }
  63. template <class Clock, class Duration>
  64. bool try_lock_until(
  65. const std::chrono::time_point<Clock, Duration>& abs_time)
  66. {
  67. std::chrono::nanoseconds nsecs = abs_time - std::chrono::steady_clock::now();
  68. if (0 < nsecs.count())
  69. {
  70. struct timespec max_wait = { 0, 0 };
  71. clock_gettime(1, &max_wait);
  72. nsecs = nsecs + std::chrono::nanoseconds(max_wait.tv_nsec);
  73. auto secs = std::chrono::duration_cast<std::chrono::seconds>(nsecs);
  74. nsecs -= secs;
  75. max_wait.tv_sec += secs.count();
  76. max_wait.tv_nsec = (long)nsecs.count();
  77. return (_Thrd_success == _Mtx_timedlock(mutex_, (xtime*)&max_wait));
  78. }
  79. else
  80. {
  81. return (_Thrd_success == _Mtx_trylock(mutex_));
  82. }
  83. }
  84. void* native_handle() noexcept
  85. {
  86. return mutex_;
  87. }
  88. private:
  89. _Mtx_t mutex_;
  90. };
  91. class RecursiveTimedMutex
  92. {
  93. public:
  94. RecursiveTimedMutex()
  95. {
  96. _Mtx_init(&mutex_, _Mtx_timed | _Mtx_recursive);
  97. }
  98. RecursiveTimedMutex(
  99. const TimedMutex&) = delete;
  100. RecursiveTimedMutex& operator =(
  101. const TimedMutex&) = delete;
  102. ~RecursiveTimedMutex()
  103. {
  104. _Mtx_destroy(mutex_);
  105. }
  106. void lock()
  107. {
  108. _Mtx_lock(mutex_);
  109. }
  110. void unlock()
  111. {
  112. _Mtx_unlock(mutex_);
  113. }
  114. template <class Rep, class Period>
  115. bool try_lock_for(
  116. const std::chrono::duration<Rep, Period>& rel_time)
  117. {
  118. return try_lock_until(chrono::steady_clock::now() + rel_time);
  119. }
  120. template <class Clock, class Duration>
  121. bool try_lock_until(
  122. const std::chrono::time_point<Clock, Duration>& abs_time)
  123. {
  124. std::chrono::nanoseconds nsecs = abs_time - std::chrono::steady_clock::now();
  125. if (0 < nsecs.count())
  126. {
  127. struct timespec max_wait = { 0, 0 };
  128. clock_gettime(1, &max_wait);
  129. nsecs = nsecs + std::chrono::nanoseconds(max_wait.tv_nsec);
  130. auto secs = std::chrono::duration_cast<std::chrono::seconds>(nsecs);
  131. nsecs -= secs;
  132. max_wait.tv_sec += secs.count();
  133. max_wait.tv_nsec = (long)nsecs.count();
  134. return (_Thrd_success == _Mtx_timedlock(mutex_, (xtime*)&max_wait));
  135. }
  136. else
  137. {
  138. return (_Thrd_success == _Mtx_trylock(mutex_));
  139. }
  140. }
  141. void* native_handle() noexcept
  142. {
  143. return mutex_;
  144. }
  145. private:
  146. _Mtx_t mutex_;
  147. };
  148. #elif _GTHREAD_USE_MUTEX_TIMEDLOCK || !defined(__linux__)
  149. using TimedMutex = std::timed_mutex;
  150. using RecursiveTimedMutex = std::recursive_timed_mutex;
  151. #else
  152. class TimedMutex
  153. {
  154. public:
  155. TimedMutex()
  156. {
  157. pthread_mutex_init(&mutex_, nullptr);
  158. }
  159. TimedMutex(
  160. const TimedMutex&) = delete;
  161. TimedMutex& operator =(
  162. const TimedMutex&) = delete;
  163. ~TimedMutex()
  164. {
  165. pthread_mutex_destroy(&mutex_);
  166. }
  167. void lock()
  168. {
  169. pthread_mutex_lock(&mutex_);
  170. }
  171. void unlock()
  172. {
  173. pthread_mutex_unlock(&mutex_);
  174. }
  175. template <class Rep, class Period>
  176. bool try_lock_for(
  177. const std::chrono::duration<Rep, Period>& rel_time)
  178. {
  179. return try_lock_until(std::chrono::steady_clock::now() + rel_time);
  180. }
  181. template <class Clock, class Duration>
  182. bool try_lock_until(
  183. const std::chrono::time_point<Clock, Duration>& abs_time)
  184. {
  185. std::chrono::nanoseconds nsecs = abs_time - std::chrono::steady_clock::now();
  186. struct timespec max_wait = { 0, 0 };
  187. clock_gettime(CLOCK_REALTIME, &max_wait);
  188. nsecs = nsecs + std::chrono::nanoseconds(max_wait.tv_nsec);
  189. auto secs = std::chrono::duration_cast<std::chrono::seconds>(nsecs);
  190. nsecs -= secs;
  191. max_wait.tv_sec += secs.count();
  192. max_wait.tv_nsec = (long)nsecs.count();
  193. return (0 == pthread_mutex_timedlock(&mutex_, &max_wait));
  194. }
  195. pthread_mutex_t* native_handle() noexcept
  196. {
  197. return &mutex_;
  198. }
  199. private:
  200. pthread_mutex_t mutex_;
  201. };
  202. class RecursiveTimedMutex
  203. {
  204. public:
  205. RecursiveTimedMutex()
  206. {
  207. pthread_mutexattr_init(&mutex_attr_);
  208. pthread_mutexattr_settype(&mutex_attr_, PTHREAD_MUTEX_RECURSIVE);
  209. pthread_mutex_init(&mutex_, &mutex_attr_);
  210. }
  211. RecursiveTimedMutex(
  212. const RecursiveTimedMutex&) = delete;
  213. RecursiveTimedMutex& operator =(
  214. const RecursiveTimedMutex&) = delete;
  215. ~RecursiveTimedMutex()
  216. {
  217. pthread_mutex_destroy(&mutex_);
  218. pthread_mutexattr_destroy(&mutex_attr_);
  219. }
  220. void lock()
  221. {
  222. pthread_mutex_lock(&mutex_);
  223. }
  224. void unlock()
  225. {
  226. pthread_mutex_unlock(&mutex_);
  227. }
  228. template <class Rep, class Period>
  229. bool try_lock_for(
  230. const std::chrono::duration<Rep, Period>& rel_time)
  231. {
  232. return try_lock_until(std::chrono::steady_clock::now() + rel_time);
  233. }
  234. template <class Clock, class Duration>
  235. bool try_lock_until(
  236. const std::chrono::time_point<Clock, Duration>& abs_time)
  237. {
  238. std::chrono::nanoseconds nsecs = abs_time - std::chrono::steady_clock::now();
  239. struct timespec max_wait = { 0, 0 };
  240. clock_gettime(CLOCK_REALTIME, &max_wait);
  241. nsecs = nsecs + std::chrono::nanoseconds(max_wait.tv_nsec);
  242. auto secs = std::chrono::duration_cast<std::chrono::seconds>(nsecs);
  243. nsecs -= secs;
  244. max_wait.tv_sec += secs.count();
  245. max_wait.tv_nsec = (long)nsecs.count();
  246. return (0 == pthread_mutex_timedlock(&mutex_, &max_wait));
  247. }
  248. pthread_mutex_t* native_handle() noexcept
  249. {
  250. return &mutex_;
  251. }
  252. private:
  253. pthread_mutexattr_t mutex_attr_;
  254. pthread_mutex_t mutex_;
  255. };
  256. #endif //_WIN32
  257. } //namespace fastrtps
  258. } //namespace eprosima
  259. #endif // _UTILS_TIMEDMUTEX_HPP_