| 1 |
/* |
| 2 |
* Copyright 2016-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#if defined(_WIN32) |
| 11 |
#include <windows.h> |
| 12 |
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600 |
| 13 |
#define USE_RWLOCK |
| 14 |
#endif |
| 15 |
#endif |
| 16 |
#include <assert.h> |
| 17 |
|
| 18 |
/* |
| 19 |
* VC++ 2008 or earlier x86 compilers do not have an inline implementation |
| 20 |
* of InterlockedOr64 for 32bit and will fail to run on Windows XP 32bit. |
| 21 |
* https://docs.microsoft.com/en-us/cpp/intrinsics/interlockedor-intrinsic-functions#requirements |
| 22 |
* To work around this problem, we implement a manual locking mechanism for |
| 23 |
* only VC++ 2008 or earlier x86 compilers. |
| 24 |
*/ |
| 25 |
|
| 26 |
#if ((defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER <= 1600) || (defined(__MINGW32__) && !defined(__MINGW64__))) |
| 27 |
#define NO_INTERLOCKEDOR64 |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <openssl/crypto.h> |
| 31 |
#include <crypto/cryptlib.h> |
| 32 |
#include "internal/common.h" |
| 33 |
#include "internal/thread_arch.h" |
| 34 |
#include "internal/threads_common.h" |
| 35 |
#include "internal/rcu.h" |
| 36 |
#include "rcu_internal.h" |
| 37 |
|
| 38 |
#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS) |
| 39 |
|
| 40 |
#ifdef USE_RWLOCK |
| 41 |
typedef struct { |
| 42 |
SRWLOCK lock; |
| 43 |
int exclusive; |
| 44 |
} CRYPTO_win_rwlock; |
| 45 |
#endif |
| 46 |
|
| 47 |
/* |
| 48 |
* This defines a quescent point (qp) |
| 49 |
* This is the barrier beyond which a writer |
| 50 |
* must wait before freeing data that was |
| 51 |
* atomically updated |
| 52 |
*/ |
| 53 |
struct rcu_qp { |
| 54 |
uint64_t users; |
| 55 |
}; |
| 56 |
|
| 57 |
struct thread_qp { |
| 58 |
struct rcu_qp *qp; |
| 59 |
unsigned int depth; |
| 60 |
CRYPTO_RCU_LOCK *lock; |
| 61 |
}; |
| 62 |
|
| 63 |
#define MAX_QPS 10 |
| 64 |
/* |
| 65 |
* This is the per thread tracking data |
| 66 |
* that is assigned to each thread participating |
| 67 |
* in an rcu qp |
| 68 |
* |
| 69 |
* qp points to the qp that it last acquired |
| 70 |
* |
| 71 |
*/ |
| 72 |
struct rcu_thr_data { |
| 73 |
struct thread_qp thread_qps[MAX_QPS]; |
| 74 |
}; |
| 75 |
|
| 76 |
/* |
| 77 |
* This is the internal version of a CRYPTO_RCU_LOCK |
| 78 |
* it is cast from CRYPTO_RCU_LOCK |
| 79 |
*/ |
| 80 |
struct rcu_lock_st { |
| 81 |
/* Callbacks to call for next ossl_synchronize_rcu */ |
| 82 |
struct rcu_cb_item *cb_items; |
| 83 |
|
| 84 |
/* The context we are being created against */ |
| 85 |
OSSL_LIB_CTX *ctx; |
| 86 |
|
| 87 |
/* Array of quiescent points for synchronization */ |
| 88 |
struct rcu_qp *qp_group; |
| 89 |
|
| 90 |
/* rcu generation counter for in-order retirement */ |
| 91 |
uint32_t id_ctr; |
| 92 |
|
| 93 |
/* Number of elements in qp_group array */ |
| 94 |
uint32_t group_count; |
| 95 |
|
| 96 |
/* Index of the current qp in the qp_group array */ |
| 97 |
uint32_t reader_idx; |
| 98 |
|
| 99 |
/* value of the next id_ctr value to be retired */ |
| 100 |
uint32_t next_to_retire; |
| 101 |
|
| 102 |
/* index of the next free rcu_qp in the qp_group */ |
| 103 |
uint32_t current_alloc_idx; |
| 104 |
|
| 105 |
/* number of qp's in qp_group array currently being retired */ |
| 106 |
uint32_t writers_alloced; |
| 107 |
|
| 108 |
/* lock protecting write side operations */ |
| 109 |
CRYPTO_MUTEX *write_lock; |
| 110 |
|
| 111 |
/* lock protecting updates to writers_alloced/current_alloc_idx */ |
| 112 |
CRYPTO_MUTEX *alloc_lock; |
| 113 |
|
| 114 |
/* signal to wake threads waiting on alloc_lock */ |
| 115 |
CRYPTO_CONDVAR *alloc_signal; |
| 116 |
|
| 117 |
/* lock to enforce in-order retirement */ |
| 118 |
CRYPTO_MUTEX *prior_lock; |
| 119 |
|
| 120 |
/* signal to wake threads waiting on prior_lock */ |
| 121 |
CRYPTO_CONDVAR *prior_signal; |
| 122 |
|
| 123 |
/* lock used with NO_INTERLOCKEDOR64: VS2010 x86 */ |
| 124 |
CRYPTO_RWLOCK *rw_lock; |
| 125 |
}; |
| 126 |
|
| 127 |
static struct rcu_qp *allocate_new_qp_group(struct rcu_lock_st *lock, |
| 128 |
uint32_t count) |
| 129 |
{ |
| 130 |
struct rcu_qp *new = OPENSSL_calloc(count, sizeof(*new)); |
| 131 |
|
| 132 |
lock->group_count = count; |
| 133 |
return new; |
| 134 |
} |
| 135 |
|
| 136 |
CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx) |
| 137 |
{ |
| 138 |
struct rcu_lock_st *new; |
| 139 |
|
| 140 |
/* |
| 141 |
* We need a minimum of 2 qps |
| 142 |
*/ |
| 143 |
if (num_writers < 2) |
| 144 |
num_writers = 2; |
| 145 |
|
| 146 |
ctx = ossl_lib_ctx_get_concrete(ctx); |
| 147 |
if (ctx == NULL) |
| 148 |
return 0; |
| 149 |
|
| 150 |
new = OPENSSL_zalloc(sizeof(*new)); |
| 151 |
|
| 152 |
if (new == NULL) |
| 153 |
return NULL; |
| 154 |
|
| 155 |
new->ctx = ctx; |
| 156 |
new->rw_lock = CRYPTO_THREAD_lock_new(); |
| 157 |
new->write_lock = ossl_crypto_mutex_new(); |
| 158 |
new->alloc_signal = ossl_crypto_condvar_new(); |
| 159 |
new->prior_signal = ossl_crypto_condvar_new(); |
| 160 |
new->alloc_lock = ossl_crypto_mutex_new(); |
| 161 |
new->prior_lock = ossl_crypto_mutex_new(); |
| 162 |
new->qp_group = allocate_new_qp_group(new, num_writers); |
| 163 |
if (new->qp_group == NULL |
| 164 |
|| new->alloc_signal == NULL |
| 165 |
|| new->prior_signal == NULL |
| 166 |
|| new->write_lock == NULL |
| 167 |
|| new->alloc_lock == NULL |
| 168 |
|| new->prior_lock == NULL |
| 169 |
|| new->rw_lock == NULL) { |
| 170 |
CRYPTO_THREAD_lock_free(new->rw_lock); |
| 171 |
OPENSSL_free(new->qp_group); |
| 172 |
ossl_crypto_condvar_free(&new->alloc_signal); |
| 173 |
ossl_crypto_condvar_free(&new->prior_signal); |
| 174 |
ossl_crypto_mutex_free(&new->alloc_lock); |
| 175 |
ossl_crypto_mutex_free(&new->prior_lock); |
| 176 |
ossl_crypto_mutex_free(&new->write_lock); |
| 177 |
OPENSSL_free(new); |
| 178 |
new = NULL; |
| 179 |
} |
| 180 |
|
| 181 |
return new; |
| 182 |
} |
| 183 |
|
| 184 |
void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock) |
| 185 |
{ |
| 186 |
CRYPTO_THREAD_lock_free(lock->rw_lock); |
| 187 |
OPENSSL_free(lock->qp_group); |
| 188 |
ossl_crypto_condvar_free(&lock->alloc_signal); |
| 189 |
ossl_crypto_condvar_free(&lock->prior_signal); |
| 190 |
ossl_crypto_mutex_free(&lock->alloc_lock); |
| 191 |
ossl_crypto_mutex_free(&lock->prior_lock); |
| 192 |
ossl_crypto_mutex_free(&lock->write_lock); |
| 193 |
OPENSSL_free(lock); |
| 194 |
} |
| 195 |
|
| 196 |
/* Read side acquisition of the current qp */ |
| 197 |
static ossl_inline struct rcu_qp *get_hold_current_qp(CRYPTO_RCU_LOCK *lock) |
| 198 |
{ |
| 199 |
uint32_t qp_idx; |
| 200 |
uint32_t tmp; |
| 201 |
uint64_t tmp64; |
| 202 |
|
| 203 |
/* get the current qp index */ |
| 204 |
for (;;) { |
| 205 |
CRYPTO_atomic_load_int((int *)&lock->reader_idx, (int *)&qp_idx, |
| 206 |
lock->rw_lock); |
| 207 |
CRYPTO_atomic_add64(&lock->qp_group[qp_idx].users, (uint64_t)1, &tmp64, |
| 208 |
lock->rw_lock); |
| 209 |
CRYPTO_atomic_load_int((int *)&lock->reader_idx, (int *)&tmp, |
| 210 |
lock->rw_lock); |
| 211 |
if (qp_idx == tmp) |
| 212 |
break; |
| 213 |
CRYPTO_atomic_add64(&lock->qp_group[qp_idx].users, (uint64_t)-1, &tmp64, |
| 214 |
lock->rw_lock); |
| 215 |
} |
| 216 |
|
| 217 |
return &lock->qp_group[qp_idx]; |
| 218 |
} |
| 219 |
|
| 220 |
static void ossl_rcu_free_local_data(void *arg) |
| 221 |
{ |
| 222 |
OSSL_LIB_CTX *ctx = arg; |
| 223 |
struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, ctx); |
| 224 |
|
| 225 |
CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, ctx, NULL); |
| 226 |
OPENSSL_free(data); |
| 227 |
} |
| 228 |
|
| 229 |
int ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock) |
| 230 |
{ |
| 231 |
struct rcu_thr_data *data; |
| 232 |
int i; |
| 233 |
int available_qp = -1; |
| 234 |
|
| 235 |
/* |
| 236 |
* we're going to access current_qp here so ask the |
| 237 |
* processor to fetch it |
| 238 |
*/ |
| 239 |
data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx); |
| 240 |
|
| 241 |
if (data == NULL) { |
| 242 |
data = OPENSSL_zalloc(sizeof(*data)); |
| 243 |
if (data == NULL) |
| 244 |
return 0; |
| 245 |
if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx, data)) { |
| 246 |
OPENSSL_free(data); |
| 247 |
return 0; |
| 248 |
} |
| 249 |
if (!ossl_init_thread_start(NULL, lock->ctx, ossl_rcu_free_local_data)) { |
| 250 |
OPENSSL_free(data); |
| 251 |
CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx, NULL); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
for (i = 0; i < MAX_QPS; i++) { |
| 257 |
if (data->thread_qps[i].qp == NULL && available_qp == -1) |
| 258 |
available_qp = i; |
| 259 |
/* If we have a hold on this lock already, we're good */ |
| 260 |
if (data->thread_qps[i].lock == lock) |
| 261 |
return 1; |
| 262 |
} |
| 263 |
|
| 264 |
/* |
| 265 |
* if we get here, then we don't have a hold on this lock yet |
| 266 |
*/ |
| 267 |
assert(available_qp != -1); |
| 268 |
|
| 269 |
data->thread_qps[available_qp].qp = get_hold_current_qp(lock); |
| 270 |
data->thread_qps[available_qp].depth = 1; |
| 271 |
data->thread_qps[available_qp].lock = lock; |
| 272 |
return 1; |
| 273 |
} |
| 274 |
|
| 275 |
void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock) |
| 276 |
{ |
| 277 |
ossl_crypto_mutex_lock(lock->write_lock); |
| 278 |
} |
| 279 |
|
| 280 |
void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock) |
| 281 |
{ |
| 282 |
ossl_crypto_mutex_unlock(lock->write_lock); |
| 283 |
} |
| 284 |
|
| 285 |
void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock) |
| 286 |
{ |
| 287 |
struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx); |
| 288 |
int i; |
| 289 |
LONG64 ret; |
| 290 |
|
| 291 |
assert(data != NULL); |
| 292 |
|
| 293 |
for (i = 0; i < MAX_QPS; i++) { |
| 294 |
if (data->thread_qps[i].lock == lock) { |
| 295 |
data->thread_qps[i].depth--; |
| 296 |
if (data->thread_qps[i].depth == 0) { |
| 297 |
CRYPTO_atomic_add64(&data->thread_qps[i].qp->users, |
| 298 |
(uint64_t)-1, (uint64_t *)&ret, |
| 299 |
lock->rw_lock); |
| 300 |
OPENSSL_assert(ret >= 0); |
| 301 |
data->thread_qps[i].qp = NULL; |
| 302 |
data->thread_qps[i].lock = NULL; |
| 303 |
} |
| 304 |
return; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/* |
| 310 |
* Write side allocation routine to get the current qp |
| 311 |
* and replace it with a new one |
| 312 |
*/ |
| 313 |
static struct rcu_qp *update_qp(CRYPTO_RCU_LOCK *lock, uint32_t *curr_id) |
| 314 |
{ |
| 315 |
uint32_t current_idx; |
| 316 |
uint32_t tmp; |
| 317 |
|
| 318 |
ossl_crypto_mutex_lock(lock->alloc_lock); |
| 319 |
/* |
| 320 |
* we need at least one qp to be available with one |
| 321 |
* left over, so that readers can start working on |
| 322 |
* one that isn't yet being waited on |
| 323 |
*/ |
| 324 |
while (lock->group_count - lock->writers_alloced < 2) |
| 325 |
/* we have to wait for one to be free */ |
| 326 |
ossl_crypto_condvar_wait(lock->alloc_signal, lock->alloc_lock); |
| 327 |
|
| 328 |
current_idx = lock->current_alloc_idx; |
| 329 |
|
| 330 |
/* Allocate the qp */ |
| 331 |
lock->writers_alloced++; |
| 332 |
|
| 333 |
/* increment the allocation index */ |
| 334 |
lock->current_alloc_idx = (lock->current_alloc_idx + 1) % lock->group_count; |
| 335 |
|
| 336 |
/* get and insert a new id */ |
| 337 |
*curr_id = lock->id_ctr; |
| 338 |
lock->id_ctr++; |
| 339 |
|
| 340 |
/* update the reader index to be the prior qp */ |
| 341 |
tmp = lock->current_alloc_idx; |
| 342 |
#if (defined(NO_INTERLOCKEDOR64)) |
| 343 |
CRYPTO_THREAD_write_lock(lock->rw_lock); |
| 344 |
lock->reader_idx = tmp; |
| 345 |
CRYPTO_THREAD_unlock(lock->rw_lock); |
| 346 |
#else |
| 347 |
InterlockedExchange((LONG volatile *)&lock->reader_idx, tmp); |
| 348 |
#endif |
| 349 |
|
| 350 |
/* wake up any waiters */ |
| 351 |
ossl_crypto_condvar_broadcast(lock->alloc_signal); |
| 352 |
ossl_crypto_mutex_unlock(lock->alloc_lock); |
| 353 |
return &lock->qp_group[current_idx]; |
| 354 |
} |
| 355 |
|
| 356 |
static void retire_qp(CRYPTO_RCU_LOCK *lock, |
| 357 |
struct rcu_qp *qp) |
| 358 |
{ |
| 359 |
ossl_crypto_mutex_lock(lock->alloc_lock); |
| 360 |
lock->writers_alloced--; |
| 361 |
ossl_crypto_condvar_broadcast(lock->alloc_signal); |
| 362 |
ossl_crypto_mutex_unlock(lock->alloc_lock); |
| 363 |
} |
| 364 |
|
| 365 |
void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock) |
| 366 |
{ |
| 367 |
struct rcu_qp *qp; |
| 368 |
uint64_t count; |
| 369 |
uint32_t curr_id; |
| 370 |
struct rcu_cb_item *cb_items, *tmpcb; |
| 371 |
|
| 372 |
/* before we do anything else, lets grab the cb list */ |
| 373 |
ossl_crypto_mutex_lock(lock->write_lock); |
| 374 |
cb_items = lock->cb_items; |
| 375 |
lock->cb_items = NULL; |
| 376 |
ossl_crypto_mutex_unlock(lock->write_lock); |
| 377 |
|
| 378 |
qp = update_qp(lock, &curr_id); |
| 379 |
|
| 380 |
/* retire in order */ |
| 381 |
ossl_crypto_mutex_lock(lock->prior_lock); |
| 382 |
while (lock->next_to_retire != curr_id) |
| 383 |
ossl_crypto_condvar_wait(lock->prior_signal, lock->prior_lock); |
| 384 |
|
| 385 |
/* wait for the reader count to reach zero */ |
| 386 |
do { |
| 387 |
CRYPTO_atomic_load(&qp->users, &count, lock->rw_lock); |
| 388 |
} while (count != (uint64_t)0); |
| 389 |
|
| 390 |
lock->next_to_retire++; |
| 391 |
ossl_crypto_condvar_broadcast(lock->prior_signal); |
| 392 |
ossl_crypto_mutex_unlock(lock->prior_lock); |
| 393 |
|
| 394 |
retire_qp(lock, qp); |
| 395 |
|
| 396 |
/* handle any callbacks that we have */ |
| 397 |
while (cb_items != NULL) { |
| 398 |
tmpcb = cb_items; |
| 399 |
cb_items = cb_items->next; |
| 400 |
tmpcb->fn(tmpcb->data); |
| 401 |
OPENSSL_free(tmpcb); |
| 402 |
} |
| 403 |
|
| 404 |
/* and we're done */ |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
CRYPTO_RCU_CB_ITEM *ossl_rcu_cb_item_new(void) |
| 409 |
{ |
| 410 |
return OPENSSL_zalloc(sizeof(CRYPTO_RCU_CB_ITEM)); |
| 411 |
} |
| 412 |
|
| 413 |
void ossl_rcu_cb_item_free(CRYPTO_RCU_CB_ITEM *item) |
| 414 |
{ |
| 415 |
OPENSSL_free(item); |
| 416 |
} |
| 417 |
|
| 418 |
/* |
| 419 |
* Note, must be called under the protection of ossl_rcu_write_lock |
| 420 |
*/ |
| 421 |
void ossl_rcu_call(CRYPTO_RCU_LOCK *lock, CRYPTO_RCU_CB_ITEM *item, |
| 422 |
rcu_cb_fn cb, void *data) |
| 423 |
{ |
| 424 |
item->fn = cb; |
| 425 |
item->data = data; |
| 426 |
item->next = lock->cb_items; |
| 427 |
lock->cb_items = item; |
| 428 |
} |
| 429 |
|
| 430 |
void *ossl_rcu_uptr_deref(void **p) |
| 431 |
{ |
| 432 |
return (void *)*p; |
| 433 |
} |
| 434 |
|
| 435 |
void ossl_rcu_assign_uptr(void **p, void **v) |
| 436 |
{ |
| 437 |
InterlockedExchangePointer((void *volatile *)p, (void *)*v); |
| 438 |
} |
| 439 |
|
| 440 |
CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void) |
| 441 |
{ |
| 442 |
CRYPTO_RWLOCK *lock; |
| 443 |
#ifdef USE_RWLOCK |
| 444 |
CRYPTO_win_rwlock *rwlock; |
| 445 |
|
| 446 |
if ((lock = OPENSSL_zalloc(sizeof(CRYPTO_win_rwlock))) == NULL) |
| 447 |
/* Don't set error, to avoid recursion blowup. */ |
| 448 |
return NULL; |
| 449 |
rwlock = lock; |
| 450 |
InitializeSRWLock(&rwlock->lock); |
| 451 |
#else |
| 452 |
|
| 453 |
if ((lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION))) == NULL) |
| 454 |
/* Don't set error, to avoid recursion blowup. */ |
| 455 |
return NULL; |
| 456 |
|
| 457 |
#if !defined(_WIN32_WCE) |
| 458 |
/* 0x400 is the spin count value suggested in the documentation */ |
| 459 |
if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) { |
| 460 |
OPENSSL_free(lock); |
| 461 |
return NULL; |
| 462 |
} |
| 463 |
#else |
| 464 |
InitializeCriticalSection(lock); |
| 465 |
#endif |
| 466 |
#endif |
| 467 |
|
| 468 |
return lock; |
| 469 |
} |
| 470 |
|
| 471 |
__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock) |
| 472 |
{ |
| 473 |
#ifdef USE_RWLOCK |
| 474 |
CRYPTO_win_rwlock *rwlock = lock; |
| 475 |
|
| 476 |
AcquireSRWLockShared(&rwlock->lock); |
| 477 |
#else |
| 478 |
EnterCriticalSection(lock); |
| 479 |
#endif |
| 480 |
return 1; |
| 481 |
} |
| 482 |
|
| 483 |
__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock) |
| 484 |
{ |
| 485 |
#ifdef USE_RWLOCK |
| 486 |
CRYPTO_win_rwlock *rwlock = lock; |
| 487 |
|
| 488 |
AcquireSRWLockExclusive(&rwlock->lock); |
| 489 |
rwlock->exclusive = 1; |
| 490 |
#else |
| 491 |
EnterCriticalSection(lock); |
| 492 |
#endif |
| 493 |
return 1; |
| 494 |
} |
| 495 |
|
| 496 |
int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock) |
| 497 |
{ |
| 498 |
#ifdef USE_RWLOCK |
| 499 |
CRYPTO_win_rwlock *rwlock = lock; |
| 500 |
|
| 501 |
if (rwlock->exclusive) { |
| 502 |
rwlock->exclusive = 0; |
| 503 |
ReleaseSRWLockExclusive(&rwlock->lock); |
| 504 |
} else { |
| 505 |
ReleaseSRWLockShared(&rwlock->lock); |
| 506 |
} |
| 507 |
#else |
| 508 |
LeaveCriticalSection(lock); |
| 509 |
#endif |
| 510 |
return 1; |
| 511 |
} |
| 512 |
|
| 513 |
void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) |
| 514 |
{ |
| 515 |
if (lock == NULL) |
| 516 |
return; |
| 517 |
|
| 518 |
#ifndef USE_RWLOCK |
| 519 |
DeleteCriticalSection(lock); |
| 520 |
#endif |
| 521 |
OPENSSL_free(lock); |
| 522 |
|
| 523 |
return; |
| 524 |
} |
| 525 |
|
| 526 |
#define ONCE_UNINITED 0 |
| 527 |
#define ONCE_ININIT 1 |
| 528 |
#define ONCE_DONE 2 |
| 529 |
|
| 530 |
/* |
| 531 |
* We don't use InitOnceExecuteOnce because that isn't available in WinXP which |
| 532 |
* we still have to support. |
| 533 |
*/ |
| 534 |
int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void)) |
| 535 |
{ |
| 536 |
LONG volatile *lock = (LONG *)once; |
| 537 |
LONG result; |
| 538 |
|
| 539 |
if (*lock == ONCE_DONE) |
| 540 |
return 1; |
| 541 |
|
| 542 |
do { |
| 543 |
result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED); |
| 544 |
if (result == ONCE_UNINITED) { |
| 545 |
init(); |
| 546 |
*lock = ONCE_DONE; |
| 547 |
return 1; |
| 548 |
} |
| 549 |
} while (result == ONCE_ININIT); |
| 550 |
|
| 551 |
return (*lock == ONCE_DONE); |
| 552 |
} |
| 553 |
|
| 554 |
int ossl_thread_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) |
| 555 |
{ |
| 556 |
|
| 557 |
*key = TlsAlloc(); |
| 558 |
if (*key == TLS_OUT_OF_INDEXES) |
| 559 |
return 0; |
| 560 |
|
| 561 |
return 1; |
| 562 |
} |
| 563 |
|
| 564 |
void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key) |
| 565 |
{ |
| 566 |
DWORD last_error; |
| 567 |
void *ret; |
| 568 |
|
| 569 |
/* |
| 570 |
* TlsGetValue clears the last error even on success, so that callers may |
| 571 |
* distinguish it successfully returning NULL or failing. It is documented |
| 572 |
* to never fail if the argument is a valid index from TlsAlloc, so we do |
| 573 |
* not need to handle this. |
| 574 |
* |
| 575 |
* However, this error-mangling behavior interferes with the caller's use of |
| 576 |
* GetLastError. In particular SSL_get_error queries the error queue to |
| 577 |
* determine whether the caller should look at the OS's errors. To avoid |
| 578 |
* destroying state, save and restore the Windows error. |
| 579 |
* |
| 580 |
* https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx |
| 581 |
*/ |
| 582 |
last_error = GetLastError(); |
| 583 |
ret = TlsGetValue(*key); |
| 584 |
SetLastError(last_error); |
| 585 |
return ret; |
| 586 |
} |
| 587 |
|
| 588 |
int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val) |
| 589 |
{ |
| 590 |
if (TlsSetValue(*key, val) == 0) |
| 591 |
return 0; |
| 592 |
|
| 593 |
return 1; |
| 594 |
} |
| 595 |
|
| 596 |
int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key) |
| 597 |
{ |
| 598 |
if (TlsFree(*key) == 0) |
| 599 |
return 0; |
| 600 |
|
| 601 |
return 1; |
| 602 |
} |
| 603 |
|
| 604 |
CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void) |
| 605 |
{ |
| 606 |
return GetCurrentThreadId(); |
| 607 |
} |
| 608 |
|
| 609 |
int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b) |
| 610 |
{ |
| 611 |
return (a == b); |
| 612 |
} |
| 613 |
|
| 614 |
int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) |
| 615 |
{ |
| 616 |
#if (defined(NO_INTERLOCKEDOR64)) |
| 617 |
if (lock == NULL || !CRYPTO_THREAD_write_lock(lock)) |
| 618 |
return 0; |
| 619 |
*val += amount; |
| 620 |
*ret = *val; |
| 621 |
|
| 622 |
if (!CRYPTO_THREAD_unlock(lock)) |
| 623 |
return 0; |
| 624 |
|
| 625 |
return 1; |
| 626 |
#else |
| 627 |
*ret = (int)InterlockedExchangeAdd((LONG volatile *)val, (LONG)amount) |
| 628 |
+ amount; |
| 629 |
return 1; |
| 630 |
#endif |
| 631 |
} |
| 632 |
|
| 633 |
int CRYPTO_atomic_add64(uint64_t *val, uint64_t op, uint64_t *ret, |
| 634 |
CRYPTO_RWLOCK *lock) |
| 635 |
{ |
| 636 |
#if (defined(NO_INTERLOCKEDOR64)) |
| 637 |
if (lock == NULL || !CRYPTO_THREAD_write_lock(lock)) |
| 638 |
return 0; |
| 639 |
*val += op; |
| 640 |
*ret = *val; |
| 641 |
|
| 642 |
if (!CRYPTO_THREAD_unlock(lock)) |
| 643 |
return 0; |
| 644 |
|
| 645 |
return 1; |
| 646 |
#else |
| 647 |
*ret = (uint64_t)InterlockedAdd64((LONG64 volatile *)val, (LONG64)op); |
| 648 |
return 1; |
| 649 |
#endif |
| 650 |
} |
| 651 |
|
| 652 |
int CRYPTO_atomic_and(uint64_t *val, uint64_t op, uint64_t *ret, |
| 653 |
CRYPTO_RWLOCK *lock) |
| 654 |
{ |
| 655 |
#if (defined(NO_INTERLOCKEDOR64)) |
| 656 |
if (lock == NULL || !CRYPTO_THREAD_write_lock(lock)) |
| 657 |
return 0; |
| 658 |
*val &= op; |
| 659 |
*ret = *val; |
| 660 |
|
| 661 |
if (!CRYPTO_THREAD_unlock(lock)) |
| 662 |
return 0; |
| 663 |
|
| 664 |
return 1; |
| 665 |
#else |
| 666 |
*ret = (uint64_t)InterlockedAnd64((LONG64 volatile *)val, (LONG64)op) & op; |
| 667 |
return 1; |
| 668 |
#endif |
| 669 |
} |
| 670 |
|
| 671 |
int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret, |
| 672 |
CRYPTO_RWLOCK *lock) |
| 673 |
{ |
| 674 |
#if (defined(NO_INTERLOCKEDOR64)) |
| 675 |
if (lock == NULL || !CRYPTO_THREAD_write_lock(lock)) |
| 676 |
return 0; |
| 677 |
*val |= op; |
| 678 |
*ret = *val; |
| 679 |
|
| 680 |
if (!CRYPTO_THREAD_unlock(lock)) |
| 681 |
return 0; |
| 682 |
|
| 683 |
return 1; |
| 684 |
#else |
| 685 |
*ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, (LONG64)op) | op; |
| 686 |
return 1; |
| 687 |
#endif |
| 688 |
} |
| 689 |
|
| 690 |
int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock) |
| 691 |
{ |
| 692 |
#if (defined(NO_INTERLOCKEDOR64)) |
| 693 |
if (lock == NULL || !CRYPTO_THREAD_read_lock(lock)) |
| 694 |
return 0; |
| 695 |
*ret = *val; |
| 696 |
if (!CRYPTO_THREAD_unlock(lock)) |
| 697 |
return 0; |
| 698 |
|
| 699 |
return 1; |
| 700 |
#else |
| 701 |
*ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, 0); |
| 702 |
return 1; |
| 703 |
#endif |
| 704 |
} |
| 705 |
|
| 706 |
int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock) |
| 707 |
{ |
| 708 |
#if (defined(NO_INTERLOCKEDOR64)) |
| 709 |
if (lock == NULL || !CRYPTO_THREAD_read_lock(lock)) |
| 710 |
return 0; |
| 711 |
*dst = val; |
| 712 |
if (!CRYPTO_THREAD_unlock(lock)) |
| 713 |
return 0; |
| 714 |
|
| 715 |
return 1; |
| 716 |
#else |
| 717 |
InterlockedExchange64(dst, val); |
| 718 |
return 1; |
| 719 |
#endif |
| 720 |
} |
| 721 |
|
| 722 |
int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock) |
| 723 |
{ |
| 724 |
#if (defined(NO_INTERLOCKEDOR64)) |
| 725 |
if (lock == NULL || !CRYPTO_THREAD_read_lock(lock)) |
| 726 |
return 0; |
| 727 |
*ret = *val; |
| 728 |
if (!CRYPTO_THREAD_unlock(lock)) |
| 729 |
return 0; |
| 730 |
|
| 731 |
return 1; |
| 732 |
#else |
| 733 |
/* On Windows, LONG (but not long) is always the same size as int. */ |
| 734 |
*ret = (int)InterlockedOr((LONG volatile *)val, 0); |
| 735 |
return 1; |
| 736 |
#endif |
| 737 |
} |
| 738 |
|
| 739 |
int openssl_init_fork_handlers(void) |
| 740 |
{ |
| 741 |
return 0; |
| 742 |
} |
| 743 |
|
| 744 |
int openssl_get_fork_id(void) |
| 745 |
{ |
| 746 |
return 0; |
| 747 |
} |
| 748 |
#endif |