Convert CRYPTO_LOCK_DSO to new multi-threading API
authorAlessandro Ghedini <alessandro@ghedini.me>
Fri, 4 Mar 2016 16:04:37 +0000 (16:04 +0000)
committerRich Salz <rsalz@openssl.org>
Tue, 8 Mar 2016 14:07:32 +0000 (09:07 -0500)
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
crypto/dso/dso_lib.c
include/openssl/crypto.h
include/openssl/dso.h

index c410eac9ee8150f0470e18bfe596990a1b952ad4..3082545e63a1d3a2a3eeccb395d6034dc617b60a 100644 (file)
@@ -120,12 +120,20 @@ DSO *DSO_new_method(DSO_METHOD *meth)
     else
         ret->meth = meth;
     ret->references = 1;
-    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
+
+    ret->lock = CRYPTO_THREAD_lock_new();
+    if (ret->lock == NULL) {
         sk_void_free(ret->meth_data);
         OPENSSL_free(ret);
+        return NULL;
+    }
+
+    if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
+        DSO_free(ret);
         ret = NULL;
     }
-    return (ret);
+
+    return ret;
 }
 
 int DSO_free(DSO *dso)
@@ -135,27 +143,30 @@ int DSO_free(DSO *dso)
     if (dso == NULL)
         return (1);
 
-    i = CRYPTO_add(&dso->references, -1, CRYPTO_LOCK_DSO);
+    if (CRYPTO_atomic_add(&dso->references, -1, &i, dso->lock) <= 0)
+        return 0;
+
     REF_PRINT_COUNT("DSO", dso);
     if (i > 0)
-        return (1);
+        return 1;
     REF_ASSERT_ISNT(i < 0);
 
     if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
         DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED);
-        return (0);
+        return 0;
     }
 
     if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
         DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED);
-        return (0);
+        return 0;
     }
 
     sk_void_free(dso->meth_data);
     OPENSSL_free(dso->filename);
     OPENSSL_free(dso->loaded_filename);
+    CRYPTO_THREAD_lock_free(dso->lock);
     OPENSSL_free(dso);
-    return (1);
+    return 1;
 }
 
 int DSO_flags(DSO *dso)
@@ -165,13 +176,19 @@ int DSO_flags(DSO *dso)
 
 int DSO_up_ref(DSO *dso)
 {
+    int i;
+
     if (dso == NULL) {
         DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
-        return (0);
+        return 0;
     }
 
-    CRYPTO_add(&dso->references, 1, CRYPTO_LOCK_DSO);
-    return (1);
+    if (CRYPTO_atomic_add(&dso->references, 1, &i, dso->lock) <= 0)
+        return 0;
+
+    REF_PRINT_COUNT("DSO", r);
+    REF_ASSERT_ISNT(i < 2);
+    return ((i > 1) ? 1 : 0);
 }
 
 DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
index 12052e14ce898e716bc6ffacceaa15c1d6dce62c..44611ca7fb0fd9716a6e323c450440a9fb6964de 100644 (file)
@@ -187,7 +187,6 @@ extern "C" {
 # define CRYPTO_LOCK_READDIR             24
 # define CRYPTO_LOCK_RSA_BLINDING        25
 # define CRYPTO_LOCK_MALLOC2             27
-# define CRYPTO_LOCK_DSO                 28
 # define CRYPTO_LOCK_DYNLOCK             29
 # define CRYPTO_LOCK_ENGINE              30
 # define CRYPTO_LOCK_UI                  31
index c1229976a7e481903756d89f507486387a7e2043..1eadbd96e1c52d0ea497b1f09211c0e4dfd1a022 100644 (file)
@@ -228,6 +228,7 @@ struct dso_st {
      * loaded.
      */
     char *loaded_filename;
+    CRYPTO_RWLOCK *lock;
 };
 
 DSO *DSO_new(void);