thread.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. libtest::stream::make_cerr(LIBYATL_DEFAULT_PARAM) << "pthread_cond_destroy: " << strerror(_err);
  55. abort();
  56. }
  57. }
  58. pthread_mutex_t* handle()
  59. {
  60. if (_err != 0)
  61. {
  62. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(_err));
  63. }
  64. return &_mutex;
  65. }
  66. private:
  67. int _err;
  68. pthread_mutex_t _mutex;
  69. };
  70. class ScopedLock
  71. {
  72. public:
  73. ScopedLock(Mutex& mutex_) :
  74. _mutex(mutex_)
  75. {
  76. init();
  77. }
  78. ~ScopedLock()
  79. {
  80. int err;
  81. if ((err= pthread_mutex_unlock(_mutex.handle())))
  82. {
  83. libtest::stream::make_cerr(LIBYATL_DEFAULT_PARAM) << "pthread_mutex_unlock: " << strerror(err);
  84. abort();
  85. }
  86. }
  87. Mutex* handle()
  88. {
  89. return &_mutex;
  90. }
  91. private:
  92. void init()
  93. {
  94. int err;
  95. if ((err= pthread_mutex_lock(_mutex.handle())))
  96. {
  97. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_lock: %s", strerror(err));
  98. }
  99. }
  100. private:
  101. Mutex& _mutex;
  102. };
  103. class Condition
  104. {
  105. public:
  106. Condition()
  107. {
  108. int err;
  109. if ((err= pthread_cond_init(&_cond, NULL)))
  110. {
  111. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_mutex_init: %s", strerror(err));
  112. }
  113. }
  114. ~Condition()
  115. {
  116. int err;
  117. if ((err= pthread_cond_destroy(&_cond)))
  118. {
  119. libtest::stream::make_cerr(LIBYATL_DEFAULT_PARAM) << "pthread_cond_destroy: " << strerror(err);
  120. abort();
  121. }
  122. }
  123. void broadcast()
  124. {
  125. int err;
  126. if ((err= pthread_cond_broadcast(&_cond)))
  127. {
  128. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
  129. }
  130. }
  131. void signal()
  132. {
  133. int err;
  134. if ((err= pthread_cond_signal(&_cond)))
  135. {
  136. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_broadcast: %s", strerror(err));
  137. }
  138. }
  139. void wait(ScopedLock& lock_)
  140. {
  141. int err;
  142. if ((err= pthread_cond_wait(&_cond, lock_.handle()->handle())))
  143. {
  144. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_cond_wait: %s", strerror(err));
  145. }
  146. }
  147. private:
  148. pthread_cond_t _cond;
  149. };
  150. class Barrier
  151. {
  152. public:
  153. explicit Barrier(uint32_t count):
  154. _threshold(count),
  155. _count(count),
  156. _generation(0)
  157. {
  158. if (_count == 0)
  159. {
  160. fatal_assert("Zero is an invalid value");
  161. }
  162. }
  163. ~Barrier()
  164. {
  165. }
  166. bool wait()
  167. {
  168. ScopedLock l(_mutex);
  169. uint32_t gen = _generation;
  170. if (--_count == 0)
  171. {
  172. _generation++;
  173. _count = _threshold;
  174. _cond.broadcast();
  175. return true;
  176. }
  177. while (gen == _generation)
  178. {
  179. _cond.wait(l);
  180. }
  181. return false;
  182. }
  183. private:
  184. Mutex _mutex;
  185. Condition _cond;
  186. uint32_t _threshold;
  187. uint32_t _count;
  188. uint32_t _generation;
  189. };
  190. class Thread
  191. {
  192. private:
  193. typedef void *(*start_routine_fn) (void *);
  194. public:
  195. template <class Function,class Arg1>
  196. Thread(Function func, Arg1 arg):
  197. _joined(false),
  198. _func((start_routine_fn)(void(*)())func),
  199. _context(arg)
  200. {
  201. int err;
  202. if ((err= pthread_create(&_thread, NULL, entry_func, (void*)this)))
  203. {
  204. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_create: %s", strerror(err));
  205. }
  206. _owner= pthread_self();
  207. }
  208. bool running() const
  209. {
  210. return (pthread_kill(_thread, 0) == 0);
  211. }
  212. bool detached()
  213. {
  214. if (EDEADLK == pthread_join(_thread, NULL))
  215. {
  216. return true;
  217. }
  218. /* Result of pthread_join was EINVAL == detached thread */
  219. return false;
  220. }
  221. bool join()
  222. {
  223. if (_thread == pthread_self())
  224. {
  225. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Thread cannot join on itself");
  226. }
  227. if (_owner != pthread_self())
  228. {
  229. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Attempt made by a non-owner thead to join on thread");
  230. }
  231. bool ret= false;
  232. {
  233. ScopedLock l(_join_mutex);
  234. if (_joined == false)
  235. {
  236. int err;
  237. if ((err= pthread_join(_thread, NULL)))
  238. {
  239. switch(err)
  240. {
  241. case EINVAL:
  242. break;
  243. case ESRCH:
  244. ret= true;
  245. break;
  246. case EDEADLK:
  247. default:
  248. throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "pthread_join: %s", strerror(err));
  249. }
  250. }
  251. else
  252. {
  253. ret= true;
  254. }
  255. _joined= true;
  256. }
  257. }
  258. return ret;
  259. }
  260. ~Thread()
  261. {
  262. join();
  263. }
  264. protected:
  265. void run()
  266. {
  267. _func(_context);
  268. }
  269. private:
  270. static void * entry_func(void* This)
  271. {
  272. ((Thread *)This)->run();
  273. return NULL;
  274. }
  275. private:
  276. bool _joined;
  277. pthread_t _thread;
  278. pthread_t _owner;
  279. start_routine_fn _func;
  280. void* _context;
  281. Mutex _join_mutex;
  282. };
  283. } // namespace thread
  284. } // namespace libtest