Convert CRYPTO_LOCK_GET*BYNAME to new multi-threading API
authorAlessandro Ghedini <alessandro@ghedini.me>
Fri, 4 Mar 2016 15:24:26 +0000 (15:24 +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/bio/b_addr.c
include/openssl/crypto.h

index 6125e86124cfa660e75a0c255fccf1242eb64703..0a6c5e9713afb5b15e8183f33c1348c215e01c51 100644 (file)
 #include <string.h>
 
 #include "bio_lcl.h"
+#include "internal/threads.h"
 
 #ifndef OPENSSL_NO_SOCK
 #include <openssl/err.h>
 #include <openssl/buffer.h>
 #include <ctype.h>
 
+static CRYPTO_RWLOCK *bio_lookup_lock;
+static CRYPTO_ONCE bio_lookup_init = CRYPTO_ONCE_STATIC_INIT;
+
 /*
  * Throughout this file and bio_lcl.h, the existence of the macro
  * AI_PASSIVE is used to detect the availability of struct addrinfo,
@@ -623,6 +627,11 @@ static int addrinfo_wrap(int family, int socktype,
     return 1;
 }
 
+static void do_bio_lookup_init(void)
+{
+    bio_lookup_lock = CRYPTO_THREAD_lock_new();
+}
+
 /*-
  * BIO_lookup - look up the node and service you want to connect to.
  * @node: the node you want to connect to.
@@ -735,8 +744,9 @@ int BIO_lookup(const char *host, const char *service,
 #endif
         char *proto = NULL;
 
-        CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME);
-        CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME);
+        CRYPTO_THREAD_run_once(&bio_lookup_init, do_bio_lookup_init);
+
+        CRYPTO_THREAD_write_lock(bio_lookup_lock);
         he_fallback_address = INADDR_ANY;
         if (host == NULL) {
             he = &he_fallback;
@@ -838,8 +848,7 @@ int BIO_lookup(const char *host, const char *service,
             ret = 1;
         }
      err:
-        CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME);
-        CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME);
+        CRYPTO_THREAD_unlock(bio_lookup_lock);
     }
 
     return ret;
index bcc5aee94c9c41b687796d538dfdb46f7b5e9f8f..b3c669d57ec06430f2b4c3f9a37b301b8213f2e5 100644 (file)
@@ -186,8 +186,6 @@ extern "C" {
 # define CRYPTO_LOCK_RAND2               19
 # define CRYPTO_LOCK_MALLOC              20
 # define CRYPTO_LOCK_BIO                 21
-# define CRYPTO_LOCK_GETHOSTBYNAME       22
-# define CRYPTO_LOCK_GETSERVBYNAME       23
 # define CRYPTO_LOCK_READDIR             24
 # define CRYPTO_LOCK_RSA_BLINDING        25
 # define CRYPTO_LOCK_DH                  26