pthread.h

Go to the documentation of this file.
00001 /*
00002 * ==============================================================================
00003 *  Name        : pthread.h
00004 *  Part of     : pthread
00005 *  Interface   : POSIX, pthread
00006 *  Description : POSIX thread exported header file 
00007 *  Version     : 
00008 
00009 Copyright © 2005-2006 Nokia Corporation
00010 All rights reserved.
00011 * © Portions copyright (c) 2007 Symbian Software Ltd. All rights reserved.
00012 Redistribution and use in source and binary forms, with or without 
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice, this 
00015    list of conditions and the following disclaimer. 
00016  * Redistributions in binary form must reproduce the above copyright notice, 
00017    this list of conditions and the following disclaimer in the documentation 
00018    and/or other materials provided with the distribution. 
00019  * Neither the name of the <ORGANIZATION> nor the names of its contributors 
00020    may be used to endorse or promote products derived from this software 
00021    without specific prior written permission. 
00022    
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00027 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00028 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
00029 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00030 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00031 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 * ==============================================================================
00034 */
00035 #ifndef  PTHREAD_H
00036 #define  PTHREAD_H
00037 
00038 #include <_ansi.h>
00039 #include <pthreadtypes.h>
00040 #include <sched.h>
00041 #include <e32def.h>
00042 #include <stddef.h>
00043 #include <time.h>
00044 #include <limits.h>
00045 #include <pthreadalias.h>
00046 
00047 #define POSIX_THREAD_DESTRUCTOR_ITERATIONS  4
00048 #define POSIX_THREAD_KEYS_MAX               128
00049 #define PTHREAD_STACK_MIN                   0x2000
00050 #define POSIX_THREAD_THREADS_MAX            64
00051 #define PTHREAD_THREADS_MAX                 1024
00052 #define POSIX_SEM_NSEMS_MAX                 256
00053 #define SEM_NSEMS_MAX                       1024
00054 #define _POSIX_SEM_VALUE_MAX                32767
00055 #define SEM_VALUE_MAX                       INT_MAX
00056 
00057 /* internal use*/
00058 typedef enum 
00059 {
00060     _ENeedsNormalInit =-3,
00061     _ENeedsRecursiveInit,
00062     _ENeedsErrorCheckInit,
00063     _EInitialized =1,
00064     _EDestroyed,
00065     _EInvalid,
00066 }_handle_state;
00067 
00068 enum _LockStatus
00069 {
00070     _ELockNotCreated,
00071     _ELockCreating,
00072     _ELockCreated,
00073 };
00074 
00075 enum _OnceStatus
00076 {
00077     _ENotDone,
00078     _EDoing,
00079     _EDone,
00080 };
00081 
00082 typedef int pthread_once_t;
00083 
00084 struct _pthread_mutex_t;
00085 typedef struct 
00086 {
00087     _handle_state  iState;
00088     struct _pthread_mutex_t* iPtr;
00089     int   iReentry;
00090 }pthread_mutex_t;
00091 
00092 typedef struct 
00093 {
00094     _handle_state iState;
00095     int iSharedType;
00096     int iMutexType;
00097 }pthread_mutexattr_t;
00098 
00099 
00100 struct _CondNode;
00101 struct _CondQueue
00102 {
00103   struct _CondNode*       iHead;
00104   struct _CondNode*       iTail;
00105   pthread_mutex_t iMutex;
00106 };
00107 
00108 typedef struct 
00109 {
00110   _handle_state    iState;
00111   struct _CondQueue       iQueue;
00112 }pthread_cond_t;
00113 
00114 typedef struct _pthread_condattr_t*   pthread_condattr_t;
00115 
00116 #define PTHREAD_ONCE_INIT _ENotDone
00117 
00118 #define PTHREAD_MUTEX_INITIALIZER { _ENeedsNormalInit, NULL,0 }
00119 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP { _ENeedsRecursiveInit, NULL, 0 }
00120 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP { _ENeedsErrorCheckInit, NULL,0 }
00121 
00122 #define PTHREAD_COND_INITIALIZER { _ENeedsNormalInit, NULL,NULL };
00123 
00124 enum 
00125 { 
00126     PTHREAD_CREATE_JOINABLE = 0,  /* Default */ 
00127     PTHREAD_CREATE_DETACHED = 1
00128 };
00129 
00130 enum 
00131 { 
00132     PTHREAD_SCOPE_SYSTEM  = 1  /* Default */
00133 };
00134 
00135 enum 
00136 {  
00137     PTHREAD_CANCEL_ASYNCHRONOUS = 0,  
00138     PTHREAD_CANCEL_DEFERRED = 1  /* Default */
00139 };
00140 
00141 enum 
00142 {  
00143     PTHREAD_PROCESS_PRIVATE = 0,   
00144 };
00145 
00146 enum
00147 {
00148   PTHREAD_MUTEX_NORMAL,
00149   PTHREAD_MUTEX_RECURSIVE,
00150   PTHREAD_MUTEX_ERRORCHECK,
00151   PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
00152 };
00153 
00154 #ifdef __cplusplus
00155 extern "C"
00156 {
00157 #endif    /* __cplusplus */
00158 
00159 typedef void * (*thread_begin_routine)(void *);
00160 /*
00161 This function creates a thread, with attributes specified by attrib,
00162 within a process. If attrib is NULL, then default attributes will be used.
00163 On successful completion, pthread_create() will store the ID of the 
00164 created thread in the location referenced by threadhdl.
00165 */ 
00166 IMPORT_C extern int pthread_create(pthread_t *threadhdl, pthread_attr_t *attrib, 
00167                    thread_begin_routine begin_routine , void * param);  
00168 /*
00169 This function return the handle to current thread.
00170 */                    
00171 IMPORT_C extern pthread_t pthread_self ();
00172 
00173 /*
00174 Compare two thread handles
00175 */
00176 IMPORT_C extern int pthread_equal(pthread_t t1,pthread_t t2);
00177 /*
00178 Waiting for thread termination
00179 */
00180 IMPORT_C extern int pthread_join(pthread_t thHandle, void **retValPtr);
00181 /*
00182 detach from thread 
00183 */
00184 IMPORT_C extern int pthread_detach(pthread_t thrHandle);
00185 /*
00186 exit current thread
00187 */
00188 IMPORT_C extern void pthread_exit(void *retValPtr);
00189 /*
00190 Initialise the thread attributes
00191 */
00192 IMPORT_C int pthread_attr_init(pthread_attr_t *attrib);
00193 /*
00194 Destroy the thread attributes
00195 */
00196 IMPORT_C int pthread_attr_destroy(pthread_attr_t *attrib);
00197 /*
00198 Get the detach_state of the current thread
00199 */
00200 IMPORT_C int pthread_attr_getdetachstate(const pthread_attr_t *attrib,
00201                                          int *detState);
00202 /*
00203 Set the detach_state of the current thread
00204 */                                         
00205 IMPORT_C int pthread_attr_setdetachstate(pthread_attr_t *attrib, 
00206                                          int detState);                                         
00207 /*
00208 Get the stack size of the current thread
00209 */
00210 IMPORT_C int pthread_attr_getstacksize(const pthread_attr_t *attrib,
00211                                        size_t *stkSize);
00212 /*
00213 Set the stack size of the current thread
00214 */                                       
00215 IMPORT_C int pthread_attr_setstacksize(pthread_attr_t *attrib, 
00216                                        size_t stkSize);                                       
00217 
00218 /*
00219 This function ensures that a piece of initialization code is executed at most once. 
00220 The once_control argument must point to a static  variable statically initialized to PTHREAD_ONCE_INIT
00221 
00222 */
00223 typedef void (*thread_init_routine) (void);
00224 IMPORT_C extern int pthread_once (pthread_once_t * once_control, 
00225                                   thread_init_routine init_routine);
00226 
00227 /*
00228 extern int pthread_key_create (pthread_key_t * key, void (*destructor) (void *));
00229 extern int pthread_key_delete (pthread_key_t key);
00230 extern int pthread_setspecific (pthread_key_t key, const void *value);
00231 extern void* pthread_getspecific (pthread_key_t key);
00232 */
00233 
00234 /*
00235 This function initializes the mutex attribute object attr and fills it with default values for the attributes.
00236 */
00237 IMPORT_C extern int pthread_mutexattr_init (pthread_mutexattr_t * attr);
00238 
00239 /*
00240 This function destroys the mutex attribute object attr and marks it as a destroyed object.
00241 */
00242 IMPORT_C extern int pthread_mutexattr_destroy (pthread_mutexattr_t * attr);
00243 
00244 /*
00245 This function retrieves the current value of the process shared attribute in 
00246 attr and stores it in the location pointed to by pshared.
00247 */
00248 IMPORT_C extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t* attr, int* pshared);
00249 
00250 /*
00251 This function sets the current value of the process shared attribute in attr to the value specifed in the  
00252 pshared variable.
00253 */
00254 IMPORT_C extern int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr, int pshared);
00255 
00256 /*
00257 This function retrieves the current value of the mutex kind attribute in attr and 
00258 stores it in the location pointed to by type.
00259 */
00260 IMPORT_C extern int pthread_mutexattr_gettype (pthread_mutexattr_t * attr, int *type);
00261 
00262 
00263 /*
00264 This function sets the current value of the mutex kind attribute in attr to the value specifed in the  type variable.
00265 */
00266 IMPORT_C extern int pthread_mutexattr_settype (pthread_mutexattr_t * attr, int type);
00267 
00268 /*
00269 This function initializes the mutex object pointed to by mutex according to the mutex attributes specified in attr. 
00270 If attr is NULL, default attributes are used instead.
00271 
00272 */
00273 IMPORT_C extern int pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * attr);
00274 
00275 /*
00276 This function destroys the mutex object.  The mutex must be unlocked on entrance.
00277 This implementation requires even statically initialized mutexes to be explicitly destroyed.
00278 */
00279 IMPORT_C extern int pthread_mutex_destroy (pthread_mutex_t * mutex);
00280 
00281 /*
00282 This function locks the given mutex. If the mutex is currently
00283 unlocked, it becomes locked and owned by the calling thread, and
00284 this function  returns immediately. If the mutex is already
00285 locked by another thread, this function suspends the calling
00286 thread until the mutex is unlocked. 
00287 
00288 If the mutex is already locked by the calling thread, the behavior of
00289 this function depends on the kind of the mutex. If the mutex is
00290 of the  PTHREAD_MUTEX_NORMAL or PTHREAD_MUTEX_DEFAULT kind, the calling thread is suspended until the mutex
00291 is unlocked, thus effectively causing the calling thread to
00292 deadlock. If the mutex is of the PTHREAD_MUTEX_ERRORCHECK kind,
00293 this function returns immediately with the error code EDEADLK.
00294 If the mutex is of the PTHREAD_MUTEX_RECURSIVE kind, this function
00295 succeeds and returns immediately, recording the number of times the
00296 calling thread has locked the mutex. An equal number of
00297 pthread_mutex_unlock operations must be performed before the mutex
00298 returns to the unlocked state.
00299 */
00300 IMPORT_C extern int pthread_mutex_lock (pthread_mutex_t * mutex);
00301 
00302 
00303 /*
00304 This function behaves identically to pthread_mutex_lock, except that if it cannot acquire the lock before 
00305 the abs_timeout time, the call returns with the error code ETIMEDOUT. 
00306 */
00307 IMPORT_C extern int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
00308 
00309 /*
00310 This function behaves identically to pthread_mutex_lock, except that it does not 
00311 block the calling thread if the mutex is already locked by another thread (or by 
00312 the calling thread in the case of a PTHREAD_MUTEX_RECURSIVE mutex). Instead, pthread_mutex_trylock 
00313 returns immediately with the error code EAGAIN.
00314 */
00315 IMPORT_C extern int pthread_mutex_trylock (pthread_mutex_t * mutex);
00316 
00317 /*
00318 This function unlocks the given mutex. The mutex is assumed
00319 to be locked and owned by the calling thread on entrance to
00320 this function. If the mutex is of the PTHREAD_MUTEX_NORMAL or PTHREAD_MUTEX_DEFAULT kind,
00321 this function always returns it to the unlocked state. If it
00322 is of the PTHREAD_MUTEX_RECURSIVE kind, it decrements the locking count of the
00323 mutex (number of pthread_mutex_lock operations performed on it by
00324 the calling thread), and only when this count reaches zero is the
00325 mutex actually unlocked.
00326 
00327 On PTHREAD_MUTEX_ERRORCHECK mutexes, this function actually checks
00328 at run-time that the mutex is locked on entrance, and that it was
00329 locked by the same thread that is now calling pthread_mutex_unlock.
00330 If these conditions are not met, an error code is returned and the
00331 mutex remains unchanged.  
00332 */
00333 IMPORT_C extern int pthread_mutex_unlock (pthread_mutex_t * mutex);
00334 
00335 /*
00336 This function initializes the condition attribute object attr and 
00337 sets it with default values for the attributes. 
00338 */
00339 IMPORT_C extern int pthread_condattr_init (pthread_condattr_t * attr);
00340 
00341 /*
00342 This function destroys the condtion variable attribute object.
00343 */
00344 IMPORT_C extern int pthread_condattr_destroy (pthread_condattr_t * attr);
00345 /*
00346 extern int pthread_condattr_getpshared (const pthread_condattr_t * attr, int *pshared);
00347 extern int pthread_condattr_setpshared (pthread_condattr_t * attr, int pshared);
00348 */
00349 
00350 /*
00351 This function initializes the condition variable object pointed to by cond 
00352 using the attributes of the attr variable. If attr is NULL, default attributes 
00353 are used instead.
00354 */
00355 IMPORT_C extern int pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * attr);
00356 
00357 /*
00358 This function destroys the condition variable  object. 
00359 */
00360 IMPORT_C extern int pthread_cond_destroy (pthread_cond_t * cond);
00361 
00362 /*
00363 This function atomically unlocks mutex and waits on cond, 
00364 as pthread_cond_wait does, but it also bounds the duration 
00365 of the wait. If cond has not been signalled within the amount 
00366 of time specified by abstime, the mutex mutex is re-acquired and 
00367 pthread_cond_timedwait returns the error ETIMEDOUT. The abstime 
00368 parameter specifies an absolute time, with the same origin as time(2) and gettimeofday(2). 
00369 */
00370 IMPORT_C extern int pthread_cond_timedwait (pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec *abstime);
00371 
00372 /*
00373 This function atomically unlocks the mutex (as per pthread_unlock_mutex) 
00374 and waits for the condition variable cond to be signalled. The thread 
00375 execution is suspended and does not consume any CPU time until the condition 
00376 variable is signalled. The mutex must be locked by the calling thread on entrance 
00377 to pthread_cond_wait. Before returning to the calling thread, pthread_cond_wait 
00378 re-acquires mutex (as per pthread_lock_mutex).
00379 */
00380 IMPORT_C extern int pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex);
00381 
00382 /*
00383 This function restarts one of the threads that are waiting on the condition 
00384 variable cond. If no threads are waiting on cond, nothing happens. If several 
00385 threads are waiting on cond, exactly one is restarted. 
00386 
00387 */
00388 IMPORT_C extern int pthread_cond_signal (pthread_cond_t * cond);
00389 
00390 /*
00391 This function restarts all the threads that are waiting on the 
00392 condition variable cond. Nothing happens if no threads are waiting on cond. 
00393 */
00394 IMPORT_C extern int pthread_cond_broadcast (pthread_cond_t * cond);
00395 
00396 
00397 typedef void (*destructor_routine)(void *);
00398 /*
00399 Thread-specific data key creation
00400 */
00401 IMPORT_C int pthread_key_create(pthread_key_t *key, destructor_routine dest);
00402 
00403 /*
00404 Thread-specific data key deletion
00405 */
00406 IMPORT_C int pthread_key_delete(pthread_key_t key);
00407 
00408 /*
00409 Setting Thread-specific data 
00410 */
00411 IMPORT_C int pthread_setspecific(pthread_key_t key, const void *val);
00412 
00413 /*
00414 Getting Thread-specific data 
00415 */
00416 IMPORT_C void* pthread_getspecific(pthread_key_t key);
00417 
00418 /*
00419 Getting contention scope 
00420 */
00421 IMPORT_C int pthread_attr_getscope(const pthread_attr_t *attrib,
00422                                    int* scope);
00423 /*
00424 Setting contention scope 
00425 */
00426 IMPORT_C int pthread_attr_setscope(pthread_attr_t *attrib,int conscope); 
00427 
00428 /*
00429 Setting scheduling policy
00430 */
00431 IMPORT_C int pthread_attr_setschedpolicy(pthread_attr_t *attrib, 
00432                                          int policy);                                  
00433 
00434 /*
00435 Getting scheduling policy of the thread
00436 */
00437 IMPORT_C int pthread_attr_getschedpolicy(const pthread_attr_t *attrib,
00438                                          int *policy);
00439 
00440 /*
00441 Getting scheduling param of the thread
00442 */
00443 IMPORT_C int pthread_attr_getschedparam(const pthread_attr_t *attrib,
00444                                         struct sched_param *param);
00445 
00446 /*
00447 Setting scheduling param
00448 */                                        
00449 IMPORT_C int pthread_attr_setschedparam(pthread_attr_t *attrib, 
00450                                         const struct sched_param *param);
00451 
00452 /*
00453 Getting dynamic scheduling param of the thread
00454 */                                        
00455 IMPORT_C int pthread_getschedparam(pthread_t thr, int *policy,
00456                                    struct sched_param *param);
00457 
00458 /*
00459 Dynamically Setting scheduling params
00460 */                                   
00461 IMPORT_C int pthread_setschedparam(pthread_t th, int policy, 
00462                                    const struct sched_param *param);
00463                                          
00464 #ifdef __cplusplus
00465 }                               /* End of  "C" */
00466 #endif                          /* __cplusplus */
00467 
00468 
00469 #endif /* PTHREAD_H */
00470 

Copyright © Nokia Corporation 2001-2008
Back to top