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