summaryrefslogtreecommitdiff
path: root/crypto/threads_pthread.c
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2020-12-22 17:43:07 +0000
committerDmitry Belyavskiy <beldmit@gmail.com>2020-12-31 13:14:38 +0100
commitd5e742de653954bfae88f0e5f6c8f0a7a5f6c437 (patch)
treeb1482462356926eb9e213f2c0294f45cea517e01 /crypto/threads_pthread.c
parent30af356df487b2dad571be15574b454daf70743c (diff)
downloadopenssl-new-d5e742de653954bfae88f0e5f6c8f0a7a5f6c437.tar.gz
Add some more CRYPTO_atomic functions
We add an implementation for CRYPTO_atomic_or() and CRYPTO_atomic_load() Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com> (Merged from https://github.com/openssl/openssl/pull/13733)
Diffstat (limited to 'crypto/threads_pthread.c')
-rw-r--r--crypto/threads_pthread.c50
1 files changed, 49 insertions, 1 deletions
diff --git a/crypto/threads_pthread.c b/crypto/threads_pthread.c
index afc29b7961..22ba793161 100644
--- a/crypto/threads_pthread.c
+++ b/crypto/threads_pthread.c
@@ -185,7 +185,7 @@ int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
return 1;
}
# endif
- if (!CRYPTO_THREAD_write_lock(lock))
+ if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
return 0;
*val += amount;
@@ -197,6 +197,54 @@ int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
return 1;
}
+int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
+ CRYPTO_RWLOCK *lock)
+{
+# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
+ if (__atomic_is_lock_free(sizeof(*val), val)) {
+ *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
+ return 1;
+ }
+# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
+ /* This will work for all future Solaris versions. */
+ if (ret != NULL) {
+ *ret = atomic_or_64_nv(val, op);
+ return 1;
+ }
+# endif
+ if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
+ return 0;
+ *val |= op;
+ *ret = *val;
+
+ if (!CRYPTO_THREAD_unlock(lock))
+ return 0;
+
+ return 1;
+}
+
+int CRYPTO_atomic_load(uint64_t *val, uint64_t *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;
+ }
+# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
+ /* This will work for all future Solaris versions. */
+ if (ret != NULL) {
+ *ret = atomic_or_64_nv(val, 0);
+ return 1;
+ }
+# endif
+ if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
+ return 0;
+ *ret = *val;
+ if (!CRYPTO_THREAD_unlock(lock))
+ return 0;
+
+ return 1;
+}
# ifndef FIPS_MODULE
# ifdef OPENSSL_SYS_UNIX