Wrap platform thread APIs.
This commit is contained in:
parent
5728bd4549
commit
8e4ed88bbe
@ -44,6 +44,7 @@ typedef struct {
|
|||||||
|
|
||||||
typedef int uv_file;
|
typedef int uv_file;
|
||||||
|
|
||||||
|
typedef pthread_t uv_thread_t;
|
||||||
typedef pthread_mutex_t uv_mutex_t;
|
typedef pthread_mutex_t uv_mutex_t;
|
||||||
typedef pthread_rwlock_t uv_rwlock_t;
|
typedef pthread_rwlock_t uv_rwlock_t;
|
||||||
|
|
||||||
|
|||||||
@ -137,6 +137,8 @@ typedef struct uv_buf_t {
|
|||||||
|
|
||||||
typedef int uv_file;
|
typedef int uv_file;
|
||||||
|
|
||||||
|
typedef HANDLE uv_thread_t;
|
||||||
|
|
||||||
typedef CRITICAL_SECTION uv_mutex_t;
|
typedef CRITICAL_SECTION uv_mutex_t;
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
|
|||||||
11
include/uv.h
11
include/uv.h
@ -1243,12 +1243,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);
|
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 int uv_mutex_init(uv_mutex_t* handle);
|
||||||
UV_EXTERN void uv_mutex_destroy(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 void uv_mutex_lock(uv_mutex_t* handle);
|
||||||
UV_EXTERN int uv_mutex_trylock(uv_mutex_t* handle);
|
UV_EXTERN int uv_mutex_trylock(uv_mutex_t* handle);
|
||||||
UV_EXTERN void uv_mutex_unlock(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 int uv_rwlock_init(uv_rwlock_t* rwlock);
|
||||||
UV_EXTERN void uv_rwlock_destroy(uv_rwlock_t* rwlock);
|
UV_EXTERN void uv_rwlock_destroy(uv_rwlock_t* rwlock);
|
||||||
UV_EXTERN void uv_rwlock_rdlock(uv_rwlock_t* rwlock);
|
UV_EXTERN void uv_rwlock_rdlock(uv_rwlock_t* rwlock);
|
||||||
@ -1258,6 +1265,10 @@ UV_EXTERN void uv_rwlock_wrlock(uv_rwlock_t* rwlock);
|
|||||||
UV_EXTERN int uv_rwlock_trywrlock(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 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 */
|
/* the presence of these unions force similar struct layout */
|
||||||
union uv_any_handle {
|
union uv_any_handle {
|
||||||
uv_tcp_t tcp;
|
uv_tcp_t tcp;
|
||||||
|
|||||||
@ -23,9 +23,9 @@
|
|||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
||||||
#ifdef NDEBUG
|
#ifdef NDEBUG
|
||||||
# define CHECK(r) ((void) (r))
|
# define CHECK(r) ((void) (r))
|
||||||
#else
|
#else
|
||||||
@ -40,6 +40,14 @@
|
|||||||
#endif
|
#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) {
|
int uv_mutex_init(uv_mutex_t* mutex) {
|
||||||
if (pthread_mutex_init(mutex, NULL))
|
if (pthread_mutex_init(mutex, NULL))
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@ -24,6 +24,7 @@
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stddef.h> /* NULL */
|
#include <stddef.h> /* NULL */
|
||||||
|
#include <stdlib.h> /* malloc */
|
||||||
#include <string.h> /* memset */
|
#include <string.h> /* memset */
|
||||||
|
|
||||||
/* use inet_pton from c-ares if necessary */
|
/* use inet_pton from c-ares if necessary */
|
||||||
@ -261,3 +262,53 @@ int uv_tcp_connect6(uv_connect_t* req,
|
|||||||
|
|
||||||
return uv__tcp_connect6(req, handle, address, cb);
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@ -101,6 +101,18 @@ void uv_once(uv_once_t* guard, void (*callback)(void)) {
|
|||||||
uv__once_inner(guard, callback);
|
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) {
|
int uv_mutex_init(uv_mutex_t* mutex) {
|
||||||
InitializeCriticalSection(mutex);
|
InitializeCriticalSection(mutex);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -117,6 +117,7 @@ TEST_DECLARE (fs_open_dir)
|
|||||||
TEST_DECLARE (threadpool_queue_work_simple)
|
TEST_DECLARE (threadpool_queue_work_simple)
|
||||||
TEST_DECLARE (thread_mutex)
|
TEST_DECLARE (thread_mutex)
|
||||||
TEST_DECLARE (thread_rwlock)
|
TEST_DECLARE (thread_rwlock)
|
||||||
|
TEST_DECLARE (thread_create)
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows)
|
TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows)
|
||||||
TEST_DECLARE (argument_escaping)
|
TEST_DECLARE (argument_escaping)
|
||||||
@ -271,6 +272,7 @@ TASK_LIST_START
|
|||||||
TEST_ENTRY (threadpool_queue_work_simple)
|
TEST_ENTRY (threadpool_queue_work_simple)
|
||||||
TEST_ENTRY (thread_mutex)
|
TEST_ENTRY (thread_mutex)
|
||||||
TEST_ENTRY (thread_rwlock)
|
TEST_ENTRY (thread_rwlock)
|
||||||
|
TEST_ENTRY (thread_create)
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
/* These are for testing the test runner. */
|
/* These are for testing the test runner. */
|
||||||
|
|||||||
51
test/test-thread.c
Normal file
51
test/test-thread.c
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/* 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>
|
||||||
|
|
||||||
|
|
||||||
|
static volatile int thread_called;
|
||||||
|
|
||||||
|
|
||||||
|
static void thread_entry(void* arg) {
|
||||||
|
ASSERT(arg == (void *) 42);
|
||||||
|
thread_called++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_IMPL(thread_create) {
|
||||||
|
uv_thread_t tid;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
r = uv_thread_create(&tid, thread_entry, (void *) 42);
|
||||||
|
ASSERT(r == 0);
|
||||||
|
|
||||||
|
r = uv_thread_join(&tid);
|
||||||
|
ASSERT(r == 0);
|
||||||
|
|
||||||
|
ASSERT(thread_called == 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
1
uv.gyp
1
uv.gyp
@ -313,6 +313,7 @@
|
|||||||
'test/test-tcp-writealot.c',
|
'test/test-tcp-writealot.c',
|
||||||
'test/test-threadpool.c',
|
'test/test-threadpool.c',
|
||||||
'test/test-mutexes.c',
|
'test/test-mutexes.c',
|
||||||
|
'test/test-thread.c',
|
||||||
'test/test-timer-again.c',
|
'test/test-timer-again.c',
|
||||||
'test/test-timer.c',
|
'test/test-timer.c',
|
||||||
'test/test-tty.c',
|
'test/test-tty.c',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user