Git 2.47-rc0
[git.git] / compat / win32 / pthread.c
blob58980a529c3eb9c2beb9f9fd6d8ca7b5ec6e3b22
1 /*
2 * Copyright (C) 2009 Andrzej K. Haczewski <ahaczewski@gmail.com>
4 * DISCLAIMER: 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.
8 */
10 #include "../../git-compat-util.h"
11 #include "pthread.h"
13 #include <errno.h>
14 #include <limits.h>
16 static unsigned __stdcall win32_start_routine(void *arg)
18 pthread_t *thread = arg;
19 thread->tid = GetCurrentThreadId();
20 thread->arg = thread->start_routine(thread->arg);
21 return 0;
24 int pthread_create(pthread_t *thread, const void *attr UNUSED,
25 void *(*start_routine)(void *), void *arg)
27 thread->arg = arg;
28 thread->start_routine = start_routine;
29 thread->handle = (HANDLE)_beginthreadex(NULL, 0, win32_start_routine,
30 thread, 0, NULL);
32 if (!thread->handle)
33 return errno;
34 else
35 return 0;
38 int win32_pthread_join(pthread_t *thread, void **value_ptr)
40 DWORD result = WaitForSingleObject(thread->handle, INFINITE);
41 switch (result) {
42 case WAIT_OBJECT_0:
43 if (value_ptr)
44 *value_ptr = thread->arg;
45 CloseHandle(thread->handle);
46 return 0;
47 case WAIT_ABANDONED:
48 CloseHandle(thread->handle);
49 return EINVAL;
50 default:
51 /* the wait failed, so do not detach */
52 return err_win_to_posix(GetLastError());
56 pthread_t pthread_self(void)
58 pthread_t t = { NULL };
59 t.tid = GetCurrentThreadId();
60 return t;