Skip to content

Commit a808ab4

Browse files
committed
crypto: use the libuv rwlock API
1 parent 1b2d333 commit a808ab4

1 file changed

Lines changed: 18 additions & 38 deletions

File tree

src/node_crypto.cc

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -83,67 +83,47 @@ static Persistent<String> ext_key_usage_symbol;
8383

8484
static Persistent<FunctionTemplate> secure_context_constructor;
8585

86-
#ifdef _WIN32
87-
88-
static HANDLE* locks;
89-
90-
91-
static void crypto_lock_init(void) {
92-
int i, n;
93-
94-
n = CRYPTO_num_locks();
95-
locks = new HANDLE[n];
96-
97-
for (i = 0; i < n; i++)
98-
if (!(locks[i] = CreateMutex(NULL, FALSE, NULL)))
99-
abort();
100-
}
101-
102-
103-
static void crypto_lock_cb(int mode, int n, const char* file, int line) {
104-
if (mode & CRYPTO_LOCK)
105-
WaitForSingleObject(locks[n], INFINITE);
106-
else
107-
ReleaseMutex(locks[n]);
108-
}
109-
110-
11186
static unsigned long crypto_id_cb(void) {
87+
#ifdef _WIN32
11288
return (unsigned long) GetCurrentThreadId();
113-
}
114-
11589
#else /* !_WIN32 */
90+
return (unsigned long) pthread_self();
91+
#endif /* !_WIN32 */
92+
}
11693

117-
static pthread_rwlock_t* locks;
94+
static uv_rwlock_t* locks;
11895

11996

12097
static void crypto_lock_init(void) {
12198
int i, n;
12299

123100
n = CRYPTO_num_locks();
124-
locks = new pthread_rwlock_t[n];
101+
locks = new uv_rwlock_t[n];
125102

126103
for (i = 0; i < n; i++)
127-
if (pthread_rwlock_init(locks + i, NULL))
104+
if (uv_rwlock_init(locks + i))
128105
abort();
129106
}
130107

131108

132109
static void crypto_lock_cb(int mode, int n, const char* file, int line) {
110+
assert((mode & CRYPTO_LOCK) || (mode & CRYPTO_UNLOCK));
111+
assert((mode & CRYPTO_READ) || (mode & CRYPTO_WRITE));
112+
133113
if (mode & CRYPTO_LOCK) {
134-
if (mode & CRYPTO_READ) pthread_rwlock_rdlock(locks + n);
135-
if (mode & CRYPTO_WRITE) pthread_rwlock_wrlock(locks + n);
114+
if (mode & CRYPTO_READ)
115+
uv_rwlock_rdlock(locks + n);
116+
else
117+
uv_rwlock_wrlock(locks + n);
136118
} else {
137-
pthread_rwlock_unlock(locks + n);
119+
if (mode & CRYPTO_READ)
120+
uv_rwlock_rdunlock(locks + n);
121+
else
122+
uv_rwlock_wrunlock(locks + n);
138123
}
139124
}
140125

141126

142-
static unsigned long crypto_id_cb(void) {
143-
return (unsigned long) pthread_self();
144-
}
145-
146-
#endif /* !_WIN32 */
147127

148128

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

0 commit comments

Comments
 (0)