forked from cloudbase/EHS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
staticssllocking.cpp
104 lines (89 loc) · 3.25 KB
/
staticssllocking.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* $Id$
*
* EHS is a library for embedding HTTP(S) support into a C++ application
*
* Copyright (C) 2004 Zachary J. Hansen
*
* Code cleanup, new features and bugfixes: Copyright (C) 2010 Fritz Elfert
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation;
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* This can be found in the 'COPYING' file.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef COMPILE_WITH_SSL
#include "staticssllocking.h"
#include "ehstypes.h"
#include <stdexcept>
using namespace std;
// deal with static variables
pthread_mutex_t * StaticSslLocking::poMutexes;
void StaticSslLocking::SslStaticLockCallback ( int inMode,
int inMutex,
const char *,
int )
{
if (inMode & CRYPTO_LOCK) {
pthread_mutex_lock(&StaticSslLocking::poMutexes[inMutex]);
} else {
pthread_mutex_unlock(&StaticSslLocking::poMutexes[inMutex]);
}
}
unsigned long StaticSslLocking::SslThreadIdCallback ( )
{
#ifdef __APPLE__
// TODO pthread_t is a pointer on OSX, so this conversion is everything else than clean
return reinterpret_cast<unsigned long>(THREADID(pthread_self()));
#else // __APPLE__
return static_cast<unsigned long>(THREADID(pthread_self()));
#endif // __APPLE__
}
StaticSslLocking::StaticSslLocking ( )
{
// allocate the needed number of locks
StaticSslLocking::poMutexes = new pthread_mutex_t[CRYPTO_num_locks()];
if ( NULL == StaticSslLocking::poMutexes ) {
throw runtime_error("StaticSslLocking::StaticSslLocking: Could not allocate CRYPTO mutexes") ;
}
// initialize the mutexes
for (int i = 0; i < CRYPTO_num_locks(); i++) {
if (0 != pthread_mutex_init(&StaticSslLocking::poMutexes[i], NULL)) {
throw runtime_error("StaticSslLocking::StaticSslLocking: Could not initialize CRYPTO mutex") ;
}
}
// set callbacks
CRYPTO_set_id_callback(StaticSslLocking::SslThreadIdCallback);
CRYPTO_set_locking_callback(StaticSslLocking::SslStaticLockCallback);
}
StaticSslLocking::~StaticSslLocking()
{
if (NULL == StaticSslLocking::poMutexes) {
// Destructors don't thow
// throw runtime_error("StaticSslLocking::~StaticSslLocking: poMutexes is NULL");
}
CRYPTO_set_id_callback(NULL);
CRYPTO_set_locking_callback(NULL);
for ( int i = 0; i < CRYPTO_num_locks(); i++) {
if (0 != pthread_mutex_destroy(&StaticSslLocking::poMutexes[i])) {
// Destructors don't thow
// throw runtime_error("StaticSslLocking::~StaticSslLocking: Could not destroy CRYPTO mutex");
}
}
delete [] StaticSslLocking::poMutexes;
StaticSslLocking::poMutexes = NULL;
}
#endif // COMPILE_WITH_SSL