epoc32/include/stdapis/pthread.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 2 2fe1408b6811
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     1 /*
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:  POSIX thread exported header file 
    15 *
    16 */
    17 
    18 #ifndef  PTHREAD_H
    19 #define  PTHREAD_H
    20 
    21 #include <pthreadtypes.h>
    22 #include <sched.h>
    23 #include <e32def.h>
    24 #include <stddef.h>
    25 #include <time.h>
    26 #include <limits.h>
    27 #include <pthreadalias.h>
    28 
    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
    38 
    39 /* internal use*/
    40 typedef enum 
    41 {
    42     _ENeedsNormalInit =-3,
    43     _ENeedsRecursiveInit,
    44     _ENeedsErrorCheckInit,
    45     _EInitialized =1,
    46     _EDestroyed,
    47     _EInvalid,
    48 }_handle_state;
    49 
    50 enum _LockStatus
    51 {
    52     _ELockNotCreated,
    53     _ELockCreating,
    54     _ELockCreated,
    55 };
    56 
    57 enum _OnceStatus
    58 {
    59     _ENotDone,
    60     _EDoing,
    61     _EDone,
    62 };
    63 
    64 typedef int pthread_once_t;
    65 
    66 struct _pthread_mutex_t;
    67 typedef struct 
    68 {
    69     _handle_state  iState;
    70     struct _pthread_mutex_t* iPtr;
    71     int   iReentry;
    72 }pthread_mutex_t;
    73 
    74 typedef struct 
    75 {
    76     _handle_state iState;
    77     int iSharedType;
    78     int iMutexType;
    79 }pthread_mutexattr_t;
    80 
    81 
    82 struct _CondNode;
    83 struct _CondQueue
    84 {
    85   struct _CondNode*       iHead;
    86   struct _CondNode*       iTail;
    87   pthread_mutex_t iMutex;
    88 };
    89 
    90 typedef struct 
    91 {
    92   _handle_state    iState;
    93   struct _CondQueue       iQueue;
    94 }pthread_cond_t;
    95 
    96 typedef struct _pthread_condattr_t*   pthread_condattr_t;
    97 
    98 #define PTHREAD_ONCE_INIT _ENotDone
    99 
   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 }
   103 
   104 #define PTHREAD_COND_INITIALIZER { _ENeedsNormalInit, NULL,NULL }
   105 
   106 enum 
   107 { 
   108     PTHREAD_CREATE_JOINABLE = 0,  /* Default */ 
   109     PTHREAD_CREATE_DETACHED = 1
   110 };
   111 
   112 enum 
   113 { 
   114     PTHREAD_SCOPE_SYSTEM  = 1  /* Default */
   115 };
   116 
   117 enum 
   118 {  
   119     PTHREAD_CANCEL_ASYNCHRONOUS = 0,  
   120     PTHREAD_CANCEL_DEFERRED = 1  /* Default */
   121 };
   122 
   123 enum 
   124 {  
   125     PTHREAD_PROCESS_PRIVATE = 0,   
   126 };
   127 
   128 enum
   129 {
   130   PTHREAD_MUTEX_NORMAL,
   131   PTHREAD_MUTEX_RECURSIVE,
   132   PTHREAD_MUTEX_ERRORCHECK,
   133   PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
   134 };
   135 
   136 
   137 
   138 #ifdef __cplusplus
   139 extern "C"
   140 {
   141 #endif    /* __cplusplus */
   142 
   143 typedef void * (*thread_begin_routine)(void *);
   144 /*
   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.
   149 */ 
   150 IMPORT_C extern int pthread_create(pthread_t *threadhdl, pthread_attr_t *attrib, 
   151                    thread_begin_routine begin_routine , void * param);  
   152 /*
   153 This function return the handle to current thread.
   154 */                    
   155 IMPORT_C extern pthread_t pthread_self ();
   156 
   157 /*
   158 Compare two thread handles
   159 */
   160 IMPORT_C extern int pthread_equal(pthread_t t1,pthread_t t2);
   161 /*
   162 Waiting for thread termination
   163 */
   164 IMPORT_C extern int pthread_join(pthread_t thHandle, void **retValPtr);
   165 /*
   166 detach from thread 
   167 */
   168 IMPORT_C extern int pthread_detach(pthread_t thrHandle);
   169 /*
   170 exit current thread
   171 */
   172 IMPORT_C extern void pthread_exit(void *retValPtr);
   173 /*
   174 Initialise the thread attributes
   175 */
   176 IMPORT_C int pthread_attr_init(pthread_attr_t *attrib);
   177 /*
   178 Destroy the thread attributes
   179 */
   180 IMPORT_C int pthread_attr_destroy(pthread_attr_t *attrib);
   181 /*
   182 Get the detach_state of the current thread
   183 */
   184 IMPORT_C int pthread_attr_getdetachstate(const pthread_attr_t *attrib,
   185                                          int *detState);
   186 /*
   187 Set the detach_state of the current thread
   188 */                                         
   189 IMPORT_C int pthread_attr_setdetachstate(pthread_attr_t *attrib, 
   190                                          int detState);                                         
   191 /*
   192 Get the stack size of the current thread
   193 */
   194 IMPORT_C int pthread_attr_getstacksize(const pthread_attr_t *attrib,
   195                                        size_t *stkSize);
   196 /*
   197 Set the stack size of the current thread
   198 */                                       
   199 IMPORT_C int pthread_attr_setstacksize(pthread_attr_t *attrib, 
   200                                        size_t stkSize);                                       
   201 
   202 /*
   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
   205 
   206 */
   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);
   210 
   211 /*
   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);
   216 */
   217 
   218 /*
   219 This function initializes the mutex attribute object attr and fills it with default values for the attributes.
   220 */
   221 IMPORT_C extern int pthread_mutexattr_init (pthread_mutexattr_t * attr);
   222 
   223 /*
   224 This function destroys the mutex attribute object attr and marks it as a destroyed object.
   225 */
   226 IMPORT_C extern int pthread_mutexattr_destroy (pthread_mutexattr_t * attr);
   227 
   228 /*
   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.
   231 */
   232 IMPORT_C extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t* attr, int* pshared);
   233 
   234 /*
   235 This function sets the current value of the process shared attribute in attr to the value specifed in the  
   236 pshared variable.
   237 */
   238 IMPORT_C extern int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr, int pshared);
   239 
   240 /*
   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.
   243 */
   244 IMPORT_C extern int pthread_mutexattr_gettype (pthread_mutexattr_t * attr, int *type);
   245 
   246 
   247 /*
   248 This function sets the current value of the mutex kind attribute in attr to the value specifed in the  type variable.
   249 */
   250 IMPORT_C extern int pthread_mutexattr_settype (pthread_mutexattr_t * attr, int type);
   251 
   252 /*
   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.
   255 
   256 */
   257 IMPORT_C extern int pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * attr);
   258 
   259 /*
   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.
   262 */
   263 IMPORT_C extern int pthread_mutex_destroy (pthread_mutex_t * mutex);
   264 
   265 /*
   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. 
   271 
   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.
   283 */
   284 IMPORT_C extern int pthread_mutex_lock (pthread_mutex_t * mutex);
   285 
   286 
   287 /*
   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. 
   290 */
   291 IMPORT_C extern int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
   292 
   293 /*
   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.
   298 */
   299 IMPORT_C extern int pthread_mutex_trylock (pthread_mutex_t * mutex);
   300 
   301 /*
   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.
   310 
   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.  
   316 */
   317 IMPORT_C extern int pthread_mutex_unlock (pthread_mutex_t * mutex);
   318 
   319 /*
   320 This function initializes the condition attribute object attr and 
   321 sets it with default values for the attributes. 
   322 */
   323 IMPORT_C extern int pthread_condattr_init (pthread_condattr_t * /*attr*/);
   324 
   325 /*
   326 This function destroys the condtion variable attribute object.
   327 */
   328 IMPORT_C extern int pthread_condattr_destroy (pthread_condattr_t * attr);
   329 /*
   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);
   332 */
   333 
   334 /*
   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 
   337 are used instead.
   338 */
   339 IMPORT_C extern int pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * /*attr*/);
   340 
   341 /*
   342 This function destroys the condition variable  object. 
   343 */
   344 IMPORT_C extern int pthread_cond_destroy (pthread_cond_t * cond);
   345 
   346 /*
   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). 
   353 */
   354 IMPORT_C extern int pthread_cond_timedwait (pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec *abstime);
   355 
   356 /*
   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).
   363 */
   364 IMPORT_C extern int pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex);
   365 
   366 /*
   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. 
   370 
   371 */
   372 IMPORT_C extern int pthread_cond_signal (pthread_cond_t * cond);
   373 
   374 /*
   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. 
   377 */
   378 IMPORT_C extern int pthread_cond_broadcast (pthread_cond_t * cond);
   379 
   380 
   381 typedef void (*destructor_routine)(void *);
   382 /*
   383 Thread-specific data key creation
   384 */
   385 IMPORT_C int pthread_key_create(pthread_key_t *key, destructor_routine dest);
   386 
   387 /*
   388 Thread-specific data key deletion
   389 */
   390 IMPORT_C int pthread_key_delete(pthread_key_t key);
   391 
   392 /*
   393 Setting Thread-specific data 
   394 */
   395 IMPORT_C int pthread_setspecific(pthread_key_t key, const void *val);
   396 
   397 /*
   398 Getting Thread-specific data 
   399 */
   400 IMPORT_C void* pthread_getspecific(pthread_key_t key);
   401 
   402 /*
   403 Getting contention scope 
   404 */
   405 IMPORT_C int pthread_attr_getscope(const pthread_attr_t *attrib,
   406                                    int* scope);
   407 /*
   408 Setting contention scope 
   409 */
   410 IMPORT_C int pthread_attr_setscope(pthread_attr_t *attrib,int conscope); 
   411 
   412 /*
   413 Setting scheduling policy
   414 */
   415 IMPORT_C int pthread_attr_setschedpolicy(pthread_attr_t *attrib, 
   416                                          int policy);                                  
   417 
   418 /*
   419 Getting scheduling policy of the thread
   420 */
   421 IMPORT_C int pthread_attr_getschedpolicy(const pthread_attr_t *attrib,
   422                                          int *policy);
   423 
   424 /*
   425 Getting scheduling param of the thread
   426 */
   427 IMPORT_C int pthread_attr_getschedparam(const pthread_attr_t *attrib,
   428                                         struct sched_param *param);
   429 
   430 /*
   431 Setting scheduling param
   432 */                                        
   433 IMPORT_C int pthread_attr_setschedparam(pthread_attr_t *attrib, 
   434                                         const struct sched_param *param);
   435 
   436 /*
   437 Getting dynamic scheduling param of the thread
   438 */                                        
   439 IMPORT_C int pthread_getschedparam(pthread_t thr, int *policy,
   440                                    struct sched_param *param);
   441 
   442 /*
   443 Dynamically Setting scheduling params
   444 */                                   
   445 IMPORT_C int pthread_setschedparam(pthread_t th, int policy, 
   446                                    const struct sched_param *param);
   447                                          
   448 #ifdef __cplusplus
   449 }                               /* End of  "C" */
   450 #endif                          /* __cplusplus */
   451 
   452 
   453 #endif /* PTHREAD_H */
   454