1 /*
2  * Code for simulating pthreads API on Windows.  This is Git-specific,
3  * but it is enough for Numexpr needs too.
4  *
5  * Copyright (C) 2009 Andrzej K. Haczewski <ahaczewski@gmail.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  * DISCLAIMER: The implementation is Git-specific, it is subset of original
26  * Pthreads API, without lots of other features that Git doesn't use.
27  * Git also makes sure that the passed arguments are valid, so there's
28  * no need for double-checking.
29  */
30 
31 #ifndef PTHREAD_H
32 #define PTHREAD_H
33 
34 #ifndef WIN32_LEAN_AND_MEAN
35 #define WIN32_LEAN_AND_MEAN
36 #endif
37 
38 #include <windows.h>
39 
40 /*
41  * Defines that adapt Windows API threads to pthreads API
42  */
43 #define pthread_mutex_t CRITICAL_SECTION
44 
45 #define pthread_mutex_init(a,b) InitializeCriticalSection((a))
46 #define pthread_mutex_destroy(a) DeleteCriticalSection((a))
47 #define pthread_mutex_lock EnterCriticalSection
48 #define pthread_mutex_unlock LeaveCriticalSection
49 
50 /*
51  * Implement simple condition variable for Windows threads, based on ACE
52  * implementation.
53  */
54 typedef struct {
55 	LONG waiters;
56 	int was_broadcast;
57 	CRITICAL_SECTION waiters_lock;
58 	HANDLE sema;
59 	HANDLE continue_broadcast;
60 } pthread_cond_t;
61 
62 extern int pthread_cond_init(pthread_cond_t *cond, const void *unused);
63 extern int pthread_cond_destroy(pthread_cond_t *cond);
64 extern int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex);
65 extern int pthread_cond_signal(pthread_cond_t *cond);
66 extern int pthread_cond_broadcast(pthread_cond_t *cond);
67 
68 /*
69  * Simple thread creation implementation using pthread API
70  */
71 typedef struct {
72 	HANDLE handle;
73 	void *(*start_routine)(void*);
74 	void *arg;
75 } pthread_t;
76 
77 extern int pthread_create(pthread_t *thread, const void *unused,
78 			  void *(*start_routine)(void*), void *arg);
79 
80 /*
81  * To avoid the need of copying a struct, we use small macro wrapper to pass
82  * pointer to win32_pthread_join instead.
83  */
84 #define pthread_join(a, b) win32_pthread_join(&(a), (b))
85 
86 extern int win32_pthread_join(pthread_t *thread, void **value_ptr);
87 
88 #endif /* PTHREAD_H */
89