test-embed.c
Go to the documentation of this file.
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 
22 #include "uv.h"
23 #include "task.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 
28 #ifndef HAVE_KQUEUE
29 # if defined(__APPLE__) || \
30  defined(__DragonFly__) || \
31  defined(__FreeBSD__) || \
32  defined(__FreeBSD_kernel__) || \
33  defined(__OpenBSD__) || \
34  defined(__NetBSD__)
35 # define HAVE_KQUEUE 1
36 # endif
37 #endif
38 
39 #ifndef HAVE_EPOLL
40 # if defined(__linux__)
41 # define HAVE_EPOLL 1
42 # endif
43 #endif
44 
45 #if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
46 
47 #if defined(HAVE_KQUEUE)
48 # include <sys/types.h>
49 # include <sys/event.h>
50 # include <sys/time.h>
51 #endif
52 
53 #if defined(HAVE_EPOLL)
54 # include <sys/epoll.h>
55 #endif
56 
57 static uv_thread_t embed_thread;
58 static uv_sem_t embed_sem;
59 static uv_timer_t embed_timer;
60 static uv_async_t embed_async;
61 static volatile int embed_closed;
62 
63 static int embed_timer_called;
64 
65 
66 static void embed_thread_runner(void* arg) {
67  int r;
68  int fd;
69  int timeout;
70 
71  while (!embed_closed) {
74 
75  do {
76 #if defined(HAVE_KQUEUE)
77  struct timespec ts;
78  ts.tv_sec = timeout / 1000;
79  ts.tv_nsec = (timeout % 1000) * 1000000;
80  r = kevent(fd, NULL, 0, NULL, 0, &ts);
81 #elif defined(HAVE_EPOLL)
82  {
83  struct epoll_event ev;
84  r = epoll_wait(fd, &ev, 1, timeout);
85  }
86 #endif
87  } while (r == -1 && errno == EINTR);
88  uv_async_send(&embed_async);
89  uv_sem_wait(&embed_sem);
90  }
91 }
92 
93 
94 static void embed_cb(uv_async_t* async) {
96 
97  uv_sem_post(&embed_sem);
98 }
99 
100 
101 static void embed_timer_cb(uv_timer_t* timer) {
102  embed_timer_called++;
103  embed_closed = 1;
104 
105  uv_close((uv_handle_t*) &embed_async, NULL);
106 }
107 #endif
108 
109 
110 TEST_IMPL(embed) {
111 #if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL)
112  uv_loop_t external;
113 
114  ASSERT(0 == uv_loop_init(&external));
115 
116  embed_timer_called = 0;
117  embed_closed = 0;
118 
119  uv_async_init(&external, &embed_async, embed_cb);
120 
121  /* Start timer in default loop */
122  uv_timer_init(uv_default_loop(), &embed_timer);
123  uv_timer_start(&embed_timer, embed_timer_cb, 250, 0);
124 
125  /* Start worker that will interrupt external loop */
126  uv_sem_init(&embed_sem, 0);
127  uv_thread_create(&embed_thread, embed_thread_runner, NULL);
128 
129  /* But run external loop */
130  uv_run(&external, UV_RUN_DEFAULT);
131 
132  uv_thread_join(&embed_thread);
133  uv_loop_close(&external);
134 
135  ASSERT(embed_timer_called == 1);
136 #endif
137 
138  return 0;
139 }
task.h
TEST_IMPL
TEST_IMPL(embed)
Definition: test-embed.c:110
ASSERT
#define ASSERT(expr)
Definition: task.h:102
uv_thread_join
UV_EXTERN int uv_thread_join(uv_thread_t *tid)
Definition: libuv/src/unix/thread.c:271
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_loop_close
UV_EXTERN int uv_loop_close(uv_loop_t *loop)
Definition: uv-common.c:761
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
uv_async_s
Definition: uv.h:834
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_backend_fd
UV_EXTERN int uv_backend_fd(const uv_loop_t *)
Definition: unix/core.c:324
epoll_event::fd
int fd
Definition: os390-syscalls.h:42
arg
Definition: cmdline.cc:40
uv_backend_timeout
UV_EXTERN int uv_backend_timeout(const uv_loop_t *)
Definition: unix/core.c:329
uv_loop_init
UV_EXTERN int uv_loop_init(uv_loop_t *loop)
Definition: loop.c:30
uv_thread_create
UV_EXTERN int uv_thread_create(uv_thread_t *tid, uv_thread_cb entry, void *arg)
Definition: libuv/src/unix/thread.c:209
epoll_wait
int epoll_wait(uv__os390_epoll *lst, struct epoll_event *events, int maxevents, int timeout)
Definition: os390-syscalls.c:284
uv_sem_t
UV_PLATFORM_SEM_T uv_sem_t
Definition: unix.h:137
uv_timer_s
Definition: uv.h:850
uv_sem_post
UV_EXTERN void uv_sem_post(uv_sem_t *sem)
Definition: libuv/src/unix/thread.c:669
uv_sem_init
UV_EXTERN int uv_sem_init(uv_sem_t *sem, unsigned int value)
Definition: libuv/src/unix/thread.c:649
uv.h
uv_async_init
UV_EXTERN int uv_async_init(uv_loop_t *, uv_async_t *async, uv_async_cb async_cb)
Definition: unix/async.c:44
async
uv_async_t async
Definition: libuv/docs/code/progress/main.c:8
epoll_event
Definition: os390-syscalls.h:40
fix_build_deps.r
r
Definition: fix_build_deps.py:491
UV_RUN_ONCE
@ UV_RUN_ONCE
Definition: uv.h:255
thread_ctx::fd
int fd
Definition: test-eintr-handling.c:46
uv_handle_s
Definition: uv.h:441
uv_timer_start
UV_EXTERN int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66
uv_thread_t
pthread_t uv_thread_t
Definition: unix.h:134
uv_loop_s
Definition: uv.h:1767
uv_sem_wait
UV_EXTERN void uv_sem_wait(uv_sem_t *sem)
Definition: libuv/src/unix/thread.c:677
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
errno.h
timer
static uv_timer_t timer
Definition: test-callback-stack.c:34
uv_async_send
UV_EXTERN int uv_async_send(uv_async_t *async)
Definition: unix/async.c:62


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:29