From 5f97e81a6915d6a1c2e4cefc92d9cf1367943762 Mon Sep 17 00:00:00 2001 From: Ray Brown Date: Sun, 2 Dec 2012 13:01:45 -0800 Subject: [PATCH] Use ssl module locking when available Thread locking callbacks into the interpreter impose considerable overhead. The standard library's ssl module registers its own thread locking and id callback functions. Registration of the latter can be detected by a call to the OpenSSL library. In this case, do not set the Python callback function and therefore keep using the more efficient ssl module functions. --- dtls/openssl.py | 1 + dtls/tlock.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/dtls/openssl.py b/dtls/openssl.py index bea985c..c48ea60 100644 --- a/dtls/openssl.py +++ b/dtls/openssl.py @@ -486,6 +486,7 @@ map(lambda x: _make_function(*x), ( ("SSLeay_version", libcrypto, ((c_char_p, "ret"), (c_int, "t"))), ("CRYPTO_set_locking_callback", libcrypto, ((None, "ret"), (c_void_p, "func")), False), + ("CRYPTO_get_id_callback", libcrypto, ((c_void_p, "ret"),), True, None), ("CRYPTO_num_locks", libcrypto, ((c_int, "ret"),)), ("DTLSv1_server_method", libssl, ((DTLSv1Method, "ret"),)), ("DTLSv1_client_method", libssl, ((DTLSv1Method, "ret"),)), diff --git a/dtls/tlock.py b/dtls/tlock.py index 4aea6f8..1771dc1 100644 --- a/dtls/tlock.py +++ b/dtls/tlock.py @@ -21,6 +21,10 @@ DO_DEBUG_LOG = False def tlock_init(): if not globals().has_key("threading"): return # nothing to configure + # The standard library ssl module's lock implementation is more efficient; + # do not override it if it has been established + if CRYPTO_get_id_callback(): + return global _locks num_locks = CRYPTO_num_locks() _locks = tuple(threading.Lock() for _ in range(num_locks))