libuv-svace-build
120 строк · 3.3 Кб
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 "task.h"23#include "uv.h"24
25#include <stdio.h>26#include <stdlib.h>27
28#define NUM_PINGS (1000 * 1000)29#define ACCESS_ONCE(type, var) (*(volatile type*) &(var))30
31static unsigned int callbacks;32static volatile int done;33
34static const char running[] = "running";35static const char stop[] = "stop";36static const char stopped[] = "stopped";37
38
39static void async_cb(uv_async_t* handle) {40if (++callbacks == NUM_PINGS) {41/* Tell the pummel thread to stop. */42ACCESS_ONCE(const char*, handle->data) = stop;43
44/* Wait for the pummel thread to acknowledge that it has stoppped. */45while (ACCESS_ONCE(const char*, handle->data) != stopped)46uv_sleep(0);47
48uv_close((uv_handle_t*) handle, NULL);49}50}
51
52
53static void pummel(void* arg) {54uv_async_t* handle = (uv_async_t*) arg;55
56while (ACCESS_ONCE(const char*, handle->data) == running)57uv_async_send(handle);58
59/* Acknowledge that we've seen handle->data change. */60ACCESS_ONCE(const char*, handle->data) = stopped;61}
62
63
64static int test_async_pummel(int nthreads) {65char fmtbuf[2][32];66uv_thread_t* tids;67uv_async_t handle;68uint64_t time;69int i;70
71tids = calloc(nthreads, sizeof(tids[0]));72ASSERT_NOT_NULL(tids);73
74ASSERT_OK(uv_async_init(uv_default_loop(), &handle, async_cb));75ACCESS_ONCE(const char*, handle.data) = running;76
77for (i = 0; i < nthreads; i++)78ASSERT_OK(uv_thread_create(tids + i, pummel, &handle));79
80time = uv_hrtime();81
82ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));83
84time = uv_hrtime() - time;85done = 1;86
87for (i = 0; i < nthreads; i++)88ASSERT_OK(uv_thread_join(tids + i));89
90printf("async_pummel_%d: %s callbacks in %.2f seconds (%s/sec)\n",91nthreads,92fmt(&fmtbuf[0], callbacks),93time / 1e9,94fmt(&fmtbuf[1], callbacks / (time / 1e9)));95
96free(tids);97
98MAKE_VALGRIND_HAPPY(uv_default_loop());99return 0;100}
101
102
103BENCHMARK_IMPL(async_pummel_1) {104return test_async_pummel(1);105}
106
107
108BENCHMARK_IMPL(async_pummel_2) {109return test_async_pummel(2);110}
111
112
113BENCHMARK_IMPL(async_pummel_4) {114return test_async_pummel(4);115}
116
117
118BENCHMARK_IMPL(async_pummel_8) {119return test_async_pummel(8);120}
121