xref: /OK3568_Linux_fs/external/mpp/osal/windows/pthread/inc/pthread.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* This is an implementation of the threads API of POSIX 1003.1-2001.
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * --------------------------------------------------------------------------
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *      Pthreads-win32 - POSIX Threads Library for Win32
6*4882a593Smuzhiyun  *      Copyright(C) 1998 John E. Bossom
7*4882a593Smuzhiyun  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *      Contact Email: rpj@callisto.canberra.edu.au
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *      The current list of contributors is contained
12*4882a593Smuzhiyun  *      in the file CONTRIBUTORS included with the source
13*4882a593Smuzhiyun  *      code distribution. The list can also be seen at the
14*4882a593Smuzhiyun  *      following World Wide Web location:
15*4882a593Smuzhiyun  *      http://sources.redhat.com/pthreads-win32/contributors.html
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  *      This library is free software; you can redistribute it and/or
18*4882a593Smuzhiyun  *      modify it under the terms of the GNU Lesser General Public
19*4882a593Smuzhiyun  *      License as published by the Free Software Foundation; either
20*4882a593Smuzhiyun  *      version 2 of the License, or (at your option) any later version.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  *      This library is distributed in the hope that it will be useful,
23*4882a593Smuzhiyun  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
24*4882a593Smuzhiyun  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25*4882a593Smuzhiyun  *      Lesser General Public License for more details.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  *      You should have received a copy of the GNU Lesser General Public
28*4882a593Smuzhiyun  *      License along with this library in the file COPYING.LIB;
29*4882a593Smuzhiyun  *      if not, write to the Free Software Foundation, Inc.,
30*4882a593Smuzhiyun  *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #if !defined( PTHREAD_H )
34*4882a593Smuzhiyun #define PTHREAD_H
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /*
37*4882a593Smuzhiyun  * See the README file for an explanation of the pthreads-win32 version
38*4882a593Smuzhiyun  * numbering scheme and how the DLL is named etc.
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun #define PTW32_VERSION 2,9,1,0
41*4882a593Smuzhiyun #define PTW32_VERSION_STRING "2, 9, 1, 0\0"
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /* There are three implementations of cancel cleanup.
44*4882a593Smuzhiyun  * Note that pthread.h is included in both application
45*4882a593Smuzhiyun  * compilation units and also internally for the library.
46*4882a593Smuzhiyun  * The code here and within the library aims to work
47*4882a593Smuzhiyun  * for all reasonable combinations of environments.
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * The three implementations are:
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  *   WIN32 SEH
52*4882a593Smuzhiyun  *   C
53*4882a593Smuzhiyun  *   C++
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * Please note that exiting a push/pop block via
56*4882a593Smuzhiyun  * "return", "exit", "break", or "continue" will
57*4882a593Smuzhiyun  * lead to different behaviour amongst applications
58*4882a593Smuzhiyun  * depending upon whether the library was built
59*4882a593Smuzhiyun  * using SEH, C++, or C. For example, a library built
60*4882a593Smuzhiyun  * with SEH will call the cleanup routine, while both
61*4882a593Smuzhiyun  * C++ and C built versions will not.
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun  * Define defaults for cleanup code.
66*4882a593Smuzhiyun  * Note: Unless the build explicitly defines one of the following, then
67*4882a593Smuzhiyun  * we default to standard C style cleanup. This style uses setjmp/longjmp
68*4882a593Smuzhiyun  * in the cancelation and thread exit implementations and therefore won't
69*4882a593Smuzhiyun  * do stack unwinding if linked to applications that have it (e.g.
70*4882a593Smuzhiyun  * C++ apps). This is currently consistent with most/all commercial Unix
71*4882a593Smuzhiyun  * POSIX threads implementations.
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun #if !defined( __CLEANUP_SEH ) && !defined( __CLEANUP_CXX ) && !defined( __CLEANUP_C )
74*4882a593Smuzhiyun # define __CLEANUP_C
75*4882a593Smuzhiyun #endif
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #if defined( __CLEANUP_SEH ) && ( !defined( _MSC_VER ) && !defined(PTW32_RC_MSC))
78*4882a593Smuzhiyun #error ERROR [__FILE__, line __LINE__]: SEH is not supported for this compiler.
79*4882a593Smuzhiyun #endif
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun  * Stop here if we are being included by the resource compiler.
83*4882a593Smuzhiyun  */
84*4882a593Smuzhiyun #if !defined(RC_INVOKED)
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun #undef PTW32_LEVEL
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun #if defined(_POSIX_SOURCE)
89*4882a593Smuzhiyun #define PTW32_LEVEL 0
90*4882a593Smuzhiyun /* Early POSIX */
91*4882a593Smuzhiyun #endif
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
94*4882a593Smuzhiyun #undef PTW32_LEVEL
95*4882a593Smuzhiyun #define PTW32_LEVEL 1
96*4882a593Smuzhiyun /* Include 1b, 1c and 1d */
97*4882a593Smuzhiyun #endif
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun #if defined(INCLUDE_NP)
100*4882a593Smuzhiyun #undef PTW32_LEVEL
101*4882a593Smuzhiyun #define PTW32_LEVEL 2
102*4882a593Smuzhiyun /* Include Non-Portable extensions */
103*4882a593Smuzhiyun #endif
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun #define PTW32_LEVEL_MAX 3
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun #if ( defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112 )  || !defined(PTW32_LEVEL)
108*4882a593Smuzhiyun #define PTW32_LEVEL PTW32_LEVEL_MAX
109*4882a593Smuzhiyun /* Include everything */
110*4882a593Smuzhiyun #endif
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun #if defined(_UWIN)
113*4882a593Smuzhiyun #   define HAVE_STRUCT_TIMESPEC 1
114*4882a593Smuzhiyun #   define HAVE_SIGNAL_H        1
115*4882a593Smuzhiyun #   undef HAVE_PTW32_CONFIG_H
116*4882a593Smuzhiyun #   pragma comment(lib, "pthread")
117*4882a593Smuzhiyun #endif
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun  * -------------------------------------------------------------
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  * Module: pthread.h
124*4882a593Smuzhiyun  *
125*4882a593Smuzhiyun  * Purpose:
126*4882a593Smuzhiyun  *      Provides an implementation of PThreads based upon the
127*4882a593Smuzhiyun  *      standard:
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  *              POSIX 1003.1-2001
130*4882a593Smuzhiyun  *  and
131*4882a593Smuzhiyun  *    The Single Unix Specification version 3
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  *    (these two are equivalent)
134*4882a593Smuzhiyun  *
135*4882a593Smuzhiyun  *      in order to enhance code portability between Windows,
136*4882a593Smuzhiyun  *  various commercial Unix implementations, and Linux.
137*4882a593Smuzhiyun  *
138*4882a593Smuzhiyun  *      See the ANNOUNCE file for a full list of conforming
139*4882a593Smuzhiyun  *      routines and defined constants, and a list of missing
140*4882a593Smuzhiyun  *      routines and constants not defined in this implementation.
141*4882a593Smuzhiyun  *
142*4882a593Smuzhiyun  * Authors:
143*4882a593Smuzhiyun  *      There have been many contributors to this library.
144*4882a593Smuzhiyun  *      The initial implementation was contributed by
145*4882a593Smuzhiyun  *      John Bossom, and several others have provided major
146*4882a593Smuzhiyun  *      sections or revisions of parts of the implementation.
147*4882a593Smuzhiyun  *      Often significant effort has been contributed to
148*4882a593Smuzhiyun  *      find and fix important bugs and other problems to
149*4882a593Smuzhiyun  *      improve the reliability of the library, which sometimes
150*4882a593Smuzhiyun  *      is not reflected in the amount of code which changed as
151*4882a593Smuzhiyun  *      result.
152*4882a593Smuzhiyun  *      As much as possible, the contributors are acknowledged
153*4882a593Smuzhiyun  *      in the ChangeLog file in the source code distribution
154*4882a593Smuzhiyun  *      where their changes are noted in detail.
155*4882a593Smuzhiyun  *
156*4882a593Smuzhiyun  *      Contributors are listed in the CONTRIBUTORS file.
157*4882a593Smuzhiyun  *
158*4882a593Smuzhiyun  *      As usual, all bouquets go to the contributors, and all
159*4882a593Smuzhiyun  *      brickbats go to the project maintainer.
160*4882a593Smuzhiyun  *
161*4882a593Smuzhiyun  * Maintainer:
162*4882a593Smuzhiyun  *      The code base for this project is coordinated and
163*4882a593Smuzhiyun  *      eventually pre-tested, packaged, and made available by
164*4882a593Smuzhiyun  *
165*4882a593Smuzhiyun  *              Ross Johnson <rpj@callisto.canberra.edu.au>
166*4882a593Smuzhiyun  *
167*4882a593Smuzhiyun  * QA Testers:
168*4882a593Smuzhiyun  *      Ultimately, the library is tested in the real world by
169*4882a593Smuzhiyun  *      a host of competent and demanding scientists and
170*4882a593Smuzhiyun  *      engineers who report bugs and/or provide solutions
171*4882a593Smuzhiyun  *      which are then fixed or incorporated into subsequent
172*4882a593Smuzhiyun  *      versions of the library. Each time a bug is fixed, a
173*4882a593Smuzhiyun  *      test case is written to prove the fix and ensure
174*4882a593Smuzhiyun  *      that later changes to the code don't reintroduce the
175*4882a593Smuzhiyun  *      same error. The number of test cases is slowly growing
176*4882a593Smuzhiyun  *      and therefore so is the code reliability.
177*4882a593Smuzhiyun  *
178*4882a593Smuzhiyun  * Compliance:
179*4882a593Smuzhiyun  *      See the file ANNOUNCE for the list of implemented
180*4882a593Smuzhiyun  *      and not-implemented routines and defined options.
181*4882a593Smuzhiyun  *      Of course, these are all defined is this file as well.
182*4882a593Smuzhiyun  *
183*4882a593Smuzhiyun  * Web site:
184*4882a593Smuzhiyun  *      The source code and other information about this library
185*4882a593Smuzhiyun  *      are available from
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  *              http://sources.redhat.com/pthreads-win32/
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * -------------------------------------------------------------
190*4882a593Smuzhiyun  */
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /* Try to avoid including windows.h */
193*4882a593Smuzhiyun #if (defined(__MINGW64__) || defined(__MINGW32__)) && defined(__cplusplus)
194*4882a593Smuzhiyun #define PTW32_INCLUDE_WINDOWS_H
195*4882a593Smuzhiyun #endif
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun #if defined(PTW32_INCLUDE_WINDOWS_H)
198*4882a593Smuzhiyun #include <windows.h>
199*4882a593Smuzhiyun #endif
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun #if defined(_MSC_VER) && _MSC_VER < 1300 || defined(__DMC__)
202*4882a593Smuzhiyun /*
203*4882a593Smuzhiyun  * VC++6.0 or early compiler's header has no DWORD_PTR type.
204*4882a593Smuzhiyun  */
205*4882a593Smuzhiyun typedef unsigned long DWORD_PTR;
206*4882a593Smuzhiyun typedef unsigned long ULONG_PTR;
207*4882a593Smuzhiyun #endif
208*4882a593Smuzhiyun /*
209*4882a593Smuzhiyun  * -----------------
210*4882a593Smuzhiyun  * autoconf switches
211*4882a593Smuzhiyun  * -----------------
212*4882a593Smuzhiyun  */
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun #if defined(HAVE_PTW32_CONFIG_H)
215*4882a593Smuzhiyun #include "config.h"
216*4882a593Smuzhiyun #endif /* HAVE_PTW32_CONFIG_H */
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun #if !defined(NEED_FTIME)
219*4882a593Smuzhiyun #include <time.h>
220*4882a593Smuzhiyun #else /* NEED_FTIME */
221*4882a593Smuzhiyun /* use native WIN32 time API */
222*4882a593Smuzhiyun #endif /* NEED_FTIME */
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun #if defined(HAVE_SIGNAL_H)
225*4882a593Smuzhiyun #include <signal.h>
226*4882a593Smuzhiyun #endif /* HAVE_SIGNAL_H */
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun #include <limits.h>
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun /*
231*4882a593Smuzhiyun  * Boolean values to make us independent of system includes.
232*4882a593Smuzhiyun  */
233*4882a593Smuzhiyun enum {
234*4882a593Smuzhiyun   PTW32_FALSE = 0,
235*4882a593Smuzhiyun   PTW32_TRUE = (! PTW32_FALSE)
236*4882a593Smuzhiyun };
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun /*
239*4882a593Smuzhiyun  * This is a duplicate of what is in the autoconf config.h,
240*4882a593Smuzhiyun  * which is only used when building the pthread-win32 libraries.
241*4882a593Smuzhiyun  */
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun #if !defined(PTW32_CONFIG_H)
244*4882a593Smuzhiyun #  if defined(WINCE)
245*4882a593Smuzhiyun #    define NEED_ERRNO
246*4882a593Smuzhiyun #    define NEED_SEM
247*4882a593Smuzhiyun #  endif
248*4882a593Smuzhiyun #  if defined(__MINGW64__)
249*4882a593Smuzhiyun #    define HAVE_STRUCT_TIMESPEC
250*4882a593Smuzhiyun #    define HAVE_MODE_T
251*4882a593Smuzhiyun #  elif defined(_UWIN) || defined(__MINGW32__)
252*4882a593Smuzhiyun #    define HAVE_MODE_T
253*4882a593Smuzhiyun #  endif
254*4882a593Smuzhiyun #endif
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /*
257*4882a593Smuzhiyun  *
258*4882a593Smuzhiyun  */
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun #if PTW32_LEVEL >= PTW32_LEVEL_MAX
261*4882a593Smuzhiyun #if defined(NEED_ERRNO)
262*4882a593Smuzhiyun #include "need_errno.h"
263*4882a593Smuzhiyun #else
264*4882a593Smuzhiyun #include <errno.h>
265*4882a593Smuzhiyun #endif
266*4882a593Smuzhiyun #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun /*
269*4882a593Smuzhiyun  * Several systems don't define some error numbers.
270*4882a593Smuzhiyun  */
271*4882a593Smuzhiyun #if !defined(ENOTSUP)
272*4882a593Smuzhiyun #  define ENOTSUP 48   /* This is the value in Solaris. */
273*4882a593Smuzhiyun #endif
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun #if !defined(ETIMEDOUT)
276*4882a593Smuzhiyun #  define ETIMEDOUT 10060 /* Same as WSAETIMEDOUT */
277*4882a593Smuzhiyun #endif
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun #if !defined(ENOSYS)
280*4882a593Smuzhiyun #  define ENOSYS 140     /* Semi-arbitrary value */
281*4882a593Smuzhiyun #endif
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun #if !defined(EDEADLK)
284*4882a593Smuzhiyun #  if defined(EDEADLOCK)
285*4882a593Smuzhiyun #    define EDEADLK EDEADLOCK
286*4882a593Smuzhiyun #  else
287*4882a593Smuzhiyun #    define EDEADLK 36     /* This is the value in MSVC. */
288*4882a593Smuzhiyun #  endif
289*4882a593Smuzhiyun #endif
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun /* POSIX 2008 - related to robust mutexes */
292*4882a593Smuzhiyun #if !defined(EOWNERDEAD)
293*4882a593Smuzhiyun #  define EOWNERDEAD 43
294*4882a593Smuzhiyun #endif
295*4882a593Smuzhiyun #if !defined(ENOTRECOVERABLE)
296*4882a593Smuzhiyun #  define ENOTRECOVERABLE 44
297*4882a593Smuzhiyun #endif
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun #include <sched.h>
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun /*
302*4882a593Smuzhiyun  * To avoid including windows.h we define only those things that we
303*4882a593Smuzhiyun  * actually need from it.
304*4882a593Smuzhiyun  */
305*4882a593Smuzhiyun #if !defined(PTW32_INCLUDE_WINDOWS_H)
306*4882a593Smuzhiyun #if !defined(HANDLE)
307*4882a593Smuzhiyun # define PTW32__HANDLE_DEF
308*4882a593Smuzhiyun # define HANDLE void *
309*4882a593Smuzhiyun #endif
310*4882a593Smuzhiyun #if !defined(DWORD)
311*4882a593Smuzhiyun # define PTW32__DWORD_DEF
312*4882a593Smuzhiyun # define DWORD unsigned long
313*4882a593Smuzhiyun #endif
314*4882a593Smuzhiyun #endif
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun #if !defined(HAVE_STRUCT_TIMESPEC)
317*4882a593Smuzhiyun #define HAVE_STRUCT_TIMESPEC
318*4882a593Smuzhiyun #if !defined(_TIMESPEC_DEFINED)
319*4882a593Smuzhiyun #define _TIMESPEC_DEFINED
320*4882a593Smuzhiyun struct timespec {
321*4882a593Smuzhiyun         time_t tv_sec;
322*4882a593Smuzhiyun         long tv_nsec;
323*4882a593Smuzhiyun };
324*4882a593Smuzhiyun #endif /* _TIMESPEC_DEFINED */
325*4882a593Smuzhiyun #endif /* HAVE_STRUCT_TIMESPEC */
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun #if !defined(SIG_BLOCK)
328*4882a593Smuzhiyun #define SIG_BLOCK 0
329*4882a593Smuzhiyun #endif /* SIG_BLOCK */
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun #if !defined(SIG_UNBLOCK)
332*4882a593Smuzhiyun #define SIG_UNBLOCK 1
333*4882a593Smuzhiyun #endif /* SIG_UNBLOCK */
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun #if !defined(SIG_SETMASK)
336*4882a593Smuzhiyun #define SIG_SETMASK 2
337*4882a593Smuzhiyun #endif /* SIG_SETMASK */
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun #if defined(__cplusplus)
340*4882a593Smuzhiyun extern "C"
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun #endif                          /* __cplusplus */
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun /*
345*4882a593Smuzhiyun  * -------------------------------------------------------------
346*4882a593Smuzhiyun  *
347*4882a593Smuzhiyun  * POSIX 1003.1-2001 Options
348*4882a593Smuzhiyun  * =========================
349*4882a593Smuzhiyun  *
350*4882a593Smuzhiyun  * Options are normally set in <unistd.h>, which is not provided
351*4882a593Smuzhiyun  * with pthreads-win32.
352*4882a593Smuzhiyun  *
353*4882a593Smuzhiyun  * For conformance with the Single Unix Specification (version 3), all of the
354*4882a593Smuzhiyun  * options below are defined, and have a value of either -1 (not supported)
355*4882a593Smuzhiyun  * or 200112L (supported).
356*4882a593Smuzhiyun  *
357*4882a593Smuzhiyun  * These options can neither be left undefined nor have a value of 0, because
358*4882a593Smuzhiyun  * either indicates that sysconf(), which is not implemented, may be used at
359*4882a593Smuzhiyun  * runtime to check the status of the option.
360*4882a593Smuzhiyun  *
361*4882a593Smuzhiyun  * _POSIX_THREADS (== 200112L)
362*4882a593Smuzhiyun  *                      If == 200112L, you can use threads
363*4882a593Smuzhiyun  *
364*4882a593Smuzhiyun  * _POSIX_THREAD_ATTR_STACKSIZE (== 200112L)
365*4882a593Smuzhiyun  *                      If == 200112L, you can control the size of a thread's
366*4882a593Smuzhiyun  *                      stack
367*4882a593Smuzhiyun  *                              pthread_attr_getstacksize
368*4882a593Smuzhiyun  *                              pthread_attr_setstacksize
369*4882a593Smuzhiyun  *
370*4882a593Smuzhiyun  * _POSIX_THREAD_ATTR_STACKADDR (== -1)
371*4882a593Smuzhiyun  *                      If == 200112L, you can allocate and control a thread's
372*4882a593Smuzhiyun  *                      stack. If not supported, the following functions
373*4882a593Smuzhiyun  *                      will return ENOSYS, indicating they are not
374*4882a593Smuzhiyun  *                      supported:
375*4882a593Smuzhiyun  *                              pthread_attr_getstackaddr
376*4882a593Smuzhiyun  *                              pthread_attr_setstackaddr
377*4882a593Smuzhiyun  *
378*4882a593Smuzhiyun  * _POSIX_THREAD_PRIORITY_SCHEDULING (== -1)
379*4882a593Smuzhiyun  *                      If == 200112L, you can use realtime scheduling.
380*4882a593Smuzhiyun  *                      This option indicates that the behaviour of some
381*4882a593Smuzhiyun  *                      implemented functions conforms to the additional TPS
382*4882a593Smuzhiyun  *                      requirements in the standard. E.g. rwlocks favour
383*4882a593Smuzhiyun  *                      writers over readers when threads have equal priority.
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * _POSIX_THREAD_PRIO_INHERIT (== -1)
386*4882a593Smuzhiyun  *                      If == 200112L, you can create priority inheritance
387*4882a593Smuzhiyun  *                      mutexes.
388*4882a593Smuzhiyun  *                              pthread_mutexattr_getprotocol +
389*4882a593Smuzhiyun  *                              pthread_mutexattr_setprotocol +
390*4882a593Smuzhiyun  *
391*4882a593Smuzhiyun  * _POSIX_THREAD_PRIO_PROTECT (== -1)
392*4882a593Smuzhiyun  *                      If == 200112L, you can create priority ceiling mutexes
393*4882a593Smuzhiyun  *                      Indicates the availability of:
394*4882a593Smuzhiyun  *                              pthread_mutex_getprioceiling
395*4882a593Smuzhiyun  *                              pthread_mutex_setprioceiling
396*4882a593Smuzhiyun  *                              pthread_mutexattr_getprioceiling
397*4882a593Smuzhiyun  *                              pthread_mutexattr_getprotocol     +
398*4882a593Smuzhiyun  *                              pthread_mutexattr_setprioceiling
399*4882a593Smuzhiyun  *                              pthread_mutexattr_setprotocol     +
400*4882a593Smuzhiyun  *
401*4882a593Smuzhiyun  * _POSIX_THREAD_PROCESS_SHARED (== -1)
402*4882a593Smuzhiyun  *                      If set, you can create mutexes and condition
403*4882a593Smuzhiyun  *                      variables that can be shared with another
404*4882a593Smuzhiyun  *                      process.If set, indicates the availability
405*4882a593Smuzhiyun  *                      of:
406*4882a593Smuzhiyun  *                              pthread_mutexattr_getpshared
407*4882a593Smuzhiyun  *                              pthread_mutexattr_setpshared
408*4882a593Smuzhiyun  *                              pthread_condattr_getpshared
409*4882a593Smuzhiyun  *                              pthread_condattr_setpshared
410*4882a593Smuzhiyun  *
411*4882a593Smuzhiyun  * _POSIX_THREAD_SAFE_FUNCTIONS (== 200112L)
412*4882a593Smuzhiyun  *                      If == 200112L you can use the special *_r library
413*4882a593Smuzhiyun  *                      functions that provide thread-safe behaviour
414*4882a593Smuzhiyun  *
415*4882a593Smuzhiyun  * _POSIX_READER_WRITER_LOCKS (== 200112L)
416*4882a593Smuzhiyun  *                      If == 200112L, you can use read/write locks
417*4882a593Smuzhiyun  *
418*4882a593Smuzhiyun  * _POSIX_SPIN_LOCKS (== 200112L)
419*4882a593Smuzhiyun  *                      If == 200112L, you can use spin locks
420*4882a593Smuzhiyun  *
421*4882a593Smuzhiyun  * _POSIX_BARRIERS (== 200112L)
422*4882a593Smuzhiyun  *                      If == 200112L, you can use barriers
423*4882a593Smuzhiyun  *
424*4882a593Smuzhiyun  *      + These functions provide both 'inherit' and/or
425*4882a593Smuzhiyun  *        'protect' protocol, based upon these macro
426*4882a593Smuzhiyun  *        settings.
427*4882a593Smuzhiyun  *
428*4882a593Smuzhiyun  * -------------------------------------------------------------
429*4882a593Smuzhiyun  */
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun /*
432*4882a593Smuzhiyun  * POSIX Options
433*4882a593Smuzhiyun  */
434*4882a593Smuzhiyun #undef _POSIX_THREADS
435*4882a593Smuzhiyun #define _POSIX_THREADS 200809L
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun #undef _POSIX_READER_WRITER_LOCKS
438*4882a593Smuzhiyun #define _POSIX_READER_WRITER_LOCKS 200809L
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun #undef _POSIX_SPIN_LOCKS
441*4882a593Smuzhiyun #define _POSIX_SPIN_LOCKS 200809L
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun #undef _POSIX_BARRIERS
444*4882a593Smuzhiyun #define _POSIX_BARRIERS 200809L
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun #undef _POSIX_THREAD_SAFE_FUNCTIONS
447*4882a593Smuzhiyun #define _POSIX_THREAD_SAFE_FUNCTIONS 200809L
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun #undef _POSIX_THREAD_ATTR_STACKSIZE
450*4882a593Smuzhiyun #define _POSIX_THREAD_ATTR_STACKSIZE 200809L
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun /*
453*4882a593Smuzhiyun  * The following options are not supported
454*4882a593Smuzhiyun  */
455*4882a593Smuzhiyun #undef _POSIX_THREAD_ATTR_STACKADDR
456*4882a593Smuzhiyun #define _POSIX_THREAD_ATTR_STACKADDR -1
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun #undef _POSIX_THREAD_PRIO_INHERIT
459*4882a593Smuzhiyun #define _POSIX_THREAD_PRIO_INHERIT -1
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun #undef _POSIX_THREAD_PRIO_PROTECT
462*4882a593Smuzhiyun #define _POSIX_THREAD_PRIO_PROTECT -1
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /* TPS is not fully supported.  */
465*4882a593Smuzhiyun #undef _POSIX_THREAD_PRIORITY_SCHEDULING
466*4882a593Smuzhiyun #define _POSIX_THREAD_PRIORITY_SCHEDULING -1
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun #undef _POSIX_THREAD_PROCESS_SHARED
469*4882a593Smuzhiyun #define _POSIX_THREAD_PROCESS_SHARED -1
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun /*
473*4882a593Smuzhiyun  * POSIX 1003.1-2001 Limits
474*4882a593Smuzhiyun  * ===========================
475*4882a593Smuzhiyun  *
476*4882a593Smuzhiyun  * These limits are normally set in <limits.h>, which is not provided with
477*4882a593Smuzhiyun  * pthreads-win32.
478*4882a593Smuzhiyun  *
479*4882a593Smuzhiyun  * PTHREAD_DESTRUCTOR_ITERATIONS
480*4882a593Smuzhiyun  *                      Maximum number of attempts to destroy
481*4882a593Smuzhiyun  *                      a thread's thread-specific data on
482*4882a593Smuzhiyun  *                      termination (must be at least 4)
483*4882a593Smuzhiyun  *
484*4882a593Smuzhiyun  * PTHREAD_KEYS_MAX
485*4882a593Smuzhiyun  *                      Maximum number of thread-specific data keys
486*4882a593Smuzhiyun  *                      available per process (must be at least 128)
487*4882a593Smuzhiyun  *
488*4882a593Smuzhiyun  * PTHREAD_STACK_MIN
489*4882a593Smuzhiyun  *                      Minimum supported stack size for a thread
490*4882a593Smuzhiyun  *
491*4882a593Smuzhiyun  * PTHREAD_THREADS_MAX
492*4882a593Smuzhiyun  *                      Maximum number of threads supported per
493*4882a593Smuzhiyun  *                      process (must be at least 64).
494*4882a593Smuzhiyun  *
495*4882a593Smuzhiyun  * SEM_NSEMS_MAX
496*4882a593Smuzhiyun  *                      The maximum number of semaphores a process can have.
497*4882a593Smuzhiyun  *                      (must be at least 256)
498*4882a593Smuzhiyun  *
499*4882a593Smuzhiyun  * SEM_VALUE_MAX
500*4882a593Smuzhiyun  *                      The maximum value a semaphore can have.
501*4882a593Smuzhiyun  *                      (must be at least 32767)
502*4882a593Smuzhiyun  *
503*4882a593Smuzhiyun  */
504*4882a593Smuzhiyun #undef _POSIX_THREAD_DESTRUCTOR_ITERATIONS
505*4882a593Smuzhiyun #define _POSIX_THREAD_DESTRUCTOR_ITERATIONS     4
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun #undef PTHREAD_DESTRUCTOR_ITERATIONS
508*4882a593Smuzhiyun #define PTHREAD_DESTRUCTOR_ITERATIONS           _POSIX_THREAD_DESTRUCTOR_ITERATIONS
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun #undef _POSIX_THREAD_KEYS_MAX
511*4882a593Smuzhiyun #define _POSIX_THREAD_KEYS_MAX                  128
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun #undef PTHREAD_KEYS_MAX
514*4882a593Smuzhiyun #define PTHREAD_KEYS_MAX                        _POSIX_THREAD_KEYS_MAX
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun #undef PTHREAD_STACK_MIN
517*4882a593Smuzhiyun #define PTHREAD_STACK_MIN                       0
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun #undef _POSIX_THREAD_THREADS_MAX
520*4882a593Smuzhiyun #define _POSIX_THREAD_THREADS_MAX               64
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun   /* Arbitrary value */
523*4882a593Smuzhiyun #undef PTHREAD_THREADS_MAX
524*4882a593Smuzhiyun #define PTHREAD_THREADS_MAX                     2019
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun #undef _POSIX_SEM_NSEMS_MAX
527*4882a593Smuzhiyun #define _POSIX_SEM_NSEMS_MAX                    256
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun   /* Arbitrary value */
530*4882a593Smuzhiyun #undef SEM_NSEMS_MAX
531*4882a593Smuzhiyun #define SEM_NSEMS_MAX                           1024
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun #undef _POSIX_SEM_VALUE_MAX
534*4882a593Smuzhiyun #define _POSIX_SEM_VALUE_MAX                    32767
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun #undef SEM_VALUE_MAX
537*4882a593Smuzhiyun #define SEM_VALUE_MAX                           INT_MAX
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun #if defined(__GNUC__) && !defined(__declspec)
541*4882a593Smuzhiyun # error Please upgrade your GNU compiler to one that supports __declspec.
542*4882a593Smuzhiyun #endif
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun /*
545*4882a593Smuzhiyun  * When building the library, you should define PTW32_BUILD so that
546*4882a593Smuzhiyun  * the variables/functions are exported correctly. When using the library,
547*4882a593Smuzhiyun  * do NOT define PTW32_BUILD, and then the variables/functions will
548*4882a593Smuzhiyun  * be imported correctly.
549*4882a593Smuzhiyun  */
550*4882a593Smuzhiyun #if !defined(PTW32_STATIC_LIB)
551*4882a593Smuzhiyun #  if defined(PTW32_BUILD)
552*4882a593Smuzhiyun #    define PTW32_DLLPORT __declspec (dllexport)
553*4882a593Smuzhiyun #  else
554*4882a593Smuzhiyun #    define PTW32_DLLPORT __declspec (dllimport)
555*4882a593Smuzhiyun #  endif
556*4882a593Smuzhiyun #else
557*4882a593Smuzhiyun #  define PTW32_DLLPORT
558*4882a593Smuzhiyun #endif
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun /*
561*4882a593Smuzhiyun  * The Open Watcom C/C++ compiler uses a non-standard calling convention
562*4882a593Smuzhiyun  * that passes function args in registers unless __cdecl is explicitly specified
563*4882a593Smuzhiyun  * in exposed function prototypes.
564*4882a593Smuzhiyun  *
565*4882a593Smuzhiyun  * We force all calls to cdecl even though this could slow Watcom code down
566*4882a593Smuzhiyun  * slightly. If you know that the Watcom compiler will be used to build both
567*4882a593Smuzhiyun  * the DLL and application, then you can probably define this as a null string.
568*4882a593Smuzhiyun  * Remember that pthread.h (this file) is used for both the DLL and application builds.
569*4882a593Smuzhiyun  */
570*4882a593Smuzhiyun #define PTW32_CDECL __cdecl
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun #if defined(_UWIN) && PTW32_LEVEL >= PTW32_LEVEL_MAX
573*4882a593Smuzhiyun #   include     <sys/types.h>
574*4882a593Smuzhiyun #else
575*4882a593Smuzhiyun /*
576*4882a593Smuzhiyun  * Generic handle type - intended to extend uniqueness beyond
577*4882a593Smuzhiyun  * that available with a simple pointer. It should scale for either
578*4882a593Smuzhiyun  * IA-32 or IA-64.
579*4882a593Smuzhiyun  */
580*4882a593Smuzhiyun typedef struct {
581*4882a593Smuzhiyun     void * p;                   /* Pointer to actual object */
582*4882a593Smuzhiyun     unsigned int x;             /* Extra information - reuse count etc */
583*4882a593Smuzhiyun } ptw32_handle_t;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun typedef ptw32_handle_t pthread_t;
586*4882a593Smuzhiyun typedef struct pthread_attr_t_ * pthread_attr_t;
587*4882a593Smuzhiyun typedef struct pthread_once_t_ pthread_once_t;
588*4882a593Smuzhiyun typedef struct pthread_key_t_ * pthread_key_t;
589*4882a593Smuzhiyun typedef struct pthread_mutex_t_ * pthread_mutex_t;
590*4882a593Smuzhiyun typedef struct pthread_mutexattr_t_ * pthread_mutexattr_t;
591*4882a593Smuzhiyun typedef struct pthread_cond_t_ * pthread_cond_t;
592*4882a593Smuzhiyun typedef struct pthread_condattr_t_ * pthread_condattr_t;
593*4882a593Smuzhiyun #endif
594*4882a593Smuzhiyun typedef struct pthread_rwlock_t_ * pthread_rwlock_t;
595*4882a593Smuzhiyun typedef struct pthread_rwlockattr_t_ * pthread_rwlockattr_t;
596*4882a593Smuzhiyun typedef struct pthread_spinlock_t_ * pthread_spinlock_t;
597*4882a593Smuzhiyun typedef struct pthread_barrier_t_ * pthread_barrier_t;
598*4882a593Smuzhiyun typedef struct pthread_barrierattr_t_ * pthread_barrierattr_t;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun /*
601*4882a593Smuzhiyun  * ====================
602*4882a593Smuzhiyun  * ====================
603*4882a593Smuzhiyun  * POSIX Threads
604*4882a593Smuzhiyun  * ====================
605*4882a593Smuzhiyun  * ====================
606*4882a593Smuzhiyun  */
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun enum {
609*4882a593Smuzhiyun /*
610*4882a593Smuzhiyun  * pthread_attr_{get,set}detachstate
611*4882a593Smuzhiyun  */
612*4882a593Smuzhiyun   PTHREAD_CREATE_JOINABLE       = 0,  /* Default */
613*4882a593Smuzhiyun   PTHREAD_CREATE_DETACHED       = 1,
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun /*
616*4882a593Smuzhiyun  * pthread_attr_{get,set}inheritsched
617*4882a593Smuzhiyun  */
618*4882a593Smuzhiyun   PTHREAD_INHERIT_SCHED         = 0,
619*4882a593Smuzhiyun   PTHREAD_EXPLICIT_SCHED        = 1,  /* Default */
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun /*
622*4882a593Smuzhiyun  * pthread_{get,set}scope
623*4882a593Smuzhiyun  */
624*4882a593Smuzhiyun   PTHREAD_SCOPE_PROCESS         = 0,
625*4882a593Smuzhiyun   PTHREAD_SCOPE_SYSTEM          = 1,  /* Default */
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun /*
628*4882a593Smuzhiyun  * pthread_setcancelstate paramters
629*4882a593Smuzhiyun  */
630*4882a593Smuzhiyun   PTHREAD_CANCEL_ENABLE         = 0,  /* Default */
631*4882a593Smuzhiyun   PTHREAD_CANCEL_DISABLE        = 1,
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun /*
634*4882a593Smuzhiyun  * pthread_setcanceltype parameters
635*4882a593Smuzhiyun  */
636*4882a593Smuzhiyun   PTHREAD_CANCEL_ASYNCHRONOUS   = 0,
637*4882a593Smuzhiyun   PTHREAD_CANCEL_DEFERRED       = 1,  /* Default */
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun /*
640*4882a593Smuzhiyun  * pthread_mutexattr_{get,set}pshared
641*4882a593Smuzhiyun  * pthread_condattr_{get,set}pshared
642*4882a593Smuzhiyun  */
643*4882a593Smuzhiyun   PTHREAD_PROCESS_PRIVATE       = 0,
644*4882a593Smuzhiyun   PTHREAD_PROCESS_SHARED        = 1,
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun /*
647*4882a593Smuzhiyun  * pthread_mutexattr_{get,set}robust
648*4882a593Smuzhiyun  */
649*4882a593Smuzhiyun   PTHREAD_MUTEX_STALLED         = 0,  /* Default */
650*4882a593Smuzhiyun   PTHREAD_MUTEX_ROBUST          = 1,
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun /*
653*4882a593Smuzhiyun  * pthread_barrier_wait
654*4882a593Smuzhiyun  */
655*4882a593Smuzhiyun   PTHREAD_BARRIER_SERIAL_THREAD = -1
656*4882a593Smuzhiyun };
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun /*
659*4882a593Smuzhiyun  * ====================
660*4882a593Smuzhiyun  * ====================
661*4882a593Smuzhiyun  * Cancelation
662*4882a593Smuzhiyun  * ====================
663*4882a593Smuzhiyun  * ====================
664*4882a593Smuzhiyun  */
665*4882a593Smuzhiyun #define PTHREAD_CANCELED       ((void *)(size_t) -1)
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun /*
669*4882a593Smuzhiyun  * ====================
670*4882a593Smuzhiyun  * ====================
671*4882a593Smuzhiyun  * Once Key
672*4882a593Smuzhiyun  * ====================
673*4882a593Smuzhiyun  * ====================
674*4882a593Smuzhiyun  */
675*4882a593Smuzhiyun #define PTHREAD_ONCE_INIT       { PTW32_FALSE, 0, 0, 0}
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun struct pthread_once_t_
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun   int          done;        /* indicates if user function has been executed */
680*4882a593Smuzhiyun   void *       lock;
681*4882a593Smuzhiyun   int          reserved1;
682*4882a593Smuzhiyun   int          reserved2;
683*4882a593Smuzhiyun };
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun /*
687*4882a593Smuzhiyun  * ====================
688*4882a593Smuzhiyun  * ====================
689*4882a593Smuzhiyun  * Object initialisers
690*4882a593Smuzhiyun  * ====================
691*4882a593Smuzhiyun  * ====================
692*4882a593Smuzhiyun  */
693*4882a593Smuzhiyun #define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t)(size_t) -1)
694*4882a593Smuzhiyun #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER ((pthread_mutex_t)(size_t) -2)
695*4882a593Smuzhiyun #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER ((pthread_mutex_t)(size_t) -3)
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun /*
698*4882a593Smuzhiyun  * Compatibility with LinuxThreads
699*4882a593Smuzhiyun  */
700*4882a593Smuzhiyun #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER
701*4882a593Smuzhiyun #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP PTHREAD_ERRORCHECK_MUTEX_INITIALIZER
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun #define PTHREAD_COND_INITIALIZER ((pthread_cond_t)(size_t) -1)
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun #define PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t)(size_t) -1)
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun #define PTHREAD_SPINLOCK_INITIALIZER ((pthread_spinlock_t)(size_t) -1)
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun /*
711*4882a593Smuzhiyun  * Mutex types.
712*4882a593Smuzhiyun  */
713*4882a593Smuzhiyun enum
714*4882a593Smuzhiyun {
715*4882a593Smuzhiyun   /* Compatibility with LinuxThreads */
716*4882a593Smuzhiyun   PTHREAD_MUTEX_FAST_NP,
717*4882a593Smuzhiyun   PTHREAD_MUTEX_RECURSIVE_NP,
718*4882a593Smuzhiyun   PTHREAD_MUTEX_ERRORCHECK_NP,
719*4882a593Smuzhiyun   PTHREAD_MUTEX_TIMED_NP = PTHREAD_MUTEX_FAST_NP,
720*4882a593Smuzhiyun   PTHREAD_MUTEX_ADAPTIVE_NP = PTHREAD_MUTEX_FAST_NP,
721*4882a593Smuzhiyun   /* For compatibility with POSIX */
722*4882a593Smuzhiyun   PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
723*4882a593Smuzhiyun   PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
724*4882a593Smuzhiyun   PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
725*4882a593Smuzhiyun   PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
726*4882a593Smuzhiyun };
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun typedef struct ptw32_cleanup_t ptw32_cleanup_t;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun #if defined(_MSC_VER)
732*4882a593Smuzhiyun /* Disable MSVC 'anachronism used' warning */
733*4882a593Smuzhiyun #pragma warning( disable : 4229 )
734*4882a593Smuzhiyun #endif
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun typedef void (* PTW32_CDECL ptw32_cleanup_callback_t)(void *);
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun #if defined(_MSC_VER)
739*4882a593Smuzhiyun #pragma warning( default : 4229 )
740*4882a593Smuzhiyun #endif
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun struct ptw32_cleanup_t
743*4882a593Smuzhiyun {
744*4882a593Smuzhiyun   ptw32_cleanup_callback_t routine;
745*4882a593Smuzhiyun   void *arg;
746*4882a593Smuzhiyun   struct ptw32_cleanup_t *prev;
747*4882a593Smuzhiyun };
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun #if defined(__CLEANUP_SEH)
750*4882a593Smuzhiyun         /*
751*4882a593Smuzhiyun          * WIN32 SEH version of cancel cleanup.
752*4882a593Smuzhiyun          */
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun #define pthread_cleanup_push( _rout, _arg ) \
755*4882a593Smuzhiyun         { \
756*4882a593Smuzhiyun             ptw32_cleanup_t     _cleanup; \
757*4882a593Smuzhiyun             \
758*4882a593Smuzhiyun         _cleanup.routine        = (ptw32_cleanup_callback_t)(_rout); \
759*4882a593Smuzhiyun             _cleanup.arg        = (_arg); \
760*4882a593Smuzhiyun             __try \
761*4882a593Smuzhiyun               { \
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun #define pthread_cleanup_pop( _execute ) \
764*4882a593Smuzhiyun               } \
765*4882a593Smuzhiyun             __finally \
766*4882a593Smuzhiyun                 { \
767*4882a593Smuzhiyun                     if( _execute || AbnormalTermination()) \
768*4882a593Smuzhiyun                       { \
769*4882a593Smuzhiyun                           (*(_cleanup.routine))( _cleanup.arg ); \
770*4882a593Smuzhiyun                       } \
771*4882a593Smuzhiyun                 } \
772*4882a593Smuzhiyun         }
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun #else /* __CLEANUP_SEH */
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun #if defined(__CLEANUP_C)
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun         /*
779*4882a593Smuzhiyun          * C implementation of PThreads cancel cleanup
780*4882a593Smuzhiyun          */
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun #define pthread_cleanup_push( _rout, _arg ) \
783*4882a593Smuzhiyun         { \
784*4882a593Smuzhiyun             ptw32_cleanup_t     _cleanup; \
785*4882a593Smuzhiyun             \
786*4882a593Smuzhiyun             ptw32_push_cleanup( &_cleanup, (ptw32_cleanup_callback_t) (_rout), (_arg) ); \
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun #define pthread_cleanup_pop( _execute ) \
789*4882a593Smuzhiyun             (void) ptw32_pop_cleanup( _execute ); \
790*4882a593Smuzhiyun         }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun #else /* __CLEANUP_C */
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun #if defined(__CLEANUP_CXX)
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun         /*
797*4882a593Smuzhiyun          * C++ version of cancel cleanup.
798*4882a593Smuzhiyun          * - John E. Bossom.
799*4882a593Smuzhiyun          */
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun         class PThreadCleanup {
802*4882a593Smuzhiyun           /*
803*4882a593Smuzhiyun            * PThreadCleanup
804*4882a593Smuzhiyun            *
805*4882a593Smuzhiyun            * Purpose
806*4882a593Smuzhiyun            *      This class is a C++ helper class that is
807*4882a593Smuzhiyun            *      used to implement pthread_cleanup_push/
808*4882a593Smuzhiyun            *      pthread_cleanup_pop.
809*4882a593Smuzhiyun            *      The destructor of this class automatically
810*4882a593Smuzhiyun            *      pops the pushed cleanup routine regardless
811*4882a593Smuzhiyun            *      of how the code exits the scope
812*4882a593Smuzhiyun            *      (i.e. such as by an exception)
813*4882a593Smuzhiyun            */
814*4882a593Smuzhiyun       ptw32_cleanup_callback_t cleanUpRout;
815*4882a593Smuzhiyun           void    *       obj;
816*4882a593Smuzhiyun           int             executeIt;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun         public:
PThreadCleanup()819*4882a593Smuzhiyun           PThreadCleanup() :
820*4882a593Smuzhiyun             cleanUpRout( 0 ),
821*4882a593Smuzhiyun             obj( 0 ),
822*4882a593Smuzhiyun             executeIt( 0 )
823*4882a593Smuzhiyun             /*
824*4882a593Smuzhiyun              * No cleanup performed
825*4882a593Smuzhiyun              */
826*4882a593Smuzhiyun             {
827*4882a593Smuzhiyun             }
828*4882a593Smuzhiyun 
PThreadCleanup(ptw32_cleanup_callback_t routine,void * arg)829*4882a593Smuzhiyun           PThreadCleanup(
830*4882a593Smuzhiyun              ptw32_cleanup_callback_t routine,
831*4882a593Smuzhiyun                          void    *       arg ) :
832*4882a593Smuzhiyun             cleanUpRout( routine ),
833*4882a593Smuzhiyun             obj( arg ),
834*4882a593Smuzhiyun             executeIt( 1 )
835*4882a593Smuzhiyun             /*
836*4882a593Smuzhiyun              * Registers a cleanup routine for 'arg'
837*4882a593Smuzhiyun              */
838*4882a593Smuzhiyun             {
839*4882a593Smuzhiyun             }
840*4882a593Smuzhiyun 
~PThreadCleanup()841*4882a593Smuzhiyun           ~PThreadCleanup()
842*4882a593Smuzhiyun             {
843*4882a593Smuzhiyun               if ( executeIt && ((void *) cleanUpRout != (void *) 0) )
844*4882a593Smuzhiyun                 {
845*4882a593Smuzhiyun                   (void) (*cleanUpRout)( obj );
846*4882a593Smuzhiyun                 }
847*4882a593Smuzhiyun             }
848*4882a593Smuzhiyun 
execute(int exec)849*4882a593Smuzhiyun           void execute( int exec )
850*4882a593Smuzhiyun             {
851*4882a593Smuzhiyun               executeIt = exec;
852*4882a593Smuzhiyun             }
853*4882a593Smuzhiyun         };
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun         /*
856*4882a593Smuzhiyun          * C++ implementation of PThreads cancel cleanup;
857*4882a593Smuzhiyun          * This implementation takes advantage of a helper
858*4882a593Smuzhiyun          * class who's destructor automatically calls the
859*4882a593Smuzhiyun          * cleanup routine if we exit our scope weirdly
860*4882a593Smuzhiyun          */
861*4882a593Smuzhiyun #define pthread_cleanup_push( _rout, _arg ) \
862*4882a593Smuzhiyun         { \
863*4882a593Smuzhiyun             PThreadCleanup  cleanup((ptw32_cleanup_callback_t)(_rout), \
864*4882a593Smuzhiyun                                     (void *) (_arg) );
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun #define pthread_cleanup_pop( _execute ) \
867*4882a593Smuzhiyun             cleanup.execute( _execute ); \
868*4882a593Smuzhiyun         }
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun #else
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun #error ERROR [__FILE__, line __LINE__]: Cleanup type undefined.
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun #endif /* __CLEANUP_CXX */
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun #endif /* __CLEANUP_C */
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun #endif /* __CLEANUP_SEH */
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun /*
881*4882a593Smuzhiyun  * ===============
882*4882a593Smuzhiyun  * ===============
883*4882a593Smuzhiyun  * Methods
884*4882a593Smuzhiyun  * ===============
885*4882a593Smuzhiyun  * ===============
886*4882a593Smuzhiyun  */
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun /*
889*4882a593Smuzhiyun  * PThread Attribute Functions
890*4882a593Smuzhiyun  */
891*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_init (pthread_attr_t * attr);
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_destroy (pthread_attr_t * attr);
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_getdetachstate (const pthread_attr_t * attr,
896*4882a593Smuzhiyun                                          int *detachstate);
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_getstackaddr (const pthread_attr_t * attr,
899*4882a593Smuzhiyun                                        void **stackaddr);
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_getstacksize (const pthread_attr_t * attr,
902*4882a593Smuzhiyun                                        size_t * stacksize);
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_setdetachstate (pthread_attr_t * attr,
905*4882a593Smuzhiyun                                          int detachstate);
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_setstackaddr (pthread_attr_t * attr,
908*4882a593Smuzhiyun                                        void *stackaddr);
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_setstacksize (pthread_attr_t * attr,
911*4882a593Smuzhiyun                                        size_t stacksize);
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_getschedparam (const pthread_attr_t *attr,
914*4882a593Smuzhiyun                                         struct sched_param *param);
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_setschedparam (pthread_attr_t *attr,
917*4882a593Smuzhiyun                                         const struct sched_param *param);
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_setschedpolicy (pthread_attr_t *,
920*4882a593Smuzhiyun                                          int);
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_getschedpolicy (const pthread_attr_t *,
923*4882a593Smuzhiyun                                          int *);
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_setinheritsched(pthread_attr_t * attr,
926*4882a593Smuzhiyun                                          int inheritsched);
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_getinheritsched(const pthread_attr_t * attr,
929*4882a593Smuzhiyun                                          int * inheritsched);
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_setscope (pthread_attr_t *,
932*4882a593Smuzhiyun                                    int);
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_attr_getscope (const pthread_attr_t *,
935*4882a593Smuzhiyun                                    int *);
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun /*
938*4882a593Smuzhiyun  * PThread Functions
939*4882a593Smuzhiyun  */
940*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_create (pthread_t * tid,
941*4882a593Smuzhiyun                             const pthread_attr_t * attr,
942*4882a593Smuzhiyun                             void *(PTW32_CDECL *start) (void *),
943*4882a593Smuzhiyun                             void *arg);
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_detach (pthread_t tid);
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_equal (pthread_t t1,
948*4882a593Smuzhiyun                            pthread_t t2);
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun PTW32_DLLPORT void PTW32_CDECL pthread_exit (void *value_ptr);
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_join (pthread_t thread,
953*4882a593Smuzhiyun                           void **value_ptr);
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun PTW32_DLLPORT pthread_t PTW32_CDECL pthread_self (void);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_cancel (pthread_t thread);
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_setcancelstate (int state,
960*4882a593Smuzhiyun                                     int *oldstate);
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_setcanceltype (int type,
963*4882a593Smuzhiyun                                    int *oldtype);
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun PTW32_DLLPORT void PTW32_CDECL pthread_testcancel (void);
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_once (pthread_once_t * once_control,
968*4882a593Smuzhiyun                           void (PTW32_CDECL *init_routine) (void));
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun #if PTW32_LEVEL >= PTW32_LEVEL_MAX
971*4882a593Smuzhiyun PTW32_DLLPORT ptw32_cleanup_t * PTW32_CDECL ptw32_pop_cleanup (int execute);
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun PTW32_DLLPORT void PTW32_CDECL ptw32_push_cleanup (ptw32_cleanup_t * cleanup,
974*4882a593Smuzhiyun                                  ptw32_cleanup_callback_t routine,
975*4882a593Smuzhiyun                                  void *arg);
976*4882a593Smuzhiyun #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun /*
979*4882a593Smuzhiyun  * Thread Specific Data Functions
980*4882a593Smuzhiyun  */
981*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_key_create (pthread_key_t * key,
982*4882a593Smuzhiyun                                 void (PTW32_CDECL *destructor) (void *));
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_key_delete (pthread_key_t key);
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_setspecific (pthread_key_t key,
987*4882a593Smuzhiyun                                  const void *value);
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun PTW32_DLLPORT void * PTW32_CDECL pthread_getspecific (pthread_key_t key);
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun /*
993*4882a593Smuzhiyun  * Mutex Attribute Functions
994*4882a593Smuzhiyun  */
995*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_init (pthread_mutexattr_t * attr);
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_destroy (pthread_mutexattr_t * attr);
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_getpshared (const pthread_mutexattr_t
1000*4882a593Smuzhiyun                                           * attr,
1001*4882a593Smuzhiyun                                           int *pshared);
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_setpshared (pthread_mutexattr_t * attr,
1004*4882a593Smuzhiyun                                           int pshared);
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_settype (pthread_mutexattr_t * attr, int kind);
1007*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_gettype (const pthread_mutexattr_t * attr, int *kind);
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_setrobust(
1010*4882a593Smuzhiyun                                            pthread_mutexattr_t *attr,
1011*4882a593Smuzhiyun                                            int robust);
1012*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_getrobust(
1013*4882a593Smuzhiyun                                            const pthread_mutexattr_t * attr,
1014*4882a593Smuzhiyun                                            int * robust);
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun /*
1017*4882a593Smuzhiyun  * Barrier Attribute Functions
1018*4882a593Smuzhiyun  */
1019*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_barrierattr_init (pthread_barrierattr_t * attr);
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_barrierattr_destroy (pthread_barrierattr_t * attr);
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_barrierattr_getpshared (const pthread_barrierattr_t
1024*4882a593Smuzhiyun                                             * attr,
1025*4882a593Smuzhiyun                                             int *pshared);
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_barrierattr_setpshared (pthread_barrierattr_t * attr,
1028*4882a593Smuzhiyun                                             int pshared);
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun /*
1031*4882a593Smuzhiyun  * Mutex Functions
1032*4882a593Smuzhiyun  */
1033*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutex_init (pthread_mutex_t * mutex,
1034*4882a593Smuzhiyun                                 const pthread_mutexattr_t * attr);
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutex_destroy (pthread_mutex_t * mutex);
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutex_lock (pthread_mutex_t * mutex);
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutex_timedlock(pthread_mutex_t * mutex,
1041*4882a593Smuzhiyun                                     const struct timespec *abstime);
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutex_trylock (pthread_mutex_t * mutex);
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutex_unlock (pthread_mutex_t * mutex);
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutex_consistent (pthread_mutex_t * mutex);
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun /*
1050*4882a593Smuzhiyun  * Spinlock Functions
1051*4882a593Smuzhiyun  */
1052*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_spin_init (pthread_spinlock_t * lock, int pshared);
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_spin_destroy (pthread_spinlock_t * lock);
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_spin_lock (pthread_spinlock_t * lock);
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_spin_trylock (pthread_spinlock_t * lock);
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_spin_unlock (pthread_spinlock_t * lock);
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun /*
1063*4882a593Smuzhiyun  * Barrier Functions
1064*4882a593Smuzhiyun  */
1065*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_barrier_init (pthread_barrier_t * barrier,
1066*4882a593Smuzhiyun                                   const pthread_barrierattr_t * attr,
1067*4882a593Smuzhiyun                                   unsigned int count);
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_barrier_destroy (pthread_barrier_t * barrier);
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_barrier_wait (pthread_barrier_t * barrier);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun /*
1074*4882a593Smuzhiyun  * Condition Variable Attribute Functions
1075*4882a593Smuzhiyun  */
1076*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_condattr_init (pthread_condattr_t * attr);
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_condattr_destroy (pthread_condattr_t * attr);
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_condattr_getpshared (const pthread_condattr_t * attr,
1081*4882a593Smuzhiyun                                          int *pshared);
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_condattr_setpshared (pthread_condattr_t * attr,
1084*4882a593Smuzhiyun                                          int pshared);
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun /*
1087*4882a593Smuzhiyun  * Condition Variable Functions
1088*4882a593Smuzhiyun  */
1089*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_cond_init (pthread_cond_t * cond,
1090*4882a593Smuzhiyun                                const pthread_condattr_t * attr);
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_cond_destroy (pthread_cond_t * cond);
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_cond_wait (pthread_cond_t * cond,
1095*4882a593Smuzhiyun                                pthread_mutex_t * mutex);
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_cond_timedwait (pthread_cond_t * cond,
1098*4882a593Smuzhiyun                                     pthread_mutex_t * mutex,
1099*4882a593Smuzhiyun                                     const struct timespec *abstime);
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_cond_signal (pthread_cond_t * cond);
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_cond_broadcast (pthread_cond_t * cond);
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun /*
1106*4882a593Smuzhiyun  * Scheduling
1107*4882a593Smuzhiyun  */
1108*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_setschedparam (pthread_t thread,
1109*4882a593Smuzhiyun                                    int policy,
1110*4882a593Smuzhiyun                                    const struct sched_param *param);
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_getschedparam (pthread_t thread,
1113*4882a593Smuzhiyun                                    int *policy,
1114*4882a593Smuzhiyun                                    struct sched_param *param);
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_setconcurrency (int);
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_getconcurrency (void);
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun /*
1121*4882a593Smuzhiyun  * Read-Write Lock Functions
1122*4882a593Smuzhiyun  */
1123*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_init(pthread_rwlock_t *lock,
1124*4882a593Smuzhiyun                                 const pthread_rwlockattr_t *attr);
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_destroy(pthread_rwlock_t *lock);
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_tryrdlock(pthread_rwlock_t *);
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_trywrlock(pthread_rwlock_t *);
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_rdlock(pthread_rwlock_t *lock);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_timedrdlock(pthread_rwlock_t *lock,
1135*4882a593Smuzhiyun                                        const struct timespec *abstime);
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_wrlock(pthread_rwlock_t *lock);
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_timedwrlock(pthread_rwlock_t *lock,
1140*4882a593Smuzhiyun                                        const struct timespec *abstime);
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_unlock(pthread_rwlock_t *lock);
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlockattr_init (pthread_rwlockattr_t * attr);
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlockattr_destroy (pthread_rwlockattr_t * attr);
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * attr,
1149*4882a593Smuzhiyun                                            int *pshared);
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_rwlockattr_setpshared (pthread_rwlockattr_t * attr,
1152*4882a593Smuzhiyun                                            int pshared);
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun #if PTW32_LEVEL >= PTW32_LEVEL_MAX - 1
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun /*
1157*4882a593Smuzhiyun  * Signal Functions. Should be defined in <signal.h> but MSVC and MinGW32
1158*4882a593Smuzhiyun  * already have signal.h that don't define these.
1159*4882a593Smuzhiyun  */
1160*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_kill(pthread_t thread, int sig);
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun /*
1163*4882a593Smuzhiyun  * Non-portable functions
1164*4882a593Smuzhiyun  */
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun /*
1167*4882a593Smuzhiyun  * Compatibility with Linux.
1168*4882a593Smuzhiyun  */
1169*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_setkind_np(pthread_mutexattr_t * attr,
1170*4882a593Smuzhiyun                                          int kind);
1171*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_getkind_np(pthread_mutexattr_t * attr,
1172*4882a593Smuzhiyun                                          int *kind);
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun /*
1175*4882a593Smuzhiyun  * Possibly supported by other POSIX threads implementations
1176*4882a593Smuzhiyun  */
1177*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_delay_np (struct timespec * interval);
1178*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_num_processors_np(void);
1179*4882a593Smuzhiyun PTW32_DLLPORT unsigned __int64 PTW32_CDECL pthread_getunique_np(pthread_t thread);
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun /*
1182*4882a593Smuzhiyun  * Useful if an application wants to statically link
1183*4882a593Smuzhiyun  * the lib rather than load the DLL at run-time.
1184*4882a593Smuzhiyun  */
1185*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_win32_process_attach_np(void);
1186*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_win32_process_detach_np(void);
1187*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_win32_thread_attach_np(void);
1188*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_win32_thread_detach_np(void);
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun /*
1191*4882a593Smuzhiyun  * Features that are auto-detected at load/run time.
1192*4882a593Smuzhiyun  */
1193*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthread_win32_test_features_np(int);
1194*4882a593Smuzhiyun enum ptw32_features {
1195*4882a593Smuzhiyun   PTW32_SYSTEM_INTERLOCKED_COMPARE_EXCHANGE = 0x0001, /* System provides it. */
1196*4882a593Smuzhiyun   PTW32_ALERTABLE_ASYNC_CANCEL              = 0x0002  /* Can cancel blocked threads. */
1197*4882a593Smuzhiyun };
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun /*
1200*4882a593Smuzhiyun  * Register a system time change with the library.
1201*4882a593Smuzhiyun  * Causes the library to perform various functions
1202*4882a593Smuzhiyun  * in response to the change. Should be called whenever
1203*4882a593Smuzhiyun  * the application's top level window receives a
1204*4882a593Smuzhiyun  * WM_TIMECHANGE message. It can be passed directly to
1205*4882a593Smuzhiyun  * pthread_create() as a new thread if desired.
1206*4882a593Smuzhiyun  */
1207*4882a593Smuzhiyun PTW32_DLLPORT void * PTW32_CDECL pthread_timechange_handler_np(void *);
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun #endif /*PTW32_LEVEL >= PTW32_LEVEL_MAX - 1 */
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun #if PTW32_LEVEL >= PTW32_LEVEL_MAX
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun /*
1214*4882a593Smuzhiyun  * Returns the Win32 HANDLE for the POSIX thread.
1215*4882a593Smuzhiyun  */
1216*4882a593Smuzhiyun PTW32_DLLPORT HANDLE PTW32_CDECL pthread_getw32threadhandle_np(pthread_t thread);
1217*4882a593Smuzhiyun /*
1218*4882a593Smuzhiyun  * Returns the win32 thread ID for POSIX thread.
1219*4882a593Smuzhiyun  */
1220*4882a593Smuzhiyun PTW32_DLLPORT DWORD PTW32_CDECL pthread_getw32threadid_np (pthread_t thread);
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun /*
1224*4882a593Smuzhiyun  * Protected Methods
1225*4882a593Smuzhiyun  *
1226*4882a593Smuzhiyun  * This function blocks until the given WIN32 handle
1227*4882a593Smuzhiyun  * is signaled or pthread_cancel had been called.
1228*4882a593Smuzhiyun  * This function allows the caller to hook into the
1229*4882a593Smuzhiyun  * PThreads cancel mechanism. It is implemented using
1230*4882a593Smuzhiyun  *
1231*4882a593Smuzhiyun  *              WaitForMultipleObjects
1232*4882a593Smuzhiyun  *
1233*4882a593Smuzhiyun  * on 'waitHandle' and a manually reset WIN32 Event
1234*4882a593Smuzhiyun  * used to implement pthread_cancel. The 'timeout'
1235*4882a593Smuzhiyun  * argument to TimedWait is simply passed to
1236*4882a593Smuzhiyun  * WaitForMultipleObjects.
1237*4882a593Smuzhiyun  */
1238*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthreadCancelableWait (HANDLE waitHandle);
1239*4882a593Smuzhiyun PTW32_DLLPORT int PTW32_CDECL pthreadCancelableTimedWait (HANDLE waitHandle,
1240*4882a593Smuzhiyun                                         DWORD timeout);
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun /*
1245*4882a593Smuzhiyun  * Thread-Safe C Runtime Library Mappings.
1246*4882a593Smuzhiyun  */
1247*4882a593Smuzhiyun #if !defined(_UWIN)
1248*4882a593Smuzhiyun #  if defined(NEED_ERRNO)
1249*4882a593Smuzhiyun      PTW32_DLLPORT int * PTW32_CDECL _errno( void );
1250*4882a593Smuzhiyun #  else
1251*4882a593Smuzhiyun #    if !defined(errno)
1252*4882a593Smuzhiyun #      if (defined(_MT) || defined(_DLL))
1253*4882a593Smuzhiyun          __declspec(dllimport) extern int * __cdecl _errno(void);
1254*4882a593Smuzhiyun #        define errno   (*_errno())
1255*4882a593Smuzhiyun #      endif
1256*4882a593Smuzhiyun #    endif
1257*4882a593Smuzhiyun #  endif
1258*4882a593Smuzhiyun #endif
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun /*
1261*4882a593Smuzhiyun  * Some compiler environments don't define some things.
1262*4882a593Smuzhiyun  */
1263*4882a593Smuzhiyun #if defined(__BORLANDC__)
1264*4882a593Smuzhiyun #  define _ftime ftime
1265*4882a593Smuzhiyun #  define _timeb timeb
1266*4882a593Smuzhiyun #endif
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun #if defined(__cplusplus)
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun /*
1271*4882a593Smuzhiyun  * Internal exceptions
1272*4882a593Smuzhiyun  */
1273*4882a593Smuzhiyun class ptw32_exception {};
1274*4882a593Smuzhiyun class ptw32_exception_cancel : public ptw32_exception {};
1275*4882a593Smuzhiyun class ptw32_exception_exit   : public ptw32_exception {};
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun #endif
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun #if PTW32_LEVEL >= PTW32_LEVEL_MAX
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun /* FIXME: This is only required if the library was built using SEH */
1282*4882a593Smuzhiyun /*
1283*4882a593Smuzhiyun  * Get internal SEH tag
1284*4882a593Smuzhiyun  */
1285*4882a593Smuzhiyun PTW32_DLLPORT DWORD PTW32_CDECL ptw32_get_exception_services_code(void);
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun #if !defined(PTW32_BUILD)
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun #if defined(__CLEANUP_SEH)
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun /*
1294*4882a593Smuzhiyun  * Redefine the SEH __except keyword to ensure that applications
1295*4882a593Smuzhiyun  * propagate our internal exceptions up to the library's internal handlers.
1296*4882a593Smuzhiyun  */
1297*4882a593Smuzhiyun #define __except( E ) \
1298*4882a593Smuzhiyun         __except( ( GetExceptionCode() == ptw32_get_exception_services_code() ) \
1299*4882a593Smuzhiyun                  ? EXCEPTION_CONTINUE_SEARCH : ( E ) )
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun #endif /* __CLEANUP_SEH */
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun #if defined(__CLEANUP_CXX)
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun /*
1306*4882a593Smuzhiyun  * Redefine the C++ catch keyword to ensure that applications
1307*4882a593Smuzhiyun  * propagate our internal exceptions up to the library's internal handlers.
1308*4882a593Smuzhiyun  */
1309*4882a593Smuzhiyun #if defined(_MSC_VER)
1310*4882a593Smuzhiyun         /*
1311*4882a593Smuzhiyun          * WARNING: Replace any 'catch( ... )' with 'PtW32CatchAll'
1312*4882a593Smuzhiyun          * if you want Pthread-Win32 cancelation and pthread_exit to work.
1313*4882a593Smuzhiyun          */
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun #if !defined(PtW32NoCatchWarn)
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun #pragma message("Specify \"/DPtW32NoCatchWarn\" compiler flag to skip this message.")
1318*4882a593Smuzhiyun #pragma message("------------------------------------------------------------------")
1319*4882a593Smuzhiyun #pragma message("When compiling applications with MSVC++ and C++ exception handling:")
1320*4882a593Smuzhiyun #pragma message("  Replace any 'catch( ... )' in routines called from POSIX threads")
1321*4882a593Smuzhiyun #pragma message("  with 'PtW32CatchAll' or 'CATCHALL' if you want POSIX thread")
1322*4882a593Smuzhiyun #pragma message("  cancelation and pthread_exit to work. For example:")
1323*4882a593Smuzhiyun #pragma message("")
1324*4882a593Smuzhiyun #pragma message("    #if defined(PtW32CatchAll)")
1325*4882a593Smuzhiyun #pragma message("      PtW32CatchAll")
1326*4882a593Smuzhiyun #pragma message("    #else")
1327*4882a593Smuzhiyun #pragma message("      catch(...)")
1328*4882a593Smuzhiyun #pragma message("    #endif")
1329*4882a593Smuzhiyun #pragma message("        {")
1330*4882a593Smuzhiyun #pragma message("          /* Catchall block processing */")
1331*4882a593Smuzhiyun #pragma message("        }")
1332*4882a593Smuzhiyun #pragma message("------------------------------------------------------------------")
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun #endif
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun #define PtW32CatchAll \
1337*4882a593Smuzhiyun         catch( ptw32_exception & ) { throw; } \
1338*4882a593Smuzhiyun         catch( ... )
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun #else /* _MSC_VER */
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun #define catch( E ) \
1343*4882a593Smuzhiyun         catch( ptw32_exception & ) { throw; } \
1344*4882a593Smuzhiyun         catch( E )
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun #endif /* _MSC_VER */
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun #endif /* __CLEANUP_CXX */
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun #endif /* ! PTW32_BUILD */
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun #if defined(__cplusplus)
1353*4882a593Smuzhiyun }                               /* End of extern "C" */
1354*4882a593Smuzhiyun #endif                          /* __cplusplus */
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun #if defined(PTW32__HANDLE_DEF)
1357*4882a593Smuzhiyun # undef HANDLE
1358*4882a593Smuzhiyun #endif
1359*4882a593Smuzhiyun #if defined(PTW32__DWORD_DEF)
1360*4882a593Smuzhiyun # undef DWORD
1361*4882a593Smuzhiyun #endif
1362*4882a593Smuzhiyun 
1363*4882a593Smuzhiyun #undef PTW32_LEVEL
1364*4882a593Smuzhiyun #undef PTW32_LEVEL_MAX
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun #endif /* ! RC_INVOKED */
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun #endif /* PTHREAD_H */
1369