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.
incoming
Ray Brown 2012-12-02 13:01:45 -08:00
parent 7c6a512f94
commit 5f97e81a69
2 changed files with 5 additions and 0 deletions

View File

@ -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"),)),

View File

@ -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))