test-lib-functions: introduce the 'test_set_port' helper function
[git/raj.git] / compat / win32 / pthread.c
blob2e7eead42cb008980c7a3b4de63050897c2996da
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 *unused,
25 void *(*start_routine)(void*), void *arg)
27 thread->arg = arg;
28 thread->start_routine = start_routine;
29 thread->handle = (HANDLE)
30 _beginthreadex(NULL, 0, win32_start_routine, 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 return 0;
46 case WAIT_ABANDONED:
47 return EINVAL;
48 default:
49 return err_win_to_posix(GetLastError());
53 pthread_t pthread_self(void)
55 pthread_t t = { NULL };
56 t.tid = GetCurrentThreadId();
57 return t;