Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Wrap platform "thread-safe run once" APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Dec 20, 2011
1 parent a993329 commit 69ce014
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
3 changes: 3 additions & 0 deletions include/uv-private/uv-unix.h
Expand Up @@ -44,6 +44,9 @@ typedef struct {

typedef int uv_file;

#define UV_ONCE_INIT PTHREAD_ONCE_INIT

typedef pthread_once_t uv_once_t;
typedef pthread_t uv_thread_t;
typedef pthread_mutex_t uv_mutex_t;
typedef pthread_rwlock_t uv_rwlock_t;
Expand Down
10 changes: 10 additions & 0 deletions include/uv-private/uv-win.h
Expand Up @@ -152,6 +152,16 @@ typedef union {
} fallback_;
} uv_rwlock_t;

#define UV_ONCE_INIT { 0, NULL, NULL }

typedef struct uv_once_s {
unsigned char ran;
/* The actual event handle must be aligned to sizeof(HANDLE), so in */
/* practice it might overlap padding a little. */
HANDLE event;
HANDLE padding;
} uv_once_t;

/* Platform-specific definitions for uv_dlopen support. */
typedef HMODULE uv_lib_t;
#define UV_DYNAMIC FAR WINAPI
Expand Down
6 changes: 6 additions & 0 deletions include/uv.h
Expand Up @@ -1339,6 +1339,12 @@ 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);

/* Runs a function once and only once. Concurrent calls to uv_once() with the
* same guard will block all callers except one (it's unspecified which one).
* The guard should be initialized statically with the UV_ONCE_INIT macro.
*/
UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void));

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);
Expand Down
5 changes: 5 additions & 0 deletions src/unix/thread.c
Expand Up @@ -151,3 +151,8 @@ int uv_rwlock_trywrlock(uv_rwlock_t* rwlock) {
void uv_rwlock_wrunlock(uv_rwlock_t* rwlock) {
CHECK(pthread_rwlock_unlock(rwlock));
}


void uv_once(uv_once_t* guard, void (*callback)(void)) {
CHECK(pthread_once(guard, callback));
}
18 changes: 0 additions & 18 deletions src/win/internal.h
Expand Up @@ -343,22 +343,4 @@ extern int uv_allow_ipv6;
extern struct sockaddr_in uv_addr_ip4_any_;
extern struct sockaddr_in6 uv_addr_ip6_any_;


/*
* Threads and synchronization
*/
typedef struct uv_once_s {
unsigned char ran;
/* The actual event handle must be aligned to sizeof(HANDLE), so in */
/* practice it might overlap padding a little. */
HANDLE event;
HANDLE padding;
} uv_once_t;

#define UV_ONCE_INIT \
{ 0, NULL, NULL }

void uv_once(uv_once_t* guard, void (*callback)(void));


#endif /* UV_WIN_INTERNAL_H_ */

0 comments on commit 69ce014

Please sign in to comment.