PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test-async.c

http://github.com/joyent/libuv
C | 136 lines | 72 code | 31 blank | 33 comment | 21 complexity | e515161680ce151d69ef0e7f2ab914c0 MD5 | raw 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
 27static uv_thread_t thread;
 28static uv_mutex_t mutex;
 29
 30static uv_prepare_t prepare;
 31static uv_async_t async;
 32
 33static volatile int async_cb_called;
 34static int prepare_cb_called;
 35static int close_cb_called;
 36
 37
 38void thread_cb(void *arg) {
 39  int n;
 40  int r;
 41
 42  for (;;) {
 43    uv_mutex_lock(&mutex);
 44    n = async_cb_called;
 45    uv_mutex_unlock(&mutex);
 46
 47    if (n == 3) {
 48      break;
 49    }
 50
 51    r = uv_async_send(&async);
 52    ASSERT(r == 0);
 53
 54    /* Work around a bug in Valgrind.
 55     *
 56     * Valgrind runs threads not in parallel but sequentially, i.e. one after
 57     * the other. It also doesn't preempt them, instead it depends on threads
 58     * yielding voluntarily by making a syscall.
 59     *
 60     * That never happens here: the pipe that is associated with the async
 61     * handle is written to once but that's too early for Valgrind's scheduler
 62     * to kick in. Afterwards, the thread busy-loops, starving the main thread.
 63     * Therefore, we yield.
 64     *
 65     * This behavior has been observed with Valgrind 3.7.0 and 3.9.0.
 66     */
 67    uv_sleep(0);
 68  }
 69}
 70
 71
 72static void close_cb(uv_handle_t* handle) {
 73  ASSERT(handle != NULL);
 74  close_cb_called++;
 75}
 76
 77
 78static void async_cb(uv_async_t* handle, int status) {
 79  int n;
 80
 81  ASSERT(handle == &async);
 82  ASSERT(status == 0);
 83
 84  uv_mutex_lock(&mutex);
 85  n = ++async_cb_called;
 86  uv_mutex_unlock(&mutex);
 87
 88  if (n == 3) {
 89    uv_close((uv_handle_t*)&async, close_cb);
 90    uv_close((uv_handle_t*)&prepare, close_cb);
 91  }
 92}
 93
 94
 95static void prepare_cb(uv_prepare_t* handle, int status) {
 96  int r;
 97
 98  ASSERT(handle == &prepare);
 99  ASSERT(status == 0);
100
101  if (prepare_cb_called++)
102    return;
103
104  r = uv_thread_create(&thread, thread_cb, NULL);
105  ASSERT(r == 0);
106  uv_mutex_unlock(&mutex);
107}
108
109
110TEST_IMPL(async) {
111  int r;
112
113  r = uv_mutex_init(&mutex);
114  ASSERT(r == 0);
115  uv_mutex_lock(&mutex);
116
117  r = uv_prepare_init(uv_default_loop(), &prepare);
118  ASSERT(r == 0);
119  r = uv_prepare_start(&prepare, prepare_cb);
120  ASSERT(r == 0);
121
122  r = uv_async_init(uv_default_loop(), &async, async_cb);
123  ASSERT(r == 0);
124
125  r = uv_run(uv_default_loop());
126  ASSERT(r == 0);
127
128  ASSERT(prepare_cb_called > 0);
129  ASSERT(async_cb_called == 3);
130  ASSERT(close_cb_called == 2);
131
132  ASSERT(0 == uv_thread_join(&thread));
133
134  MAKE_VALGRIND_HAPPY();
135  return 0;
136}