Optional Two-phase heap tracing
[hiphop-php.git] / hphp / util / ssl-init.cpp
bloba9e3925ab31c9578c3abf82f3ca04acdf8a3cbdf
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/util/ssl-init.h"
18 #include "hphp/util/mutex.h"
19 #include "hphp/util/process.h"
20 #include <openssl/crypto.h>
22 namespace HPHP {
23 ///////////////////////////////////////////////////////////////////////////////
25 static Mutex *s_locks = nullptr;
27 static unsigned long callback_thread_id() {
28 return (unsigned long)Process::GetThreadId();
31 static void
32 callback_locking(int mode, int type, const char* /*file*/, int /*line*/) {
33 if (mode & CRYPTO_LOCK) {
34 s_locks[type].lock();
35 } else {
36 s_locks[type].unlock();
40 static bool s_isSSLInited = false;
42 struct SSLUnitializer {
43 ~SSLUnitializer() {
44 delete [] s_locks;
47 static SSLUnitializer s_ssl_uninitializer;
49 void SSLInit::Init() {
50 assert(!s_isSSLInited);
51 s_locks = new Mutex[CRYPTO_num_locks()];
52 CRYPTO_set_id_callback((unsigned long (*)())callback_thread_id);
53 CRYPTO_set_locking_callback(
54 (void (*)(int mode, int type, const char *file, int line))
55 callback_locking);
56 s_isSSLInited = true;
59 bool SSLInit::IsInited() {
60 return s_isSSLInited;
63 ///////////////////////////////////////////////////////////////////////////////