test_threads.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "or.h"
  7. #include "compat_threads.h"
  8. #include "test.h"
  9. /** mutex for thread test to stop the threads hitting data at the same time. */
  10. static tor_mutex_t *thread_test_mutex_ = NULL;
  11. /** mutexes for the thread test to make sure that the threads have to
  12. * interleave somewhat. */
  13. static tor_mutex_t *thread_test_start1_ = NULL,
  14. *thread_test_start2_ = NULL;
  15. /** Shared strmap for the thread test. */
  16. static strmap_t *thread_test_strmap_ = NULL;
  17. /** The name of thread1 for the thread test */
  18. static char *thread1_name_ = NULL;
  19. /** The name of thread2 for the thread test */
  20. static char *thread2_name_ = NULL;
  21. static int thread_fns_failed = 0;
  22. static unsigned long thread_fn_tid1, thread_fn_tid2;
  23. static void thread_test_func_(void* _s) ATTR_NORETURN;
  24. /** How many iterations have the threads in the unit test run? */
  25. static tor_threadlocal_t count;
  26. /** Helper function for threading unit tests: This function runs in a
  27. * subthread. It grabs its own mutex (start1 or start2) to make sure that it
  28. * should start, then it repeatedly alters _test_thread_strmap protected by
  29. * thread_test_mutex_. */
  30. static void
  31. thread_test_func_(void* _s)
  32. {
  33. char *s = _s;
  34. int i;
  35. tor_mutex_t *m;
  36. char buf[64];
  37. char **cp;
  38. int *mycount = tor_malloc_zero(sizeof(int));
  39. tor_threadlocal_set(&count, mycount);
  40. if (!strcmp(s, "thread 1")) {
  41. m = thread_test_start1_;
  42. cp = &thread1_name_;
  43. thread_fn_tid1 = tor_get_thread_id();
  44. } else {
  45. m = thread_test_start2_;
  46. cp = &thread2_name_;
  47. thread_fn_tid2 = tor_get_thread_id();
  48. }
  49. tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
  50. *cp = tor_strdup(buf);
  51. tor_mutex_acquire(m);
  52. for (i=0; i<10000; ++i) {
  53. tor_mutex_acquire(thread_test_mutex_);
  54. strmap_set(thread_test_strmap_, "last to run", *cp);
  55. tor_mutex_release(thread_test_mutex_);
  56. int *tls_count = tor_threadlocal_get(&count);
  57. tor_assert(tls_count == mycount);
  58. ++*tls_count;
  59. }
  60. tor_mutex_acquire(thread_test_mutex_);
  61. strmap_set(thread_test_strmap_, s, *cp);
  62. if (in_main_thread())
  63. ++thread_fns_failed;
  64. tor_mutex_release(thread_test_mutex_);
  65. tor_free(mycount);
  66. tor_mutex_release(m);
  67. spawn_exit();
  68. }
  69. /** Run unit tests for threading logic. */
  70. static void
  71. test_threads_basic(void *arg)
  72. {
  73. char *s1 = NULL, *s2 = NULL;
  74. int done = 0, timedout = 0;
  75. time_t started;
  76. (void) arg;
  77. tt_int_op(tor_threadlocal_init(&count), OP_EQ, 0);
  78. set_main_thread();
  79. thread_test_mutex_ = tor_mutex_new();
  80. thread_test_start1_ = tor_mutex_new();
  81. thread_test_start2_ = tor_mutex_new();
  82. thread_test_strmap_ = strmap_new();
  83. s1 = tor_strdup("thread 1");
  84. s2 = tor_strdup("thread 2");
  85. tor_mutex_acquire(thread_test_start1_);
  86. tor_mutex_acquire(thread_test_start2_);
  87. spawn_func(thread_test_func_, s1);
  88. spawn_func(thread_test_func_, s2);
  89. tor_mutex_release(thread_test_start2_);
  90. tor_mutex_release(thread_test_start1_);
  91. started = time(NULL);
  92. while (!done) {
  93. tor_mutex_acquire(thread_test_mutex_);
  94. strmap_assert_ok(thread_test_strmap_);
  95. if (strmap_get(thread_test_strmap_, "thread 1") &&
  96. strmap_get(thread_test_strmap_, "thread 2")) {
  97. done = 1;
  98. } else if (time(NULL) > started + 150) {
  99. timedout = done = 1;
  100. }
  101. tor_mutex_release(thread_test_mutex_);
  102. /* Prevent the main thread from starving the worker threads. */
  103. tor_sleep_msec(10);
  104. }
  105. tor_mutex_acquire(thread_test_start1_);
  106. tor_mutex_release(thread_test_start1_);
  107. tor_mutex_acquire(thread_test_start2_);
  108. tor_mutex_release(thread_test_start2_);
  109. tor_mutex_free(thread_test_mutex_);
  110. if (timedout) {
  111. tt_assert(strmap_get(thread_test_strmap_, "thread 1"));
  112. tt_assert(strmap_get(thread_test_strmap_, "thread 2"));
  113. tt_assert(!timedout);
  114. }
  115. /* different thread IDs. */
  116. tt_assert(strcmp(strmap_get(thread_test_strmap_, "thread 1"),
  117. strmap_get(thread_test_strmap_, "thread 2")));
  118. tt_assert(!strcmp(strmap_get(thread_test_strmap_, "thread 1"),
  119. strmap_get(thread_test_strmap_, "last to run")) ||
  120. !strcmp(strmap_get(thread_test_strmap_, "thread 2"),
  121. strmap_get(thread_test_strmap_, "last to run")));
  122. tt_int_op(thread_fns_failed, OP_EQ, 0);
  123. tt_int_op(thread_fn_tid1, OP_NE, thread_fn_tid2);
  124. done:
  125. tor_free(s1);
  126. tor_free(s2);
  127. tor_free(thread1_name_);
  128. tor_free(thread2_name_);
  129. if (thread_test_strmap_)
  130. strmap_free(thread_test_strmap_, NULL);
  131. if (thread_test_start1_)
  132. tor_mutex_free(thread_test_start1_);
  133. if (thread_test_start2_)
  134. tor_mutex_free(thread_test_start2_);
  135. }
  136. typedef struct cv_testinfo_s {
  137. tor_cond_t *cond;
  138. tor_mutex_t *mutex;
  139. int value;
  140. int addend;
  141. int shutdown;
  142. int n_shutdown;
  143. int n_wakeups;
  144. int n_timeouts;
  145. int n_threads;
  146. const struct timeval *tv;
  147. } cv_testinfo_t;
  148. static cv_testinfo_t *
  149. cv_testinfo_new(void)
  150. {
  151. cv_testinfo_t *i = tor_malloc_zero(sizeof(*i));
  152. i->cond = tor_cond_new();
  153. i->mutex = tor_mutex_new_nonrecursive();
  154. return i;
  155. }
  156. static void
  157. cv_testinfo_free(cv_testinfo_t *i)
  158. {
  159. if (!i)
  160. return;
  161. tor_cond_free(i->cond);
  162. tor_mutex_free(i->mutex);
  163. tor_free(i);
  164. }
  165. static void cv_test_thr_fn_(void *arg) ATTR_NORETURN;
  166. static void
  167. cv_test_thr_fn_(void *arg)
  168. {
  169. cv_testinfo_t *i = arg;
  170. int tid, r;
  171. tor_mutex_acquire(i->mutex);
  172. tid = i->n_threads++;
  173. tor_mutex_release(i->mutex);
  174. (void) tid;
  175. tor_mutex_acquire(i->mutex);
  176. while (1) {
  177. if (i->addend) {
  178. i->value += i->addend;
  179. i->addend = 0;
  180. }
  181. if (i->shutdown) {
  182. ++i->n_shutdown;
  183. i->shutdown = 0;
  184. tor_mutex_release(i->mutex);
  185. spawn_exit();
  186. }
  187. r = tor_cond_wait(i->cond, i->mutex, i->tv);
  188. ++i->n_wakeups;
  189. if (r == 1) {
  190. ++i->n_timeouts;
  191. tor_mutex_release(i->mutex);
  192. spawn_exit();
  193. }
  194. }
  195. }
  196. static void
  197. test_threads_conditionvar(void *arg)
  198. {
  199. cv_testinfo_t *ti=NULL;
  200. const struct timeval msec100 = { 0, 100*1000 };
  201. const int timeout = !strcmp(arg, "tv");
  202. ti = cv_testinfo_new();
  203. if (timeout) {
  204. ti->tv = &msec100;
  205. }
  206. spawn_func(cv_test_thr_fn_, ti);
  207. spawn_func(cv_test_thr_fn_, ti);
  208. spawn_func(cv_test_thr_fn_, ti);
  209. spawn_func(cv_test_thr_fn_, ti);
  210. tor_mutex_acquire(ti->mutex);
  211. ti->addend = 7;
  212. ti->shutdown = 1;
  213. tor_cond_signal_one(ti->cond);
  214. tor_mutex_release(ti->mutex);
  215. #define SPIN() \
  216. while (1) { \
  217. tor_mutex_acquire(ti->mutex); \
  218. if (ti->addend == 0) { \
  219. break; \
  220. } \
  221. tor_mutex_release(ti->mutex); \
  222. }
  223. SPIN();
  224. ti->addend = 30;
  225. ti->shutdown = 1;
  226. tor_cond_signal_all(ti->cond);
  227. tor_mutex_release(ti->mutex);
  228. SPIN();
  229. ti->addend = 1000;
  230. if (! timeout) ti->shutdown = 1;
  231. tor_cond_signal_one(ti->cond);
  232. tor_mutex_release(ti->mutex);
  233. SPIN();
  234. ti->addend = 300;
  235. if (! timeout) ti->shutdown = 1;
  236. tor_cond_signal_all(ti->cond);
  237. tor_mutex_release(ti->mutex);
  238. SPIN();
  239. tor_mutex_release(ti->mutex);
  240. tt_int_op(ti->value, OP_EQ, 1337);
  241. if (!timeout) {
  242. tt_int_op(ti->n_shutdown, OP_EQ, 4);
  243. } else {
  244. tor_sleep_msec(200);
  245. tor_mutex_acquire(ti->mutex);
  246. tt_int_op(ti->n_shutdown, OP_EQ, 2);
  247. tt_int_op(ti->n_timeouts, OP_EQ, 2);
  248. tor_mutex_release(ti->mutex);
  249. }
  250. done:
  251. cv_testinfo_free(ti);
  252. }
  253. #define THREAD_TEST(name) \
  254. { #name, test_threads_##name, TT_FORK, NULL, NULL }
  255. struct testcase_t thread_tests[] = {
  256. THREAD_TEST(basic),
  257. { "conditionvar", test_threads_conditionvar, TT_FORK,
  258. &passthrough_setup, (void*)"no-tv" },
  259. { "conditionvar_timeout", test_threads_conditionvar, TT_FORK,
  260. &passthrough_setup, (void*)"tv" },
  261. END_OF_TESTCASES
  262. };