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