test-embed.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. {
  70. struct epoll_event ev;
  71. r = epoll_wait(fd, &ev, 1, timeout);
  72. }
  73. #endif
  74. } while (r == -1 && errno == EINTR);
  75. uv_async_send(&embed_async);
  76. uv_sem_wait(&embed_sem);
  77. }
  78. }
  79. static void embed_cb(uv_async_t* async, int status) {
  80. uv_run(uv_default_loop(), UV_RUN_ONCE);
  81. uv_sem_post(&embed_sem);
  82. }
  83. static void embed_timer_cb(uv_timer_t* timer, int status) {
  84. embed_timer_called++;
  85. embed_closed = 1;
  86. uv_close((uv_handle_t*) &embed_async, NULL);
  87. }
  88. #endif
  89. TEST_IMPL(embed) {
  90. #if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
  91. uv_loop_t* external;
  92. external = uv_loop_new();
  93. ASSERT(external != NULL);
  94. embed_timer_called = 0;
  95. embed_closed = 0;
  96. uv_async_init(external, &embed_async, embed_cb);
  97. /* Start timer in default loop */
  98. uv_timer_init(uv_default_loop(), &embed_timer);
  99. uv_timer_start(&embed_timer, embed_timer_cb, 250, 0);
  100. /* Start worker that will interrupt external loop */
  101. uv_sem_init(&embed_sem, 0);
  102. uv_thread_create(&embed_thread, embed_thread_runner, NULL);
  103. /* But run external loop */
  104. uv_run(external, UV_RUN_DEFAULT);
  105. uv_thread_join(&embed_thread);
  106. uv_loop_delete(external);
  107. ASSERT(embed_timer_called == 1);
  108. #endif
  109. return 0;
  110. }