1 /*
2  * Public domain
3  * pthread.h compatibility shim
4  */
5 module libressl_d.compat.pthread;
6 
7 
8 private static import core.sys.windows.winbase;
9 private static import core.sys.windows.windef;
10 private static import core.sys.windows.winnt;
11 public import core.sys.posix.pthread;
12 public import core.sys.windows.windows;
13 
14 extern (C):
15 nothrow @nogc:
16 
17 version (Windows) {
18 	private static import core.stdc.errno;
19 	public import core.stdc.stdlib;
20 	//#include <malloc.h>
21 
22 	/*
23 	 * Static once initialization values.
24 	 */
25 	//#define PTHREAD_ONCE_INIT { INIT_ONCE_STATIC_INIT }
26 
27 	/+
28 	/*
29 	 * Static mutex initialization values.
30 	 */
31 	#define PTHREAD_MUTEX_INITIALIZER { .lock = null }
32 
33 	/*
34 	 * Once definitions.
35 	 */
36 	struct pthread_once
37 	{
38 		INIT_ONCE once;
39 	}
40 
41 	alias pthread_once_t = .pthread_once;
42 
43 	//pragma(inline, true)
44 	extern (Windows)
45 	core.sys.windows.windef.BOOL _pthread_once_win32_cb(PINIT_ONCE once, core.sys.windows.winnt.PVOID param, core.sys.windows.winnt.PVOID* context)
46 
47 		do
48 		{
49 			void function() cb = param;
50 			cb();
51 
52 			return core.sys.windows.windef.TRUE;
53 		}
54 
55 	pragma(inline, true)
56 	int pthread_once(.pthread_once_t* once, void function() cb)
57 
58 		in
59 		{
60 			assert(once != null);
61 		}
62 
63 		do
64 		{
65 			core.sys.windows.windef.BOOL rc = InitOnceExecuteOnce(&once.once, ._pthread_once_win32_cb, cb, null);
66 
67 			if (rc == 0) {
68 				return -1;
69 			} else {
70 				return 0;
71 			}
72 		}
73 
74 	alias pthread_t = core.sys.windows.windef.DWORD;
75 
76 	pragma(inline, true)
77 	.pthread_t pthread_self()
78 
79 		do
80 		{
81 			return core.sys.windows.winbase.GetCurrentThreadId();
82 		}
83 
84 	pragma(inline, true)
85 	int pthread_equal(.pthread_t t1, .pthread_t t2)
86 
87 		do
88 		{
89 			return t1 == t2;
90 		}
91 
92 	struct pthread_mutex
93 	{
94 		volatile core.sys.windows.winbase.LPCRITICAL_SECTION lock;
95 	}
96 
97 	alias pthread_mutex_t = .pthread_mutex;
98 	alias pthread_mutexattr_t = void;
99 
100 	pragma(inline, true)
101 	int pthread_mutex_init(.pthread_mutex_t* mutex, const (.pthread_mutexattr_t)* attr)
102 
103 		in
104 		{
105 			assert(mutext != null);
106 		}
107 
108 		do
109 		{
110 			mutex.lock = core.stdc.stdlib.malloc(core.sys.windows.winbase.CRITICAL_SECTION.sizeof);
111 
112 			if (mutex.lock == null) {
113 				core.stdc.stdlib.exit(core.stdc.errno.ENOMEM);
114 			}
115 
116 			core.sys.windows.winbase.InitializeCriticalSection(mutex.lock);
117 
118 			return 0;
119 		}
120 
121 	pragma(inline, true)
122 	int pthread_mutex_lock(.pthread_mutex_t* mutex)
123 
124 		in
125 		{
126 			assert(mutext != null);
127 		}
128 
129 		do
130 		{
131 			if (mutex.lock == null) {
132 				core.sys.windows.winbase.LPCRITICAL_SECTION lcs = core.stdc.stdlib.malloc(core.sys.windows.winbase.CRITICAL_SECTION.sizeof);
133 
134 				if (lcs == null) {
135 					core.stdc.stdlib.exit(core.stdc.errno.ENOMEM);
136 				}
137 
138 				core.sys.windows.winbase.InitializeCriticalSection(lcs);
139 
140 				if (core.sys.windows.winbase.InterlockedCompareExchangePointer(cast(core.sys.windows.winnt.PVOID*)(&mutex.lock), cast(core.sys.windows.winnt.PVOID)(lcs), null) != null) {
141 					core.sys.windows.winbase.DeleteCriticalSection(lcs);
142 					core.stdc.stdlib.free(lcs);
143 				}
144 			}
145 
146 			core.sys.windows.winbase.EnterCriticalSection(mutex.lock);
147 
148 			return 0;
149 		}
150 
151 	pragma(inline, true)
152 	int pthread_mutex_unlock(.pthread_mutex_t* mutex)
153 
154 		in
155 		{
156 			assert(mutext != null);
157 		}
158 
159 		do
160 		{
161 			core.sys.windows.winbase.LeaveCriticalSection(mutex.lock);
162 
163 			return 0;
164 		}
165 
166 	pragma(inline, true)
167 	int pthread_mutex_destroy(.pthread_mutex_t* mutex)
168 
169 		in
170 		{
171 			assert(mutext != null);
172 		}
173 
174 		do
175 		{
176 			core.sys.windows.winbase.DeleteCriticalSection(mutex.lock);
177 			core.stdc.stdlib.free(mutex.lock);
178 
179 			return 0;
180 		}
181 	+/
182 }