libuv-svace-build
96 строк · 2.6 Кб
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
25#ifdef _WIN3226
27TEST_IMPL(eintr_handling) {28RETURN_SKIP("Test not implemented on Windows.");29}
30
31#else /* !_WIN32 */32
33#include <string.h>34#include <unistd.h>35
36static uv_loop_t* loop;37static uv_fs_t read_req;38static uv_buf_t iov;39
40static char buf[32];41static char test_buf[] = "test-buffer\n";42int pipe_fds[2];43
44struct thread_ctx {45uv_barrier_t barrier;46int fd;47};48
49static void thread_main(void* arg) {50int nwritten;51ASSERT_OK(kill(getpid(), SIGUSR1));52
53do54nwritten = write(pipe_fds[1], test_buf, sizeof(test_buf));55while (nwritten == -1 && errno == EINTR);56
57ASSERT_EQ(nwritten, sizeof(test_buf));58}
59
60static void sig_func(uv_signal_t* handle, int signum) {61uv_signal_stop(handle);62}
63
64TEST_IMPL(eintr_handling) {65struct thread_ctx ctx;66uv_thread_t thread;67uv_signal_t signal;68int nread;69
70iov = uv_buf_init(buf, sizeof(buf));71loop = uv_default_loop();72
73ASSERT_OK(uv_signal_init(loop, &signal));74ASSERT_OK(uv_signal_start(&signal, sig_func, SIGUSR1));75
76ASSERT_OK(pipe(pipe_fds));77ASSERT_OK(uv_thread_create(&thread, thread_main, &ctx));78
79nread = uv_fs_read(loop, &read_req, pipe_fds[0], &iov, 1, -1, NULL);80
81ASSERT_EQ(nread, sizeof(test_buf));82ASSERT_OK(strcmp(buf, test_buf));83
84ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));85
86ASSERT_OK(close(pipe_fds[0]));87ASSERT_OK(close(pipe_fds[1]));88uv_close((uv_handle_t*) &signal, NULL);89
90ASSERT_OK(uv_thread_join(&thread));91
92MAKE_VALGRIND_HAPPY(loop);93return 0;94}
95
96#endif /* !_WIN32 */97