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

Commit

Permalink
crypto: use the libuv rwlock API
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Nov 18, 2011
1 parent 1b2d333 commit a808ab4
Showing 1 changed file with 18 additions and 38 deletions.
56 changes: 18 additions & 38 deletions src/node_crypto.cc
Expand Up @@ -83,67 +83,47 @@ static Persistent<String> ext_key_usage_symbol;

static Persistent<FunctionTemplate> secure_context_constructor;

#ifdef _WIN32

static HANDLE* locks;


static void crypto_lock_init(void) {
int i, n;

n = CRYPTO_num_locks();
locks = new HANDLE[n];

for (i = 0; i < n; i++)
if (!(locks[i] = CreateMutex(NULL, FALSE, NULL)))
abort();
}


static void crypto_lock_cb(int mode, int n, const char* file, int line) {
if (mode & CRYPTO_LOCK)
WaitForSingleObject(locks[n], INFINITE);
else
ReleaseMutex(locks[n]);
}


static unsigned long crypto_id_cb(void) {
#ifdef _WIN32
return (unsigned long) GetCurrentThreadId();
}

#else /* !_WIN32 */
return (unsigned long) pthread_self();
#endif /* !_WIN32 */
}

static pthread_rwlock_t* locks;
static uv_rwlock_t* locks;


static void crypto_lock_init(void) {
int i, n;

n = CRYPTO_num_locks();
locks = new pthread_rwlock_t[n];
locks = new uv_rwlock_t[n];

for (i = 0; i < n; i++)
if (pthread_rwlock_init(locks + i, NULL))
if (uv_rwlock_init(locks + i))
abort();
}


static void crypto_lock_cb(int mode, int n, const char* file, int line) {
assert((mode & CRYPTO_LOCK) || (mode & CRYPTO_UNLOCK));
assert((mode & CRYPTO_READ) || (mode & CRYPTO_WRITE));

if (mode & CRYPTO_LOCK) {
if (mode & CRYPTO_READ) pthread_rwlock_rdlock(locks + n);
if (mode & CRYPTO_WRITE) pthread_rwlock_wrlock(locks + n);
if (mode & CRYPTO_READ)
uv_rwlock_rdlock(locks + n);
else
uv_rwlock_wrlock(locks + n);
} else {
pthread_rwlock_unlock(locks + n);
if (mode & CRYPTO_READ)
uv_rwlock_rdunlock(locks + n);
else
uv_rwlock_wrunlock(locks + n);
}
}


static unsigned long crypto_id_cb(void) {
return (unsigned long) pthread_self();
}

#endif /* !_WIN32 */


void SecureContext::Initialize(Handle<Object> target) {
Expand Down

0 comments on commit a808ab4

Please sign in to comment.