| 
									
										
										
										
											2016-05-18 03:38:09 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-03-20 20:07:54 +08:00
										 |  |  |  * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 21:03:01 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 03:38:09 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-26 19:40:15 +08:00
										 |  |  | #if defined(_WIN32)
 | 
					
						
							|  |  |  | # include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
 | 
					
						
							|  |  |  | #  define USE_RWLOCK
 | 
					
						
							|  |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2016-06-26 19:40:15 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | #include <assert.h>
 | 
					
						
							| 
									
										
										
										
											2016-06-26 19:40:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-24 09:34:05 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * VC++ 2008 or earlier x86 compilers do not have an inline implementation | 
					
						
							|  |  |  |  * of InterlockedOr64 for 32bit and will fail to run on Windows XP 32bit. | 
					
						
							|  |  |  |  * https://docs.microsoft.com/en-us/cpp/intrinsics/interlockedor-intrinsic-functions#requirements
 | 
					
						
							|  |  |  |  * To work around this problem, we implement a manual locking mechanism for | 
					
						
							|  |  |  |  * only VC++ 2008 or earlier x86 compilers. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 19:54:46 +08:00
										 |  |  | #if ((defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER <= 1600) || (defined(__MINGW32__) && !defined(__MINGW64__)))
 | 
					
						
							| 
									
										
										
										
											2022-07-24 09:34:05 +08:00
										 |  |  | # define NO_INTERLOCKEDOR64
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  | #include <openssl/crypto.h>
 | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | #include <crypto/cryptlib.h>
 | 
					
						
							|  |  |  | #include "internal/common.h"
 | 
					
						
							|  |  |  | #include "internal/thread_arch.h"
 | 
					
						
							|  |  |  | #include "internal/rcu.h"
 | 
					
						
							|  |  |  | #include "rcu_internal.h"
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # ifdef USE_RWLOCK
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     SRWLOCK lock; | 
					
						
							|  |  |  |     int exclusive; | 
					
						
							|  |  |  | } CRYPTO_win_rwlock; | 
					
						
							|  |  |  | # endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * users is broken up into 2 parts | 
					
						
							|  |  |  |  * bits 0-31 current readers | 
					
						
							|  |  |  |  * bit 32-63 ID | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | # define READER_SHIFT 0
 | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | # define ID_SHIFT 32
 | 
					
						
							|  |  |  | /* TODO: READER_SIZE 16 in threads_pthread.c */ | 
					
						
							|  |  |  | # define READER_SIZE 32
 | 
					
						
							|  |  |  | # define ID_SIZE 32
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # define READER_MASK     (((uint64_t)1 << READER_SIZE) - 1)
 | 
					
						
							|  |  |  | # define ID_MASK         (((uint64_t)1 << ID_SIZE) - 1)
 | 
					
						
							|  |  |  | # define READER_COUNT(x) ((uint32_t)(((uint64_t)(x) >> READER_SHIFT) & \
 | 
					
						
							|  |  |  |                                      READER_MASK)) | 
					
						
							|  |  |  | # define ID_VAL(x)       ((uint32_t)(((uint64_t)(x) >> ID_SHIFT) & ID_MASK))
 | 
					
						
							|  |  |  | # define VAL_READER      ((int64_t)1 << READER_SHIFT)
 | 
					
						
							|  |  |  | # define VAL_ID(x)       ((uint64_t)x << ID_SHIFT)
 | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * This defines a quescent point (qp) | 
					
						
							|  |  |  |  * This is the barrier beyond which a writer | 
					
						
							|  |  |  |  * must wait before freeing data that was | 
					
						
							|  |  |  |  * atomically updated | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | struct rcu_qp { | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  |     volatile uint64_t users; | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct thread_qp { | 
					
						
							|  |  |  |     struct rcu_qp *qp; | 
					
						
							|  |  |  |     unsigned int depth; | 
					
						
							|  |  |  |     CRYPTO_RCU_LOCK *lock; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # define MAX_QPS 10
 | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This is the per thread tracking data | 
					
						
							|  |  |  |  * that is assigned to each thread participating | 
					
						
							|  |  |  |  * in an rcu qp | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * qp points to the qp that it last acquired | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | struct rcu_thr_data { | 
					
						
							|  |  |  |     struct thread_qp thread_qps[MAX_QPS]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * This is the internal version of a CRYPTO_RCU_LOCK | 
					
						
							|  |  |  |  * it is cast from CRYPTO_RCU_LOCK | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | struct rcu_lock_st { | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  |     /* Callbacks to call for next ossl_synchronize_rcu */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     struct rcu_cb_item *cb_items; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* The context we are being created against */ | 
					
						
							| 
									
										
										
										
											2024-04-16 04:56:29 +08:00
										 |  |  |     OSSL_LIB_CTX *ctx; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* rcu generation counter for in-order retirement */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     uint32_t id_ctr; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* TODO: can be moved before id_ctr for better alignment */ | 
					
						
							|  |  |  |     /* Array of quiescent points for synchronization */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     struct rcu_qp *qp_group; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Number of elements in qp_group array */ | 
					
						
							|  |  |  |     uint32_t group_count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Index of the current qp in the qp_group array */ | 
					
						
							| 
									
										
											  
											
												threads_win: fix build error with mingw64
This fixes a build error regression on mingw64 introduced by me in
16beec98d26644b96d57bd8da477166d0bc7d05c
In get_hold_current_qp, uint32_t variables were improperly
used to hold the value of reader_idx, which is defined as long int.
So I used CRYPTO_atomic_load_int, where a comment states
On Windows, LONG is always the same size as int
There is a size confusion, because
Win32 VC x86/x64: LONG, long, long int are 32 bit
MingW-W64: LONG, long, long int are 32 bit
cygwin64: LONG is 32 bit, long, long int are 64 bit
Fix:
- define reader_idx as uint32_t
- edit misleading comment, to clarify:
On Windows, LONG (but not long) is always the same size as int.
Fixes the following build error, reported in [1].
crypto/threads_win.c: In function 'get_hold_current_qp':
crypto/threads_win.c:184:32: error: passing argument 1 of 'CRYPTO_atomic_load_int' from incompatible pointer type [-Wincompatible-pointer-types]
  184 |         CRYPTO_atomic_load_int(&lock->reader_idx, (int *)&qp_idx,
      |                                ^~~~~~~~~~~~~~~~~
      |                                |
      |                                volatile long int *
[1] https://github.com/openssl/openssl/pull/24405#issuecomment-2211602282
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:28:28 +08:00
										 |  |  |     uint32_t reader_idx; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* value of the next id_ctr value to be retired */ | 
					
						
							|  |  |  |     uint32_t next_to_retire; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* index of the next free rcu_qp in the qp_group */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     uint32_t current_alloc_idx; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* number of qp's in qp_group array currently being retired */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     uint32_t writers_alloced; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* lock protecting write side operations */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     CRYPTO_MUTEX *write_lock; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* lock protecting updates to writers_alloced/current_alloc_idx */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     CRYPTO_MUTEX *alloc_lock; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* signal to wake threads waiting on alloc_lock */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     CRYPTO_CONDVAR *alloc_signal; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* lock to enforce in-order retirement */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     CRYPTO_MUTEX *prior_lock; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* signal to wake threads waiting on prior_lock */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     CRYPTO_CONDVAR *prior_signal; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* lock used with NO_INTERLOCKEDOR64: VS2010 x86 */ | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |     CRYPTO_RWLOCK *rw_lock; | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | /* TODO: count should be unsigned, e.g uint32_t */ | 
					
						
							|  |  |  | /* a negative value could result in unexpected behaviour */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | static struct rcu_qp *allocate_new_qp_group(struct rcu_lock_st *lock, | 
					
						
							|  |  |  |                                             int count) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct rcu_qp *new = | 
					
						
							|  |  |  |         OPENSSL_zalloc(sizeof(*new) * count); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     lock->group_count = count; | 
					
						
							|  |  |  |     return new; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-16 04:56:29 +08:00
										 |  |  | CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     struct rcu_lock_st *new; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (num_writers < 1) | 
					
						
							|  |  |  |         num_writers = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-16 04:56:29 +08:00
										 |  |  |     ctx = ossl_lib_ctx_get_concrete(ctx); | 
					
						
							|  |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     new = OPENSSL_zalloc(sizeof(*new)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (new == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-16 04:56:29 +08:00
										 |  |  |     new->ctx = ctx; | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |     new->rw_lock = CRYPTO_THREAD_lock_new(); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     new->write_lock = ossl_crypto_mutex_new(); | 
					
						
							|  |  |  |     new->alloc_signal = ossl_crypto_condvar_new(); | 
					
						
							|  |  |  |     new->prior_signal = ossl_crypto_condvar_new(); | 
					
						
							|  |  |  |     new->alloc_lock = ossl_crypto_mutex_new(); | 
					
						
							|  |  |  |     new->prior_lock = ossl_crypto_mutex_new(); | 
					
						
							|  |  |  |     new->qp_group = allocate_new_qp_group(new, num_writers + 1); | 
					
						
							|  |  |  |     if (new->qp_group == NULL | 
					
						
							|  |  |  |         || new->alloc_signal == NULL | 
					
						
							|  |  |  |         || new->prior_signal == NULL | 
					
						
							|  |  |  |         || new->write_lock == NULL | 
					
						
							|  |  |  |         || new->alloc_lock == NULL | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |         || new->prior_lock == NULL | 
					
						
							|  |  |  |         || new->rw_lock == NULL) { | 
					
						
							|  |  |  |         CRYPTO_THREAD_lock_free(new->rw_lock); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |         OPENSSL_free(new->qp_group); | 
					
						
							|  |  |  |         ossl_crypto_condvar_free(&new->alloc_signal); | 
					
						
							|  |  |  |         ossl_crypto_condvar_free(&new->prior_signal); | 
					
						
							|  |  |  |         ossl_crypto_mutex_free(&new->alloc_lock); | 
					
						
							|  |  |  |         ossl_crypto_mutex_free(&new->prior_lock); | 
					
						
							|  |  |  |         ossl_crypto_mutex_free(&new->write_lock); | 
					
						
							|  |  |  |         OPENSSL_free(new); | 
					
						
							|  |  |  |         new = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return new; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |     CRYPTO_THREAD_lock_free(lock->rw_lock); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     OPENSSL_free(lock->qp_group); | 
					
						
							|  |  |  |     ossl_crypto_condvar_free(&lock->alloc_signal); | 
					
						
							|  |  |  |     ossl_crypto_condvar_free(&lock->prior_signal); | 
					
						
							|  |  |  |     ossl_crypto_mutex_free(&lock->alloc_lock); | 
					
						
							|  |  |  |     ossl_crypto_mutex_free(&lock->prior_lock); | 
					
						
							|  |  |  |     ossl_crypto_mutex_free(&lock->write_lock); | 
					
						
							|  |  |  |     OPENSSL_free(lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | /* Read side acquisition of the current qp */ | 
					
						
							| 
									
										
										
										
											2024-05-03 12:51:08 +08:00
										 |  |  | static ossl_inline struct rcu_qp *get_hold_current_qp(CRYPTO_RCU_LOCK *lock) | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     uint32_t qp_idx; | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |     uint32_t tmp; | 
					
						
							|  |  |  |     uint64_t tmp64; | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* get the current qp index */ | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  |         CRYPTO_atomic_load_int((int *)&lock->reader_idx, (int *)&qp_idx, | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |                                lock->rw_lock); | 
					
						
							|  |  |  |         CRYPTO_atomic_add64(&lock->qp_group[qp_idx].users, VAL_READER, &tmp64, | 
					
						
							|  |  |  |                             lock->rw_lock); | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  |         CRYPTO_atomic_load_int((int *)&lock->reader_idx, (int *)&tmp, | 
					
						
							|  |  |  |                                lock->rw_lock); | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |         if (qp_idx == tmp) | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |         CRYPTO_atomic_add64(&lock->qp_group[qp_idx].users, -VAL_READER, &tmp64, | 
					
						
							|  |  |  |                             lock->rw_lock); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return &lock->qp_group[qp_idx]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-16 04:56:29 +08:00
										 |  |  | static void ossl_rcu_free_local_data(void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     OSSL_LIB_CTX *ctx = arg; | 
					
						
							|  |  |  |     CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(ctx); | 
					
						
							|  |  |  |     struct rcu_thr_data *data = CRYPTO_THREAD_get_local(lkey); | 
					
						
							|  |  |  |     OPENSSL_free(data); | 
					
						
							| 
									
										
										
										
											2024-06-18 02:12:46 +08:00
										 |  |  |     CRYPTO_THREAD_set_local(lkey, NULL); | 
					
						
							| 
									
										
										
										
											2024-04-16 04:56:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct rcu_thr_data *data; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     int available_qp = -1; | 
					
						
							| 
									
										
										
										
											2024-04-16 04:56:29 +08:00
										 |  |  |     CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(lock->ctx); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * we're going to access current_qp here so ask the | 
					
						
							|  |  |  |      * processor to fetch it | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-04-16 04:56:29 +08:00
										 |  |  |     data = CRYPTO_THREAD_get_local(lkey); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (data == NULL) { | 
					
						
							|  |  |  |         data = OPENSSL_zalloc(sizeof(*data)); | 
					
						
							|  |  |  |         OPENSSL_assert(data != NULL); | 
					
						
							| 
									
										
										
										
											2024-04-16 04:56:29 +08:00
										 |  |  |         CRYPTO_THREAD_set_local(lkey, data); | 
					
						
							|  |  |  |         ossl_init_thread_start(NULL, lock->ctx, ossl_rcu_free_local_data); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < MAX_QPS; i++) { | 
					
						
							|  |  |  |         if (data->thread_qps[i].qp == NULL && available_qp == -1) | 
					
						
							|  |  |  |             available_qp = i; | 
					
						
							|  |  |  |         /* If we have a hold on this lock already, we're good */ | 
					
						
							|  |  |  |         if (data->thread_qps[i].lock == lock) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * if we get here, then we don't have a hold on this lock yet | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     assert(available_qp != -1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data->thread_qps[available_qp].qp = get_hold_current_qp(lock); | 
					
						
							|  |  |  |     data->thread_qps[available_qp].depth = 1; | 
					
						
							|  |  |  |     data->thread_qps[available_qp].lock = lock; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ossl_crypto_mutex_lock(lock->write_lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ossl_crypto_mutex_unlock(lock->write_lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-16 04:56:29 +08:00
										 |  |  |     CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(lock->ctx); | 
					
						
							|  |  |  |     struct rcu_thr_data *data = CRYPTO_THREAD_get_local(lkey); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     int i; | 
					
						
							|  |  |  |     LONG64 ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(data != NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < MAX_QPS; i++) { | 
					
						
							|  |  |  |         if (data->thread_qps[i].lock == lock) { | 
					
						
							|  |  |  |             data->thread_qps[i].depth--; | 
					
						
							|  |  |  |             if (data->thread_qps[i].depth == 0) { | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |                 CRYPTO_atomic_add64(&data->thread_qps[i].qp->users, | 
					
						
							|  |  |  |                                     -VAL_READER, (uint64_t *)&ret, | 
					
						
							|  |  |  |                                     lock->rw_lock); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |                 OPENSSL_assert(ret >= 0); | 
					
						
							|  |  |  |                 data->thread_qps[i].qp = NULL; | 
					
						
							|  |  |  |                 data->thread_qps[i].lock = NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Write side allocation routine to get the current qp | 
					
						
							|  |  |  |  * and replace it with a new one | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | static struct rcu_qp *update_qp(CRYPTO_RCU_LOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint64_t new_id; | 
					
						
							|  |  |  |     uint32_t current_idx; | 
					
						
							|  |  |  |     uint32_t tmp; | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |     uint64_t tmp64; | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ossl_crypto_mutex_lock(lock->alloc_lock); | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * we need at least one qp to be available with one | 
					
						
							|  |  |  |      * left over, so that readers can start working on | 
					
						
							|  |  |  |      * one that isn't yet being waited on | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     while (lock->group_count - lock->writers_alloced < 2) | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  |         /* we have to wait for one to be free */ | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |         ossl_crypto_condvar_wait(lock->alloc_signal, lock->alloc_lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     current_idx = lock->current_alloc_idx; | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     /* Allocate the qp */ | 
					
						
							|  |  |  |     lock->writers_alloced++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* increment the allocation index */ | 
					
						
							|  |  |  |     lock->current_alloc_idx = | 
					
						
							|  |  |  |         (lock->current_alloc_idx + 1) % lock->group_count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* get and insert a new id */ | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  |     new_id = VAL_ID(lock->id_ctr); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     lock->id_ctr++; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												threads_pthread, threads_win: improve code consistency
Improve code consistency between threads_pthread.c and threads_win.c
threads_pthread.c has good comments, let's copy them to threads_win.c
In many places uint64_t or LONG int was used, and assignments were
performed between variables with different sizes.
Unify the code to use uint32_t. In 32 bit architectures it is easier
to perform 32 bit atomic operations. The size is large enough to hold
the list of operations.
Fix result of atomic_or_uint_nv improperly casted to int *
instead of int.
Note:
In general size_t should be preferred for size and index, due to its
descriptive name, however it is more convenient to use uint32_t for
consistency between platforms and atomic calls.
READER_COUNT and ID_VAL return results that fit 32 bit. Cast them to
uint32_t to save a few CPU cycles, since they are used in 32 bit
operations anyway.
TODO:
In struct rcu_lock_st, qp_group can be moved before id_ctr
for better alignment, which would save 8 bytes.
allocate_new_qp_group has a parameter count of type int.
Signed values should be avoided as size or index.
It is better to use unsigned, e.g uint32_t, even though
internally this is assigned to a uint32_t variable.
READER_SIZE is 16 in threads_pthread.c, and 32 in threads_win.c
Using a common size for consistency should be prefered.
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:29:09 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Even though we are under a write side lock here | 
					
						
							|  |  |  |      * We need to use atomic instructions to ensure that the results | 
					
						
							|  |  |  |      * of this update are published to the read side prior to updating the | 
					
						
							|  |  |  |      * reader idx below | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |     CRYPTO_atomic_and(&lock->qp_group[current_idx].users, ID_MASK, &tmp64, | 
					
						
							|  |  |  |                       lock->rw_lock); | 
					
						
							|  |  |  |     CRYPTO_atomic_add64(&lock->qp_group[current_idx].users, new_id, &tmp64, | 
					
						
							|  |  |  |                         lock->rw_lock); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* update the reader index to be the prior qp */ | 
					
						
							|  |  |  |     tmp = lock->current_alloc_idx; | 
					
						
							| 
									
										
										
										
											2024-07-27 01:09:06 +08:00
										 |  |  |     InterlockedExchange((LONG volatile *)&lock->reader_idx, tmp); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* wake up any waiters */ | 
					
						
							|  |  |  |     ossl_crypto_condvar_broadcast(lock->alloc_signal); | 
					
						
							|  |  |  |     ossl_crypto_mutex_unlock(lock->alloc_lock); | 
					
						
							|  |  |  |     return &lock->qp_group[current_idx]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void retire_qp(CRYPTO_RCU_LOCK *lock, | 
					
						
							|  |  |  |                       struct rcu_qp *qp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ossl_crypto_mutex_lock(lock->alloc_lock); | 
					
						
							|  |  |  |     lock->writers_alloced--; | 
					
						
							|  |  |  |     ossl_crypto_condvar_broadcast(lock->alloc_signal); | 
					
						
							|  |  |  |     ossl_crypto_mutex_unlock(lock->alloc_lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct rcu_qp *qp; | 
					
						
							|  |  |  |     uint64_t count; | 
					
						
							|  |  |  |     struct rcu_cb_item *cb_items, *tmpcb; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* before we do anything else, lets grab the cb list */ | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  |     cb_items = InterlockedExchangePointer((void * volatile *)&lock->cb_items, | 
					
						
							|  |  |  |                                           NULL); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     qp = update_qp(lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* wait for the reader count to reach zero */ | 
					
						
							|  |  |  |     do { | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |         CRYPTO_atomic_load(&qp->users, &count, lock->rw_lock); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     } while (READER_COUNT(count) != 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* retire in order */ | 
					
						
							|  |  |  |     ossl_crypto_mutex_lock(lock->prior_lock); | 
					
						
							|  |  |  |     while (lock->next_to_retire != ID_VAL(count)) | 
					
						
							|  |  |  |         ossl_crypto_condvar_wait(lock->prior_signal, lock->prior_lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     lock->next_to_retire++; | 
					
						
							|  |  |  |     ossl_crypto_condvar_broadcast(lock->prior_signal); | 
					
						
							|  |  |  |     ossl_crypto_mutex_unlock(lock->prior_lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     retire_qp(lock, qp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* handle any callbacks that we have */ | 
					
						
							|  |  |  |     while (cb_items != NULL) { | 
					
						
							|  |  |  |         tmpcb = cb_items; | 
					
						
							|  |  |  |         cb_items = cb_items->next; | 
					
						
							|  |  |  |         tmpcb->fn(tmpcb->data); | 
					
						
							|  |  |  |         OPENSSL_free(tmpcb); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* and we're done */ | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ossl_rcu_call(CRYPTO_RCU_LOCK *lock, rcu_cb_fn cb, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct rcu_cb_item *new; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     new = OPENSSL_zalloc(sizeof(struct rcu_cb_item)); | 
					
						
							|  |  |  |     if (new == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     new->data = data; | 
					
						
							|  |  |  |     new->fn = cb; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  |     new->next = InterlockedExchangePointer((void * volatile *)&lock->cb_items, | 
					
						
							|  |  |  |                                            new); | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void *ossl_rcu_uptr_deref(void **p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return (void *)*p; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ossl_rcu_assign_uptr(void **p, void **v) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     InterlockedExchangePointer((void * volatile *)p, (void *)*v); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  | CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-06 03:13:55 +08:00
										 |  |  |     CRYPTO_RWLOCK *lock; | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # ifdef USE_RWLOCK
 | 
					
						
							|  |  |  |     CRYPTO_win_rwlock *rwlock; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     if ((lock = OPENSSL_zalloc(sizeof(CRYPTO_win_rwlock))) == NULL) | 
					
						
							| 
									
										
										
										
											2022-10-03 21:23:05 +08:00
										 |  |  |         /* Don't set error, to avoid recursion blowup. */ | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     rwlock = lock; | 
					
						
							|  |  |  |     InitializeSRWLock(&rwlock->lock); | 
					
						
							|  |  |  | # else
 | 
					
						
							| 
									
										
										
										
											2018-04-06 03:13:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 23:39:56 +08:00
										 |  |  |     if ((lock = OPENSSL_zalloc(sizeof(CRITICAL_SECTION))) == NULL) | 
					
						
							| 
									
										
										
										
											2018-04-06 03:13:55 +08:00
										 |  |  |         /* Don't set error, to avoid recursion blowup. */ | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | #  if !defined(_WIN32_WCE)
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |     /* 0x400 is the spin count value suggested in the documentation */ | 
					
						
							| 
									
										
										
										
											2016-03-02 04:59:48 +08:00
										 |  |  |     if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) { | 
					
						
							|  |  |  |         OPENSSL_free(lock); | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2016-03-02 04:59:48 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | #  else
 | 
					
						
							| 
									
										
										
										
											2019-03-27 15:15:31 +08:00
										 |  |  |     InitializeCriticalSection(lock); | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2019-09-11 16:40:18 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return lock; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 04:31:56 +08:00
										 |  |  | __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock) | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # ifdef USE_RWLOCK
 | 
					
						
							|  |  |  |     CRYPTO_win_rwlock *rwlock = lock; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     AcquireSRWLockShared(&rwlock->lock); | 
					
						
							|  |  |  | # else
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |     EnterCriticalSection(lock); | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 04:31:56 +08:00
										 |  |  | __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock) | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # ifdef USE_RWLOCK
 | 
					
						
							|  |  |  |     CRYPTO_win_rwlock *rwlock = lock; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     AcquireSRWLockExclusive(&rwlock->lock); | 
					
						
							|  |  |  |     rwlock->exclusive = 1; | 
					
						
							|  |  |  | # else
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |     EnterCriticalSection(lock); | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # ifdef USE_RWLOCK
 | 
					
						
							|  |  |  |     CRYPTO_win_rwlock *rwlock = lock; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (rwlock->exclusive) { | 
					
						
							|  |  |  |         rwlock->exclusive = 0; | 
					
						
							|  |  |  |         ReleaseSRWLockExclusive(&rwlock->lock); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         ReleaseSRWLockShared(&rwlock->lock); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | # else
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |     LeaveCriticalSection(lock); | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (lock == NULL) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # ifndef USE_RWLOCK
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |     DeleteCriticalSection(lock); | 
					
						
							| 
									
										
										
										
											2021-03-02 03:38:02 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |     OPENSSL_free(lock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 01:43:07 +08:00
										 |  |  | # define ONCE_UNINITED     0
 | 
					
						
							|  |  |  | # define ONCE_ININIT       1
 | 
					
						
							|  |  |  | # define ONCE_DONE         2
 | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 03:11:09 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * We don't use InitOnceExecuteOnce because that isn't available in WinXP which | 
					
						
							|  |  |  |  * we still have to support. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  | int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     LONG volatile *lock = (LONG *)once; | 
					
						
							|  |  |  |     LONG result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (*lock == ONCE_DONE) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |         result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED); | 
					
						
							|  |  |  |         if (result == ONCE_UNINITED) { | 
					
						
							| 
									
										
										
										
											2016-11-13 20:48:15 +08:00
										 |  |  |             init(); | 
					
						
							| 
									
										
										
										
											2016-11-14 19:55:13 +08:00
										 |  |  |             *lock = ONCE_DONE; | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } while (result == ONCE_ININIT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return (*lock == ONCE_DONE); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     *key = TlsAlloc(); | 
					
						
							|  |  |  |     if (*key == TLS_OUT_OF_INDEXES) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-21 05:24:30 +08:00
										 |  |  |     DWORD last_error; | 
					
						
							|  |  |  |     void *ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * TlsGetValue clears the last error even on success, so that callers may | 
					
						
							|  |  |  |      * distinguish it successfully returning NULL or failing. It is documented | 
					
						
							|  |  |  |      * to never fail if the argument is a valid index from TlsAlloc, so we do | 
					
						
							|  |  |  |      * not need to handle this. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * However, this error-mangling behavior interferes with the caller's use of | 
					
						
							|  |  |  |      * GetLastError. In particular SSL_get_error queries the error queue to | 
					
						
							|  |  |  |      * determine whether the caller should look at the OS's errors. To avoid | 
					
						
							|  |  |  |      * destroying state, save and restore the Windows error. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
 | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     last_error = GetLastError(); | 
					
						
							|  |  |  |     ret = TlsGetValue(*key); | 
					
						
							|  |  |  |     SetLastError(last_error); | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (TlsSetValue(*key, val) == 0) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (TlsFree(*key) == 0) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return GetCurrentThreadId(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return (a == b); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-20 05:37:21 +08:00
										 |  |  |     *ret = (int)InterlockedExchangeAdd((LONG volatile *)val, (LONG)amount) | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  |         + amount; | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  | int CRYPTO_atomic_add64(uint64_t *val, uint64_t op, uint64_t *ret, | 
					
						
							|  |  |  |                         CRYPTO_RWLOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # if (defined(NO_INTERLOCKEDOR64))
 | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |     if (lock == NULL || !CRYPTO_THREAD_write_lock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     *val += op; | 
					
						
							|  |  |  |     *ret = *val; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!CRYPTO_THREAD_unlock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # else
 | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |     *ret = (uint64_t)InterlockedAdd64((LONG64 volatile *)val, (LONG64)op); | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CRYPTO_atomic_and(uint64_t *val, uint64_t op, uint64_t *ret, | 
					
						
							|  |  |  |                       CRYPTO_RWLOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # if (defined(NO_INTERLOCKEDOR64))
 | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |     if (lock == NULL || !CRYPTO_THREAD_write_lock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     *val &= op; | 
					
						
							|  |  |  |     *ret = *val; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!CRYPTO_THREAD_unlock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # else
 | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  |     *ret = (uint64_t)InterlockedAnd64((LONG64 volatile *)val, (LONG64)op) & op; | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2024-06-28 13:16:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 01:43:07 +08:00
										 |  |  | int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret, | 
					
						
							|  |  |  |                      CRYPTO_RWLOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # if (defined(NO_INTERLOCKEDOR64))
 | 
					
						
							| 
									
										
										
										
											2022-07-24 09:34:05 +08:00
										 |  |  |     if (lock == NULL || !CRYPTO_THREAD_write_lock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     *val |= op; | 
					
						
							|  |  |  |     *ret = *val; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!CRYPTO_THREAD_unlock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # else
 | 
					
						
							| 
									
										
										
										
											2020-12-23 01:43:07 +08:00
										 |  |  |     *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, (LONG64)op) | op; | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2020-12-23 01:43:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # if (defined(NO_INTERLOCKEDOR64))
 | 
					
						
							| 
									
										
										
										
											2022-07-24 09:34:05 +08:00
										 |  |  |     if (lock == NULL || !CRYPTO_THREAD_read_lock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     *ret = *val; | 
					
						
							|  |  |  |     if (!CRYPTO_THREAD_unlock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # else
 | 
					
						
							| 
									
										
										
										
											2020-12-23 01:43:07 +08:00
										 |  |  |     *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, 0); | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2020-12-23 01:43:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-09 00:58:07 +08:00
										 |  |  | int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # if (defined(NO_INTERLOCKEDOR64))
 | 
					
						
							| 
									
										
										
										
											2024-03-09 00:58:07 +08:00
										 |  |  |     if (lock == NULL || !CRYPTO_THREAD_read_lock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     *dst = val; | 
					
						
							|  |  |  |     if (!CRYPTO_THREAD_unlock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # else
 | 
					
						
							| 
									
										
										
										
											2024-03-09 00:58:07 +08:00
										 |  |  |     InterlockedExchange64(dst, val); | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2024-03-09 00:58:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-17 20:15:01 +08:00
										 |  |  | int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # if (defined(NO_INTERLOCKEDOR64))
 | 
					
						
							| 
									
										
										
										
											2023-05-17 20:15:01 +08:00
										 |  |  |     if (lock == NULL || !CRYPTO_THREAD_read_lock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     *ret = *val; | 
					
						
							|  |  |  |     if (!CRYPTO_THREAD_unlock(lock)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # else
 | 
					
						
							| 
									
										
											  
											
												threads_win: fix build error with mingw64
This fixes a build error regression on mingw64 introduced by me in
16beec98d26644b96d57bd8da477166d0bc7d05c
In get_hold_current_qp, uint32_t variables were improperly
used to hold the value of reader_idx, which is defined as long int.
So I used CRYPTO_atomic_load_int, where a comment states
On Windows, LONG is always the same size as int
There is a size confusion, because
Win32 VC x86/x64: LONG, long, long int are 32 bit
MingW-W64: LONG, long, long int are 32 bit
cygwin64: LONG is 32 bit, long, long int are 64 bit
Fix:
- define reader_idx as uint32_t
- edit misleading comment, to clarify:
On Windows, LONG (but not long) is always the same size as int.
Fixes the following build error, reported in [1].
crypto/threads_win.c: In function 'get_hold_current_qp':
crypto/threads_win.c:184:32: error: passing argument 1 of 'CRYPTO_atomic_load_int' from incompatible pointer type [-Wincompatible-pointer-types]
  184 |         CRYPTO_atomic_load_int(&lock->reader_idx, (int *)&qp_idx,
      |                                ^~~~~~~~~~~~~~~~~
      |                                |
      |                                volatile long int *
[1] https://github.com/openssl/openssl/pull/24405#issuecomment-2211602282
Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24803)
											
										 
											2024-07-10 22:28:28 +08:00
										 |  |  |     /* On Windows, LONG (but not long) is always the same size as int. */ | 
					
						
							| 
									
										
										
										
											2023-05-17 20:15:01 +08:00
										 |  |  |     *ret = (int)InterlockedOr((LONG volatile *)val, 0); | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-07-19 16:57:24 +08:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2023-05-17 20:15:01 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-23 02:00:06 +08:00
										 |  |  | int openssl_init_fork_handlers(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 03:03:09 +08:00
										 |  |  | int openssl_get_fork_id(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-10-26 00:43:55 +08:00
										 |  |  | #endif
 |