Skip to content

Commit 912b263

Browse files
j6tgitster
authored andcommitted
Windows: more pthreads functions
This adds: pthread_self pthread_equal pthread_exit pthread_key_create pthread_setspecific pthread_getspecific Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5f8763a commit 912b263

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

compat/win32/pthread.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
static unsigned __stdcall win32_start_routine(void *arg)
1717
{
1818
pthread_t *thread = arg;
19+
thread->tid = GetCurrentThreadId();
1920
thread->arg = thread->start_routine(thread->arg);
2021
return 0;
2122
}
@@ -49,6 +50,13 @@ int win32_pthread_join(pthread_t *thread, void **value_ptr)
4950
}
5051
}
5152

53+
pthread_t pthread_self(void)
54+
{
55+
pthread_t t = { 0 };
56+
t.tid = GetCurrentThreadId();
57+
return t;
58+
}
59+
5260
int pthread_cond_init(pthread_cond_t *cond, const void *unused)
5361
{
5462
cond->waiters = 0;

compat/win32/pthread.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ typedef struct {
5252
HANDLE handle;
5353
void *(*start_routine)(void*);
5454
void *arg;
55+
DWORD tid;
5556
} pthread_t;
5657

5758
extern int pthread_create(pthread_t *thread, const void *unused,
@@ -65,4 +66,28 @@ extern int pthread_create(pthread_t *thread, const void *unused,
6566

6667
extern int win32_pthread_join(pthread_t *thread, void **value_ptr);
6768

69+
#define pthread_equal(t1, t2) ((t1).tid == (t2).tid)
70+
extern pthread_t pthread_self(void);
71+
72+
static inline int pthread_exit(void *ret)
73+
{
74+
ExitThread((DWORD)ret);
75+
}
76+
77+
typedef DWORD pthread_key_t;
78+
static inline int pthread_key_create(pthread_key_t *keyp, void (*destructor)(void *value))
79+
{
80+
return (*keyp = TlsAlloc()) == TLS_OUT_OF_INDEXES ? EAGAIN : 0;
81+
}
82+
83+
static inline int pthread_setspecific(pthread_key_t key, const void *value)
84+
{
85+
return TlsSetValue(key, (void *)value) ? 0 : EINVAL;
86+
}
87+
88+
static inline void *pthread_getspecific(pthread_key_t key)
89+
{
90+
return TlsGetValue(key);
91+
}
92+
6893
#endif /* PTHREAD_H */

0 commit comments

Comments
 (0)