-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpth.h
307 lines (231 loc) · 9.56 KB
/
pth.h
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* pth.h - GNU Pth emulation for W32 (MS Windows).
* Copyright (c) 1999-2003 Ralf S. Engelschall <[email protected]>
* Copyright (C) 2004, 2006, 2007, 2008, 2010 g10 Code GmbH
*
* This file is part of W32PTH.
*
* W32PTH is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* W32PTh is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* ------------------------------------------------------------------
* This code is based on Ralf Engelschall's GNU Pth, a non-preemptive
* thread scheduling library which can be found at
* http://www.gnu.org/software/pth/.
*/
#ifndef _W32_PTH_H
#define _W32_PTH_H
#ifdef _PTH_H_
#error w32pth conflict. A vanilla pth.h has already been included.
#endif
#include <winsock2.h> /* Newer mingw version require this to be
included before windows.h. */
/* #include <windows.h> /\* We need this for sockaddr et al. FIXME: too */
/* heavyweight - may be we should factor such */
/* code out to a second header and adjust all */
/* user files to include it only if required. *\/ */
/* Mingw64 defines sigset_t only of _POSIX is defined. We don't want
to do define this macro because we are a public header. Thus we
use a bit of mingw64 internal knowledge to declare sigset_t. */
#include <sys/types.h>
#if __MINGW64_VERSION_MAJOR >= 2 && !defined(_POSIX)
typedef _sigset_t sigset_t;
#endif /* mingw64 >= 2 */
#ifndef W32_PTH_HANDLE_INTERNAL
#define W32_PTH_HANDLE_INTERNAL int
#endif
/* These are needed for pipe support. Sigh. */
int pth_pipe (int filedes[2], int inherit_idx);
int pth_close (int fd);
/* We need to define value for the how argument of pth_sigmask. This
is required because Mingw does not yet define sigprocmask. We use
an enum to error out if Mingw eventually defines them. Also define
the sigset_t. */
#ifdef __MINGW32__
enum
{
SIG_BLOCK = 0,
SIG_UNBLOCK = 1,
SIG_SETMASK = 2
};
#endif /*__MINGW32__*/
/* Filedescriptor blocking modes. */
enum
{
PTH_FDMODE_ERROR = -1,
PTH_FDMODE_POLL = 0,
PTH_FDMODE_BLOCK,
PTH_FDMODE_NONBLOCK
};
/* Mutex values. */
#define PTH_MUTEX_INITIALIZED (1<<0)
#define PTH_MUTEX_LOCKED (1<<1)
/* Note: We can't do static initialization, thus we don't define the
initializer PTH_MUTEX_INIT. */
/* Read-write lock values. */
enum
{
PTH_RWLOCK_RD,
PTH_RWLOCK_RW
};
/* Note: We can't do static initialization, thus we don't define the
initializer PTH_RWLOCK_INIT. */
#define PTH_KEY_INIT (1<<0)
/* Event subject classes. */
#define PTH_EVENT_FD (1<<1)
#define PTH_EVENT_SELECT (1<<2)
#define PTH_EVENT_SIGS (1<<3)
#define PTH_EVENT_TIME (1<<4)
#define PTH_EVENT_MSG (1<<5)
#define PTH_EVENT_MUTEX (1<<6)
#define PTH_EVENT_COND (1<<7)
#define PTH_EVENT_TID (1<<8)
#define PTH_EVENT_FUNC (1<<9)
#define PTH_EVENT_HANDLE (1<<10) /* A generic waitable W32 HANDLE. */
/* Event occurrence restrictions. */
#define PTH_UNTIL_OCCURRED (1<<11)
#define PTH_UNTIL_FD_READABLE (1<<12)
#define PTH_UNTIL_FD_WRITEABLE (1<<13)
#define PTH_UNTIL_FD_EXCEPTION (1<<14)
#define PTH_UNTIL_TID_NEW (1<<15)
#define PTH_UNTIL_TID_READY (1<<16)
#define PTH_UNTIL_TID_WAITING (1<<17)
#define PTH_UNTIL_TID_DEAD (1<<18)
/* Event structure handling modes. */
#define PTH_MODE_REUSE (1<<20)
#define PTH_MODE_CHAIN (1<<21)
#define PTH_MODE_STATIC (1<<22)
/* Attribute commands for pth_attr_get and pth_attr_set(). */
enum
{
PTH_ATTR_PRIO, /* RW [int] Priority of thread. */
PTH_ATTR_NAME, /* RW [char *] Name of thread. */
PTH_ATTR_JOINABLE, /* RW [int] Thread detachment type. */
PTH_ATTR_CANCEL_STATE, /* RW [unsigned int] Thread cancellation state.*/
PTH_ATTR_STACK_SIZE, /* RW [unsigned int] Stack size. */
PTH_ATTR_STACK_ADDR, /* RW [char *] Stack lower address. */
PTH_ATTR_DISPATCHES, /* RO [int] Total number of
thread dispatches. */
PTH_ATTR_TIME_SPAWN, /* RO [pth_time_t] Time thread was spawned. */
PTH_ATTR_TIME_LAST, /* RO [pth_time_t] Time thread was
last dispatched. */
PTH_ATTR_TIME_RAN, /* RO [pth_time_t] Time thread was running. */
PTH_ATTR_START_FUNC, /* RO [void *(*)(void *)] Thread start function.*/
PTH_ATTR_START_ARG, /* RO [void *] Thread start argument. */
PTH_ATTR_STATE, /* RO [pth_state_t] Scheduling state. */
PTH_ATTR_EVENTS, /* RO [pth_event_t] Events the thread
is waiting for. */
PTH_ATTR_BOUND /* RO [int] Whether object is
bound to thread. */
};
/* Queries for pth_ctrl(). */
#define PTH_CTRL_GETAVLOAD (1<<1)
#define PTH_CTRL_GETPRIO (1<<2)
#define PTH_CTRL_GETNAME (1<<3)
#define PTH_CTRL_GETTHREADS_NEW (1<<4)
#define PTH_CTRL_GETTHREADS_READY (1<<5)
#define PTH_CTRL_GETTHREADS_RUNNING (1<<6)
#define PTH_CTRL_GETTHREADS_WAITING (1<<7)
#define PTH_CTRL_GETTHREADS_SUSPENDED (1<<8)
#define PTH_CTRL_GETTHREADS_DEAD (1<<9)
#define PTH_CTRL_DUMPSTATE (1<<10)
#define PTH_CTRL_GETTHREADS ( PTH_CTRL_GETTHREADS_NEW \
| PTH_CTRL_GETTHREADS_READY \
| PTH_CTRL_GETTHREADS_RUNNING \
| PTH_CTRL_GETTHREADS_WAITING \
| PTH_CTRL_GETTHREADS_SUSPENDED \
| PTH_CTRL_GETTHREADS_DEAD )
/* Event status codes. */
typedef enum
{
PTH_STATUS_PENDING,
PTH_STATUS_OCCURRED,
PTH_STATUS_FAILED
}
pth_status_t;
/* Event deallocation types. */
enum
{
PTH_FREE_THIS,
PTH_FREE_ALL
};
/* The Pth thread handle object. */
typedef void *pth_t;
/* The Mutex object. */
typedef W32_PTH_HANDLE_INTERNAL pth_mutex_t;
/* The read-write lock object. */
typedef W32_PTH_HANDLE_INTERNAL pth_rwlock_t;
/* The Event object. */
struct pth_event_s;
typedef struct pth_event_s *pth_event_t;
/* The Attribute object. */
struct pth_attr_s;
typedef struct pth_attr_s *pth_attr_t;
/* The Key object. */
typedef int pth_key_t;
/* The Pth time object. */
typedef struct timeval pth_time_t;
/* Function prototypes. */
int pth_init (void);
int pth_kill (void);
long pth_ctrl (unsigned long query, ...);
int pth_read_ev (int fd, void *buffer, size_t size, pth_event_t ev);
int pth_read (int fd, void *buffer, size_t size);
int pth_write_ev (int fd, const void *buffer, size_t size, pth_event_t ev);
int pth_write (int fd, const void *buffer, size_t size);
int pth_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds,
const struct timeval *timeout);
int pth_select_ev (int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds,
const struct timeval * timeout, pth_event_t ev_extra);
int pth_accept (int fd, struct sockaddr *addr, int *addrlen);
int pth_accept_ev (int fd, struct sockaddr *addr, int *addrlen,
pth_event_t hd);
int pth_connect (int fd, struct sockaddr *name, int namelen);
int pth_mutex_release (pth_mutex_t *hd);
int pth_mutex_acquire(pth_mutex_t *hd, int try_only, pth_event_t ev_extra);
int pth_mutex_init (pth_mutex_t *hd);
/* We need this under windows, otherwise we would leak handles. */
int pth_mutex_destroy (pth_mutex_t *hd);
int pth_rwlock_init (pth_rwlock_t *rwlock);
int pth_rwlock_acquire (pth_rwlock_t *rwlock, int op, int try, pth_event_t ev);
int pth_rwlock_release (pth_rwlock_t *rwlock);
pth_attr_t pth_attr_new (void);
int pth_attr_destroy (pth_attr_t hd);
int pth_attr_set (pth_attr_t hd, int field, ...);
pth_t pth_spawn (pth_attr_t hd, void *(*func)(void *), void *arg);
pth_t pth_self (void);
int pth_join (pth_t hd, void **value);
int pth_abort (pth_t hd);
void pth_exit (void *value);
unsigned int pth_waitpid (unsigned int, int *status, int options);
int pth_wait (pth_event_t hd);
int pth_sleep (int n);
int pth_usleep (unsigned int usec);
pth_time_t pth_timeout (long sec, long usec);
pth_event_t pth_event_isolate (pth_event_t hd);
int pth_event_free (pth_event_t hd, int mode);
int pth_event_status (pth_event_t hd);
int pth_event_occurred (pth_event_t hd);
pth_event_t pth_event_concat (pth_event_t ev, ...);
pth_event_t pth_event (unsigned long spec, ...);
int pth_sigmask (int how, const sigset_t *set, sigset_t *old);
int pth_yield (pth_t tid);
void pth_enter (void);
void pth_leave (void);
/* Special W32 function to cope with the problem that pth_self returns
just a pseudo handle which is not very useful for debugging. */
unsigned long pth_thread_id (void);
#define PTH_HAVE_PTH_THREAD_ID 1
/*-- pth_util.c --*/
/* void sigemptyset (struct sigset_s * ss); */
/* int sigaddset (struct sigset_s * ss, int signo); */
#endif /*_W32_PTH_H*/