thread.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #pragma once
  37. #include <pthread.h>
  38. namespace libtest
  39. {
  40. namespace thread
  41. {
  42. class Mutex
  43. {
  44. public:
  45. Mutex() :
  46. _err(0)
  47. {
  48. _err= pthread_mutex_init(&_mutex, NULL);
  49. }
  50. ~Mutex()
  51. {
  52. if ((_err= pthread_mutex_destroy(&_mutex)))
  53. {
  54. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_destroy: %s", strerror(_err));
  55. }
  56. }
  57. pthread_mutex_t* handle()
  58. {
  59. if (_err != 0)
  60. {
  61. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(_err));
  62. }
  63. return &_mutex;
  64. }
  65. private:
  66. int _err;
  67. pthread_mutex_t _mutex;
  68. };
  69. class ScopedLock
  70. {
  71. public:
  72. ScopedLock(Mutex& mutex_) :
  73. _mutex(mutex_)
  74. {
  75. init();
  76. }
  77. ~ScopedLock()
  78. {
  79. int err;
  80. if ((err= pthread_mutex_unlock(_mutex.handle())))
  81. {
  82. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_unlock: %s", strerror(err));
  83. }
  84. }
  85. Mutex* handle()
  86. {
  87. return &_mutex;
  88. }
  89. private:
  90. void init()
  91. {
  92. int err;
  93. if ((err= pthread_mutex_lock(_mutex.handle())))
  94. {
  95. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_lock: %s", strerror(err));
  96. }
  97. }
  98. private:
  99. Mutex& _mutex;
  100. };
  101. class Condition
  102. {
  103. public:
  104. Condition()
  105. {
  106. int err;
  107. if ((err= pthread_cond_init(&_cond, NULL)))
  108. {
  109. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(err));
  110. }
  111. }
  112. ~Condition()
  113. {
  114. int err;
  115. if ((err= pthread_cond_destroy(&_cond)))
  116. {
  117. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_destroy: %s", strerror(err));
  118. }
  119. }
  120. void broadcast()
  121. {
  122. int err;
  123. if ((err= pthread_cond_broadcast(&_cond)))
  124. {
  125. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
  126. }
  127. }
  128. void signal()
  129. {
  130. int err;
  131. if ((err= pthread_cond_signal(&_cond)))
  132. {
  133. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
  134. }
  135. }
  136. void wait(ScopedLock& lock_)
  137. {
  138. int err;
  139. if ((err= pthread_cond_wait(&_cond, lock_.handle()->handle())))
  140. {
  141. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_wait: %s", strerror(err));
  142. }
  143. }
  144. private:
  145. pthread_cond_t _cond;
  146. };
  147. class Barrier
  148. {
  149. public:
  150. explicit Barrier(uint32_t count):
  151. _threshold(count),
  152. _count(count),
  153. _generation(0)
  154. {
  155. if (_count == 0)
  156. {
  157. fatal_assert("Zero is an invalid value");
  158. }
  159. }
  160. ~Barrier()
  161. {
  162. }
  163. bool wait()
  164. {
  165. ScopedLock l(_mutex);
  166. uint32_t gen = _generation;
  167. if (--_count == 0)
  168. {
  169. _generation++;
  170. _count = _threshold;
  171. _cond.broadcast();
  172. return true;
  173. }
  174. while (gen == _generation)
  175. {
  176. _cond.wait(l);
  177. }
  178. return false;
  179. }
  180. private:
  181. Mutex _mutex;
  182. Condition _cond;
  183. uint32_t _threshold;
  184. uint32_t _count;
  185. uint32_t _generation;
  186. };
  187. class Thread
  188. {
  189. private:
  190. typedef void *(*start_routine_fn) (void *);
  191. public:
  192. template <class Function,class Arg1>
  193. Thread(Function func, Arg1 arg):
  194. _joined(false),
  195. _func((start_routine_fn)func),
  196. _context(arg)
  197. {
  198. int err;
  199. if ((err= pthread_create(&_thread, NULL, entry_func, (void*)this)))
  200. {
  201. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_create: %s", strerror(err));
  202. }
  203. _owner= pthread_self();
  204. }
  205. bool running() const
  206. {
  207. return (pthread_kill(_thread, 0) == 0);
  208. }
  209. bool detached()
  210. {
  211. if (EDEADLK == pthread_join(_thread, NULL))
  212. {
  213. return true;
  214. }
  215. /* Result of pthread_join was EINVAL == detached thread */
  216. return false;
  217. }
  218. bool join()
  219. {
  220. if (_thread == pthread_self())
  221. {
  222. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Thread cannot join on itself");
  223. }
  224. if (_owner != pthread_self())
  225. {
  226. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Attempt made by a non-owner thead to join on thread");
  227. }
  228. bool ret= false;
  229. {
  230. ScopedLock l(_join_mutex);
  231. if (_joined == false)
  232. {
  233. int err;
  234. if ((err= pthread_join(_thread, NULL)))
  235. {
  236. switch(err)
  237. {
  238. case EINVAL:
  239. break;
  240. case ESRCH:
  241. ret= true;
  242. break;
  243. case EDEADLK:
  244. default:
  245. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_join: %s", strerror(err));
  246. }
  247. }
  248. else
  249. {
  250. ret= true;
  251. }
  252. _joined= true;
  253. }
  254. }
  255. return ret;
  256. }
  257. ~Thread()
  258. {
  259. join();
  260. }
  261. protected:
  262. void run()
  263. {
  264. _func(_context);
  265. }
  266. private:
  267. static void * entry_func(void* This)
  268. {
  269. ((Thread *)This)->run();
  270. return NULL;
  271. }
  272. private:
  273. bool _joined;
  274. pthread_t _thread;
  275. pthread_t _owner;
  276. start_routine_fn _func;
  277. void* _context;
  278. Mutex _join_mutex;
  279. };
  280. } // namespace thread
  281. } // namespace libtest