1 #ifndef CYGONCE_PTHREAD_H
2 #define CYGONCE_PTHREAD_H
3 //=============================================================================
7 // POSIX pthread header
9 //=============================================================================
10 //####ECOSGPLCOPYRIGHTBEGIN####
11 // -------------------------------------------
12 // This file is part of eCos, the Embedded Configurable Operating System.
13 // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
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.
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
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.
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.
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.
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####
46 // Contributors: nickg
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.
55 // #include <pthread.h>
59 //####DESCRIPTIONEND####
61 //=============================================================================
63 #include <pkgconf/hal.h>
64 #include <pkgconf/kernel.h>
65 #include <pkgconf/posix.h>
67 #include <cyg/infra/cyg_type.h>
69 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_MINIMUM
71 #include <stddef.h> // NULL, size_t
75 #include <sys/types.h>
77 #include <sched.h> // SCHED_*
79 //=============================================================================
80 // General thread operations
82 //-----------------------------------------------------------------------------
83 // Thread creation and management.
86 __externC int pthread_create (pthread_t *__pthread,
87 const pthread_attr_t *__attr,
88 void *(*__start_routine) (void *),
91 // Get current thread id.
92 __externC pthread_t pthread_self (void);
94 // Compare two thread identifiers.
95 __externC int pthread_equal (pthread_t __thread1, pthread_t __thread2);
97 // Terminate current thread.
98 __externC void pthread_exit (void *__retval) CYGBLD_ATTRIB_NORET;
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
103 __externC int pthread_join (pthread_t __pthread, void **__thread_return);
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 __pthread);
109 //-----------------------------------------------------------------------------
110 // Thread attribute handling.
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
121 __externC int pthread_attr_init (pthread_attr_t *__attr);
123 // Destroy thread attributes object
124 __externC int pthread_attr_destroy (pthread_attr_t *__attr);
127 // Set the detachstate attribute
128 __externC int pthread_attr_setdetachstate (pthread_attr_t *__attr,
131 // Get the detachstate attribute
132 __externC int pthread_attr_getdetachstate (const pthread_attr_t *__attr,
136 // Set scheduling contention scope
137 __externC int pthread_attr_setscope (pthread_attr_t *__attr, int __scope);
139 // Get scheduling contention scope
140 __externC int pthread_attr_getscope (const pthread_attr_t *__attr, int *__scope);
143 // Set scheduling inheritance attribute
144 __externC int pthread_attr_setinheritsched (pthread_attr_t *__attr, int __inherit);
146 // Get scheduling inheritance attribute
147 __externC int pthread_attr_getinheritsched (const pthread_attr_t *__attr,
151 // Set scheduling policy
152 __externC int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy);
154 // Get scheduling policy
155 __externC int pthread_attr_getschedpolicy (const pthread_attr_t *__attr,
159 // Set scheduling parameters
160 __externC int pthread_attr_setschedparam (pthread_attr_t *__attr,
161 const struct sched_param *__param);
163 // Get scheduling parameters
164 __externC int pthread_attr_getschedparam (const pthread_attr_t *__attr,
165 struct sched_param *__param);
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
171 __externC int pthread_attr_setstackaddr (pthread_attr_t *__attr, void *__stackaddr);
173 // Get any previously set stack address.
174 __externC int pthread_attr_getstackaddr (const pthread_attr_t *__attr,
178 // Set minimum creation stack size.
179 __externC int pthread_attr_setstacksize (pthread_attr_t *__attr,
182 // Get current minimal stack size.
183 __externC int pthread_attr_getstacksize (const pthread_attr_t *__attr,
184 size_t *__stacksize);
186 //-----------------------------------------------------------------------------
187 // Thread scheduling controls
189 // Set scheduling policy and parameters for the thread
190 __externC int pthread_setschedparam (pthread_t __pthread,
192 const struct sched_param *__param);
194 // Get scheduling policy and parameters for the thread
195 __externC int pthread_getschedparam (pthread_t __pthread,
197 struct sched_param *__param);
201 //=============================================================================
202 // Dynamic package initialization
204 // Initializer for pthread_once_t instances
205 #define PTHREAD_ONCE_INIT 0
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));
213 //=============================================================================
214 //Thread specific data
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 *));
224 __externC int pthread_key_delete (pthread_key_t __key);
226 // Store the pointer value in the thread-specific data slot addressed
228 __externC int pthread_setspecific (pthread_key_t __key, const void *__pointer);
230 // Retrieve the pointer value in the thread-specific data slot addressed
232 __externC void *pthread_getspecific (pthread_key_t __key);
236 //=============================================================================
237 // Thread Cancellation
239 //-----------------------------------------------------------------------------
240 // Data structure used to manage cleanup functions
242 struct pthread_cleanup_buffer
244 struct pthread_cleanup_buffer *prev; // Chain cleanup buffers
245 void (*routine) (void *); // Function to call
246 void *arg; // Arg to pass
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).
256 __externC int pthread_canceled_dummy_var;
258 #define PTHREAD_CANCELED ((void *)(&pthread_canceled_dummy_var))
260 //-----------------------------------------------------------------------------
261 // Cancelability enable and type
263 #define PTHREAD_CANCEL_ENABLE 1
264 #define PTHREAD_CANCEL_DISABLE 2
266 #define PTHREAD_CANCEL_ASYNCHRONOUS 1
267 #define PTHREAD_CANCEL_DEFERRED 2
269 //-----------------------------------------------------------------------------
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);
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);
280 // Cancel the thread.
281 __externC int pthread_cancel (pthread_t __pthread);
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);
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) \
292 struct pthread_cleanup_buffer _buffer_; \
293 pthread_cleanup_push_inner (&_buffer_, (__routine), (__arg));
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)); \
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 *),
307 __externC void pthread_cleanup_pop_inner (struct pthread_cleanup_buffer *__buffer,
311 // -------------------------------------------------------------------------
312 // eCos-specific function to measure stack usage of the supplied thread
314 #ifdef CYGFUN_KERNEL_THREADS_STACK_MEASUREMENT
315 __externC size_t pthread_measure_stack_usage (pthread_t __pthread);
318 //-----------------------------------------------------------------------------
319 #endif // ifndef CYGONCE_PTHREAD_H