1 /* w32-pth.h - GNU Pth emulation for W32 (MS Windows).
2 * Copyright (c) 1999-2003 Ralf S. Engelschall <rse@engelschall.com>
3 * Copyright (C) 2004, 2006 g10 Code GmbH
5 * This file is part of JNLIB.
7 * JNLIB is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
12 * JNLIB is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * ------------------------------------------------------------------
23 * This code is based on Ralf Engelschall's GNU Pth, a non-preemptive
24 * thread scheduling library which can be found at
25 * http://www.gnu.org/software/pth/.
28 /* Note that this header is usually used through a symlinked pth.h
29 file. This is needed so that we don't have a pth.h file here which
30 would conflict if a system really has pth available. */
34 #include <windows.h> /* We need this for sockaddr et al. FIXME: too
35 heavyweight - may be we should factor such
36 code out to a second header and adjust all
37 user files to include it only if required. */
39 #ifndef W32_PTH_HANDLE_INTERNAL
40 #define W32_PTH_HANDLE_INTERNAL int
44 /* Filedescriptor blocking modes. */
47 PTH_FDMODE_ERROR
= -1,
55 #define PTH_MUTEX_INITIALIZED (1<<0)
56 #define PTH_MUTEX_LOCKED (1<<1)
58 /* Note: We can't do static initialization, thus we don't define the
59 initializer PTH_MUTEX_INIT. */
62 #define PTH_KEY_INIT (1<<0)
65 /* Event subject classes. */
66 #define PTH_EVENT_FD (1<<1)
67 #define PTH_EVENT_SELECT (1<<2)
68 #define PTH_EVENT_SIGS (1<<3)
69 #define PTH_EVENT_TIME (1<<4)
70 #define PTH_EVENT_MSG (1<<5)
71 #define PTH_EVENT_MUTEX (1<<6)
72 #define PTH_EVENT_COND (1<<7)
73 #define PTH_EVENT_TID (1<<8)
74 #define PTH_EVENT_FUNC (1<<9)
78 /* Event occurrence restrictions. */
79 #define PTH_UNTIL_OCCURRED (1<<11)
80 #define PTH_UNTIL_FD_READABLE (1<<12)
81 #define PTH_UNTIL_FD_WRITEABLE (1<<13)
82 #define PTH_UNTIL_FD_EXCEPTION (1<<14)
83 #define PTH_UNTIL_TID_NEW (1<<15)
84 #define PTH_UNTIL_TID_READY (1<<16)
85 #define PTH_UNTIL_TID_WAITING (1<<17)
86 #define PTH_UNTIL_TID_DEAD (1<<18)
89 /* Event structure handling modes. */
90 #define PTH_MODE_REUSE (1<<20)
91 #define PTH_MODE_CHAIN (1<<21)
92 #define PTH_MODE_STATIC (1<<22)
95 /* Attribute commands for pth_attr_get and pth_attr_set(). */
98 PTH_ATTR_PRIO
, /* RW [int] Priority of thread. */
99 PTH_ATTR_NAME
, /* RW [char *] Name of thread. */
100 PTH_ATTR_JOINABLE
, /* RW [int] Thread detachment type. */
101 PTH_ATTR_CANCEL_STATE
, /* RW [unsigned int] Thread cancellation state.*/
102 PTH_ATTR_STACK_SIZE
, /* RW [unsigned int] Stack size. */
103 PTH_ATTR_STACK_ADDR
, /* RW [char *] Stack lower address. */
104 PTH_ATTR_DISPATCHES
, /* RO [int] Total number of
105 thread dispatches. */
106 PTH_ATTR_TIME_SPAWN
, /* RO [pth_time_t] Time thread was spawned. */
107 PTH_ATTR_TIME_LAST
, /* RO [pth_time_t] Time thread was
109 PTH_ATTR_TIME_RAN
, /* RO [pth_time_t] Time thread was running. */
110 PTH_ATTR_START_FUNC
, /* RO [void *(*)(void *)] Thread start function.*/
111 PTH_ATTR_START_ARG
, /* RO [void *] Thread start argument. */
112 PTH_ATTR_STATE
, /* RO [pth_state_t] Scheduling state. */
113 PTH_ATTR_EVENTS
, /* RO [pth_event_t] Events the thread
115 PTH_ATTR_BOUND
/* RO [int] Whether object is
121 /* Queries for pth_ctrl(). */
122 #define PTH_CTRL_GETAVLOAD (1<<1)
123 #define PTH_CTRL_GETPRIO (1<<2)
124 #define PTH_CTRL_GETNAME (1<<3)
125 #define PTH_CTRL_GETTHREADS_NEW (1<<4)
126 #define PTH_CTRL_GETTHREADS_READY (1<<5)
127 #define PTH_CTRL_GETTHREADS_RUNNING (1<<6)
128 #define PTH_CTRL_GETTHREADS_WAITING (1<<7)
129 #define PTH_CTRL_GETTHREADS_SUSPENDED (1<<8)
130 #define PTH_CTRL_GETTHREADS_DEAD (1<<9)
131 #define PTH_CTRL_DUMPSTATE (1<<10)
133 #define PTH_CTRL_GETTHREADS ( PTH_CTRL_GETTHREADS_NEW \
134 | PTH_CTRL_GETTHREADS_READY \
135 | PTH_CTRL_GETTHREADS_RUNNING \
136 | PTH_CTRL_GETTHREADS_WAITING \
137 | PTH_CTRL_GETTHREADS_SUSPENDED \
138 | PTH_CTRL_GETTHREADS_DEAD )
141 /* Event status codes. */
151 /* Event deallocation types. */
159 /* The Pth thread handle object. */
163 /* The Mutex object. */
164 typedef W32_PTH_HANDLE_INTERNAL pth_mutex_t
;
167 /* The Event object. */
169 typedef struct pth_event_s
*pth_event_t
;
172 /* The Attribute object. */
174 typedef struct pth_attr_s
*pth_attr_t
;
177 /* The Key object. */
178 typedef int pth_key_t
;
181 /* The Pth time object. */
182 typedef struct timeval pth_time_t
;
185 /* Function prototypes. */
188 long pth_ctrl (unsigned long query
, ...);
190 int pth_read_ev (int fd
, void *buffer
, size_t size
, pth_event_t ev
);
191 int pth_read (int fd
, void *buffer
, size_t size
);
192 int pth_write_ev (int fd
, const void *buffer
, size_t size
, pth_event_t ev
);
193 int pth_write (int fd
, const void *buffer
, size_t size
);
195 int pth_select (int nfds
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*efds
,
196 const struct timeval
*timeout
);
198 int pth_accept (int fd
, struct sockaddr
*addr
, int *addrlen
);
199 int pth_accept_ev (int fd
, struct sockaddr
*addr
, int *addrlen
,
202 int pth_connect (int fd
, struct sockaddr
*name
, int namelen
);
205 int pth_mutex_release (pth_mutex_t
*hd
);
206 int pth_mutex_acquire(pth_mutex_t
*hd
, int try_only
, pth_event_t ev_extra
);
207 int pth_mutex_init (pth_mutex_t
*hd
);
210 pth_attr_t
pth_attr_new (void);
211 int pth_attr_destroy (pth_attr_t hd
);
212 int pth_attr_set (pth_attr_t hd
, int field
, ...);
214 pth_t
pth_spawn (pth_attr_t hd
, void *(*func
)(void *), void *arg
);
215 pth_t
pth_self (void);
216 int pth_join (pth_t hd
, void **value
);
217 int pth_abort (pth_t hd
);
218 void pth_exit (void *value
);
220 unsigned int pth_waitpid (unsigned int, int *status
, int options
);
221 int pth_wait (pth_event_t hd
);
223 int pth_sleep (int n
);
224 pth_time_t
pth_timeout (long sec
, long usec
);
228 pth_event_t
pth_event_isolate (pth_event_t hd
);
229 int pth_event_free (pth_event_t hd
, int mode
);
230 int pth_event_status (pth_event_t hd
);
231 int pth_event_occurred (pth_event_t hd
);
232 pth_event_t
pth_event_concat (pth_event_t ev
, ...);
233 pth_event_t
pth_event (unsigned long spec
, ...);
239 /* void sigemptyset (struct sigset_s * ss); */
241 /* int sigaddset (struct sigset_s * ss, int signo); */