xref: /third_party/libuv/test/test-embed.c (revision e66f31c5)
1/* Copyright libuv project 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#if !defined(_WIN32) && !defined(_AIX)
29#include <poll.h>
30#endif
31
32static uv_async_t async;
33static uv_barrier_t barrier;
34
35
36static void thread_main(void* arg) {
37  ASSERT_LE(0, uv_barrier_wait(&barrier));
38  uv_sleep(250);
39  ASSERT_OK(uv_async_send(&async));
40}
41
42
43static void async_cb(uv_async_t* handle) {
44  uv_close((uv_handle_t*) handle, NULL);
45}
46
47
48TEST_IMPL(embed) {
49  uv_thread_t thread;
50  uv_loop_t* loop;
51
52  loop = uv_default_loop();
53  ASSERT_OK(uv_async_init(loop, &async, async_cb));
54  ASSERT_OK(uv_barrier_init(&barrier, 2));
55  ASSERT_OK(uv_thread_create(&thread, thread_main, NULL));
56  ASSERT_LE(0, uv_barrier_wait(&barrier));
57
58  while (uv_loop_alive(loop)) {
59#if defined(_WIN32) || defined(_AIX)
60    ASSERT_LE(0, uv_run(loop, UV_RUN_ONCE));
61#else
62    int rc;
63    do {
64      struct pollfd p;
65      p.fd = uv_backend_fd(loop);
66      p.events = POLLIN;
67      p.revents = 0;
68      rc = poll(&p, 1, uv_backend_timeout(loop));
69    } while (rc == -1 && errno == EINTR);
70    ASSERT_LE(0, uv_run(loop, UV_RUN_NOWAIT));
71#endif
72  }
73
74  ASSERT_OK(uv_thread_join(&thread));
75  uv_barrier_destroy(&barrier);
76
77  MAKE_VALGRIND_HAPPY(loop);
78  return 0;
79}
80