diff --git a/examples/shrpx.cc b/examples/shrpx.cc index b93ba8a..686fa4e 100644 --- a/examples/shrpx.cc +++ b/examples/shrpx.cc @@ -47,6 +47,7 @@ #include "shrpx_config.h" #include "shrpx_listen_handler.h" +#include "shrpx_ssl.h" namespace shrpx { @@ -446,8 +447,12 @@ int main(int argc, char **argv) OpenSSL_add_all_algorithms(); SSL_load_error_strings(); SSL_library_init(); + ssl::setup_ssl_lock(); event_loop(); + + ssl::teardown_ssl_lock(); + return 0; } diff --git a/examples/shrpx_ssl.cc b/examples/shrpx_ssl.cc index 5cc2056..b1196c6 100644 --- a/examples/shrpx_ssl.cc +++ b/examples/shrpx_ssl.cc @@ -27,6 +27,9 @@ #include #include #include +#include + +#include #include #include @@ -146,6 +149,43 @@ ClientHandler* accept_ssl_connection(event_base *evbase, SSL_CTX *ssl_ctx, } } +namespace { +pthread_mutex_t *ssl_locks; +} // namespace + +namespace { +void ssl_locking_cb(int mode, int type, const char *file, int line) +{ + if(mode & CRYPTO_LOCK) { + pthread_mutex_lock(&(ssl_locks[type])); + } else { + pthread_mutex_unlock(&(ssl_locks[type])); + } +} +} // namespace + +void setup_ssl_lock() +{ + ssl_locks = new pthread_mutex_t[CRYPTO_num_locks()]; + for(int i = 0; i < CRYPTO_num_locks(); ++i) { + // Always returns 0 + pthread_mutex_init(&(ssl_locks[i]), 0); + } + //CRYPTO_set_id_callback(ssl_thread_id); OpenSSL manual says that if + // threadid_func is not specified using + // CRYPTO_THREADID_set_callback(), then default implementation is + // used. We use this default one. + CRYPTO_set_locking_callback(ssl_locking_cb); +} + +void teardown_ssl_lock() +{ + for(int i = 0; i < CRYPTO_num_locks(); ++i) { + pthread_mutex_destroy(&(ssl_locks[i])); + } + delete [] ssl_locks; +} + } // namespace ssl } // namespace shrpx diff --git a/examples/shrpx_ssl.h b/examples/shrpx_ssl.h index 1f3e362..65395db 100644 --- a/examples/shrpx_ssl.h +++ b/examples/shrpx_ssl.h @@ -44,6 +44,10 @@ ClientHandler* accept_ssl_connection(event_base *evbase, SSL_CTX *ssl_ctx, evutil_socket_t fd, sockaddr *addr, int addrlen); +void setup_ssl_lock(); + +void teardown_ssl_lock(); + } // namespace ssl } // namespace shrpx