1 1.1 christos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 1.1 christos * 3 1.1 christos * Permission is hereby granted, free of charge, to any person obtaining a copy 4 1.1 christos * of this software and associated documentation files (the "Software"), to 5 1.1 christos * deal in the Software without restriction, including without limitation the 6 1.1 christos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 1.1 christos * sell copies of the Software, and to permit persons to whom the Software is 8 1.1 christos * furnished to do so, subject to the following conditions: 9 1.1 christos * 10 1.1 christos * The above copyright notice and this permission notice shall be included in 11 1.1 christos * all copies or substantial portions of the Software. 12 1.1 christos * 13 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 1.1 christos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 1.1 christos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 1.1 christos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 1.1 christos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 1.1 christos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 1.1 christos * IN THE SOFTWARE. 20 1.1 christos */ 21 1.1 christos 22 1.1 christos #include "uv.h" 23 1.1 christos #include "task.h" 24 1.1 christos #include <stdio.h> 25 1.1 christos #include <stdlib.h> 26 1.1 christos 27 1.1 christos static uv_thread_t thread; 28 1.1 christos static uv_mutex_t mutex; 29 1.1 christos 30 1.1 christos static uv_prepare_t prepare; 31 1.1 christos static uv_async_t async; 32 1.1 christos 33 1.1 christos static volatile int async_cb_called; 34 1.1 christos static int prepare_cb_called; 35 1.1 christos static int close_cb_called; 36 1.1 christos 37 1.1 christos 38 1.1 christos static void thread_cb(void *arg) { 39 1.1 christos int n; 40 1.1 christos int r; 41 1.1 christos 42 1.1 christos for (;;) { 43 1.1 christos uv_mutex_lock(&mutex); 44 1.1 christos n = async_cb_called; 45 1.1 christos uv_mutex_unlock(&mutex); 46 1.1 christos 47 1.1 christos if (n == 3) { 48 1.1 christos break; 49 1.1 christos } 50 1.1 christos 51 1.1 christos r = uv_async_send(&async); 52 1.1.1.3 christos ASSERT_OK(r); 53 1.1 christos 54 1.1 christos /* Work around a bug in Valgrind. 55 1.1 christos * 56 1.1 christos * Valgrind runs threads not in parallel but sequentially, i.e. one after 57 1.1 christos * the other. It also doesn't preempt them, instead it depends on threads 58 1.1 christos * yielding voluntarily by making a syscall. 59 1.1 christos * 60 1.1 christos * That never happens here: the pipe that is associated with the async 61 1.1 christos * handle is written to once but that's too early for Valgrind's scheduler 62 1.1 christos * to kick in. Afterwards, the thread busy-loops, starving the main thread. 63 1.1 christos * Therefore, we yield. 64 1.1 christos * 65 1.1 christos * This behavior has been observed with Valgrind 3.7.0 and 3.9.0. 66 1.1 christos */ 67 1.1 christos uv_sleep(0); 68 1.1 christos } 69 1.1 christos } 70 1.1 christos 71 1.1 christos 72 1.1 christos static void close_cb(uv_handle_t* handle) { 73 1.1.1.2 christos ASSERT_NOT_NULL(handle); 74 1.1 christos close_cb_called++; 75 1.1 christos } 76 1.1 christos 77 1.1 christos 78 1.1 christos static void async_cb(uv_async_t* handle) { 79 1.1 christos int n; 80 1.1 christos 81 1.1.1.3 christos ASSERT_PTR_EQ(handle, &async); 82 1.1 christos 83 1.1 christos uv_mutex_lock(&mutex); 84 1.1 christos n = ++async_cb_called; 85 1.1 christos uv_mutex_unlock(&mutex); 86 1.1 christos 87 1.1 christos if (n == 3) { 88 1.1 christos uv_close((uv_handle_t*)&async, close_cb); 89 1.1 christos uv_close((uv_handle_t*)&prepare, close_cb); 90 1.1 christos } 91 1.1 christos } 92 1.1 christos 93 1.1 christos 94 1.1 christos static void prepare_cb(uv_prepare_t* handle) { 95 1.1 christos int r; 96 1.1 christos 97 1.1.1.3 christos ASSERT_PTR_EQ(handle, &prepare); 98 1.1 christos 99 1.1 christos if (prepare_cb_called++) 100 1.1 christos return; 101 1.1 christos 102 1.1 christos r = uv_thread_create(&thread, thread_cb, NULL); 103 1.1.1.3 christos ASSERT_OK(r); 104 1.1 christos uv_mutex_unlock(&mutex); 105 1.1 christos } 106 1.1 christos 107 1.1 christos 108 1.1 christos TEST_IMPL(async) { 109 1.1 christos int r; 110 1.1 christos 111 1.1 christos r = uv_mutex_init(&mutex); 112 1.1.1.3 christos ASSERT_OK(r); 113 1.1 christos uv_mutex_lock(&mutex); 114 1.1 christos 115 1.1 christos r = uv_prepare_init(uv_default_loop(), &prepare); 116 1.1.1.3 christos ASSERT_OK(r); 117 1.1 christos r = uv_prepare_start(&prepare, prepare_cb); 118 1.1.1.3 christos ASSERT_OK(r); 119 1.1 christos 120 1.1 christos r = uv_async_init(uv_default_loop(), &async, async_cb); 121 1.1.1.3 christos ASSERT_OK(r); 122 1.1 christos 123 1.1 christos r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); 124 1.1.1.3 christos ASSERT_OK(r); 125 1.1 christos 126 1.1.1.3 christos ASSERT_GT(prepare_cb_called, 0); 127 1.1.1.3 christos ASSERT_EQ(3, async_cb_called); 128 1.1.1.3 christos ASSERT_EQ(2, close_cb_called); 129 1.1 christos 130 1.1.1.3 christos ASSERT_OK(uv_thread_join(&thread)); 131 1.1 christos 132 1.1.1.3 christos MAKE_VALGRIND_HAPPY(uv_default_loop()); 133 1.1 christos return 0; 134 1.1 christos } 135