test_threads.c 8.2 KB

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