2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: POSIX thread exported header file
21 #include <pthreadtypes.h>
27 #include <pthreadalias.h>
29 #define POSIX_THREAD_DESTRUCTOR_ITERATIONS 4
30 #define POSIX_THREAD_KEYS_MAX 128
31 #define PTHREAD_STACK_MIN 0x2000
32 #define POSIX_THREAD_THREADS_MAX 64
33 #define PTHREAD_THREADS_MAX 1024
34 #define POSIX_SEM_NSEMS_MAX 256
35 #define SEM_NSEMS_MAX 1024
36 #define _POSIX_SEM_VALUE_MAX 32767
37 #define SEM_VALUE_MAX INT_MAX
42 _ENeedsNormalInit =-3,
44 _ENeedsErrorCheckInit,
64 typedef int pthread_once_t;
66 struct _pthread_mutex_t;
70 struct _pthread_mutex_t* iPtr;
85 struct _CondNode* iHead;
86 struct _CondNode* iTail;
87 pthread_mutex_t iMutex;
93 struct _CondQueue iQueue;
96 typedef struct _pthread_condattr_t* pthread_condattr_t;
98 #define PTHREAD_ONCE_INIT _ENotDone
100 #define PTHREAD_MUTEX_INITIALIZER { _ENeedsNormalInit, NULL,0 }
101 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP { _ENeedsRecursiveInit, NULL, 0 }
102 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP { _ENeedsErrorCheckInit, NULL,0 }
104 #define PTHREAD_COND_INITIALIZER { _ENeedsNormalInit, NULL,NULL }
108 PTHREAD_CREATE_JOINABLE = 0, /* Default */
109 PTHREAD_CREATE_DETACHED = 1
114 PTHREAD_SCOPE_SYSTEM = 1 /* Default */
119 PTHREAD_CANCEL_ASYNCHRONOUS = 0,
120 PTHREAD_CANCEL_DEFERRED = 1 /* Default */
125 PTHREAD_PROCESS_PRIVATE = 0,
130 PTHREAD_MUTEX_NORMAL,
131 PTHREAD_MUTEX_RECURSIVE,
132 PTHREAD_MUTEX_ERRORCHECK,
133 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
141 #endif /* __cplusplus */
143 typedef void * (*thread_begin_routine)(void *);
145 This function creates a thread, with attributes specified by attrib,
146 within a process. If attrib is NULL, then default attributes will be used.
147 On successful completion, pthread_create() will store the ID of the
148 created thread in the location referenced by threadhdl.
150 IMPORT_C extern int pthread_create(pthread_t *threadhdl, pthread_attr_t *attrib,
151 thread_begin_routine begin_routine , void * param);
153 This function return the handle to current thread.
155 IMPORT_C extern pthread_t pthread_self ();
158 Compare two thread handles
160 IMPORT_C extern int pthread_equal(pthread_t t1,pthread_t t2);
162 Waiting for thread termination
164 IMPORT_C extern int pthread_join(pthread_t thHandle, void **retValPtr);
168 IMPORT_C extern int pthread_detach(pthread_t thrHandle);
172 IMPORT_C extern void pthread_exit(void *retValPtr);
174 Initialise the thread attributes
176 IMPORT_C int pthread_attr_init(pthread_attr_t *attrib);
178 Destroy the thread attributes
180 IMPORT_C int pthread_attr_destroy(pthread_attr_t *attrib);
182 Get the detach_state of the current thread
184 IMPORT_C int pthread_attr_getdetachstate(const pthread_attr_t *attrib,
187 Set the detach_state of the current thread
189 IMPORT_C int pthread_attr_setdetachstate(pthread_attr_t *attrib,
192 Get the stack size of the current thread
194 IMPORT_C int pthread_attr_getstacksize(const pthread_attr_t *attrib,
197 Set the stack size of the current thread
199 IMPORT_C int pthread_attr_setstacksize(pthread_attr_t *attrib,
203 This function ensures that a piece of initialization code is executed at most once.
204 The once_control argument must point to a static variable statically initialized to PTHREAD_ONCE_INIT
207 typedef void (*thread_init_routine) (void);
208 IMPORT_C extern int pthread_once (pthread_once_t * once_control,
209 thread_init_routine init_routine);
212 extern int pthread_key_create (pthread_key_t * key, void (*destructor) (void *));
213 extern int pthread_key_delete (pthread_key_t key);
214 extern int pthread_setspecific (pthread_key_t key, const void *value);
215 extern void* pthread_getspecific (pthread_key_t key);
219 This function initializes the mutex attribute object attr and fills it with default values for the attributes.
221 IMPORT_C extern int pthread_mutexattr_init (pthread_mutexattr_t * attr);
224 This function destroys the mutex attribute object attr and marks it as a destroyed object.
226 IMPORT_C extern int pthread_mutexattr_destroy (pthread_mutexattr_t * attr);
229 This function retrieves the current value of the process shared attribute in
230 attr and stores it in the location pointed to by pshared.
232 IMPORT_C extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t* attr, int* pshared);
235 This function sets the current value of the process shared attribute in attr to the value specifed in the
238 IMPORT_C extern int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr, int pshared);
241 This function retrieves the current value of the mutex kind attribute in attr and
242 stores it in the location pointed to by type.
244 IMPORT_C extern int pthread_mutexattr_gettype (pthread_mutexattr_t * attr, int *type);
248 This function sets the current value of the mutex kind attribute in attr to the value specifed in the type variable.
250 IMPORT_C extern int pthread_mutexattr_settype (pthread_mutexattr_t * attr, int type);
253 This function initializes the mutex object pointed to by mutex according to the mutex attributes specified in attr.
254 If attr is NULL, default attributes are used instead.
257 IMPORT_C extern int pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * attr);
260 This function destroys the mutex object. The mutex must be unlocked on entrance.
261 This implementation requires even statically initialized mutexes to be explicitly destroyed.
263 IMPORT_C extern int pthread_mutex_destroy (pthread_mutex_t * mutex);
266 This function locks the given mutex. If the mutex is currently
267 unlocked, it becomes locked and owned by the calling thread, and
268 this function returns immediately. If the mutex is already
269 locked by another thread, this function suspends the calling
270 thread until the mutex is unlocked.
272 If the mutex is already locked by the calling thread, the behavior of
273 this function depends on the kind of the mutex. If the mutex is
274 of the PTHREAD_MUTEX_NORMAL or PTHREAD_MUTEX_DEFAULT kind, the calling thread is suspended until the mutex
275 is unlocked, thus effectively causing the calling thread to
276 deadlock. If the mutex is of the PTHREAD_MUTEX_ERRORCHECK kind,
277 this function returns immediately with the error code EDEADLK.
278 If the mutex is of the PTHREAD_MUTEX_RECURSIVE kind, this function
279 succeeds and returns immediately, recording the number of times the
280 calling thread has locked the mutex. An equal number of
281 pthread_mutex_unlock operations must be performed before the mutex
282 returns to the unlocked state.
284 IMPORT_C extern int pthread_mutex_lock (pthread_mutex_t * mutex);
288 This function behaves identically to pthread_mutex_lock, except that if it cannot acquire the lock before
289 the abs_timeout time, the call returns with the error code ETIMEDOUT.
291 IMPORT_C extern int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
294 This function behaves identically to pthread_mutex_lock, except that it does not
295 block the calling thread if the mutex is already locked by another thread (or by
296 the calling thread in the case of a PTHREAD_MUTEX_RECURSIVE mutex). Instead, pthread_mutex_trylock
297 returns immediately with the error code EAGAIN.
299 IMPORT_C extern int pthread_mutex_trylock (pthread_mutex_t * mutex);
302 This function unlocks the given mutex. The mutex is assumed
303 to be locked and owned by the calling thread on entrance to
304 this function. If the mutex is of the PTHREAD_MUTEX_NORMAL or PTHREAD_MUTEX_DEFAULT kind,
305 this function always returns it to the unlocked state. If it
306 is of the PTHREAD_MUTEX_RECURSIVE kind, it decrements the locking count of the
307 mutex (number of pthread_mutex_lock operations performed on it by
308 the calling thread), and only when this count reaches zero is the
309 mutex actually unlocked.
311 On PTHREAD_MUTEX_ERRORCHECK mutexes, this function actually checks
312 at run-time that the mutex is locked on entrance, and that it was
313 locked by the same thread that is now calling pthread_mutex_unlock.
314 If these conditions are not met, an error code is returned and the
315 mutex remains unchanged.
317 IMPORT_C extern int pthread_mutex_unlock (pthread_mutex_t * mutex);
320 This function initializes the condition attribute object attr and
321 sets it with default values for the attributes.
323 IMPORT_C extern int pthread_condattr_init (pthread_condattr_t * /*attr*/);
326 This function destroys the condtion variable attribute object.
328 IMPORT_C extern int pthread_condattr_destroy (pthread_condattr_t * attr);
330 extern int pthread_condattr_getpshared (const pthread_condattr_t * attr, int *pshared);
331 extern int pthread_condattr_setpshared (pthread_condattr_t * attr, int pshared);
335 This function initializes the condition variable object pointed to by cond
336 using the attributes of the attr variable. If attr is NULL, default attributes
339 IMPORT_C extern int pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * /*attr*/);
342 This function destroys the condition variable object.
344 IMPORT_C extern int pthread_cond_destroy (pthread_cond_t * cond);
347 This function atomically unlocks mutex and waits on cond,
348 as pthread_cond_wait does, but it also bounds the duration
349 of the wait. If cond has not been signalled within the amount
350 of time specified by abstime, the mutex mutex is re-acquired and
351 pthread_cond_timedwait returns the error ETIMEDOUT. The abstime
352 parameter specifies an absolute time, with the same origin as time(2) and gettimeofday(2).
354 IMPORT_C extern int pthread_cond_timedwait (pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec *abstime);
357 This function atomically unlocks the mutex (as per pthread_unlock_mutex)
358 and waits for the condition variable cond to be signalled. The thread
359 execution is suspended and does not consume any CPU time until the condition
360 variable is signalled. The mutex must be locked by the calling thread on entrance
361 to pthread_cond_wait. Before returning to the calling thread, pthread_cond_wait
362 re-acquires mutex (as per pthread_lock_mutex).
364 IMPORT_C extern int pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex);
367 This function restarts one of the threads that are waiting on the condition
368 variable cond. If no threads are waiting on cond, nothing happens. If several
369 threads are waiting on cond, exactly one is restarted.
372 IMPORT_C extern int pthread_cond_signal (pthread_cond_t * cond);
375 This function restarts all the threads that are waiting on the
376 condition variable cond. Nothing happens if no threads are waiting on cond.
378 IMPORT_C extern int pthread_cond_broadcast (pthread_cond_t * cond);
381 typedef void (*destructor_routine)(void *);
383 Thread-specific data key creation
385 IMPORT_C int pthread_key_create(pthread_key_t *key, destructor_routine dest);
388 Thread-specific data key deletion
390 IMPORT_C int pthread_key_delete(pthread_key_t key);
393 Setting Thread-specific data
395 IMPORT_C int pthread_setspecific(pthread_key_t key, const void *val);
398 Getting Thread-specific data
400 IMPORT_C void* pthread_getspecific(pthread_key_t key);
403 Getting contention scope
405 IMPORT_C int pthread_attr_getscope(const pthread_attr_t *attrib,
408 Setting contention scope
410 IMPORT_C int pthread_attr_setscope(pthread_attr_t *attrib,int conscope);
413 Setting scheduling policy
415 IMPORT_C int pthread_attr_setschedpolicy(pthread_attr_t *attrib,
419 Getting scheduling policy of the thread
421 IMPORT_C int pthread_attr_getschedpolicy(const pthread_attr_t *attrib,
425 Getting scheduling param of the thread
427 IMPORT_C int pthread_attr_getschedparam(const pthread_attr_t *attrib,
428 struct sched_param *param);
431 Setting scheduling param
433 IMPORT_C int pthread_attr_setschedparam(pthread_attr_t *attrib,
434 const struct sched_param *param);
437 Getting dynamic scheduling param of the thread
439 IMPORT_C int pthread_getschedparam(pthread_t thr, int *policy,
440 struct sched_param *param);
443 Dynamically Setting scheduling params
445 IMPORT_C int pthread_setschedparam(pthread_t th, int policy,
446 const struct sched_param *param);
450 #endif /* __cplusplus */
453 #endif /* PTHREAD_H */