Removed global mutex

This commit is contained in:
yhirose 2018-04-20 09:37:59 -04:00
parent 5574d82eb3
commit 755f05c02b

View File

@ -316,6 +316,7 @@ private:
virtual bool read_and_close_socket(socket_t sock); virtual bool read_and_close_socket(socket_t sock);
SSL_CTX* ctx_; SSL_CTX* ctx_;
std::mutex ctx_mutex_;
}; };
class SSLClient : public Client { class SSLClient : public Client {
@ -334,6 +335,7 @@ private:
virtual bool read_and_close_socket(socket_t sock, Request& req, Response& res); virtual bool read_and_close_socket(socket_t sock, Request& req, Response& res);
SSL_CTX* ctx_; SSL_CTX* ctx_;
std::mutex ctx_mutex_;
}; };
#endif #endif
@ -2029,18 +2031,17 @@ inline std::shared_ptr<Response> Client::post(const char* path, const Headers& h
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
namespace detail { namespace detail {
// TODO: OpenSSL 1.0.2 occasionally crashes... The upcoming 1.1.0 is going to be thread safe.
static std::mutex ssl_ctx_mutex_;
template <typename U, typename V, typename T> template <typename U, typename V, typename T>
inline bool read_and_close_socket_ssl( inline bool read_and_close_socket_ssl(
socket_t sock, bool keep_alive, socket_t sock, bool keep_alive,
SSL_CTX* ctx, U SSL_connect_or_accept, V setup, // TODO: OpenSSL 1.0.2 occasionally crashes... The upcoming 1.1.0 is going to be thread safe.
SSL_CTX* ctx, std::mutex& ctx_mutex,
U SSL_connect_or_accept, V setup,
T callback) T callback)
{ {
SSL* ssl = nullptr; SSL* ssl = nullptr;
{ {
std::lock_guard<std::mutex> guard(ssl_ctx_mutex_); std::lock_guard<std::mutex> guard(ctx_mutex);
ssl = SSL_new(ctx); ssl = SSL_new(ctx);
if (!ssl) { if (!ssl) {
@ -2079,7 +2080,7 @@ inline bool read_and_close_socket_ssl(
SSL_shutdown(ssl); SSL_shutdown(ssl);
{ {
std::lock_guard<std::mutex> guard(ssl_ctx_mutex_); std::lock_guard<std::mutex> guard(ctx_mutex);
SSL_free(ssl); SSL_free(ssl);
} }
@ -2172,7 +2173,7 @@ inline bool SSLServer::read_and_close_socket(socket_t sock)
return detail::read_and_close_socket_ssl( return detail::read_and_close_socket_ssl(
sock, sock,
keep_alive, keep_alive,
ctx_, ctx_, ctx_mutex_,
SSL_accept, SSL_accept,
[](SSL* /*ssl*/) {}, [](SSL* /*ssl*/) {},
[this](Stream& strm, bool last_connection) { [this](Stream& strm, bool last_connection) {
@ -2204,7 +2205,8 @@ inline bool SSLClient::read_and_close_socket(socket_t sock, Request& req, Respon
{ {
return is_valid() && detail::read_and_close_socket_ssl( return is_valid() && detail::read_and_close_socket_ssl(
sock, false, sock, false,
ctx_, SSL_connect, ctx_, ctx_mutex_,
SSL_connect,
[&](SSL* ssl) { [&](SSL* ssl) {
SSL_set_tlsext_host_name(ssl, host_.c_str()); SSL_set_tlsext_host_name(ssl, host_.c_str());
}, },