test_threads.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, 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_mutex_release(m);
  66. spawn_exit();
  67. }
  68. /** Run unit tests for threading logic. */
  69. static void
  70. test_threads_basic(void *arg)
  71. {
  72. char *s1 = NULL, *s2 = NULL;
  73. int done = 0, timedout = 0;
  74. time_t started;
  75. #ifndef _WIN32
  76. struct timeval tv;
  77. tv.tv_sec=0;
  78. tv.tv_usec=100*1000;
  79. #endif
  80. (void) arg;
  81. tt_int_op(tor_threadlocal_init(&count), OP_EQ, 0);
  82. set_main_thread();
  83. thread_test_mutex_ = tor_mutex_new();
  84. thread_test_start1_ = tor_mutex_new();
  85. thread_test_start2_ = tor_mutex_new();
  86. thread_test_strmap_ = strmap_new();
  87. s1 = tor_strdup("thread 1");
  88. s2 = tor_strdup("thread 2");
  89. tor_mutex_acquire(thread_test_start1_);
  90. tor_mutex_acquire(thread_test_start2_);
  91. spawn_func(thread_test_func_, s1);
  92. spawn_func(thread_test_func_, s2);
  93. tor_mutex_release(thread_test_start2_);
  94. tor_mutex_release(thread_test_start1_);
  95. started = time(NULL);
  96. while (!done) {
  97. tor_mutex_acquire(thread_test_mutex_);
  98. strmap_assert_ok(thread_test_strmap_);
  99. if (strmap_get(thread_test_strmap_, "thread 1") &&
  100. strmap_get(thread_test_strmap_, "thread 2")) {
  101. done = 1;
  102. } else if (time(NULL) > started + 150) {
  103. timedout = done = 1;
  104. }
  105. tor_mutex_release(thread_test_mutex_);
  106. #ifndef _WIN32
  107. /* Prevent the main thread from starving the worker threads. */
  108. select(0, NULL, NULL, NULL, &tv);
  109. #endif
  110. }
  111. tor_mutex_acquire(thread_test_start1_);
  112. tor_mutex_release(thread_test_start1_);
  113. tor_mutex_acquire(thread_test_start2_);
  114. tor_mutex_release(thread_test_start2_);
  115. tor_mutex_free(thread_test_mutex_);
  116. if (timedout) {
  117. tt_assert(strmap_get(thread_test_strmap_, "thread 1"));
  118. tt_assert(strmap_get(thread_test_strmap_, "thread 2"));
  119. tt_assert(!timedout);
  120. }
  121. /* different thread IDs. */
  122. tt_assert(strcmp(strmap_get(thread_test_strmap_, "thread 1"),
  123. strmap_get(thread_test_strmap_, "thread 2")));
  124. tt_assert(!strcmp(strmap_get(thread_test_strmap_, "thread 1"),
  125. strmap_get(thread_test_strmap_, "last to run")) ||
  126. !strcmp(strmap_get(thread_test_strmap_, "thread 2"),
  127. strmap_get(thread_test_strmap_, "last to run")));
  128. tt_int_op(thread_fns_failed, ==, 0);
  129. tt_int_op(thread_fn_tid1, !=, thread_fn_tid2);
  130. done:
  131. tor_free(s1);
  132. tor_free(s2);
  133. tor_free(thread1_name_);
  134. tor_free(thread2_name_);
  135. if (thread_test_strmap_)
  136. strmap_free(thread_test_strmap_, NULL);
  137. if (thread_test_start1_)
  138. tor_mutex_free(thread_test_start1_);
  139. if (thread_test_start2_)
  140. tor_mutex_free(thread_test_start2_);
  141. }
  142. typedef struct cv_testinfo_s {
  143. tor_cond_t *cond;
  144. tor_mutex_t *mutex;
  145. int value;
  146. int addend;
  147. int shutdown;
  148. int n_shutdown;
  149. int n_wakeups;
  150. int n_timeouts;
  151. int n_threads;
  152. const struct timeval *tv;
  153. } cv_testinfo_t;
  154. static cv_testinfo_t *
  155. cv_testinfo_new(void)
  156. {
  157. cv_testinfo_t *i = tor_malloc_zero(sizeof(*i));
  158. i->cond = tor_cond_new();
  159. i->mutex = tor_mutex_new_nonrecursive();
  160. return i;
  161. }
  162. static void
  163. cv_testinfo_free(cv_testinfo_t *i)
  164. {
  165. if (!i)
  166. return;
  167. tor_cond_free(i->cond);
  168. tor_mutex_free(i->mutex);
  169. tor_free(i);
  170. }
  171. static void cv_test_thr_fn_(void *arg) ATTR_NORETURN;
  172. static void
  173. cv_test_thr_fn_(void *arg)
  174. {
  175. cv_testinfo_t *i = arg;
  176. int tid, r;
  177. tor_mutex_acquire(i->mutex);
  178. tid = i->n_threads++;
  179. tor_mutex_release(i->mutex);
  180. (void) tid;
  181. tor_mutex_acquire(i->mutex);
  182. while (1) {
  183. if (i->addend) {
  184. i->value += i->addend;
  185. i->addend = 0;
  186. }
  187. if (i->shutdown) {
  188. ++i->n_shutdown;
  189. i->shutdown = 0;
  190. tor_mutex_release(i->mutex);
  191. spawn_exit();
  192. }
  193. r = tor_cond_wait(i->cond, i->mutex, i->tv);
  194. ++i->n_wakeups;
  195. if (r == 1) {
  196. ++i->n_timeouts;
  197. tor_mutex_release(i->mutex);
  198. spawn_exit();
  199. }
  200. }
  201. }
  202. static void
  203. test_threads_conditionvar(void *arg)
  204. {
  205. cv_testinfo_t *ti=NULL;
  206. const struct timeval msec100 = { 0, 100*1000 };
  207. const int timeout = !strcmp(arg, "tv");
  208. ti = cv_testinfo_new();
  209. if (timeout) {
  210. ti->tv = &msec100;
  211. }
  212. spawn_func(cv_test_thr_fn_, ti);
  213. spawn_func(cv_test_thr_fn_, ti);
  214. spawn_func(cv_test_thr_fn_, ti);
  215. spawn_func(cv_test_thr_fn_, ti);
  216. tor_mutex_acquire(ti->mutex);
  217. ti->addend = 7;
  218. ti->shutdown = 1;
  219. tor_cond_signal_one(ti->cond);
  220. tor_mutex_release(ti->mutex);
  221. #define SPIN() \
  222. while (1) { \
  223. tor_mutex_acquire(ti->mutex); \
  224. if (ti->addend == 0) { \
  225. break; \
  226. } \
  227. tor_mutex_release(ti->mutex); \
  228. }
  229. SPIN();
  230. ti->addend = 30;
  231. ti->shutdown = 1;
  232. tor_cond_signal_all(ti->cond);
  233. tor_mutex_release(ti->mutex);
  234. SPIN();
  235. ti->addend = 1000;
  236. if (! timeout) ti->shutdown = 1;
  237. tor_cond_signal_one(ti->cond);
  238. tor_mutex_release(ti->mutex);
  239. SPIN();
  240. ti->addend = 300;
  241. if (! timeout) ti->shutdown = 1;
  242. tor_cond_signal_all(ti->cond);
  243. tor_mutex_release(ti->mutex);
  244. SPIN();
  245. tor_mutex_release(ti->mutex);
  246. tt_int_op(ti->value, ==, 1337);
  247. if (!timeout) {
  248. tt_int_op(ti->n_shutdown, ==, 4);
  249. } else {
  250. #ifdef _WIN32
  251. Sleep(500); /* msec */
  252. #elif defined(HAVE_USLEEP)
  253. usleep(500*1000); /* usec */
  254. #else
  255. {
  256. struct tv = { 0, 500*1000 };
  257. select(0, NULL, NULL, NULL, &tv);
  258. }
  259. #endif
  260. tor_mutex_acquire(ti->mutex);
  261. tt_int_op(ti->n_shutdown, ==, 2);
  262. tt_int_op(ti->n_timeouts, ==, 2);
  263. tor_mutex_release(ti->mutex);
  264. }
  265. done:
  266. cv_testinfo_free(ti);
  267. }
  268. #define THREAD_TEST(name) \
  269. { #name, test_threads_##name, TT_FORK, NULL, NULL }
  270. struct testcase_t thread_tests[] = {
  271. THREAD_TEST(basic),
  272. { "conditionvar", test_threads_conditionvar, TT_FORK,
  273. &passthrough_setup, (void*)"no-tv" },
  274. { "conditionvar_timeout", test_threads_conditionvar, TT_FORK,
  275. &passthrough_setup, (void*)"tv" },
  276. END_OF_TESTCASES
  277. };