1 /*
2  *  Written by Joel Sherrill <joel.sherrill@OARcorp.com>.
3  *
4  *  COPYRIGHT (c) 1989-2013, 2015.
5  *  On-Line Applications Research Corporation (OAR).
6  *
7  *  Permission to use, copy, modify, and distribute this software for any
8  *  purpose without fee is hereby granted, provided that this entire notice
9  *  is included in all copies of any software which is or includes a copy
10  *  or modification of this software.
11  *
12  *  THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
13  *  WARRANTY.  IN PARTICULAR,  THE AUTHOR MAKES NO REPRESENTATION
14  *  OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS
15  *  SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
16  */
17 
18 #ifndef _SYS__PTHREADTYPES_H_
19 #define	_SYS__PTHREADTYPES_H_
20 
21 #if defined(_POSIX_THREADS) || __POSIX_VISIBLE >= 199506
22 
23 #include <sys/sched.h>
24 
25 /*
26  *  2.5 Primitive System Data Types,  P1003.1c/D10, p. 19.
27  */
28 
29 #if defined(__XMK__)
30 typedef unsigned int pthread_t;          /* identify a thread */
31 #else
32 typedef __uint32_t pthread_t;            /* identify a thread */
33 #endif
34 
35 /* P1003.1c/D10, p. 118-119 */
36 #define PTHREAD_SCOPE_PROCESS 0
37 #define PTHREAD_SCOPE_SYSTEM  1
38 
39 /* P1003.1c/D10, p. 111 */
40 #define PTHREAD_INHERIT_SCHED  1      /* scheduling policy and associated */
41                                       /*   attributes are inherited from */
42                                       /*   the calling thread. */
43 #define PTHREAD_EXPLICIT_SCHED 2      /* set from provided attribute object */
44 
45 /* P1003.1c/D10, p. 141 */
46 #define PTHREAD_CREATE_DETACHED 0
47 #define PTHREAD_CREATE_JOINABLE  1
48 
49 #if defined(__XMK__)
50 typedef struct pthread_attr_s {
51   int contentionscope;
52   struct sched_param schedparam;
53   int  detachstate;
54   void *stackaddr;
55   size_t stacksize;
56 } pthread_attr_t;
57 
58 #define PTHREAD_STACK_MIN       200
59 
60 #else /* !defined(__XMK__) */
61 typedef struct {
62   int is_initialized;
63   void *stackaddr;
64   int stacksize;
65   int contentionscope;
66   int inheritsched;
67   int schedpolicy;
68   struct sched_param schedparam;
69 
70   /* P1003.4b/D8, p. 54 adds cputime_clock_allowed attribute.  */
71 #if defined(_POSIX_THREAD_CPUTIME)
72   int  cputime_clock_allowed;  /* see time.h */
73 #endif
74   int  detachstate;
75 } pthread_attr_t;
76 
77 #endif /* !defined(__XMK__) */
78 
79 #if defined(_POSIX_THREAD_PROCESS_SHARED)
80 /* NOTE: P1003.1c/D10, p. 81 defines following values for process_shared.  */
81 
82 #define PTHREAD_PROCESS_PRIVATE 0 /* visible within only the creating process */
83 #define PTHREAD_PROCESS_SHARED  1 /* visible too all processes with access to */
84                                   /*   the memory where the resource is */
85                                   /*   located */
86 #endif
87 
88 #if defined(_POSIX_THREAD_PRIO_PROTECT)
89 /* Mutexes */
90 
91 /* Values for blocking protocol. */
92 
93 #define PTHREAD_PRIO_NONE    0
94 #define PTHREAD_PRIO_INHERIT 1
95 #define PTHREAD_PRIO_PROTECT 2
96 #endif
97 
98 #if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES)
99 
100 /* Values for mutex type */
101 
102 /* The following defines are part of the X/Open System Interface (XSI). */
103 
104 /*
105  * This type of mutex does not detect deadlock. A thread attempting to
106  * relock this mutex without first unlocking it shall deadlock. Attempting
107  * to unlock a mutex locked by a different thread results in undefined
108  * behavior.  Attempting to unlock an unlocked mutex results in undefined
109  * behavior.
110  */
111 #define PTHREAD_MUTEX_NORMAL     0
112 
113 /*
114  * A thread attempting to relock this mutex without first unlocking
115  * it shall succeed in locking the mutex.  The relocking deadlock which
116  * can occur with mutexes of type PTHREAD_MUTEX_NORMAL cannot occur with
117  * this type of mutex.  Multiple locks of this mutex shall require the
118  * same number of unlocks to release the mutex before another thread can
119  * acquire the mutex. A thread attempting to unlock a mutex which another
120  * thread has locked shall return with an error.  A thread attempting to
121  * unlock an unlocked mutex shall return with an error.
122  */
123 #define PTHREAD_MUTEX_RECURSIVE  1
124 
125 /*
126  * This type of mutex provides error checking. A thread attempting
127  * to relock this mutex without first unlocking it shall return with an
128  * error. A thread attempting to unlock a mutex which another thread has
129  * locked shall return with an error. A thread attempting to unlock an
130  * unlocked mutex shall return with an error.
131  */
132 #define PTHREAD_MUTEX_ERRORCHECK 2
133 
134 /*
135  * Attempting to recursively lock a mutex of this type results
136  * in undefined behavior. Attempting to unlock a mutex of this type
137  * which was not locked by the calling thread results in undefined
138  * behavior. Attempting to unlock a mutex of this type which is not locked
139  * results in undefined behavior. An implementation may map this mutex to
140  * one of the other mutex types.
141  */
142 #define PTHREAD_MUTEX_DEFAULT    3
143 
144 #endif /* !defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES) */
145 
146 #if defined(__XMK__)
147 typedef unsigned int pthread_mutex_t;    /* identify a mutex */
148 
149 typedef struct {
150   int type;
151 } pthread_mutexattr_t;
152 
153 #else /* !defined(__XMK__) */
154 typedef __uint32_t pthread_mutex_t;      /* identify a mutex */
155 
156 typedef struct {
157   int   is_initialized;
158 #if defined(_POSIX_THREAD_PROCESS_SHARED)
159   int   process_shared;  /* allow mutex to be shared amongst processes */
160 #endif
161 #if defined(_POSIX_THREAD_PRIO_PROTECT)
162   int   prio_ceiling;
163   int   protocol;
164 #endif
165 #if defined(_UNIX98_THREAD_MUTEX_ATTRIBUTES)
166   int type;
167 #endif
168   int   recursive;
169 } pthread_mutexattr_t;
170 #endif /* !defined(__XMK__) */
171 
172 #define _PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) 0xFFFFFFFF)
173 
174 /* Condition Variables */
175 
176 typedef __uint32_t pthread_cond_t;       /* identify a condition variable */
177 
178 #define _PTHREAD_COND_INITIALIZER ((pthread_cond_t) 0xFFFFFFFF)
179 
180 typedef struct {
181   int      is_initialized;
182   clock_t  clock;             /* specifiy clock for timeouts */
183 #if defined(_POSIX_THREAD_PROCESS_SHARED)
184   int      process_shared;    /* allow this to be shared amongst processes */
185 #endif
186 } pthread_condattr_t;         /* a condition attribute object */
187 
188 /* Keys */
189 
190 typedef __uint32_t pthread_key_t;        /* thread-specific data keys */
191 
192 typedef struct {
193   int   is_initialized;  /* is this structure initialized? */
194   int   init_executed;   /* has the initialization routine been run? */
195 } pthread_once_t;       /* dynamic package initialization */
196 
197 #define _PTHREAD_ONCE_INIT  { 1, 0 }  /* is initialized and not run */
198 #endif /* defined(_POSIX_THREADS) || __POSIX_VISIBLE >= 199506 */
199 
200 /* POSIX Barrier Types */
201 
202 #if defined(_POSIX_BARRIERS)
203 typedef __uint32_t pthread_barrier_t;        /* POSIX Barrier Object */
204 typedef struct {
205   int   is_initialized;  /* is this structure initialized? */
206 #if defined(_POSIX_THREAD_PROCESS_SHARED)
207   int   process_shared;       /* allow this to be shared amongst processes */
208 #endif
209 } pthread_barrierattr_t;
210 #endif /* defined(_POSIX_BARRIERS) */
211 
212 /* POSIX Spin Lock Types */
213 
214 #if defined(_POSIX_SPIN_LOCKS)
215 typedef __uint32_t pthread_spinlock_t;        /* POSIX Spin Lock Object */
216 #endif /* defined(_POSIX_SPIN_LOCKS) */
217 
218 /* POSIX Reader/Writer Lock Types */
219 
220 #if defined(_POSIX_READER_WRITER_LOCKS)
221 typedef __uint32_t pthread_rwlock_t;         /* POSIX RWLock Object */
222 
223 #define _PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t) 0xFFFFFFFF)
224 
225 typedef struct {
226   int   is_initialized;       /* is this structure initialized? */
227 #if defined(_POSIX_THREAD_PROCESS_SHARED)
228   int   process_shared;       /* allow this to be shared amongst processes */
229 #endif
230 } pthread_rwlockattr_t;
231 #endif /* defined(_POSIX_READER_WRITER_LOCKS) */
232 
233 #endif /* ! _SYS__PTHREADTYPES_H_ */
234