test-embed.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <errno.h>
  26. #ifndef HAVE_KQUEUE
  27. # if defined(__APPLE__) || \
  28. defined(__DragonFly__) || \
  29. defined(__FreeBSD__) || \
  30. defined(__OpenBSD__) || \
  31. defined(__NetBSD__)
  32. # define HAVE_KQUEUE 1
  33. # endif
  34. #endif
  35. #ifndef HAVE_EPOLL
  36. # if defined(__linux__)
  37. # define HAVE_EPOLL 1
  38. # endif
  39. #endif
  40. #if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
  41. #if defined(HAVE_KQUEUE)
  42. # include <sys/types.h>
  43. # include <sys/event.h>
  44. # include <sys/time.h>
  45. #endif
  46. #if defined(HAVE_EPOLL)
  47. # include <sys/epoll.h>
  48. #endif
  49. static uv_thread_t embed_thread;
  50. static uv_sem_t embed_sem;
  51. static uv_timer_t embed_timer;
  52. static uv_async_t embed_async;
  53. static volatile int embed_closed;
  54. static int embed_timer_called;
  55. static void embed_thread_runner(void* arg) {
  56. int r;
  57. int fd;
  58. int timeout;
  59. while (!embed_closed) {
  60. fd = uv_backend_fd(uv_default_loop());
  61. timeout = uv_backend_timeout(uv_default_loop());
  62. do {
  63. #if defined(HAVE_KQUEUE)
  64. struct timespec ts;
  65. ts.tv_sec = timeout / 1000;
  66. ts.tv_nsec = (timeout % 1000) * 1000000;
  67. r = kevent(fd, NULL, 0, NULL, 0, &ts);
  68. #elif defined(HAVE_EPOLL)
  69. r = epoll_wait(fd, NULL, 0, timeout);
  70. #endif
  71. } while (r == -1 && errno == EINTR);
  72. uv_async_send(&embed_async);
  73. uv_sem_wait(&embed_sem);
  74. }
  75. }
  76. static void embed_cb(uv_async_t* async, int status) {
  77. uv_run(uv_default_loop(), UV_RUN_ONCE);
  78. uv_sem_post(&embed_sem);
  79. }
  80. static void embed_timer_cb(uv_timer_t* timer, int status) {
  81. embed_timer_called++;
  82. embed_closed = 1;
  83. uv_close((uv_handle_t*) &embed_async, NULL);
  84. }
  85. #endif
  86. TEST_IMPL(embed) {
  87. #if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
  88. uv_loop_t* external;
  89. external = uv_loop_new();
  90. ASSERT(external != NULL);
  91. embed_timer_called = 0;
  92. embed_closed = 0;
  93. uv_async_init(external, &embed_async, embed_cb);
  94. /* Start timer in default loop */
  95. uv_timer_init(uv_default_loop(), &embed_timer);
  96. uv_timer_start(&embed_timer, embed_timer_cb, 250, 0);
  97. /* Start worker that will interrupt external loop */
  98. uv_sem_init(&embed_sem, 0);
  99. uv_thread_create(&embed_thread, embed_thread_runner, NULL);
  100. /* But run external loop */
  101. uv_run(external, UV_RUN_DEFAULT);
  102. uv_thread_join(&embed_thread);
  103. uv_loop_delete(external);
  104. ASSERT(embed_timer_called == 1);
  105. #endif
  106. return 0;
  107. }