Fixes for Windows cas/threading stuff
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
diff --git a/src/thread-utils.h b/src/thread-utils.h
index 7b66318..e53a60c 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -71,16 +71,15 @@ GIT_INLINE(int) git_atomic_dec(git_atomic *a)
GIT_INLINE(void *) git___compare_and_swap(
volatile void **ptr, void *oldval, void *newval)
{
- bool swapped;
+ void *foundval;
#if defined(GIT_WIN32)
- swapped = ((LONGLONG)oldval == InterlockedCompareExchange64(
- (LONGLONG volatile *)ptr, (LONGLONG)newval, (LONGLONG)oldval));
+ foundval = InterlockedCompareExchangePointer(ptr, newval, oldval);
#elif defined(__GNUC__)
- swapped = (__sync_val_compare_and_swap(ptr, oldval, newval) == oldval);
+ foundval = __sync_val_compare_and_swap(ptr, oldval, newval);
#else
# error "Unsupported architecture for atomic operations"
#endif
- return swapped ? oldval : newval;
+ return (foundval == oldval) ? oldval : newval;
}
#else
diff --git a/src/win32/pthread.c b/src/win32/pthread.c
index 105f4b8..c78213f 100644
--- a/src/win32/pthread.c
+++ b/src/win32/pthread.c
@@ -14,18 +14,23 @@ int pthread_create(
void *GIT_RESTRICT arg)
{
GIT_UNUSED(attr);
- *thread = (pthread_t) CreateThread(
+ *thread = CreateThread(
NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, arg, 0, NULL);
return *thread ? 0 : -1;
}
int pthread_join(pthread_t thread, void **value_ptr)
{
- int ret;
- ret = WaitForSingleObject(thread, INFINITE);
- if (ret && value_ptr)
- GetExitCodeThread(thread, (void*) value_ptr);
- return -(!!ret);
+ DWORD ret = WaitForSingleObject(thread, INFINITE);
+
+ if (ret == WAIT_OBJECT_0) {
+ if (value_ptr != NULL)
+ GetExitCodeThread(thread, (void *)value_ptr);
+ CloseHandle(thread);
+ return 0;
+ }
+
+ return -1;
}
int pthread_mutex_init(pthread_mutex_t *GIT_RESTRICT mutex,