1
0

threads_win.c 19 KB

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