test_threads.c 8.0 KB

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