1 |
27 |
unneback |
#ifndef CYGONCE_PTHREAD_H
|
2 |
|
|
#define CYGONCE_PTHREAD_H
|
3 |
|
|
//=============================================================================
|
4 |
|
|
//
|
5 |
|
|
// pthread.h
|
6 |
|
|
//
|
7 |
|
|
// POSIX pthread header
|
8 |
|
|
//
|
9 |
|
|
//=============================================================================
|
10 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
11 |
|
|
// -------------------------------------------
|
12 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
13 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
14 |
|
|
//
|
15 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
16 |
|
|
// the terms of the GNU General Public License as published by the Free
|
17 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
18 |
|
|
//
|
19 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
20 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
21 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
22 |
|
|
// for more details.
|
23 |
|
|
//
|
24 |
|
|
// You should have received a copy of the GNU General Public License along
|
25 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
26 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
27 |
|
|
//
|
28 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
29 |
|
|
// or inline functions from this file, or you compile this file and link it
|
30 |
|
|
// with other works to produce a work based on this file, this file does not
|
31 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
32 |
|
|
// License. However the source code for this file must still be made available
|
33 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
36 |
|
|
// this file might be covered by the GNU General Public License.
|
37 |
|
|
//
|
38 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
39 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
40 |
|
|
// -------------------------------------------
|
41 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
42 |
|
|
//=============================================================================
|
43 |
|
|
//#####DESCRIPTIONBEGIN####
|
44 |
|
|
//
|
45 |
|
|
// Author(s): nickg
|
46 |
|
|
// Contributors: nickg
|
47 |
|
|
// Date: 2000-03-17
|
48 |
|
|
// Purpose: POSIX pthread header
|
49 |
|
|
// Description: This header contains all the definitions needed to support
|
50 |
|
|
// pthreads under eCos. The reader is referred to the POSIX
|
51 |
|
|
// standard or equivalent documentation for details of the
|
52 |
|
|
// functionality contained herein.
|
53 |
|
|
//
|
54 |
|
|
// Usage:
|
55 |
|
|
// #include <pthread.h>
|
56 |
|
|
// ...
|
57 |
|
|
//
|
58 |
|
|
//
|
59 |
|
|
//####DESCRIPTIONEND####
|
60 |
|
|
//
|
61 |
|
|
//=============================================================================
|
62 |
|
|
|
63 |
|
|
#include <pkgconf/hal.h>
|
64 |
|
|
#include <pkgconf/kernel.h>
|
65 |
|
|
#include <pkgconf/posix.h>
|
66 |
|
|
|
67 |
|
|
#include <cyg/infra/cyg_type.h>
|
68 |
|
|
|
69 |
|
|
#include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_MINIMUM
|
70 |
|
|
|
71 |
|
|
#include <stddef.h> // NULL, size_t
|
72 |
|
|
|
73 |
|
|
#include <limits.h>
|
74 |
|
|
|
75 |
|
|
#include <sys/types.h>
|
76 |
|
|
|
77 |
|
|
#include <sched.h> // SCHED_*
|
78 |
|
|
|
79 |
|
|
//=============================================================================
|
80 |
|
|
// General thread operations
|
81 |
|
|
|
82 |
|
|
//-----------------------------------------------------------------------------
|
83 |
|
|
// Thread creation and management.
|
84 |
|
|
|
85 |
|
|
// Create a thread.
|
86 |
|
|
externC int pthread_create ( pthread_t *thread,
|
87 |
|
|
const pthread_attr_t *attr,
|
88 |
|
|
void *(*start_routine) (void *),
|
89 |
|
|
void *arg);
|
90 |
|
|
|
91 |
|
|
// Get current thread id.
|
92 |
|
|
externC pthread_t pthread_self ( void );
|
93 |
|
|
|
94 |
|
|
// Compare two thread identifiers.
|
95 |
|
|
externC int pthread_equal (pthread_t thread1, pthread_t thread2);
|
96 |
|
|
|
97 |
|
|
// Terminate current thread.
|
98 |
|
|
externC void pthread_exit (void *retval) CYGBLD_ATTRIB_NORET;
|
99 |
|
|
|
100 |
|
|
// Wait for the thread to terminate. If thread_return is not NULL then
|
101 |
|
|
// the retval from the thread's call to pthread_exit() is stored at
|
102 |
|
|
// *thread_return.
|
103 |
|
|
externC int pthread_join (pthread_t thread, void **thread_return);
|
104 |
|
|
|
105 |
|
|
// Set the detachstate of the thread to "detached". The thread then does not
|
106 |
|
|
// need to be joined and its resources will be freed when it exits.
|
107 |
|
|
externC int pthread_detach (pthread_t thread);
|
108 |
|
|
|
109 |
|
|
//-----------------------------------------------------------------------------
|
110 |
|
|
// Thread attribute handling.
|
111 |
|
|
|
112 |
|
|
// Initialize attributes object with default attributes:
|
113 |
|
|
// detachstate == PTHREAD_JOINABLE
|
114 |
|
|
// scope == PTHREAD_SCOPE_SYSTEM
|
115 |
|
|
// inheritsched == PTHREAD_EXPLICIT_SCHED
|
116 |
|
|
// schedpolicy == SCHED_OTHER
|
117 |
|
|
// schedparam == unset
|
118 |
|
|
// stackaddr == unset
|
119 |
|
|
// stacksize == 0
|
120 |
|
|
//
|
121 |
|
|
externC int pthread_attr_init (pthread_attr_t *attr);
|
122 |
|
|
|
123 |
|
|
// Destroy thread attributes object
|
124 |
|
|
externC int pthread_attr_destroy (pthread_attr_t *attr);
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
// Set the detachstate attribute
|
128 |
|
|
externC int pthread_attr_setdetachstate (pthread_attr_t *attr,
|
129 |
|
|
int detachstate);
|
130 |
|
|
|
131 |
|
|
// Get the detachstate attribute
|
132 |
|
|
externC int pthread_attr_getdetachstate (const pthread_attr_t *attr,
|
133 |
|
|
int *detachstate);
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
// Set scheduling contention scope
|
137 |
|
|
externC int pthread_attr_setscope (pthread_attr_t *attr, int scope);
|
138 |
|
|
|
139 |
|
|
// Get scheduling contention scope
|
140 |
|
|
externC int pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
// Set scheduling inheritance attribute
|
144 |
|
|
externC int pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
|
145 |
|
|
|
146 |
|
|
// Get scheduling inheritance attribute
|
147 |
|
|
externC int pthread_attr_getinheritsched (const pthread_attr_t *attr,
|
148 |
|
|
int *inherit);
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
// Set scheduling policy
|
152 |
|
|
externC int pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
|
153 |
|
|
|
154 |
|
|
// Get scheduling policy
|
155 |
|
|
externC int pthread_attr_getschedpolicy (const pthread_attr_t *attr,
|
156 |
|
|
int *policy);
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
// Set scheduling parameters
|
160 |
|
|
externC int pthread_attr_setschedparam (pthread_attr_t *attr,
|
161 |
|
|
const struct sched_param *param);
|
162 |
|
|
|
163 |
|
|
// Get scheduling parameters
|
164 |
|
|
externC int pthread_attr_getschedparam (const pthread_attr_t *attr,
|
165 |
|
|
struct sched_param *param);
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
// Set starting address of stack. Whether this is at the start or end of
|
169 |
|
|
// the memory block allocated for the stack depends on whether the stack
|
170 |
|
|
// grows up or down.
|
171 |
|
|
externC int pthread_attr_setstackaddr (pthread_attr_t *attr, void *stackaddr);
|
172 |
|
|
|
173 |
|
|
// Get any previously set stack address.
|
174 |
|
|
externC int pthread_attr_getstackaddr (const pthread_attr_t *attr,
|
175 |
|
|
void **stackaddr);
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
// Set minimum creation stack size.
|
179 |
|
|
externC int pthread_attr_setstacksize (pthread_attr_t *attr,
|
180 |
|
|
size_t stacksize);
|
181 |
|
|
|
182 |
|
|
// Get current minimal stack size.
|
183 |
|
|
externC int pthread_attr_getstacksize (const pthread_attr_t *attr,
|
184 |
|
|
size_t *stacksize);
|
185 |
|
|
|
186 |
|
|
//-----------------------------------------------------------------------------
|
187 |
|
|
// Thread scheduling controls
|
188 |
|
|
|
189 |
|
|
// Set scheduling policy and parameters for the thread
|
190 |
|
|
externC int pthread_setschedparam (pthread_t thread,
|
191 |
|
|
int policy,
|
192 |
|
|
const struct sched_param *param);
|
193 |
|
|
|
194 |
|
|
// Get scheduling policy and parameters for the thread
|
195 |
|
|
externC int pthread_getschedparam (pthread_t thread,
|
196 |
|
|
int *policy,
|
197 |
|
|
struct sched_param *param);
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
|
|
//=============================================================================
|
202 |
|
|
// Dynamic package initialization
|
203 |
|
|
|
204 |
|
|
// Initializer for pthread_once_t instances
|
205 |
|
|
#define PTHREAD_ONCE_INIT 0
|
206 |
|
|
|
207 |
|
|
// Call init_routine just the once per control variable.
|
208 |
|
|
externC int pthread_once (pthread_once_t *once_control,
|
209 |
|
|
void (*init_routine) (void));
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
//=============================================================================
|
214 |
|
|
//Thread specific data
|
215 |
|
|
|
216 |
|
|
// Create a key to identify a location in the thread specific data area.
|
217 |
|
|
// Each thread has its own distinct thread-specific data area but all are
|
218 |
|
|
// addressed by the same keys. The destructor function is called whenever a
|
219 |
|
|
// thread exits and the value associated with the key is non-NULL.
|
220 |
|
|
externC int pthread_key_create (pthread_key_t *key,
|
221 |
|
|
void (*destructor) (void *));
|
222 |
|
|
|
223 |
|
|
// Delete key.
|
224 |
|
|
externC int pthread_key_delete (pthread_key_t key);
|
225 |
|
|
|
226 |
|
|
// Store the pointer value in the thread-specific data slot addressed
|
227 |
|
|
// by the key.
|
228 |
|
|
externC int pthread_setspecific (pthread_key_t key, const void *pointer);
|
229 |
|
|
|
230 |
|
|
// Retrieve the pointer value in the thread-specific data slot addressed
|
231 |
|
|
// by the key.
|
232 |
|
|
externC void *pthread_getspecific (pthread_key_t key);
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
//=============================================================================
|
237 |
|
|
// Thread Cancellation
|
238 |
|
|
|
239 |
|
|
//-----------------------------------------------------------------------------
|
240 |
|
|
// Data structure used to manage cleanup functions
|
241 |
|
|
|
242 |
|
|
struct pthread_cleanup_buffer
|
243 |
|
|
{
|
244 |
|
|
struct pthread_cleanup_buffer *prev; // Chain cleanup buffers
|
245 |
|
|
void (*routine) (void *); // Function to call
|
246 |
|
|
void *arg; // Arg to pass
|
247 |
|
|
};
|
248 |
|
|
|
249 |
|
|
//-----------------------------------------------------------------------------
|
250 |
|
|
// Thread cancelled return value.
|
251 |
|
|
// This is a value returned as the retval in pthread_join() of a
|
252 |
|
|
// thread that has been cancelled. By making it the address of a
|
253 |
|
|
// location we define we can ensure that it differs from NULL and any
|
254 |
|
|
// other valid pointer (as required by the standard).
|
255 |
|
|
|
256 |
|
|
externC int pthread_canceled_dummy_var;
|
257 |
|
|
|
258 |
|
|
#define PTHREAD_CANCELED ((void *)(&pthread_canceled_dummy_var))
|
259 |
|
|
|
260 |
|
|
//-----------------------------------------------------------------------------
|
261 |
|
|
// Cancelability enable and type
|
262 |
|
|
|
263 |
|
|
#define PTHREAD_CANCEL_ENABLE 1
|
264 |
|
|
#define PTHREAD_CANCEL_DISABLE 2
|
265 |
|
|
|
266 |
|
|
#define PTHREAD_CANCEL_ASYNCHRONOUS 1
|
267 |
|
|
#define PTHREAD_CANCEL_DEFERRED 2
|
268 |
|
|
|
269 |
|
|
//-----------------------------------------------------------------------------
|
270 |
|
|
// Functions
|
271 |
|
|
|
272 |
|
|
// Set cancel state of current thread to ENABLE or DISABLE.
|
273 |
|
|
// Returns old state in *oldstate.
|
274 |
|
|
externC int pthread_setcancelstate (int state, int *oldstate);
|
275 |
|
|
|
276 |
|
|
// Set cancel type of current thread to ASYNCHRONOUS or DEFERRED.
|
277 |
|
|
// Returns old type in *oldtype.
|
278 |
|
|
externC int pthread_setcanceltype (int type, int *oldtype);
|
279 |
|
|
|
280 |
|
|
// Cancel the thread.
|
281 |
|
|
externC int pthread_cancel (pthread_t thread);
|
282 |
|
|
|
283 |
|
|
// Test for a pending cancellation for the current thread and terminate
|
284 |
|
|
// the thread if there is one.
|
285 |
|
|
externC void pthread_testcancel (void);
|
286 |
|
|
|
287 |
|
|
// Install a cleanup routine.
|
288 |
|
|
// Note that pthread_cleanup_push() and pthread_cleanup_pop() are macros that
|
289 |
|
|
// must be used in matching pairs and at the same brace nesting level.
|
290 |
|
|
#define pthread_cleanup_push(routine,arg) \
|
291 |
|
|
{ \
|
292 |
|
|
struct pthread_cleanup_buffer _buffer_; \
|
293 |
|
|
pthread_cleanup_push_inner (&_buffer_, (routine), (arg));
|
294 |
|
|
|
295 |
|
|
// Remove a cleanup handler installed by the matching pthread_cleanup_push().
|
296 |
|
|
// If execute is non-zero, the handler function is called.
|
297 |
|
|
#define pthread_cleanup_pop(execute) \
|
298 |
|
|
pthread_cleanup_pop_inner (&_buffer_, (execute)); \
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
|
302 |
|
|
// These two functions actually implement the cleanup push and pop functionality.
|
303 |
|
|
externC void pthread_cleanup_push_inner (struct pthread_cleanup_buffer *buffer,
|
304 |
|
|
void (*routine) (void *),
|
305 |
|
|
void *arg);
|
306 |
|
|
|
307 |
|
|
externC void pthread_cleanup_pop_inner (struct pthread_cleanup_buffer *buffer,
|
308 |
|
|
int execute);
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
// -------------------------------------------------------------------------
|
312 |
|
|
// eCos-specific function to measure stack usage of the supplied thread
|
313 |
|
|
|
314 |
|
|
#ifdef CYGFUN_KERNEL_THREADS_STACK_MEASUREMENT
|
315 |
|
|
externC size_t pthread_measure_stack_usage (pthread_t thread);
|
316 |
|
|
#endif
|
317 |
|
|
|
318 |
|
|
//-----------------------------------------------------------------------------
|
319 |
|
|
#endif // ifndef CYGONCE_PTHREAD_H
|
320 |
|
|
// End of pthread.h
|