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 Free Software Foundation, 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
20 // eCos is distributed in the hope that it will be useful, but WITHOUT
21 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 // You should have received a copy of the GNU General Public License
26 // along with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 // As a special exception, if other files instantiate templates or use
30 // macros or inline functions from this file, or you compile this file
31 // and link it with other works to produce a work based on this file,
32 // this file does not by itself cause the resulting work to be covered by
33 // the GNU General Public License. However the source code for this file
34 // must still be made available in accordance with section (3) of the GNU
35 // General Public License v2.
37 // This exception does not invalidate any other reasons why a work based
38 // on this file might be covered by the GNU General Public License.
39 // -------------------------------------------
40 // ####ECOSGPLCOPYRIGHTEND####
41 //=============================================================================
42 //#####DESCRIPTIONBEGIN####
45 // Contributors: nickg
47 // Purpose: POSIX pthread header
48 // Description: This header contains all the definitions needed to support
49 // pthreads under eCos. The reader is referred to the POSIX
50 // standard or equivalent documentation for details of the
51 // functionality contained herein.
54 // #include <pthread.h>
58 //####DESCRIPTIONEND####
60 //=============================================================================
62 #include <pkgconf/hal.h>
63 #include <pkgconf/kernel.h>
64 #include <pkgconf/posix.h>
66 #include <cyg/infra/cyg_type.h>
68 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_MINIMUM
70 #include <stddef.h> // NULL, size_t
74 #include <sys/types.h>
76 #include <sched.h> // SCHED_*
78 //=============================================================================
79 // General thread operations
81 //-----------------------------------------------------------------------------
82 // Thread creation and management.
85 __externC
int pthread_create (pthread_t
*__pthread
,
86 const pthread_attr_t
*__attr
,
87 void *(*__start_routine
) (void *),
90 // Get current thread id.
91 __externC pthread_t
pthread_self (void);
93 // Compare two thread identifiers.
94 __externC
int pthread_equal (pthread_t __thread1
, pthread_t __thread2
);
96 // Terminate current thread.
97 __externC
void pthread_exit (void *__retval
) CYGBLD_ATTRIB_NORET
;
99 // Wait for the thread to terminate. If thread_return is not NULL then
100 // the retval from the thread's call to pthread_exit() is stored at
102 __externC
int pthread_join (pthread_t __pthread
, void **__thread_return
);
104 // Set the detachstate of the thread to "detached". The thread then does not
105 // need to be joined and its resources will be freed when it exits.
106 __externC
int pthread_detach (pthread_t __pthread
);
108 //-----------------------------------------------------------------------------
109 // Thread attribute handling.
111 // Initialize attributes object with default attributes:
112 // detachstate == PTHREAD_JOINABLE
113 // scope == PTHREAD_SCOPE_SYSTEM
114 // inheritsched == PTHREAD_EXPLICIT_SCHED
115 // schedpolicy == SCHED_OTHER
116 // schedparam == unset
117 // stackaddr == unset
120 __externC
int pthread_attr_init (pthread_attr_t
*__attr
);
122 // Destroy thread attributes object
123 __externC
int pthread_attr_destroy (pthread_attr_t
*__attr
);
126 // Set the detachstate attribute
127 __externC
int pthread_attr_setdetachstate (pthread_attr_t
*__attr
,
130 // Get the detachstate attribute
131 __externC
int pthread_attr_getdetachstate (const pthread_attr_t
*__attr
,
135 // Set scheduling contention scope
136 __externC
int pthread_attr_setscope (pthread_attr_t
*__attr
, int __scope
);
138 // Get scheduling contention scope
139 __externC
int pthread_attr_getscope (const pthread_attr_t
*__attr
, int *__scope
);
142 // Set scheduling inheritance attribute
143 __externC
int pthread_attr_setinheritsched (pthread_attr_t
*__attr
, int __inherit
);
145 // Get scheduling inheritance attribute
146 __externC
int pthread_attr_getinheritsched (const pthread_attr_t
*__attr
,
150 // Set scheduling policy
151 __externC
int pthread_attr_setschedpolicy (pthread_attr_t
*__attr
, int __policy
);
153 // Get scheduling policy
154 __externC
int pthread_attr_getschedpolicy (const pthread_attr_t
*__attr
,
158 // Set scheduling parameters
159 __externC
int pthread_attr_setschedparam (pthread_attr_t
*__attr
,
160 const struct sched_param
*__param
);
162 // Get scheduling parameters
163 __externC
int pthread_attr_getschedparam (const pthread_attr_t
*__attr
,
164 struct sched_param
*__param
);
167 // Set starting address of stack. Whether this is at the start or end of
168 // the memory block allocated for the stack depends on whether the stack
170 __externC
int pthread_attr_setstackaddr (pthread_attr_t
*__attr
, void *__stackaddr
);
172 // Get any previously set stack address.
173 __externC
int pthread_attr_getstackaddr (const pthread_attr_t
*__attr
,
177 // Set minimum creation stack size.
178 __externC
int pthread_attr_setstacksize (pthread_attr_t
*__attr
,
181 // Get current minimal stack size.
182 __externC
int pthread_attr_getstacksize (const pthread_attr_t
*__attr
,
183 size_t *__stacksize
);
185 //-----------------------------------------------------------------------------
186 // Thread scheduling controls
188 // Set scheduling policy and parameters for the thread
189 __externC
int pthread_setschedparam (pthread_t __pthread
,
191 const struct sched_param
*__param
);
193 // Get scheduling policy and parameters for the thread
194 __externC
int pthread_getschedparam (pthread_t __pthread
,
196 struct sched_param
*__param
);
200 //=============================================================================
201 // Dynamic package initialization
203 // Initializer for pthread_once_t instances
204 #define PTHREAD_ONCE_INIT 0
206 // Call init_routine just the once per control variable.
207 __externC
int pthread_once (pthread_once_t
*__once_control
,
208 void (*__init_routine
) (void));
212 //=============================================================================
213 //Thread specific data
215 // Create a key to identify a location in the thread specific data area.
216 // Each thread has its own distinct thread-specific data area but all are
217 // addressed by the same keys. The destructor function is called whenever a
218 // thread exits and the value associated with the key is non-NULL.
219 __externC
int pthread_key_create (pthread_key_t
*__key
,
220 void (*__destructor
) (void *));
223 __externC
int pthread_key_delete (pthread_key_t __key
);
225 // Store the pointer value in the thread-specific data slot addressed
227 __externC
int pthread_setspecific (pthread_key_t __key
, const void *__pointer
);
229 // Retrieve the pointer value in the thread-specific data slot addressed
231 __externC
void *pthread_getspecific (pthread_key_t __key
);
235 //=============================================================================
236 // Thread Cancellation
238 //-----------------------------------------------------------------------------
239 // Data structure used to manage cleanup functions
241 struct pthread_cleanup_buffer
243 struct pthread_cleanup_buffer
*prev
; // Chain cleanup buffers
244 void (*routine
) (void *); // Function to call
245 void *arg
; // Arg to pass
248 //-----------------------------------------------------------------------------
249 // Thread cancelled return value.
250 // This is a value returned as the retval in pthread_join() of a
251 // thread that has been cancelled. By making it the address of a
252 // location we define we can ensure that it differs from NULL and any
253 // other valid pointer (as required by the standard).
255 __externC
int pthread_canceled_dummy_var
;
257 #define PTHREAD_CANCELED ((void *)(&pthread_canceled_dummy_var))
259 //-----------------------------------------------------------------------------
260 // Cancelability enable and type
262 #define PTHREAD_CANCEL_ENABLE 1
263 #define PTHREAD_CANCEL_DISABLE 2
265 #define PTHREAD_CANCEL_ASYNCHRONOUS 1
266 #define PTHREAD_CANCEL_DEFERRED 2
268 //-----------------------------------------------------------------------------
271 // Set cancel state of current thread to ENABLE or DISABLE.
272 // Returns old state in *oldstate.
273 __externC
int pthread_setcancelstate (int __state
, int *__oldstate
);
275 // Set cancel type of current thread to ASYNCHRONOUS or DEFERRED.
276 // Returns old type in *oldtype.
277 __externC
int pthread_setcanceltype (int __type
, int *__oldtype
);
279 // Cancel the thread.
280 __externC
int pthread_cancel (pthread_t __pthread
);
282 // Test for a pending cancellation for the current thread and terminate
283 // the thread if there is one.
284 __externC
void pthread_testcancel (void);
286 // Install a cleanup routine.
287 // Note that pthread_cleanup_push() and pthread_cleanup_pop() are macros that
288 // must be used in matching pairs and at the same brace nesting level.
289 #define pthread_cleanup_push(__routine, __arg) \
291 struct pthread_cleanup_buffer _buffer_; \
292 pthread_cleanup_push_inner (&_buffer_, (__routine), (__arg));
294 // Remove a cleanup handler installed by the matching pthread_cleanup_push().
295 // If execute is non-zero, the handler function is called.
296 #define pthread_cleanup_pop(__execute) \
297 pthread_cleanup_pop_inner (&_buffer_, (__execute)); \
301 // These two functions actually implement the cleanup push and pop functionality.
302 __externC
void pthread_cleanup_push_inner (struct pthread_cleanup_buffer
*__buffer
,
303 void (*__routine
) (void *),
306 __externC
void pthread_cleanup_pop_inner (struct pthread_cleanup_buffer
*__buffer
,
311 __externC
int pthread_mutexattr_settype(pthread_mutexattr_t
*attr
, int type
);
317 // -------------------------------------------------------------------------
318 // eCos-specific function to measure stack usage of the supplied thread
320 #ifdef CYGFUN_KERNEL_THREADS_STACK_MEASUREMENT
321 __externC
size_t pthread_measure_stack_usage (pthread_t __pthread
);
325 //-----------------------------------------------------------------------------
326 #endif // ifndef CYGONCE_PTHREAD_H