diff options
author | Pauli <paul.dale@oracle.com> | 2017-09-25 04:04:42 +0200 |
---|---|---|
committer | Pauli <paul.dale@oracle.com> | 2017-10-10 00:45:52 +0200 |
commit | 94683b7acb62a5d99b8b4d66283e6d6dd0007f7a (patch) | |
tree | 10cec2abdb2f43af401d65c1060c083bb0865fdf /crypto | |
parent | EVP_EncryptInit.pod: EVP_CIPHER_mode and EVP_CIPHER_CTX_mode update (diff) | |
download | openssl-94683b7acb62a5d99b8b4d66283e6d6dd0007f7a.tar.xz openssl-94683b7acb62a5d99b8b4d66283e6d6dd0007f7a.zip |
Add a CRYPTO_atomic_read call which allows an int variable to be read
in an atomic fashion.
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4414)
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/threads_none.c | 6 | ||||
-rw-r--r-- | crypto/threads_pthread.c | 19 | ||||
-rw-r--r-- | crypto/threads_win.c | 6 |
3 files changed, 31 insertions, 0 deletions
diff --git a/crypto/threads_none.c b/crypto/threads_none.c index ffad7576b1..7e9ec2d151 100644 --- a/crypto/threads_none.c +++ b/crypto/threads_none.c @@ -125,6 +125,12 @@ int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) return 1; } +int CRYPTO_atomic_read(int *val, int *ret, CRYPTO_RWLOCK *lock) +{ + *ret = *val; + return 1; +} + int openssl_init_fork_handlers(void) { return 0; diff --git a/crypto/threads_pthread.c b/crypto/threads_pthread.c index 9644c25aea..3f8ada2c37 100644 --- a/crypto/threads_pthread.c +++ b/crypto/threads_pthread.c @@ -169,6 +169,25 @@ int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) return 1; } +int CRYPTO_atomic_read(int *val, int *ret, CRYPTO_RWLOCK *lock) +{ +# if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) + if (__atomic_is_lock_free(sizeof(*val), val)) { + __atomic_load(val, ret, __ATOMIC_ACQUIRE); + return 1; + } +# endif + if (!CRYPTO_THREAD_write_lock(lock)) + return 0; + + *ret = *val; + + if (!CRYPTO_THREAD_unlock(lock)) + return 0; + + return 1; +} + # ifdef OPENSSL_SYS_UNIX static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT; diff --git a/crypto/threads_win.c b/crypto/threads_win.c index 512e19f5f3..6f9c7b1bd3 100644 --- a/crypto/threads_win.c +++ b/crypto/threads_win.c @@ -133,6 +133,12 @@ int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) return 1; } +int CRYPTO_atomic_read(int *val, int *ret, CRYPTO_RWLOCK *lock) +{ + InterlockedCompareExchange(val, 0, 0); + return 1; +} + int openssl_init_fork_handlers(void) { return 0; |