sl@0: /* sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: POSIX thread exported header file sl@0: * sl@0: */ sl@0: sl@0: #ifndef PTHREAD_H sl@0: #define PTHREAD_H sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #define POSIX_THREAD_DESTRUCTOR_ITERATIONS 4 sl@0: #define POSIX_THREAD_KEYS_MAX 128 sl@0: #define PTHREAD_STACK_MIN 0x2000 sl@0: #define POSIX_THREAD_THREADS_MAX 64 sl@0: #define PTHREAD_THREADS_MAX 1024 sl@0: #define POSIX_SEM_NSEMS_MAX 256 sl@0: #define SEM_NSEMS_MAX 1024 sl@0: #define _POSIX_SEM_VALUE_MAX 32767 sl@0: #define SEM_VALUE_MAX INT_MAX sl@0: sl@0: /* internal use*/ sl@0: typedef enum sl@0: { sl@0: _ENeedsNormalInit =-3, sl@0: _ENeedsRecursiveInit, sl@0: _ENeedsErrorCheckInit, sl@0: _EInitialized =1, sl@0: _EDestroyed, sl@0: _EInvalid, sl@0: }_handle_state; sl@0: sl@0: enum _LockStatus sl@0: { sl@0: _ELockNotCreated, sl@0: _ELockCreating, sl@0: _ELockCreated, sl@0: }; sl@0: sl@0: enum _OnceStatus sl@0: { sl@0: _ENotDone, sl@0: _EDoing, sl@0: _EDone, sl@0: }; sl@0: sl@0: typedef int pthread_once_t; sl@0: sl@0: struct _pthread_mutex_t; sl@0: typedef struct sl@0: { sl@0: _handle_state iState; sl@0: struct _pthread_mutex_t* iPtr; sl@0: int iReentry; sl@0: }pthread_mutex_t; sl@0: sl@0: typedef struct sl@0: { sl@0: _handle_state iState; sl@0: int iSharedType; sl@0: int iMutexType; sl@0: }pthread_mutexattr_t; sl@0: sl@0: sl@0: struct _CondNode; sl@0: struct _CondQueue sl@0: { sl@0: struct _CondNode* iHead; sl@0: struct _CondNode* iTail; sl@0: pthread_mutex_t iMutex; sl@0: }; sl@0: sl@0: typedef struct sl@0: { sl@0: _handle_state iState; sl@0: struct _CondQueue iQueue; sl@0: }pthread_cond_t; sl@0: sl@0: typedef struct _pthread_condattr_t* pthread_condattr_t; sl@0: sl@0: #define PTHREAD_ONCE_INIT _ENotDone sl@0: sl@0: #define PTHREAD_MUTEX_INITIALIZER { _ENeedsNormalInit, NULL,0 } sl@0: #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP { _ENeedsRecursiveInit, NULL, 0 } sl@0: #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP { _ENeedsErrorCheckInit, NULL,0 } sl@0: sl@0: #define PTHREAD_COND_INITIALIZER { _ENeedsNormalInit, NULL,NULL } sl@0: sl@0: enum sl@0: { sl@0: PTHREAD_CREATE_JOINABLE = 0, /* Default */ sl@0: PTHREAD_CREATE_DETACHED = 1 sl@0: }; sl@0: sl@0: enum sl@0: { sl@0: PTHREAD_SCOPE_SYSTEM = 1 /* Default */ sl@0: }; sl@0: sl@0: enum sl@0: { sl@0: PTHREAD_CANCEL_ASYNCHRONOUS = 0, sl@0: PTHREAD_CANCEL_DEFERRED = 1 /* Default */ sl@0: }; sl@0: sl@0: enum sl@0: { sl@0: PTHREAD_PROCESS_PRIVATE = 0, sl@0: }; sl@0: sl@0: enum sl@0: { sl@0: PTHREAD_MUTEX_NORMAL, sl@0: PTHREAD_MUTEX_RECURSIVE, sl@0: PTHREAD_MUTEX_ERRORCHECK, sl@0: PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL sl@0: }; sl@0: sl@0: sl@0: sl@0: #ifdef __cplusplus sl@0: extern "C" sl@0: { sl@0: #endif /* __cplusplus */ sl@0: sl@0: typedef void * (*thread_begin_routine)(void *); sl@0: /* sl@0: This function creates a thread, with attributes specified by attrib, sl@0: within a process. If attrib is NULL, then default attributes will be used. sl@0: On successful completion, pthread_create() will store the ID of the sl@0: created thread in the location referenced by threadhdl. sl@0: */ sl@0: IMPORT_C extern int pthread_create(pthread_t *threadhdl, pthread_attr_t *attrib, sl@0: thread_begin_routine begin_routine , void * param); sl@0: /* sl@0: This function return the handle to current thread. sl@0: */ sl@0: IMPORT_C extern pthread_t pthread_self (); sl@0: sl@0: /* sl@0: Compare two thread handles sl@0: */ sl@0: IMPORT_C extern int pthread_equal(pthread_t t1,pthread_t t2); sl@0: /* sl@0: Waiting for thread termination sl@0: */ sl@0: IMPORT_C extern int pthread_join(pthread_t thHandle, void **retValPtr); sl@0: /* sl@0: detach from thread sl@0: */ sl@0: IMPORT_C extern int pthread_detach(pthread_t thrHandle); sl@0: /* sl@0: exit current thread sl@0: */ sl@0: IMPORT_C extern void pthread_exit(void *retValPtr); sl@0: /* sl@0: Initialise the thread attributes sl@0: */ sl@0: IMPORT_C int pthread_attr_init(pthread_attr_t *attrib); sl@0: /* sl@0: Destroy the thread attributes sl@0: */ sl@0: IMPORT_C int pthread_attr_destroy(pthread_attr_t *attrib); sl@0: /* sl@0: Get the detach_state of the current thread sl@0: */ sl@0: IMPORT_C int pthread_attr_getdetachstate(const pthread_attr_t *attrib, sl@0: int *detState); sl@0: /* sl@0: Set the detach_state of the current thread sl@0: */ sl@0: IMPORT_C int pthread_attr_setdetachstate(pthread_attr_t *attrib, sl@0: int detState); sl@0: /* sl@0: Get the stack size of the current thread sl@0: */ sl@0: IMPORT_C int pthread_attr_getstacksize(const pthread_attr_t *attrib, sl@0: size_t *stkSize); sl@0: /* sl@0: Set the stack size of the current thread sl@0: */ sl@0: IMPORT_C int pthread_attr_setstacksize(pthread_attr_t *attrib, sl@0: size_t stkSize); sl@0: sl@0: /* sl@0: This function ensures that a piece of initialization code is executed at most once. sl@0: The once_control argument must point to a static variable statically initialized to PTHREAD_ONCE_INIT sl@0: sl@0: */ sl@0: typedef void (*thread_init_routine) (void); sl@0: IMPORT_C extern int pthread_once (pthread_once_t * once_control, sl@0: thread_init_routine init_routine); sl@0: sl@0: /* sl@0: extern int pthread_key_create (pthread_key_t * key, void (*destructor) (void *)); sl@0: extern int pthread_key_delete (pthread_key_t key); sl@0: extern int pthread_setspecific (pthread_key_t key, const void *value); sl@0: extern void* pthread_getspecific (pthread_key_t key); sl@0: */ sl@0: sl@0: /* sl@0: This function initializes the mutex attribute object attr and fills it with default values for the attributes. sl@0: */ sl@0: IMPORT_C extern int pthread_mutexattr_init (pthread_mutexattr_t * attr); sl@0: sl@0: /* sl@0: This function destroys the mutex attribute object attr and marks it as a destroyed object. sl@0: */ sl@0: IMPORT_C extern int pthread_mutexattr_destroy (pthread_mutexattr_t * attr); sl@0: sl@0: /* sl@0: This function retrieves the current value of the process shared attribute in sl@0: attr and stores it in the location pointed to by pshared. sl@0: */ sl@0: IMPORT_C extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t* attr, int* pshared); sl@0: sl@0: /* sl@0: This function sets the current value of the process shared attribute in attr to the value specifed in the sl@0: pshared variable. sl@0: */ sl@0: IMPORT_C extern int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr, int pshared); sl@0: sl@0: /* sl@0: This function retrieves the current value of the mutex kind attribute in attr and sl@0: stores it in the location pointed to by type. sl@0: */ sl@0: IMPORT_C extern int pthread_mutexattr_gettype (pthread_mutexattr_t * attr, int *type); sl@0: sl@0: sl@0: /* sl@0: This function sets the current value of the mutex kind attribute in attr to the value specifed in the type variable. sl@0: */ sl@0: IMPORT_C extern int pthread_mutexattr_settype (pthread_mutexattr_t * attr, int type); sl@0: sl@0: /* sl@0: This function initializes the mutex object pointed to by mutex according to the mutex attributes specified in attr. sl@0: If attr is NULL, default attributes are used instead. sl@0: sl@0: */ sl@0: IMPORT_C extern int pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * attr); sl@0: sl@0: /* sl@0: This function destroys the mutex object. The mutex must be unlocked on entrance. sl@0: This implementation requires even statically initialized mutexes to be explicitly destroyed. sl@0: */ sl@0: IMPORT_C extern int pthread_mutex_destroy (pthread_mutex_t * mutex); sl@0: sl@0: /* sl@0: This function locks the given mutex. If the mutex is currently sl@0: unlocked, it becomes locked and owned by the calling thread, and sl@0: this function returns immediately. If the mutex is already sl@0: locked by another thread, this function suspends the calling sl@0: thread until the mutex is unlocked. sl@0: sl@0: If the mutex is already locked by the calling thread, the behavior of sl@0: this function depends on the kind of the mutex. If the mutex is sl@0: of the PTHREAD_MUTEX_NORMAL or PTHREAD_MUTEX_DEFAULT kind, the calling thread is suspended until the mutex sl@0: is unlocked, thus effectively causing the calling thread to sl@0: deadlock. If the mutex is of the PTHREAD_MUTEX_ERRORCHECK kind, sl@0: this function returns immediately with the error code EDEADLK. sl@0: If the mutex is of the PTHREAD_MUTEX_RECURSIVE kind, this function sl@0: succeeds and returns immediately, recording the number of times the sl@0: calling thread has locked the mutex. An equal number of sl@0: pthread_mutex_unlock operations must be performed before the mutex sl@0: returns to the unlocked state. sl@0: */ sl@0: IMPORT_C extern int pthread_mutex_lock (pthread_mutex_t * mutex); sl@0: sl@0: sl@0: /* sl@0: This function behaves identically to pthread_mutex_lock, except that if it cannot acquire the lock before sl@0: the abs_timeout time, the call returns with the error code ETIMEDOUT. sl@0: */ sl@0: IMPORT_C extern int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime); sl@0: sl@0: /* sl@0: This function behaves identically to pthread_mutex_lock, except that it does not sl@0: block the calling thread if the mutex is already locked by another thread (or by sl@0: the calling thread in the case of a PTHREAD_MUTEX_RECURSIVE mutex). Instead, pthread_mutex_trylock sl@0: returns immediately with the error code EAGAIN. sl@0: */ sl@0: IMPORT_C extern int pthread_mutex_trylock (pthread_mutex_t * mutex); sl@0: sl@0: /* sl@0: This function unlocks the given mutex. The mutex is assumed sl@0: to be locked and owned by the calling thread on entrance to sl@0: this function. If the mutex is of the PTHREAD_MUTEX_NORMAL or PTHREAD_MUTEX_DEFAULT kind, sl@0: this function always returns it to the unlocked state. If it sl@0: is of the PTHREAD_MUTEX_RECURSIVE kind, it decrements the locking count of the sl@0: mutex (number of pthread_mutex_lock operations performed on it by sl@0: the calling thread), and only when this count reaches zero is the sl@0: mutex actually unlocked. sl@0: sl@0: On PTHREAD_MUTEX_ERRORCHECK mutexes, this function actually checks sl@0: at run-time that the mutex is locked on entrance, and that it was sl@0: locked by the same thread that is now calling pthread_mutex_unlock. sl@0: If these conditions are not met, an error code is returned and the sl@0: mutex remains unchanged. sl@0: */ sl@0: IMPORT_C extern int pthread_mutex_unlock (pthread_mutex_t * mutex); sl@0: sl@0: /* sl@0: This function initializes the condition attribute object attr and sl@0: sets it with default values for the attributes. sl@0: */ sl@0: IMPORT_C extern int pthread_condattr_init (pthread_condattr_t * /*attr*/); sl@0: sl@0: /* sl@0: This function destroys the condtion variable attribute object. sl@0: */ sl@0: IMPORT_C extern int pthread_condattr_destroy (pthread_condattr_t * attr); sl@0: /* sl@0: extern int pthread_condattr_getpshared (const pthread_condattr_t * attr, int *pshared); sl@0: extern int pthread_condattr_setpshared (pthread_condattr_t * attr, int pshared); sl@0: */ sl@0: sl@0: /* sl@0: This function initializes the condition variable object pointed to by cond sl@0: using the attributes of the attr variable. If attr is NULL, default attributes sl@0: are used instead. sl@0: */ sl@0: IMPORT_C extern int pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * /*attr*/); sl@0: sl@0: /* sl@0: This function destroys the condition variable object. sl@0: */ sl@0: IMPORT_C extern int pthread_cond_destroy (pthread_cond_t * cond); sl@0: sl@0: /* sl@0: This function atomically unlocks mutex and waits on cond, sl@0: as pthread_cond_wait does, but it also bounds the duration sl@0: of the wait. If cond has not been signalled within the amount sl@0: of time specified by abstime, the mutex mutex is re-acquired and sl@0: pthread_cond_timedwait returns the error ETIMEDOUT. The abstime sl@0: parameter specifies an absolute time, with the same origin as time(2) and gettimeofday(2). sl@0: */ sl@0: IMPORT_C extern int pthread_cond_timedwait (pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec *abstime); sl@0: sl@0: /* sl@0: This function atomically unlocks the mutex (as per pthread_unlock_mutex) sl@0: and waits for the condition variable cond to be signalled. The thread sl@0: execution is suspended and does not consume any CPU time until the condition sl@0: variable is signalled. The mutex must be locked by the calling thread on entrance sl@0: to pthread_cond_wait. Before returning to the calling thread, pthread_cond_wait sl@0: re-acquires mutex (as per pthread_lock_mutex). sl@0: */ sl@0: IMPORT_C extern int pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex); sl@0: sl@0: /* sl@0: This function restarts one of the threads that are waiting on the condition sl@0: variable cond. If no threads are waiting on cond, nothing happens. If several sl@0: threads are waiting on cond, exactly one is restarted. sl@0: sl@0: */ sl@0: IMPORT_C extern int pthread_cond_signal (pthread_cond_t * cond); sl@0: sl@0: /* sl@0: This function restarts all the threads that are waiting on the sl@0: condition variable cond. Nothing happens if no threads are waiting on cond. sl@0: */ sl@0: IMPORT_C extern int pthread_cond_broadcast (pthread_cond_t * cond); sl@0: sl@0: sl@0: typedef void (*destructor_routine)(void *); sl@0: /* sl@0: Thread-specific data key creation sl@0: */ sl@0: IMPORT_C int pthread_key_create(pthread_key_t *key, destructor_routine dest); sl@0: sl@0: /* sl@0: Thread-specific data key deletion sl@0: */ sl@0: IMPORT_C int pthread_key_delete(pthread_key_t key); sl@0: sl@0: /* sl@0: Setting Thread-specific data sl@0: */ sl@0: IMPORT_C int pthread_setspecific(pthread_key_t key, const void *val); sl@0: sl@0: /* sl@0: Getting Thread-specific data sl@0: */ sl@0: IMPORT_C void* pthread_getspecific(pthread_key_t key); sl@0: sl@0: /* sl@0: Getting contention scope sl@0: */ sl@0: IMPORT_C int pthread_attr_getscope(const pthread_attr_t *attrib, sl@0: int* scope); sl@0: /* sl@0: Setting contention scope sl@0: */ sl@0: IMPORT_C int pthread_attr_setscope(pthread_attr_t *attrib,int conscope); sl@0: sl@0: /* sl@0: Setting scheduling policy sl@0: */ sl@0: IMPORT_C int pthread_attr_setschedpolicy(pthread_attr_t *attrib, sl@0: int policy); sl@0: sl@0: /* sl@0: Getting scheduling policy of the thread sl@0: */ sl@0: IMPORT_C int pthread_attr_getschedpolicy(const pthread_attr_t *attrib, sl@0: int *policy); sl@0: sl@0: /* sl@0: Getting scheduling param of the thread sl@0: */ sl@0: IMPORT_C int pthread_attr_getschedparam(const pthread_attr_t *attrib, sl@0: struct sched_param *param); sl@0: sl@0: /* sl@0: Setting scheduling param sl@0: */ sl@0: IMPORT_C int pthread_attr_setschedparam(pthread_attr_t *attrib, sl@0: const struct sched_param *param); sl@0: sl@0: /* sl@0: Getting dynamic scheduling param of the thread sl@0: */ sl@0: IMPORT_C int pthread_getschedparam(pthread_t thr, int *policy, sl@0: struct sched_param *param); sl@0: sl@0: /* sl@0: Dynamically Setting scheduling params sl@0: */ sl@0: IMPORT_C int pthread_setschedparam(pthread_t th, int policy, sl@0: const struct sched_param *param); sl@0: sl@0: #ifdef __cplusplus sl@0: } /* End of "C" */ sl@0: #endif /* __cplusplus */ sl@0: sl@0: sl@0: #endif /* PTHREAD_H */ sl@0: