1 /*
2   Copyright 2012-2014 David Robillard <http://drobilla.net>
3 
4   Permission to use, copy, modify, and/or distribute this software for any
5   purpose with or without fee is hereby granted, provided that the above
6   copyright notice and this permission notice appear in all copies.
7 
8   THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 
17 #ifndef ZIX_THREAD_H
18 #define ZIX_THREAD_H
19 
20 #ifdef _WIN32
21 #    include <windows.h>
22 #else
23 #    include <errno.h>
24 #    include <pthread.h>
25 #endif
26 
27 #include "zix/common.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #else
32 #    include <stdbool.h>
33 #endif
34 
35 /**
36    @addtogroup zix
37    @{
38    @name Thread
39    @{
40 */
41 
42 #ifdef _WIN32
43 typedef HANDLE ZixThread;
44 #else
45 typedef pthread_t ZixThread;
46 #endif
47 
48 /**
49    Initialize `thread` to a new thread.
50 
51    The thread will immediately be launched, calling `function` with `arg`
52    as the only parameter.
53 */
54 static inline ZixStatus
55 zix_thread_create(ZixThread* thread,
56                   size_t     stack_size,
57                   void*      (*function)(void*),
58                   void*      arg);
59 
60 /**
61    Join `thread` (block until `thread` exits).
62 */
63 static inline ZixStatus
64 zix_thread_join(ZixThread thread, void** retval);
65 
66 #ifdef _WIN32
67 
68 static inline ZixStatus
zix_thread_create(ZixThread * thread,size_t stack_size,void * (* function)(void *),void * arg)69 zix_thread_create(ZixThread* thread,
70                   size_t     stack_size,
71                   void*      (*function)(void*),
72                   void*      arg)
73 {
74 	*thread = CreateThread(NULL, stack_size,
75 	                       (LPTHREAD_START_ROUTINE)function, arg,
76 	                       0, NULL);
77 	return *thread ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
78 }
79 
80 static inline ZixStatus
zix_thread_join(ZixThread thread,void ** retval)81 zix_thread_join(ZixThread thread, void** retval)
82 {
83 	return WaitForSingleObject(thread, INFINITE)
84 		? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
85 }
86 
87 #else  /* !defined(_WIN32) */
88 
89 static inline ZixStatus
zix_thread_create(ZixThread * thread,size_t stack_size,void * (* function)(void *),void * arg)90 zix_thread_create(ZixThread* thread,
91                   size_t     stack_size,
92                   void*      (*function)(void*),
93                   void*      arg)
94 {
95 	pthread_attr_t attr;
96 	pthread_attr_init(&attr);
97 	pthread_attr_setstacksize(&attr, stack_size);
98 
99 	const int ret = pthread_create(thread, NULL, function, arg);
100 	pthread_attr_destroy(&attr);
101 
102 	if (ret == EAGAIN) {
103 		return ZIX_STATUS_NO_MEM;
104 	} else if (ret == EINVAL) {
105 		return ZIX_STATUS_BAD_ARG;
106 	} else if (ret == EPERM) {
107 		return ZIX_STATUS_BAD_PERMS;
108 	} else if (ret) {
109 		return ZIX_STATUS_ERROR;
110 	}
111 
112 	return ZIX_STATUS_SUCCESS;
113 }
114 
115 static inline ZixStatus
zix_thread_join(ZixThread thread,void ** retval)116 zix_thread_join(ZixThread thread, void** retval)
117 {
118 	return pthread_join(thread, retval)
119 		? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
120 }
121 
122 #endif
123 
124 /**
125    @}
126    @}
127 */
128 
129 #ifdef __cplusplus
130 }  /* extern "C" */
131 #endif
132 
133 #endif  /* ZIX_THREAD_H */
134