test-thread.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 void getaddrinfo_do(struct getaddrinfo_req* req) {
  50. int r;
  51. r = uv_getaddrinfo(req->loop,
  52. &req->handle,
  53. getaddrinfo_cb,
  54. "localhost",
  55. NULL,
  56. NULL);
  57. ASSERT(r == 0);
  58. }
  59. static void getaddrinfo_cb(uv_getaddrinfo_t* handle,
  60. int status,
  61. struct addrinfo* res) {
  62. struct getaddrinfo_req* req;
  63. ASSERT(status == 0);
  64. req = container_of(handle, struct getaddrinfo_req, handle);
  65. uv_freeaddrinfo(res);
  66. if (--req->counter)
  67. getaddrinfo_do(req);
  68. }
  69. static void fs_do(struct fs_req* req) {
  70. int r;
  71. r = uv_fs_stat(req->loop, &req->handle, ".", fs_cb);
  72. ASSERT(r == 0);
  73. }
  74. static void fs_cb(uv_fs_t* handle) {
  75. struct fs_req* req = container_of(handle, struct fs_req, handle);
  76. uv_fs_req_cleanup(handle);
  77. if (--req->counter)
  78. fs_do(req);
  79. }
  80. static void do_work(void* arg) {
  81. struct getaddrinfo_req getaddrinfo_reqs[16];
  82. struct fs_req fs_reqs[16];
  83. uv_loop_t* loop;
  84. size_t i;
  85. int r;
  86. struct test_thread* thread = arg;
  87. loop = uv_loop_new();
  88. ASSERT(loop != NULL);
  89. for (i = 0; i < ARRAY_SIZE(getaddrinfo_reqs); i++) {
  90. struct getaddrinfo_req* req = getaddrinfo_reqs + i;
  91. req->counter = 16;
  92. req->loop = loop;
  93. getaddrinfo_do(req);
  94. }
  95. for (i = 0; i < ARRAY_SIZE(fs_reqs); i++) {
  96. struct fs_req* req = fs_reqs + i;
  97. req->counter = 16;
  98. req->loop = loop;
  99. fs_do(req);
  100. }
  101. r = uv_run(loop, UV_RUN_DEFAULT);
  102. ASSERT(r == 0);
  103. uv_loop_delete(loop);
  104. thread->thread_called = 1;
  105. }
  106. static void thread_entry(void* arg) {
  107. ASSERT(arg == (void *) 42);
  108. thread_called++;
  109. }
  110. TEST_IMPL(thread_create) {
  111. uv_thread_t tid;
  112. int r;
  113. r = uv_thread_create(&tid, thread_entry, (void *) 42);
  114. ASSERT(r == 0);
  115. r = uv_thread_join(&tid);
  116. ASSERT(r == 0);
  117. ASSERT(thread_called == 1);
  118. return 0;
  119. }
  120. /* Hilariously bad test name. Run a lot of tasks in the thread pool and verify
  121. * that each "finished" callback is run in its originating thread.
  122. */
  123. TEST_IMPL(threadpool_multiple_event_loops) {
  124. struct test_thread threads[8];
  125. size_t i;
  126. int r;
  127. memset(threads, 0, sizeof(threads));
  128. for (i = 0; i < ARRAY_SIZE(threads); i++) {
  129. r = uv_thread_create(&threads[i].thread_id, do_work, &threads[i]);
  130. ASSERT(r == 0);
  131. }
  132. for (i = 0; i < ARRAY_SIZE(threads); i++) {
  133. r = uv_thread_join(&threads[i].thread_id);
  134. ASSERT(r == 0);
  135. ASSERT(threads[i].thread_called);
  136. }
  137. return 0;
  138. }