test-thread.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h> /* memset */
  26. #ifdef __POSIX__
  27. #include <pthread.h>
  28. #endif
  29. struct getaddrinfo_req {
  30. uv_thread_t thread_id;
  31. unsigned int counter;
  32. uv_loop_t* loop;
  33. uv_getaddrinfo_t handle;
  34. };
  35. struct fs_req {
  36. uv_thread_t thread_id;
  37. unsigned int counter;
  38. uv_loop_t* loop;
  39. uv_fs_t handle;
  40. };
  41. struct test_thread {
  42. uv_thread_t thread_id;
  43. int thread_called;
  44. };
  45. static void getaddrinfo_do(struct getaddrinfo_req* req);
  46. static void getaddrinfo_cb(uv_getaddrinfo_t* handle,
  47. int status,
  48. struct addrinfo* res);
  49. static void fs_do(struct fs_req* req);
  50. static void fs_cb(uv_fs_t* handle);
  51. static int thread_called;
  52. static uv_key_t tls_key;
  53. static void getaddrinfo_do(struct getaddrinfo_req* req) {
  54. int r;
  55. r = uv_getaddrinfo(req->loop,
  56. &req->handle,
  57. getaddrinfo_cb,
  58. "localhost",
  59. NULL,
  60. NULL);
  61. ASSERT(r == 0);
  62. }
  63. static void getaddrinfo_cb(uv_getaddrinfo_t* handle,
  64. int status,
  65. struct addrinfo* res) {
  66. struct getaddrinfo_req* req;
  67. ASSERT(status == 0);
  68. req = container_of(handle, struct getaddrinfo_req, handle);
  69. uv_freeaddrinfo(res);
  70. if (--req->counter)
  71. getaddrinfo_do(req);
  72. }
  73. static void fs_do(struct fs_req* req) {
  74. int r;
  75. r = uv_fs_stat(req->loop, &req->handle, ".", fs_cb);
  76. ASSERT(r == 0);
  77. }
  78. static void fs_cb(uv_fs_t* handle) {
  79. struct fs_req* req = container_of(handle, struct fs_req, handle);
  80. uv_fs_req_cleanup(handle);
  81. if (--req->counter)
  82. fs_do(req);
  83. }
  84. static void do_work(void* arg) {
  85. struct getaddrinfo_req getaddrinfo_reqs[4];
  86. struct fs_req fs_reqs[4];
  87. uv_loop_t loop;
  88. size_t i;
  89. struct test_thread* thread = arg;
  90. ASSERT(0 == uv_loop_init(&loop));
  91. for (i = 0; i < ARRAY_SIZE(getaddrinfo_reqs); i++) {
  92. struct getaddrinfo_req* req = getaddrinfo_reqs + i;
  93. req->counter = 4;
  94. req->loop = &loop;
  95. getaddrinfo_do(req);
  96. }
  97. for (i = 0; i < ARRAY_SIZE(fs_reqs); i++) {
  98. struct fs_req* req = fs_reqs + i;
  99. req->counter = 4;
  100. req->loop = &loop;
  101. fs_do(req);
  102. }
  103. ASSERT(0 == uv_run(&loop, UV_RUN_DEFAULT));
  104. ASSERT(0 == uv_loop_close(&loop));
  105. thread->thread_called = 1;
  106. }
  107. static void thread_entry(void* arg) {
  108. ASSERT(arg == (void *) 42);
  109. thread_called++;
  110. }
  111. TEST_IMPL(thread_create) {
  112. uv_thread_t tid;
  113. int r;
  114. r = uv_thread_create(&tid, thread_entry, (void *) 42);
  115. ASSERT(r == 0);
  116. r = uv_thread_join(&tid);
  117. ASSERT(r == 0);
  118. ASSERT(thread_called == 1);
  119. return 0;
  120. }
  121. /* Hilariously bad test name. Run a lot of tasks in the thread pool and verify
  122. * that each "finished" callback is run in its originating thread.
  123. */
  124. TEST_IMPL(threadpool_multiple_event_loops) {
  125. struct test_thread threads[8];
  126. size_t i;
  127. int r;
  128. memset(threads, 0, sizeof(threads));
  129. for (i = 0; i < ARRAY_SIZE(threads); i++) {
  130. r = uv_thread_create(&threads[i].thread_id, do_work, &threads[i]);
  131. ASSERT(r == 0);
  132. }
  133. for (i = 0; i < ARRAY_SIZE(threads); i++) {
  134. r = uv_thread_join(&threads[i].thread_id);
  135. ASSERT(r == 0);
  136. ASSERT(threads[i].thread_called == 1);
  137. }
  138. return 0;
  139. }
  140. static void tls_thread(void* arg) {
  141. ASSERT(NULL == uv_key_get(&tls_key));
  142. uv_key_set(&tls_key, arg);
  143. ASSERT(arg == uv_key_get(&tls_key));
  144. uv_key_set(&tls_key, NULL);
  145. ASSERT(NULL == uv_key_get(&tls_key));
  146. }
  147. TEST_IMPL(thread_local_storage) {
  148. char name[] = "main";
  149. uv_thread_t threads[2];
  150. ASSERT(0 == uv_key_create(&tls_key));
  151. ASSERT(NULL == uv_key_get(&tls_key));
  152. uv_key_set(&tls_key, name);
  153. ASSERT(name == uv_key_get(&tls_key));
  154. ASSERT(0 == uv_thread_create(threads + 0, tls_thread, threads + 0));
  155. ASSERT(0 == uv_thread_create(threads + 1, tls_thread, threads + 1));
  156. ASSERT(0 == uv_thread_join(threads + 0));
  157. ASSERT(0 == uv_thread_join(threads + 1));
  158. uv_key_delete(&tls_key);
  159. return 0;
  160. }
  161. static void thread_check_stack(void* arg) {
  162. #if defined(__APPLE__)
  163. size_t expected;
  164. expected = arg == NULL ? 0 : ((uv_thread_options_t*)arg)->stack_size;
  165. /* 512 kB is the default stack size of threads other than the main thread
  166. * on MacOS. */
  167. if (expected == 0)
  168. expected = 512 * 1024;
  169. ASSERT(pthread_get_stacksize_np(pthread_self()) >= expected);
  170. #elif defined(__linux__) && defined(__GLIBC__)
  171. size_t expected;
  172. struct rlimit lim;
  173. size_t stack_size;
  174. pthread_attr_t attr;
  175. ASSERT(0 == getrlimit(RLIMIT_STACK, &lim));
  176. if (lim.rlim_cur == RLIM_INFINITY)
  177. lim.rlim_cur = 2 << 20; /* glibc default. */
  178. ASSERT(0 == pthread_getattr_np(pthread_self(), &attr));
  179. ASSERT(0 == pthread_attr_getstacksize(&attr, &stack_size));
  180. expected = arg == NULL ? 0 : ((uv_thread_options_t*)arg)->stack_size;
  181. if (expected == 0)
  182. expected = (size_t)lim.rlim_cur;
  183. ASSERT(stack_size >= expected);
  184. ASSERT(0 == pthread_attr_destroy(&attr));
  185. #endif
  186. }
  187. TEST_IMPL(thread_stack_size) {
  188. uv_thread_t thread;
  189. ASSERT(0 == uv_thread_create(&thread, thread_check_stack, NULL));
  190. ASSERT(0 == uv_thread_join(&thread));
  191. return 0;
  192. }
  193. TEST_IMPL(thread_stack_size_explicit) {
  194. uv_thread_t thread;
  195. uv_thread_options_t options;
  196. options.flags = UV_THREAD_HAS_STACK_SIZE;
  197. options.stack_size = 1024 * 1024;
  198. ASSERT(0 == uv_thread_create_ex(&thread, &options,
  199. thread_check_stack, &options));
  200. ASSERT(0 == uv_thread_join(&thread));
  201. options.stack_size = 8 * 1024 * 1024; /* larger than most default os sizes */
  202. ASSERT(0 == uv_thread_create_ex(&thread, &options,
  203. thread_check_stack, &options));
  204. ASSERT(0 == uv_thread_join(&thread));
  205. options.stack_size = 0;
  206. ASSERT(0 == uv_thread_create_ex(&thread, &options,
  207. thread_check_stack, &options));
  208. ASSERT(0 == uv_thread_join(&thread));
  209. #ifdef PTHREAD_STACK_MIN
  210. options.stack_size = PTHREAD_STACK_MIN - 42; /* unaligned size */
  211. ASSERT(0 == uv_thread_create_ex(&thread, &options,
  212. thread_check_stack, &options));
  213. ASSERT(0 == uv_thread_join(&thread));
  214. options.stack_size = PTHREAD_STACK_MIN / 2 - 42; /* unaligned size */
  215. ASSERT(0 == uv_thread_create_ex(&thread, &options,
  216. thread_check_stack, &options));
  217. ASSERT(0 == uv_thread_join(&thread));
  218. #endif
  219. /* unaligned size, should be larger than PTHREAD_STACK_MIN */
  220. options.stack_size = 1234567;
  221. ASSERT(0 == uv_thread_create_ex(&thread, &options,
  222. thread_check_stack, &options));
  223. ASSERT(0 == uv_thread_join(&thread));
  224. return 0;
  225. }