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 * See original implementation: http://bit.ly/1vkDjo
55 * ACE homepage: http://www.cse.wustl.edu/~schmidt/ACE.html
56 * See also: http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
57 */
58 typedef struct {
59 LONG waiters;
60 int was_broadcast;
61 CRITICAL_SECTION waiters_lock;
62 HANDLE sema;
63 HANDLE continue_broadcast;
64 } pthread_cond_t;
65
66 extern int pthread_cond_init(pthread_cond_t *cond, const void *unused);
67 extern int pthread_cond_destroy(pthread_cond_t *cond);
68 extern int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex);
69 extern int pthread_cond_signal(pthread_cond_t *cond);
70 extern int pthread_cond_broadcast(pthread_cond_t *cond);
71
72 /*
73 * Simple thread creation implementation using pthread API
74 */
75 typedef struct {
76 HANDLE handle;
77 void *(*start_routine)(void*);
78 void *arg;
79 } pthread_t;
80
81 extern int pthread_create(pthread_t *thread, const void *unused,
82 void *(*start_routine)(void*), void *arg);
83
84 /*
85 * To avoid the need of copying a struct, we use small macro wrapper to pass
86 * pointer to win32_pthread_join instead.
87 */
88 #define pthread_join(a, b) win32_pthread_join(&(a), (b))
89
90 extern int win32_pthread_join(pthread_t *thread, void **value_ptr);
91
92 /**
93 * pthread_once implementation based on the MS Windows One-Time Initialization
94 * (https://docs.microsoft.com/en-us/windows/desktop/Sync/one-time-initialization)
95 * APIs.
96 */
97 typedef INIT_ONCE pthread_once_t;
98 #define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
99 #define pthread_once blosc_internal_pthread_once /* Avoid symbol conflicts */
blosc_internal_pthread_once(pthread_once_t * once_control,void (* init_routine)(void))100 static int blosc_internal_pthread_once(pthread_once_t* once_control,
101 void (*init_routine)(void)) {
102 BOOL pending;
103 InitOnceBeginInitialize(once_control, /*dwFlags=*/0, /*fPending=*/&pending,
104 NULL);
105 if (pending == TRUE) {
106 init_routine();
107 InitOnceComplete(once_control, /*dwFlags=*/0, /*lpContext=*/NULL);
108 }
109 return 0;
110 }
111
112 #endif /* PTHREAD_H */
113