xref: /freebsd/contrib/processor-trace/include/windows/threads.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*
2  * Copyright (c) 2014-2019, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *  * Neither the name of Intel Corporation nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  *
29  * It looks like there is still no support for C11's threads.h.
30  *
31  * We implement the few features we actually need hoping that this file will
32  * soon go away.
33  */
34 
35 #ifndef THREADS_H
36 #define THREADS_H
37 
38 #include "windows.h"
39 
40 
41 enum {
42 	thrd_success	= 1,
43 	thrd_error
44 };
45 
46 
47 struct pt_thread {
48 	HANDLE handle;
49 };
50 typedef struct pt_thread thrd_t;
51 
52 typedef int (*thrd_start_t)(void *);
53 
54 
55 struct thrd_args {
56 	thrd_start_t fun;
57 	void *arg;
58 };
59 
60 static DWORD WINAPI thrd_routine(void *arg)
61 {
62 	struct thrd_args *args;
63 	int result;
64 
65 	args = (struct thrd_args *) arg;
66 	if (!args)
67 		return (DWORD) -1;
68 
69 	result = -1;
70 	if (args->fun)
71 		result = args->fun(args->arg);
72 
73 	free(args);
74 
75 	return (DWORD) result;
76 }
77 
78 static inline int thrd_create(thrd_t *thrd, thrd_start_t fun, void *arg)
79 {
80 	struct thrd_args *args;
81 	HANDLE handle;
82 
83 	if (!thrd || !fun)
84 		return thrd_error;
85 
86 	args = malloc(sizeof(*args));
87 	if (!args)
88 		return thrd_error;
89 
90 	args->fun = fun;
91 	args->arg = arg;
92 
93 	handle = CreateThread(NULL, 0, thrd_routine, args, 0, NULL);
94 	if (!handle) {
95 		free(args);
96 		return thrd_error;
97 	}
98 
99 	thrd->handle = handle;
100 	return thrd_success;
101 }
102 
103 static inline int thrd_join(thrd_t *thrd, int *res)
104 {
105 	DWORD status;
106 	BOOL success;
107 
108 	if (!thrd)
109 		return thrd_error;
110 
111 	status = WaitForSingleObject(thrd->handle, INFINITE);
112 	if (status)
113 		return thrd_error;
114 
115 	if (res) {
116 		DWORD result;
117 
118 		success = GetExitCodeThread(thrd->handle, &result);
119 		if (!success) {
120 			(void) CloseHandle(thrd->handle);
121 			return thrd_error;
122 		}
123 
124 		*res = (int) result;
125 	}
126 
127 	success = CloseHandle(thrd->handle);
128 	if (!success)
129 		return thrd_error;
130 
131 	return thrd_success;
132 }
133 
134 struct pt_mutex {
135 	CRITICAL_SECTION cs;
136 };
137 typedef struct pt_mutex mtx_t;
138 
139 enum {
140 	mtx_plain
141 };
142 
143 static inline int mtx_init(mtx_t *mtx, int type)
144 {
145 	if (!mtx || type != mtx_plain)
146 		return thrd_error;
147 
148 	InitializeCriticalSection(&mtx->cs);
149 
150 	return thrd_success;
151 }
152 
153 static inline void mtx_destroy(mtx_t *mtx)
154 {
155 	if (mtx)
156 		DeleteCriticalSection(&mtx->cs);
157 }
158 
159 static inline int mtx_lock(mtx_t *mtx)
160 {
161 	if (!mtx)
162 		return thrd_error;
163 
164 	EnterCriticalSection(&mtx->cs);
165 
166 	return thrd_success;
167 }
168 
169 static inline int mtx_unlock(mtx_t *mtx)
170 {
171 	if (!mtx)
172 		return thrd_error;
173 
174 	LeaveCriticalSection(&mtx->cs);
175 
176 	return thrd_success;
177 }
178 
179 
180 struct pt_cond {
181 	CONDITION_VARIABLE cond;
182 };
183 typedef struct pt_cond cnd_t;
184 
185 static inline int cnd_init(cnd_t *cnd)
186 {
187 	if (!cnd)
188 		return thrd_error;
189 
190 	InitializeConditionVariable(&cnd->cond);
191 
192 	return thrd_success;
193 }
194 
195 static inline int cnd_destroy(cnd_t *cnd)
196 {
197 	if (!cnd)
198 		return thrd_error;
199 
200 	/* Nothing to do. */
201 
202 	return thrd_success;
203 }
204 
205 static inline int cnd_signal(cnd_t *cnd)
206 {
207 	if (!cnd)
208 		return thrd_error;
209 
210 	WakeConditionVariable(&cnd->cond);
211 
212 	return thrd_success;
213 }
214 
215 static inline int cnd_broadcast(cnd_t *cnd)
216 {
217 	if (!cnd)
218 		return thrd_error;
219 
220 	WakeAllConditionVariable(&cnd->cond);
221 
222 	return thrd_success;
223 }
224 
225 static inline int cnd_wait(cnd_t *cnd, mtx_t *mtx)
226 {
227 	BOOL success;
228 
229 	if (!cnd || !mtx)
230 		return thrd_error;
231 
232 	success = SleepConditionVariableCS(&cnd->cond, &mtx->cs, INFINITE);
233 	if (!success)
234 		return thrd_error;
235 
236 	return thrd_success;
237 }
238 
239 #endif /* THREADS_H */
240