xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/regress_thread.h (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: regress_thread.h,v 1.6 2024/08/18 20:47:23 christos Exp $	*/
2b8ecfcfeSchristos 
3b8ecfcfeSchristos /*
4b8ecfcfeSchristos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5b8ecfcfeSchristos  *
6b8ecfcfeSchristos  * Redistribution and use in source and binary forms, with or without
7b8ecfcfeSchristos  * modification, are permitted provided that the following conditions
8b8ecfcfeSchristos  * are met:
9b8ecfcfeSchristos  * 1. Redistributions of source code must retain the above copyright
10b8ecfcfeSchristos  *    notice, this list of conditions and the following disclaimer.
11b8ecfcfeSchristos  * 2. Redistributions in binary form must reproduce the above copyright
12b8ecfcfeSchristos  *    notice, this list of conditions and the following disclaimer in the
13b8ecfcfeSchristos  *    documentation and/or other materials provided with the distribution.
14b8ecfcfeSchristos  * 3. The name of the author may not be used to endorse or promote products
15b8ecfcfeSchristos  *    derived from this software without specific prior written permission.
16b8ecfcfeSchristos  *
17b8ecfcfeSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18b8ecfcfeSchristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19b8ecfcfeSchristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20b8ecfcfeSchristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21b8ecfcfeSchristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22b8ecfcfeSchristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23b8ecfcfeSchristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24b8ecfcfeSchristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25b8ecfcfeSchristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26b8ecfcfeSchristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27b8ecfcfeSchristos  */
28b8ecfcfeSchristos 
29b8ecfcfeSchristos #ifndef REGRESS_THREAD_H_INCLUDED_
30b8ecfcfeSchristos #define REGRESS_THREAD_H_INCLUDED_
31b8ecfcfeSchristos 
32*eabc0478Schristos #if defined(_WIN32) /** _WIN32 */
33*eabc0478Schristos #define THREAD_T void * /* HANDLE */
34*eabc0478Schristos #define THREAD_FN unsigned __stdcall
35*eabc0478Schristos #define THREAD_RETURN() return (0)
36*eabc0478Schristos #define THREAD_SELF() GetCurrentThreadId()
37*eabc0478Schristos #define THREAD_START(threadvar, fn, arg) do {                         \
38*eabc0478Schristos 	uintptr_t threadhandle = _beginthreadex(NULL,0,fn,(arg),0,NULL);  \
39*eabc0478Schristos 	(threadvar) = (THREAD_T)threadhandle;                             \
40*eabc0478Schristos 	thread_setup(threadvar);                                          \
41*eabc0478Schristos } while (0)
42*eabc0478Schristos #define THREAD_JOIN(th) WaitForSingleObject(th, INFINITE)
43*eabc0478Schristos #else /* !_WIN32 */
44*eabc0478Schristos #include <pthread.h>
45b8ecfcfeSchristos #define THREAD_T pthread_t
46b8ecfcfeSchristos #define THREAD_FN void *
47b8ecfcfeSchristos #define THREAD_RETURN() return (NULL)
48*eabc0478Schristos #define THREAD_SELF() pthread_self()
49b8ecfcfeSchristos #define THREAD_START(threadvar, fn, arg) do {          \
50*eabc0478Schristos 	if (!pthread_create(&(threadvar), NULL, fn, arg))  \
51*eabc0478Schristos 		thread_setup(threadvar);                       \
52b8ecfcfeSchristos } while (0)
53*eabc0478Schristos #define THREAD_JOIN(th) pthread_join(th, NULL)
54*eabc0478Schristos #endif /* \!_WIN32 */
55*eabc0478Schristos 
56*eabc0478Schristos void thread_setup(THREAD_T pthread);
57b8ecfcfeSchristos 
58b8ecfcfeSchristos #endif
59