yate: Call pthread_key_create when the key is first accessed
[feed/telephony.git] / net / yate / patches / 120-create-thread-key-on-access.patch
1 --- a/engine/Thread.cpp
2 +++ b/engine/Thread.cpp
3 @@ -106,21 +106,18 @@ static DWORD getTls()
4 return tls_index;
5 }
6 #else /* _WINDOWS */
7 -static pthread_key_t current_key;
8 -
9 -class ThreadPrivateKeyAlloc
10 +static pthread_key_t& current_key()
11 {
12 -public:
13 - ThreadPrivateKeyAlloc()
14 - {
15 - if (::pthread_key_create(&current_key,ThreadPrivate::destroyFunc)) {
16 + static pthread_key_t* current_key = NULL;
17 + if (!current_key) {
18 + current_key = new pthread_key_t;
19 + if (::pthread_key_create(current_key, ThreadPrivate::destroyFunc)) {
20 abortOnBug(true);
21 Debug(DebugFail,"Failed to create current thread key!");
22 }
23 }
24 -};
25 -
26 -static ThreadPrivateKeyAlloc keyAllocator;
27 + return *current_key;
28 +}
29 #endif /* _WINDOWS */
30
31 static TokenDict s_prio[] = {
32 @@ -309,7 +306,7 @@ void ThreadPrivate::run()
33 #ifdef _WINDOWS
34 ::TlsSetValue(getTls(),this);
35 #else
36 - ::pthread_setspecific(current_key,this);
37 + ::pthread_setspecific(current_key(),this);
38 pthread_cleanup_push(cleanupFunc,this);
39 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
40 ::pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,0);
41 @@ -421,7 +418,7 @@ ThreadPrivate* ThreadPrivate::current()
42 #ifdef _WINDOWS
43 return reinterpret_cast<ThreadPrivate *>(::TlsGetValue(getTls()));
44 #else
45 - return reinterpret_cast<ThreadPrivate *>(::pthread_getspecific(current_key));
46 + return reinterpret_cast<ThreadPrivate *>(::pthread_getspecific(current_key()));
47 #endif
48 }
49