Add support for reference counting using C11 atomics
[openssl.git] / crypto / engine / eng_lib.c
index 28de21d73de6d6ecb0a168a8a34ee2d62a1a00e4..8a21f38028d1b37ac86bac86ebd2316e53b070f9 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "eng_int.h"
 #include <openssl/rand.h>
+#include "internal/refcount.h"
 
 CRYPTO_RWLOCK *global_engine_lock;
 
@@ -72,10 +73,14 @@ int engine_free_util(ENGINE *e, int locked)
 
     if (e == NULL)
         return 1;
+#ifdef HAVE_ATOMICS
+    CRYPTO_DOWN_REF(&e->struct_ref, &i, global_engine_lock);
+#else
     if (locked)
         CRYPTO_atomic_add(&e->struct_ref, -1, &i, global_engine_lock);
     else
         i = --e->struct_ref;
+#endif
     engine_ref_debug(e, 0, -1)
     if (i > 0)
         return 1;