Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
uv: upgrade to ef811b1
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Nov 28, 2011
1 parent 2445fb8 commit 62944f8
Show file tree
Hide file tree
Showing 18 changed files with 418 additions and 26 deletions.
1 change: 1 addition & 0 deletions deps/uv/include/uv-private/uv-unix.h
Expand Up @@ -44,6 +44,7 @@ typedef struct {

typedef int uv_file;

typedef pthread_t uv_thread_t;
typedef pthread_mutex_t uv_mutex_t;
typedef pthread_rwlock_t uv_rwlock_t;

Expand Down
2 changes: 2 additions & 0 deletions deps/uv/include/uv-private/uv-win.h
Expand Up @@ -137,6 +137,8 @@ typedef struct uv_buf_t {

typedef int uv_file;

typedef HANDLE uv_thread_t;

typedef CRITICAL_SECTION uv_mutex_t;

typedef union {
Expand Down
26 changes: 26 additions & 0 deletions deps/uv/include/uv.h
Expand Up @@ -370,6 +370,21 @@ UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
UV_EXTERN uv_buf_t uv_buf_init(char* base, size_t len);


/*
* Utility function. Copies up to `size` characters from `src` to `dst`
* and ensures that `dst` is properly NUL terminated unless `size` is zero.
*/
UV_EXTERN size_t uv_strlcpy(char* dst, const char* src, size_t size);

/*
* Utility function. Appends `src` to `dst` and ensures that `dst` is
* properly NUL terminated unless `size` is zero or `dst` does not
* contain a NUL byte. `size` is the total length of `dst` so at most
* `size - strlen(dst) - 1` characters will be copied from `src`.
*/
UV_EXTERN size_t uv_strlcat(char* dst, const char* src, size_t size);


#define UV_STREAM_FIELDS \
/* number of bytes queued for writing */ \
size_t write_queue_size; \
Expand Down Expand Up @@ -1242,12 +1257,19 @@ UV_EXTERN uv_err_t uv_dlclose(uv_lib_t library);
*/
UV_EXTERN uv_err_t uv_dlsym(uv_lib_t library, const char* name, void** ptr);

/*
* The mutex functions return 0 on success, -1 on error
* (unless the return type is void, of course).
*/
UV_EXTERN int uv_mutex_init(uv_mutex_t* handle);
UV_EXTERN void uv_mutex_destroy(uv_mutex_t* handle);
UV_EXTERN void uv_mutex_lock(uv_mutex_t* handle);
UV_EXTERN int uv_mutex_trylock(uv_mutex_t* handle);
UV_EXTERN void uv_mutex_unlock(uv_mutex_t* handle);

/*
* Same goes for the read/write lock functions.
*/
UV_EXTERN int uv_rwlock_init(uv_rwlock_t* rwlock);
UV_EXTERN void uv_rwlock_destroy(uv_rwlock_t* rwlock);
UV_EXTERN void uv_rwlock_rdlock(uv_rwlock_t* rwlock);
Expand All @@ -1257,6 +1279,10 @@ UV_EXTERN void uv_rwlock_wrlock(uv_rwlock_t* rwlock);
UV_EXTERN int uv_rwlock_trywrlock(uv_rwlock_t* rwlock);
UV_EXTERN void uv_rwlock_wrunlock(uv_rwlock_t* rwlock);

UV_EXTERN int uv_thread_create(uv_thread_t *tid,
void (*entry)(void *arg), void *arg);
UV_EXTERN int uv_thread_join(uv_thread_t *tid);

/* the presence of these unions force similar struct layout */
union uv_any_handle {
uv_tcp_t tcp;
Expand Down
18 changes: 0 additions & 18 deletions deps/uv/src/unix/core.c
Expand Up @@ -827,21 +827,3 @@ int uv__cloexec(int fd, int set) {
return 0;
#endif
}


/* TODO move to uv-common.c? */
size_t uv__strlcpy(char* dst, const char* src, size_t size) {
const char *org;

if (size == 0) {
return 0;
}

org = src;
while (--size && *src) {
*dst++ = *src++;
}
*dst = '\0';

return src - org;
}
2 changes: 0 additions & 2 deletions deps/uv/src/unix/internal.h
Expand Up @@ -142,8 +142,6 @@ enum {
UV_TCP_KEEPALIVE = 0x100 /* Turn on keep-alive. */
};

size_t uv__strlcpy(char* dst, const char* src, size_t size);

int uv__close(int fd);
void uv__req_init(uv_req_t*);
void uv__handle_init(uv_loop_t* loop, uv_handle_t* handle, uv_handle_type type);
Expand Down
4 changes: 2 additions & 2 deletions deps/uv/src/unix/pipe.c
Expand Up @@ -74,7 +74,7 @@ int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
}

memset(&saddr, 0, sizeof saddr);
uv__strlcpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
uv_strlcpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
saddr.sun_family = AF_UNIX;

if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr) == -1) {
Expand Down Expand Up @@ -197,7 +197,7 @@ void uv_pipe_connect(uv_connect_t* req,
}

memset(&saddr, 0, sizeof saddr);
uv__strlcpy(saddr.sun_path, name, sizeof(saddr.sun_path));
uv_strlcpy(saddr.sun_path, name, sizeof(saddr.sun_path));
saddr.sun_family = AF_UNIX;

/* We don't check for EINPROGRESS. Think about it: the socket
Expand Down
4 changes: 2 additions & 2 deletions deps/uv/src/unix/stream.c
Expand Up @@ -145,7 +145,7 @@ void uv__stream_destroy(uv_stream_t* stream) {

req = ngx_queue_data(q, uv_write_t, queue);
if (req->cb) {
uv__set_artificial_error(stream->loop, req->error);
uv__set_sys_error(stream->loop, req->error);
req->cb(req, req->error ? -1 : 0);
}
}
Expand Down Expand Up @@ -490,7 +490,7 @@ static void uv__write_callbacks(uv_stream_t* stream) {

/* NOTE: call callback AFTER freeing the request data. */
if (req->cb) {
uv__set_artificial_error(stream->loop, req->error);
uv__set_sys_error(stream->loop, req->error);
req->cb(req, req->error ? -1 : 0);
}

Expand Down
10 changes: 9 additions & 1 deletion deps/uv/src/unix/thread.c
Expand Up @@ -23,9 +23,9 @@
#include "internal.h"

#include <pthread.h>
#include <assert.h>
#include <errno.h>


#ifdef NDEBUG
# define CHECK(r) ((void) (r))
#else
Expand All @@ -40,6 +40,14 @@
#endif


int uv_thread_join(uv_thread_t *tid) {
if (pthread_join(*tid, NULL))
return -1;
else
return 0;
}


int uv_mutex_init(uv_mutex_t* mutex) {
if (pthread_mutex_init(mutex, NULL))
return -1;
Expand Down
86 changes: 86 additions & 0 deletions deps/uv/src/uv-common.c
Expand Up @@ -24,6 +24,7 @@

#include <assert.h>
#include <stddef.h> /* NULL */
#include <stdlib.h> /* malloc */
#include <string.h> /* memset */

/* use inet_pton from c-ares if necessary */
Expand All @@ -40,6 +41,41 @@ uv_counters_t* uv_counters() {
}


size_t uv_strlcpy(char* dst, const char* src, size_t size) {
size_t n;

if (size == 0)
return 0;

for (n = 0; n < (size - 1) && *src != '\0'; n++)
*dst++ = *src++;

*dst = '\0';

return n;
}


size_t uv_strlcat(char* dst, const char* src, size_t size) {
size_t n;

if (size == 0)
return 0;

for (n = 0; n < size && *dst != '\0'; n++, dst++);

if (n == size)
return n;

while (n < (size - 1) && *src != '\0')
n++, *dst++ = *src++;

*dst = '\0';

return n;
}


uv_buf_t uv_buf_init(char* base, size_t len) {
uv_buf_t buf;
buf.base = base;
Expand Down Expand Up @@ -261,3 +297,53 @@ int uv_tcp_connect6(uv_connect_t* req,

return uv__tcp_connect6(req, handle, address, cb);
}


#ifdef _WIN32
static DWORD __stdcall uv__thread_start(void *ctx_v)
#else
static void *uv__thread_start(void *ctx_v)
#endif
{
void (*entry)(void *arg);
void *arg;

struct {
void (*entry)(void *arg);
void *arg;
} *ctx;

ctx = ctx_v;
arg = ctx->arg;
entry = ctx->entry;
free(ctx);
entry(arg);

return 0;
}


int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
struct {
void (*entry)(void *arg);
void *arg;
} *ctx;

if ((ctx = malloc(sizeof *ctx)) == NULL)
return -1;

ctx->entry = entry;
ctx->arg = arg;

#ifdef _WIN32
*tid = (HANDLE) _beginthreadex(NULL, 0, uv__thread_start, ctx, 0, NULL);
if (*tid == 0) {
#else
if (pthread_create(tid, NULL, uv__thread_start, ctx)) {
#endif
free(ctx);
return -1;
}

return 0;
}
12 changes: 12 additions & 0 deletions deps/uv/src/win/thread.c
Expand Up @@ -101,6 +101,18 @@ void uv_once(uv_once_t* guard, void (*callback)(void)) {
uv__once_inner(guard, callback);
}


int uv_thread_join(uv_thread_t *tid) {
if (WaitForSingleObject(*tid, INFINITE))
return -1;
else {
CloseHandle(*tid);
*tid = 0;
return 0;
}
}


int uv_mutex_init(uv_mutex_t* mutex) {
InitializeCriticalSection(mutex);
return 0;
Expand Down
2 changes: 2 additions & 0 deletions deps/uv/test/benchmark-list.h
Expand Up @@ -43,6 +43,7 @@ BENCHMARK_DECLARE (udp_packet_storm_1000v1000)
BENCHMARK_DECLARE (gethostbyname)
BENCHMARK_DECLARE (getaddrinfo)
BENCHMARK_DECLARE (spawn)
BENCHMARK_DECLARE (thread_create)
HELPER_DECLARE (tcp4_blackhole_server)
HELPER_DECLARE (tcp_pump_server)
HELPER_DECLARE (pipe_pump_server)
Expand Down Expand Up @@ -100,4 +101,5 @@ TASK_LIST_START
BENCHMARK_ENTRY (getaddrinfo)

BENCHMARK_ENTRY (spawn)
BENCHMARK_ENTRY (thread_create)
TASK_LIST_END
64 changes: 64 additions & 0 deletions deps/uv/test/benchmark-thread.c
@@ -0,0 +1,64 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "uv.h"
#include "task.h"

#include <stdio.h>
#include <stdlib.h>

#define NUM_THREADS (100 * 1000)

static volatile int num_threads;


static void thread_entry(void* arg) {
ASSERT(arg == (void *) 42);
num_threads++;
/* FIXME write barrier? */
}


BENCHMARK_IMPL(thread_create) {
uint64_t start_time;
double duration;
uv_thread_t tid;
int i, r;

start_time = uv_hrtime();

for (i = 0; i < NUM_THREADS; i++) {
r = uv_thread_create(&tid, thread_entry, (void *) 42);
ASSERT(r == 0);

r = uv_thread_join(&tid);
ASSERT(r == 0);
}

duration = (uv_hrtime() - start_time) / 1e9;

ASSERT(num_threads == NUM_THREADS);

printf("%d threads created in %.2f seconds (%.0f/s)\n",
NUM_THREADS, duration, NUM_THREADS / duration);

return 0;
}

0 comments on commit 62944f8

Please sign in to comment.