Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb59176

Browse files
committed
Win32: Fix object::cache::threadmania test on x64
1 parent df19219 commit fb59176

File tree

8 files changed

+96
-68
lines changed

8 files changed

+96
-68
lines changed

src/global.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void git__shutdown(void)
7878
static DWORD _tls_index;
7979
static volatile LONG _mutex = 0;
8080

81-
static int synchronized_threads_init()
81+
static int synchronized_threads_init(void)
8282
{
8383
int error;
8484

@@ -112,7 +112,7 @@ int git_threads_init(void)
112112
return error;
113113
}
114114

115-
static void synchronized_threads_shutdown()
115+
static void synchronized_threads_shutdown(void)
116116
{
117117
/* Shut down any subsystems that have global state */
118118
git__shutdown();

src/pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ static int ll_find_deltas(git_packbuilder *pb, git_pobject **list,
12091209
git_mutex_unlock(&target->mutex);
12101210

12111211
if (!sub_size) {
1212-
git_thread_join(target->thread, NULL);
1212+
git_thread_join(&target->thread, NULL);
12131213
git_cond_free(&target->cond);
12141214
git_mutex_free(&target->mutex);
12151215
active_threads--;

src/thread-utils.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,23 @@ typedef git_atomic git_atomic_ssize;
4040

4141
#ifdef GIT_THREADS
4242

43+
#if defined(GIT_WIN32)
44+
45+
#define git_thread git_win32_thread
46+
#define git_thread_create(thread, attr, start_routine, arg) \
47+
git_win32__thread_create(thread, attr, start_routine, arg)
48+
#define git_thread_join(thread_ptr, status) \
49+
git_win32__thread_join(thread_ptr, status)
50+
51+
#else
52+
4353
#define git_thread pthread_t
4454
#define git_thread_create(thread, attr, start_routine, arg) \
4555
pthread_create(thread, attr, start_routine, arg)
46-
#define git_thread_kill(thread) pthread_cancel(thread)
47-
#define git_thread_exit(status) pthread_exit(status)
48-
#define git_thread_join(id, status) pthread_join(id, status)
56+
#define git_thread_join(thread_ptr, status) \
57+
pthread_join(*(thread_ptr), status)
58+
59+
#endif
4960

5061
#if defined(GIT_WIN32)
5162
#define git_thread_yield() Sleep(0)
@@ -179,8 +190,6 @@ GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
179190

180191
#define git_thread unsigned int
181192
#define git_thread_create(thread, attr, start_routine, arg) 0
182-
#define git_thread_kill(thread) (void)0
183-
#define git_thread_exit(status) (void)0
184193
#define git_thread_join(id, status) (void)0
185194
#define git_thread_yield() (void)0
186195

src/win32/pthread.c

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,63 @@
88
#include "pthread.h"
99
#include "../global.h"
1010

11-
int pthread_create(
12-
pthread_t *GIT_RESTRICT thread,
11+
#define CLEAN_THREAD_EXIT 0x6F012842
12+
13+
/* The thread procedure stub used to invoke the caller's procedure
14+
* and capture the return value for later collection. Windows will
15+
* only hold a DWORD, but we need to be able to store an entire
16+
* void pointer. This requires the indirection. */
17+
static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
18+
{
19+
git_win32_thread *thread = lpParameter;
20+
21+
thread->value = thread->proc(thread->value);
22+
23+
return CLEAN_THREAD_EXIT;
24+
}
25+
26+
int git_win32__thread_create(
27+
git_win32_thread *GIT_RESTRICT thread,
1328
const pthread_attr_t *GIT_RESTRICT attr,
1429
void *(*start_routine)(void*),
1530
void *GIT_RESTRICT arg)
1631
{
1732
GIT_UNUSED(attr);
18-
*thread = CreateThread(
19-
NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, arg, 0, NULL);
20-
return *thread ? 0 : -1;
33+
34+
thread->value = arg;
35+
thread->proc = start_routine;
36+
thread->thread = CreateThread(
37+
NULL, 0, git_win32__threadproc, thread, 0, NULL);
38+
39+
return thread->thread ? 0 : -1;
2140
}
2241

23-
int pthread_join(pthread_t thread, void **value_ptr)
42+
int git_win32__thread_join(
43+
git_win32_thread *thread,
44+
void **value_ptr)
2445
{
25-
DWORD ret = WaitForSingleObject(thread, INFINITE);
46+
DWORD exit;
47+
48+
if (WaitForSingleObject(thread->thread, INFINITE) != WAIT_OBJECT_0)
49+
return -1;
50+
51+
if (!GetExitCodeThread(thread->thread, &exit)) {
52+
CloseHandle(thread->thread);
53+
return -1;
54+
}
2655

27-
if (ret == WAIT_OBJECT_0) {
28-
if (value_ptr != NULL) {
29-
*value_ptr = NULL;
30-
GetExitCodeThread(thread, (void *)value_ptr);
31-
}
32-
CloseHandle(thread);
33-
return 0;
56+
/* Check for the thread having exited uncleanly. If exit was unclean,
57+
* then we don't have a return value to give back to the caller. */
58+
if (exit != CLEAN_THREAD_EXIT) {
59+
assert(false);
60+
thread->value = NULL;
3461
}
3562

36-
return -1;
63+
if (value_ptr)
64+
*value_ptr = thread->value;
65+
66+
CloseHandle(thread->thread);
67+
return 0;
3768
}
3869

3970
int pthread_mutex_init(
@@ -144,9 +175,6 @@ int pthread_num_processors_np(void)
144175
return n ? n : 1;
145176
}
146177

147-
148-
static HINSTANCE win32_kernel32_dll;
149-
150178
typedef void (WINAPI *win32_srwlock_fn)(GIT_SRWLOCK *);
151179

152180
static win32_srwlock_fn win32_srwlock_initialize;
@@ -159,7 +187,7 @@ int pthread_rwlock_init(
159187
pthread_rwlock_t *GIT_RESTRICT lock,
160188
const pthread_rwlockattr_t *GIT_RESTRICT attr)
161189
{
162-
(void)attr;
190+
GIT_UNUSED(attr);
163191

164192
if (win32_srwlock_initialize)
165193
win32_srwlock_initialize(&lock->native.srwl);
@@ -217,38 +245,22 @@ int pthread_rwlock_destroy(pthread_rwlock_t *lock)
217245
return 0;
218246
}
219247

220-
221-
static void win32_pthread_shutdown(void)
222-
{
223-
if (win32_kernel32_dll) {
224-
FreeLibrary(win32_kernel32_dll);
225-
win32_kernel32_dll = NULL;
226-
}
227-
}
228-
229248
int win32_pthread_initialize(void)
230249
{
231-
if (win32_kernel32_dll)
232-
return 0;
233-
234-
win32_kernel32_dll = LoadLibrary("Kernel32.dll");
235-
if (!win32_kernel32_dll) {
236-
giterr_set(GITERR_OS, "Could not load Kernel32.dll!");
237-
return -1;
250+
HMODULE hModule = GetModuleHandleW(L"kernel32");
251+
252+
if (hModule) {
253+
win32_srwlock_initialize = (win32_srwlock_fn)
254+
GetProcAddress(hModule, "InitializeSRWLock");
255+
win32_srwlock_acquire_shared = (win32_srwlock_fn)
256+
GetProcAddress(hModule, "AcquireSRWLockShared");
257+
win32_srwlock_release_shared = (win32_srwlock_fn)
258+
GetProcAddress(hModule, "ReleaseSRWLockShared");
259+
win32_srwlock_acquire_exclusive = (win32_srwlock_fn)
260+
GetProcAddress(hModule, "AcquireSRWLockExclusive");
261+
win32_srwlock_release_exclusive = (win32_srwlock_fn)
262+
GetProcAddress(hModule, "ReleaseSRWLockExclusive");
238263
}
239264

240-
win32_srwlock_initialize = (win32_srwlock_fn)
241-
GetProcAddress(win32_kernel32_dll, "InitializeSRWLock");
242-
win32_srwlock_acquire_shared = (win32_srwlock_fn)
243-
GetProcAddress(win32_kernel32_dll, "AcquireSRWLockShared");
244-
win32_srwlock_release_shared = (win32_srwlock_fn)
245-
GetProcAddress(win32_kernel32_dll, "ReleaseSRWLockShared");
246-
win32_srwlock_acquire_exclusive = (win32_srwlock_fn)
247-
GetProcAddress(win32_kernel32_dll, "AcquireSRWLockExclusive");
248-
win32_srwlock_release_exclusive = (win32_srwlock_fn)
249-
GetProcAddress(win32_kernel32_dll, "ReleaseSRWLockExclusive");
250-
251-
git__on_shutdown(win32_pthread_shutdown);
252-
253265
return 0;
254266
}

src/win32/pthread.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616
# define GIT_RESTRICT __restrict__
1717
#endif
1818

19+
typedef struct {
20+
HANDLE thread;
21+
void *(*proc)(void *);
22+
void *value;
23+
} git_win32_thread;
24+
1925
typedef int pthread_mutexattr_t;
2026
typedef int pthread_condattr_t;
2127
typedef int pthread_attr_t;
2228
typedef int pthread_rwlockattr_t;
2329

2430
typedef CRITICAL_SECTION pthread_mutex_t;
25-
typedef HANDLE pthread_t;
2631
typedef HANDLE pthread_cond_t;
2732

2833
typedef struct { void *Ptr; } GIT_SRWLOCK;
@@ -36,13 +41,15 @@ typedef struct {
3641

3742
#define PTHREAD_MUTEX_INITIALIZER {(void*)-1}
3843

39-
int pthread_create(
40-
pthread_t *GIT_RESTRICT thread,
41-
const pthread_attr_t *GIT_RESTRICT attr,
42-
void *(*start_routine)(void*),
43-
void *GIT_RESTRICT arg);
44+
int git_win32__thread_create(
45+
git_win32_thread *GIT_RESTRICT,
46+
const pthread_attr_t *GIT_RESTRICT,
47+
void *(*) (void *),
48+
void *GIT_RESTRICT);
4449

45-
int pthread_join(pthread_t, void **);
50+
int git_win32__thread_join(
51+
git_win32_thread *,
52+
void **);
4653

4754
int pthread_mutex_init(
4855
pthread_mutex_t *GIT_RESTRICT mutex,

tests/object/cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void test_object_cache__threadmania(void)
229229

230230
#ifdef GIT_THREADS
231231
for (th = 0; th < THREADCOUNT; ++th) {
232-
cl_git_pass(git_thread_join(t[th], &data));
232+
cl_git_pass(git_thread_join(&t[th], &data));
233233
cl_assert_equal_i(th, ((int *)data)[0]);
234234
git__free(data);
235235
}
@@ -276,7 +276,7 @@ void test_object_cache__fast_thread_rush(void)
276276
#ifdef GIT_THREADS
277277
for (th = 0; th < THREADCOUNT*2; ++th) {
278278
void *rval;
279-
cl_git_pass(git_thread_join(t[th], &rval));
279+
cl_git_pass(git_thread_join(&t[th], &rval));
280280
cl_assert_equal_i(th, *((int *)rval));
281281
}
282282
#endif

tests/threads/refdb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void test_threads_refdb__iterator(void)
8484

8585
#ifdef GIT_THREADS
8686
for (t = 0; t < THREADS; ++t) {
87-
cl_git_pass(git_thread_join(th[t], NULL));
87+
cl_git_pass(git_thread_join(&th[t], NULL));
8888
}
8989
#endif
9090

@@ -215,7 +215,7 @@ void test_threads_refdb__edit_while_iterate(void)
215215
}
216216

217217
for (t = 0; t < THREADS; ++t) {
218-
cl_git_pass(git_thread_join(th[t], NULL));
218+
cl_git_pass(git_thread_join(&th[t], NULL));
219219
}
220220
#endif
221221
}

tests/threads/thread_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void run_in_parallel(
3232

3333
#ifdef GIT_THREADS
3434
for (t = 0; t < threads; ++t)
35-
cl_git_pass(git_thread_join(th[t], NULL));
35+
cl_git_pass(git_thread_join(&th[t], NULL));
3636
memset(th, 0, threads * sizeof(git_thread));
3737
#endif
3838

0 commit comments

Comments
 (0)