Skip to content

Commit 8970acb

Browse files
committed
thread: don't use the global tlsdata for thread exit
We want to store a pointer to emulate `pthread_exit` on Windows. Do this within the threading infrastructure so that it could potentially be re-used outside of the context of libgit2 itself.
1 parent c40d2dc commit 8970acb

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/threadstate.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ typedef struct {
1414
git_error error_t;
1515
git_buf error_buf;
1616
char oid_fmt[GIT_OID_HEXSZ+1];
17-
18-
/* On Windows, this is the current child thread that was started by
19-
* `git_thread_create`. This is used to set the thread's exit code
20-
* when terminated by `git_thread_exit`. It is unused on POSIX.
21-
*/
22-
git_thread *current_thread;
2317
} git_threadstate;
2418

2519
extern int git_threadstate_global_init(void);

src/win32/thread.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
#include "thread.h"
99

10-
#include "../tlsdata.h"
11-
1210
#define CLEAN_THREAD_EXIT 0x6F012842
1311

1412
typedef void (WINAPI *win32_srwlock_fn)(GIT_SRWLOCK *);
@@ -19,6 +17,8 @@ static win32_srwlock_fn win32_srwlock_release_shared;
1917
static win32_srwlock_fn win32_srwlock_acquire_exclusive;
2018
static win32_srwlock_fn win32_srwlock_release_exclusive;
2119

20+
static DWORD fls_index;
21+
2222
/* The thread procedure stub used to invoke the caller's procedure
2323
* and capture the return value for later collection. Windows will
2424
* only hold a DWORD, but we need to be able to store an entire
@@ -28,13 +28,18 @@ static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
2828
git_thread *thread = lpParameter;
2929

3030
/* Set the current thread for `git_thread_exit` */
31-
GIT_TLSDATA->current_thread = thread;
31+
FlsSetValue(fls_index, thread);
3232

3333
thread->result = thread->proc(thread->param);
3434

3535
return CLEAN_THREAD_EXIT;
3636
}
3737

38+
static void git_threads_global_shutdown(void)
39+
{
40+
FlsFree(fls_index);
41+
}
42+
3843
int git_threads_global_init(void)
3944
{
4045
HMODULE hModule = GetModuleHandleW(L"kernel32");
@@ -52,6 +57,11 @@ int git_threads_global_init(void)
5257
GetProcAddress(hModule, "ReleaseSRWLockExclusive");
5358
}
5459

60+
if ((fls_index = FlsAlloc(NULL)) == FLS_OUT_OF_INDEXES)
61+
return -1;
62+
63+
git__on_shutdown(git_threads_global_shutdown);
64+
5565
return 0;
5666
}
5767

@@ -99,8 +109,11 @@ int git_thread_join(
99109

100110
void git_thread_exit(void *value)
101111
{
102-
assert(GIT_TLSDATA->current_thread);
103-
GIT_TLSDATA->current_thread->result = value;
112+
git_thread *thread = FlsGetValue(fls_index);
113+
114+
if (thread)
115+
thread->result = value;
116+
104117
ExitThread(CLEAN_THREAD_EXIT);
105118
}
106119

0 commit comments

Comments
 (0)