-
Notifications
You must be signed in to change notification settings - Fork 9
/
pthread_gettid_helpers.cpp
65 lines (54 loc) · 1.23 KB
/
pthread_gettid_helpers.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
#include <pthread.h>
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
extern int (*glibc_pthread_mutex_lock)(pthread_mutex_t *m);
extern int (*glibc_pthread_mutex_unlock)(pthread_mutex_t *m);
extern "C"
{
typedef struct
{
pid_t tid;
pthread_t t;
} thread_to_tid_t;
thread_to_tid_t thread_to_tid[512] = {{0,0}};
void my_settid_to_thread(pthread_t t, pid_t tid)
{
glibc_pthread_mutex_lock(&mutex);
for(int i=0;i<512;i++)
{
if(thread_to_tid[i].tid == 0 && thread_to_tid[i].t == 0)
{
thread_to_tid[i].tid = tid;
thread_to_tid[i].t = t;
}
}
glibc_pthread_mutex_unlock(&mutex);
}
pid_t my_gettid_from_thread(pthread_t t)
{
glibc_pthread_mutex_lock(&mutex);
pid_t tid = 0;
for(int i=0;i<512;i++)
{
if(thread_to_tid[i].t == t)
{
tid = thread_to_tid[i].tid;
break;
}
}
glibc_pthread_mutex_unlock(&mutex);
return tid;
}
void my_remtid(pthread_t t)
{
glibc_pthread_mutex_lock(&mutex);
for(int i=0;i<512;i++)
{
if(thread_to_tid[i].t == t)
{
thread_to_tid[i].tid = 0;
thread_to_tid[i].t = 0;
}
}
glibc_pthread_mutex_unlock(&mutex);
}
}