1
0

test-thread.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. struct getaddrinfo_req {
  27. uv_thread_t thread_id;
  28. unsigned int counter;
  29. uv_loop_t* loop;
  30. uv_getaddrinfo_t handle;
  31. };
  32. struct fs_req {
  33. uv_thread_t thread_id;
  34. unsigned int counter;
  35. uv_loop_t* loop;
  36. uv_fs_t handle;
  37. };
  38. struct test_thread {
  39. uv_thread_t thread_id;
  40. volatile int thread_called;
  41. };
  42. static void getaddrinfo_do(struct getaddrinfo_req* req);
  43. static void getaddrinfo_cb(uv_getaddrinfo_t* handle,
  44. int status,
  45. struct addrinfo* res);
  46. static void fs_do(struct fs_req* req);
  47. static void fs_cb(uv_fs_t* handle);
  48. static volatile int thread_called;
  49. static uv_key_t tls_key;
  50. static void getaddrinfo_do(struct getaddrinfo_req* req) {
  51. int r;
  52. r = uv_getaddrinfo(req->loop,
  53. &req->handle,
  54. getaddrinfo_cb,
  55. "localhost",
  56. NULL,
  57. NULL);
  58. ASSERT(r == 0);
  59. }
  60. static void getaddrinfo_cb(uv_getaddrinfo_t* handle,
  61. int status,
  62. struct addrinfo* res) {
  63. struct getaddrinfo_req* req;
  64. ASSERT(status == 0);
  65. req = container_of(handle, struct getaddrinfo_req, handle);
  66. uv_freeaddrinfo(res);
  67. if (--req->counter)
  68. getaddrinfo_do(req);
  69. }
  70. static void fs_do(struct fs_req* req) {
  71. int r;
  72. r = uv_fs_stat(req->loop, &req->handle, ".", fs_cb);
  73. ASSERT(r == 0);
  74. }
  75. static void fs_cb(uv_fs_t* handle) {
  76. struct fs_req* req = container_of(handle, struct fs_req, handle);
  77. uv_fs_req_cleanup(handle);
  78. if (--req->counter)
  79. fs_do(req);
  80. }
  81. static void do_work(void* arg) {
  82. struct getaddrinfo_req getaddrinfo_reqs[16];
  83. struct fs_req fs_reqs[16];
  84. uv_loop_t* loop;
  85. size_t i;
  86. int r;
  87. struct test_thread* thread = arg;
  88. loop = uv_loop_new();
  89. ASSERT(loop != NULL);
  90. for (i = 0; i < ARRAY_SIZE(getaddrinfo_reqs); i++) {
  91. struct getaddrinfo_req* req = getaddrinfo_reqs + i;
  92. req->counter = 16;
  93. req->loop = loop;
  94. getaddrinfo_do(req);
  95. }
  96. for (i = 0; i < ARRAY_SIZE(fs_reqs); i++) {
  97. struct fs_req* req = fs_reqs + i;
  98. req->counter = 16;
  99. req->loop = loop;
  100. fs_do(req);
  101. }
  102. r = uv_run(loop, UV_RUN_DEFAULT);
  103. ASSERT(r == 0);
  104. uv_loop_delete(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);
  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. }