盒子
盒子

TLS for linux and windows

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
#include <new>
#include <cassert>
#ifdef __linux__
#include <pthread.h>
#endif
typedef void * TLSValueType;
typedef void (* TLSInitCleanupFunc)(void *);
#ifdef _MSC_VER
typedef DWORD TLSKeyType;
#else //__linux__
typedef pthread_key_t * TLSKeyType;
#endif
inline TLSKeyType TLSInit(TLSInitCleanupFunc);
inline TLSValueType TLSGetValue(TLSKeyType);
inline void TLSSetValue(TLSKeyType, TLSValueType);
inline void TLSCleanup(TLSKeyType);
#ifdef _MSC_VER
TLSKeyType TLSInit(TLSInitCleanupFunc)
{
return TlsAlloc();
}
FLSKeyType FLSInit(FLSInitCleanupFunc cleanupfunc)
{
return FlsAlloc(cleanupfunc);
}
TLSValueType TLSGetValue(TLSKeyType key)
{
return TlsGetValue(key);
}
void TLSSetValue(TLSKeyType key, TLSValueType val)
{
TlsSetValue(key, val);
}
void TLSCleanup(TLSKeyType key)
{
TlsFree(key);
}
#else //__linux__
TLSKeyType TLSInit(TLSInitCleanupFunc cleanupfunc)
{
pthread_key_t * key = new pthread_key_t;
pthread_key_create(key, cleanupfunc);
return key;
}
TLSValueType TLSGetValue(TLSKeyType key)
{
return pthread_getspecific(*key);
}
void TLSSetValue(TLSKeyType key, TLSValueType value)
{
pthread_setspecific(*key, value);
}
void TLSCleanup(TLSKeyType key)
{
pthread_key_delete(*key);
delete key;
}
#endif

呼呼呼山(http://code4fun.me)
2019-04-15 15:09:51