2 * Copyright (C) 2009 Andrzej K. Haczewski <ahaczewski@gmail.com>
4 * DISCLAMER: The implementation is Git-specific, it is subset of original
5 * Pthreads API, without lots of other features that Git doesn't use.
6 * Git also makes sure that the passed arguments are valid, so there's
7 * no need for double-checking.
10 #include "../../git-compat-util.h"
16 static unsigned __stdcall win32_start_routine(void *arg)
18 pthread_t *thread = arg;
19 thread->arg = thread->start_routine(thread->arg);
23 int pthread_create(pthread_t *thread, const void *unused,
24 void *(*start_routine)(void*), void *arg)
27 thread->start_routine = start_routine;
28 thread->handle = (HANDLE)
29 _beginthreadex(NULL, 0, win32_start_routine, thread, 0, NULL);
37 int win32_pthread_join(pthread_t *thread, void **value_ptr)
39 DWORD result = WaitForSingleObject(thread->handle, INFINITE);
43 *value_ptr = thread->arg;
48 return err_win_to_posix(GetLastError());
52 int pthread_cond_init(pthread_cond_t *cond, const void *unused)
56 cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
58 die("CreateSemaphore() failed");
62 int pthread_cond_destroy(pthread_cond_t *cond)
64 CloseHandle(cond->sema);
70 int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex)
72 InterlockedIncrement(&cond->waiters);
75 * Unlock external mutex and wait for signal.
76 * NOTE: we've held mutex locked long enough to increment
77 * waiters count above, so there's no problem with
78 * leaving mutex unlocked before we wait on semaphore.
80 LeaveCriticalSection(mutex);
82 /* let's wait - ignore return value */
83 WaitForSingleObject(cond->sema, INFINITE);
85 /* we're done waiting, so make sure we decrease waiters count */
86 InterlockedDecrement(&cond->waiters);
88 /* lock external mutex again */
89 EnterCriticalSection(mutex);
94 int pthread_cond_signal(pthread_cond_t *cond)
97 * Access to waiters count is atomic; see "Interlocked Variable Access"
98 * http://msdn.microsoft.com/en-us/library/ms684122(VS.85).aspx
100 int have_waiters = cond->waiters > 0;
103 * Signal only when there are waiters
106 return ReleaseSemaphore(cond->sema, 1, NULL) ?
107 0 : err_win_to_posix(GetLastError());