Update contrib.
1 /** @file ../inc/pthread.h
5 /** @def POSIX_THREAD_KEYS_MAX
7 Represents maximum value
13 /** @def PTHREAD_STACK_MIN
15 Minimum supported stack size for a thread
21 /** @def POSIX_THREAD_THREADS_MAX
23 Thread max. Value is 64
29 /** @def PTHREAD_THREADS_MAX
31 Maximum number of threads supported per process. Value is 1024
37 /** @def SEM_VALUE_MAX
39 Semaphores have a maximum value past which they cannot be incremented. The macro SEM_VALUE_MAX is defined to be this maximum value.
45 /** @def PTHREAD_COND_INITIALIZER
47 Value is 4. Describes condition initializer.
53 /** @def SEM_NSEMS_MAX
55 The maximum number of semaphores a process can have.value is 1024
61 /** @def POSIX_SEM_NSEMS_MAX
63 Number of semaphores a process can have.
69 /** @def POSIX_THREAD_DESTRUCTOR_ITERATIONS
71 Controlling the iterations of destructors for thread-specific data.
77 /** @typedef typedef int pthread_once_t
79 Used for dynamic package initialization.
85 /** @typedef typedef struct _pthread_condattr_t* pthread_condattr_t
87 Used to identify a condition attribute object
93 /** @struct pthread_mutex_t
95 Thread mutex type. Used for mutexes.
101 /** @struct pthread_mutexattr_t
103 Used to identify a mutex attribute object.
106 @externallyDefinedApi
109 /** @struct pthread_cond_t
111 Used for condition variables.
114 @externallyDefinedApi
117 /** @fn pthread_create(pthread_t *threadhdl, pthread_attr_t *attrib, thread_begin_routine begin_routine, void *param)
122 @return If successful, the pthread_create function will return zero.
123 Otherwise an error number will be returned to
126 The pthread_create function is used to create a new thread, with attributes specified by attrib ,
129 the default attributes are used.
130 If the attributes specified by attrib are modified later, the thread's attributes are not affected.
132 successful completion pthread_create will store the ID of the created thread in the location specified by threadhdl .
134 The thread is created executing begin_routine with param as its sole argument.
135 If the begin_routine returns, the effect is as if there was an implicit call to pthread_exit using the return value of start_routine as the exit status.
136 Note that the thread in which main was originally invoked differs from this.
137 When it returns from main ,
138 the effect is as if there was an implicit call to exit using the return value of main as the exit status.
144 void *a_thread_func_1_1(void*)
151 if(pthread_create(&new;_th, NULL, a_thread_func_1_1, NULL) != 0)
153 perror("Error creating thread
167 @externallyDefinedApi
170 /** @fn pthread_self(void)
172 @return The pthread_self function returns the thread ID of the calling thread.
174 The pthread_self function returns the thread ID of the calling thread.
181 new_th2=pthread_self();
184 @see pthread_create()
191 @externallyDefinedApi
194 /** @fn pthread_equal(pthread_t t1, pthread_t t2)
197 @return The pthread_equal function will return non-zero if the thread IDs t1 and t2 correspond to the same thread, otherwise it will return zero.
199 The pthread_equal function compares the thread IDs t1 and t2 .
203 void *a_thread_func_1_2(void*)
210 pthread_t new_th1, new_th2;
211 /* Create a new thread. */
212 if(pthread_create(&new;_th1, NULL, a_thread_func_1_2, NULL) != 0)
214 perror("Error creating thread
218 /* Create another new thread. */
219 if(pthread_create(&new;_th2, NULL, a_thread_func_1_2, NULL) != 0)
221 perror("Error creating thread
225 /* Call pthread_equal() and pass to it the 2 new threads.
226 * It should return a zero value, indicating that
227 * they are not equal. */
228 if(pthread_equal(new_th1, new_th2) != 0)
230 printf("pthread_equal FAILED
237 @see pthread_create()
244 @externallyDefinedApi
247 /** @fn pthread_join(pthread_t thrHandle, void **retValPtr)
250 @return If successful, the pthread_join function will return zero.
251 Otherwise an error number will be returned to
254 The pthread_join function suspends execution of the calling thread until the target thrHandle terminates unless the target thrHandle has already terminated.
256 On return from a successful pthread_join call with a non-NULL retValPtr argument, the value passed to pthread_exit by the terminating thread is stored in the location referenced by retValPtr .
257 When a pthread_join returns successfully, the target thread has been terminated.
259 of multiple simultaneous calls to pthread_join specifying the same target thread are undefined.
265 /* Thread's function. */
266 void *a_thread_func_1_1(void*)
269 printf("Wait for 3 seconds for thread to finish execution:0);
272 printf("Waited (%d) second0, i);
280 /* Create a new thread. */
281 if(pthread_create(&new;_th, NULL, a_thread_func_1_1, NULL) != 0)
283 perror("Error creating thread
287 /* Wait for thread to return */
288 if(pthread_join(new_th, NULL) != 0)
290 perror("Error in pthread_join()
297 @see pthread_create()
303 @externallyDefinedApi
306 /** @fn pthread_detach(pthread_t thrHandle)
308 @return If successful, the pthread_detach function will return zero.
309 Otherwise an error number will be returned to
312 The pthread_detach function is used to indicate to the implementation that storage for the
313 thread thrHandle can be reclaimed when the thread terminates.
314 If thrHandle has not terminated, pthread_detach will not cause it to terminate.
315 The effect of multiple pthread_detach calls on the same target thread is unspecified.
319 void *a_thread_func_1_1(void*)
326 pthread_attr_t new_attr;
329 /* Initialize attribute */
330 if(pthread_attr_init(&new;_attr) != 0)
332 perror("Cannot initialize attribute object
336 /* Set the attribute object to be joinable */
337 if(pthread_attr_setdetachstate(&new;_attr, PTHREAD_CREATE_JOINABLE) != 0)
339 perror("Error in pthread_attr_setdetachstate()
343 /* Create the thread */
344 if(pthread_create(&new;_th, &new;_attr, a_thread_func_1_1, NULL) != 0)
346 perror("Error creating thread
350 /* Detach the thread. */
351 if(pthread_detach(new_th) != 0)
353 printf("Error detaching thread
365 @externallyDefinedApi
368 /** @fn pthread_exit(void *retValPtr)
370 @return The pthread_exit function cannot return to its caller.
372 The pthread_exit function terminates the calling thread and makes the value retValPtr available to any successful join with the terminating thread.
373 Thread termination does not release any application
374 visible process resources, including, but not limited to, mutexes and
375 file descriptors, nor does it perform any process level cleanup
376 actions, including, but not limited to, calling atexit routines that may exist.
378 An implicit call to pthread_exit is made when a thread other than the thread in which main was first invoked returns from the start routine that was used to create
380 The function's return value serves as the thread's exit status.
382 The behavior of pthread_exit is undefined if called from a cancellation handler or destructor function
383 that was invoked as the result of an implicit or explicit call to pthread_exit .
385 After a thread has terminated, the result of access to local (auto)
386 variables of the thread is undefined.
387 Thus, references to local variables
388 of the exiting thread should not be used for the pthread_exit retValPtr parameter value.
390 The process will exit with an exit status of 0 after the last thread has
392 The behavior is as if the implementation called exit with a zero argument at thread termination time.
396 /* Thread's function. */
397 void *a_thread_func_1_1(void*)
399 /* .. Do some thing .. */
400 pthread_exit((void*)RETURN_CODE);
406 @see pthread_create()
413 @externallyDefinedApi
416 /** @fn pthread_attr_init(pthread_attr_t *attrib)
419 Note: This description also covers the following functions -
420 pthread_attr_destroy() pthread_attr_setstacksize() pthread_attr_getstacksize() pthread_attr_setdetachstate() pthread_attr_getdetachstate() pthread_attr_setschedparam() pthread_attr_getschedparam() pthread_attr_setschedpolicy() pthread_attr_getschedpolicy() pthread_attr_setscope() pthread_attr_getscope()
422 @return If successful, these functions return 0.
423 Otherwise, an error number is returned to indicate the error.
425 Thread attributes are used to specify parameters to pthread_create .
426 One attribute object can be used in multiple calls to pthread_create ,
427 with or without modifications between calls.
429 The pthread_attr_init function initializes attrib with all the default thread attributes.
431 The pthread_attr_destroy function destroys attrib .
433 The pthread_attr_set* functions set the attribute that corresponds to each function name.
435 The pthread_attr_get* functions copy the value of the attribute that corresponds to each function name
436 to the location pointed to by the second function parameter.
440 /* ***************************************************** */
441 /* Example for pthread_attr_init & pthread_attr_destroy */
442 /* ***************************************************** */
443 pthread_attr_t new_attr;
444 /* Initialize attribute */
445 if(pthread_attr_init(&new;_attr) != 0)
447 printf("Cannot initialize attribute object
449 return -1; /* or exit */
452 /* Destroy attribute */
453 if(pthread_attr_destroy(&new;_attr) != 0)
455 perror("Cannot destroy the attribute object
457 return -1; /* or exit*/
462 /* ***************************************************** */
463 /* Example for pthread_attr_getdetachstate */
464 /* ***************************************************** */
465 pthread_attr_t new_attr;
467 /* Initialize attribute */
468 if(pthread_attr_init(&new;_attr) != 0)
470 printf("Cannot initialize attribute object
474 if(pthread_attr_getdetachstate(&new;_attr, &detach;_state) != 0)
476 printf("Cannot get the state
480 if(detach_state == PTHREAD_CREATE_JOINABLE)
482 printf("State is joinable
487 printf("State is detached
491 /* Destroy attribute */
495 /* ***************************************************** */
496 /* Example for pthread_attr_getschedpolicy */
497 /* ***************************************************** */
498 pthread_attr_t new_attr;
501 /* Initialize attribute */
502 if(pthread_attr_init(&new;_attr) != 0)
504 printf("Cannot initialize attribute object
506 return -1; /* or exit */
508 rc = pthread_attr_getschedpolicy(new_attr, &policy;);
510 printf("pthread_attr_getschedpolicy failed
516 printf("Scheduling policy: SCHED_FIFO 0);
519 printf("Scheduling policy: SCHED_RR
523 printf("Scheduling policy: SCHED_OTHER
528 /* Destroy attribute */
532 /* ***************************************************** */
533 /* Example for pthread_attr_getscope */
534 /* ***************************************************** */
535 pthread_attr_t new_attr;
538 /* Initialize attribute */
539 if(pthread_attr_init(&new;_attr) != 0)
541 printf("Cannot initialize attribute object
543 return -1; /* or exit */
545 rc = pthread_attr_getscope(new_attr, &scope;);
547 printf("pthread_attr_getscope failed");
552 printf("scope: System
556 printf("scope: Process
561 /* Destroy attribute */
565 /* ***************************************************** */
566 /* Example for pthread_attr_getstacksize */
567 /* ***************************************************** */
568 pthread_attr_t new_attr;
571 /* Initialize attribute */
572 if(pthread_attr_init(&new;_attr) != 0)
574 printf("Cannot initialize attribute object
576 return -1; /* or exit */
578 /* Get the default stack_size value */
579 rc = pthread_attr_getstacksize(&new;_attr, &stack;_size);
581 printf("pthread_attr_getstacksize failed");
587 /* Destroy attribute */
591 /* ***************************************************** */
592 /* Example for pthread_attr_getschedparam */
593 /* ***************************************************** */
596 struct sched_param param;
597 struct sched_param param2;
598 rc = pthread_attr_init(&attr;);
600 printf("pthread_attr_init failed
604 param.sched_priority = 50;
605 rc = pthread_attr_setschedparam(&attr;, & param);
607 printf("pthread_attr_setschedparam failed
611 rc = pthread_attr_getschedparam(&attr;, & param2);
613 printf("pthread_attr_getschedparam failed
617 /* priority will be stored in param2.sched_priority */
619 /* Destroy attribute */
623 /* ***************************************************** */
624 /* Example for pthread_attr_setdetachstate */
625 /* ***************************************************** */
626 pthread_attr_t new_attr;
628 /* Initialize attribute */
629 if(pthread_attr_init(&new;_attr) != 0)
631 perror("Cannot initialize attribute object
635 /* Set the attribute object to PTHREAD_CREATE_JOINABLE. */
636 if(pthread_attr_setdetachstate(&new;_attr, PTHREAD_CREATE_JOINABLE) != 0)
638 perror("Error in pthread_attr_setdetachstate()
643 /* Destroy attribute */
647 /* ***************************************************** */
648 /* Example for pthread_attr_setschedparam */
649 /* ***************************************************** */
652 struct sched_param param;
653 rc = pthread_attr_init(&attr;);
655 printf("pthread_attr_init failed
659 param.sched_priority = 50;
660 rc = pthread_attr_setschedparam(&attr;, & param);
662 printf("pthread_attr_setschedparam failed
667 /* Destroy attribute */
671 /* ***************************************************** */
672 /* Example for pthread_attr_setschedpolicy */
673 /* ***************************************************** */
676 int policy = SCHED_RR;
677 if(pthread_attr_init(&attr;) != 0) {
678 printf("Error on pthread_attr_init()
683 if((rc=pthread_attr_setschedpolicy(&attr;,policy)) != 0) {
684 printf("Error on pthread_attr_setschedpolicy() rc=%d
689 /* Destroy attribute */
693 /* ***************************************************** */
694 /* Example for pthread_attr_setstacksize */
695 /* ***************************************************** */
698 int policy = SCHED_RR;
699 if(pthread_attr_init(&attr;) != 0) {
700 printf("Error on pthread_attr_init()
705 if((rc=pthread_attr_setstacksize(&attr;,8192)) != 0) {
706 printf("Error on pthread_attr_setstacksize() rc=%d
711 /* Destroy attribute */
715 /* ***************************************************** */
716 /* Example for pthread_attr_setscope */
717 /* ***************************************************** */
720 /* Initialize attr */
721 rc = pthread_attr_init(&attr;);
723 perror("pthread_attr_init failed");
726 rc = pthread_attr_setscope(&attr;, PTHREAD_SCOPE_SYSTEM);
728 perror("PTHREAD_SCOPE_SYSTEM is not supported");
732 /* Destroy attribute */
735 @see pthread_create()
741 @externallyDefinedApi
744 /** @fn pthread_attr_destroy(pthread_attr_t *attrib)
746 Refer pthread_attr_init() for the documentation
747 @see pthread_create()
753 @externallyDefinedApi
756 /** @fn pthread_attr_getdetachstate(const pthread_attr_t *attrib, int *detState)
759 Refer pthread_attr_init() for the documentation
760 @see pthread_create()
766 @externallyDefinedApi
769 /** @fn pthread_attr_setdetachstate(pthread_attr_t *attrib, int detState)
772 Refer pthread_attr_init() for the documentation
773 @see pthread_create()
779 @externallyDefinedApi
782 /** @fn pthread_attr_getstacksize(const pthread_attr_t *attrib, size_t *stkSize)
785 Refer pthread_attr_init() for the documentation
786 @see pthread_create()
792 @externallyDefinedApi
795 /** @fn pthread_attr_setstacksize(pthread_attr_t *attrib, size_t stkSize)
798 Refer pthread_attr_init() for the documentation
799 @see pthread_create()
805 @externallyDefinedApi
808 /** @fn pthread_once(pthread_once_t *once_control, thread_init_routine init_routine)
811 @return If successful, the pthread_once function will return zero.
812 Otherwise an error number will be returned to
815 The first call to pthread_once by any thread in a process, with a given once_control ,
816 will call the init_routine with no arguments.
817 Subsequent calls to pthread_once with the same once_control will not call the init_routine .
818 On return from pthread_once ,
819 it is guaranteed that init_routine has completed.
820 The once_control parameter is used to determine whether the associated initialization
821 routine has been called.
823 The constant PTHREAD_ONCE_INIT is defined by header \#include \<pthread.h\> .
825 The behavior of pthread_once is undefined if once_control has automatic storage duration or is not initialized by PTHREAD_ONCE_INIT .
829 /* The init function that pthread_once calls */
830 void an_init_func_1_1()
832 printf (" Initialized ..
837 pthread_once_t once_control = PTHREAD_ONCE_INIT;
838 /* Call pthread_once, passing it the once_control */
839 pthread_once(&once;_control, an_init_func_1_1);
840 /* Call pthread_once again. The init function should not be
842 pthread_once(&once;_control, an_init_func_1_1);
849 @externallyDefinedApi
852 /** @fn pthread_mutexattr_init(pthread_mutexattr_t *attr)
854 Note: This description also covers the following functions -
855 pthread_mutexattr_destroy() pthread_mutexattr_settype() pthread_mutexattr_gettype() pthread_mutexattr_getpshared() pthread_mutexattr_setpshared()
857 @return If successful, these functions return 0.
858 Otherwise, an error number is returned to indicate the error.
860 Mutex attributes are used to specify parameters to pthread_mutex_init .
861 One attribute object can be used in multiple calls to pthread_mutex_init ,
862 with or without modifications between calls.
864 The pthread_mutexattr_init function initializes attr with all the default mutex attributes.
866 The pthread_mutexattr_destroy function destroys attr .
868 The pthread_mutexattr_set* functions set the attribute that corresponds to each function name.
870 The pthread_mutexattr_get* functions copy the value of the attribute that corresponds to each function name
871 to the location pointed to by the second function parameter.
875 pthread_mutexattr_t mta;
877 /* Initialize a mutex attributes object */
878 if((rc=pthread_mutexattr_init(&mta;)) != 0)
880 fprintf(stderr,"Cannot initialize mutex attributes object
884 /* Destroy the mutex attributes object */
885 if(pthread_mutexattr_destroy(&mta;) != 0)
887 fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d
894 pthread_mutexattr_t mta;
896 /* Initialize a mutex attributes object */
897 if(pthread_mutexattr_init(&mta;) != 0)
899 perror("Error at pthread_mutexattr_init()
903 /* The default 'pshared' attribute is PTHREAD_PROCESS_PRIVATE */
904 if(pthread_mutexattr_getpshared(&mta;, &pshared;) != 0)
906 fprintf(stderr,"Error obtaining the attribute process-shared
910 if (pshared != PTHREAD_PROCESS_PRIVATE)
911 printf (" pshared is NOT PTHREAD_PROCESS_PRIVATE
916 pthread_mutexattr_t mta;
918 /* Initialize a mutex attributes object */
919 if(pthread_mutexattr_init(&mta;) != 0)
921 perror("Error at pthread_mutexattr_init()
925 /* The default 'type' attribute is PTHREAD_MUTEX_DEFAULT */
926 if(pthread_mutexattr_gettype(&mta;, &type;) != 0)
928 fprintf(stderr,"pthread_mutexattr_gettype(): Error obtaining the attribute 'type'
932 if(type != PTHREAD_MUTEX_DEFAULT)
934 printf("FAILED: Incorrect default mutexattr 'type' value: %d
941 pthread_mutexattr_t mta;
943 /* Initialize a mutex attributes object */
944 if((rc=pthread_mutexattr_init(&mta;)) != 0)
946 fprintf(stderr,"Cannot initialize mutex attributes object
950 /* Destroy the mutex attributes object */
951 if(pthread_mutexattr_destroy(&mta;) != 0)
953 fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d
960 pthread_mutexattr_t mta;
962 /* Initialize a mutex attributes object */
963 if(pthread_mutexattr_init(&mta;) != 0)
965 perror("Error at pthread_mutexattr_init()
969 /* Set the 'pshared' attribute to PTHREAD_PROCESS_PRIVATE */
970 if((ret=pthread_mutexattr_setpshared(&mta;, PTHREAD_PROCESS_PRIVATE)) != 0)
972 printf("FAILED: Cannot set pshared attribute to PTHREAD_PROCESS_PRIVATE. Error: %d
979 pthread_mutexattr_t mta;
981 /* Initialize a mutex attributes object */
982 if(pthread_mutexattr_init(&mta;) != 0)
984 perror("Error at pthread_mutexattr_init()
988 if(pthread_mutexattr_gettype(&mta;, &type;) != 0)
990 printf("Error getting the attribute 'type'
994 if(type != PTHREAD_MUTEX_DEFAULT)
996 printf("FAILED: Default value of the 'type' attribute is not PTHREAD_MUTEX_DEFAULT
1000 if(pthread_mutexattr_settype(&mta;, PTHREAD_MUTEX_NORMAL) != 0)
1002 printf("FAILED: Error setting the attribute 'type'
1008 @see pthread_mutex_init()
1014 @externallyDefinedApi
1017 /** @fn pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
1019 Refer pthread_mutexattr_init() for the documentation
1020 @see pthread_mutex_init()
1026 @externallyDefinedApi
1029 /** @fn pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *pshared)
1032 Refer pthread_mutexattr_init() for the documentation
1033 @see pthread_mutex_init()
1039 @externallyDefinedApi
1042 /** @fn pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type)
1045 Refer pthread_mutexattr_init() for the documentation
1046 @see pthread_mutex_init()
1052 @externallyDefinedApi
1055 /** @fn pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
1057 @param type # use integer valuses between 1 and 10
1058 Refer pthread_mutexattr_init() for the documentation
1059 @see pthread_mutex_init()
1065 @externallyDefinedApi
1068 /** @fn pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
1071 @return If successful, pthread_mutex_init will return zero and put the new mutex id into mutex ,
1072 otherwise an error number will be returned to indicate the error.
1074 The pthread_mutex_init function creates a new mutex, with attributes specified with attr .
1075 If attr is NULL the default attributes are used.
1079 pthread_mutex_t mutex;
1083 /* Initialize a mutex object with the default mutex attributes */
1084 if((rc=pthread_mutex_init(&mutex;,NULL)) != 0)
1086 fprintf(stderr,"Error at pthread_mutex_init(), rc=%d0,rc);
1092 @see pthread_mutex_destroy()
1093 @see pthread_mutex_lock()
1094 @see pthread_mutex_trylock()
1095 @see pthread_mutex_unlock()
1101 @externallyDefinedApi
1104 /** @fn pthread_mutex_destroy(pthread_mutex_t *mutex)
1106 @return If successful, pthread_mutex_destroy will return zero, otherwise an error number will be returned to
1109 The pthread_mutex_destroy function frees the resources allocated for mutex .
1113 pthread_mutexattr_t mta;
1115 /* Initialize a mutex attributes object */
1116 if((rc=pthread_mutexattr_init(&mta;)) != 0) {
1117 fprintf(stderr,"Error at pthread_mutexattr_init(), rc=%d
1121 /* Destroy the mutex attributes object */
1122 if((rc=pthread_mutexattr_destroy(&mta;)) != 0) {
1123 fprintf(stderr,"Error at pthread_mutexattr_destroy(), rc=%d
1129 @see pthread_mutex_init()
1130 @see pthread_mutex_lock()
1131 @see pthread_mutex_trylock()
1132 @see pthread_mutex_unlock()
1138 @externallyDefinedApi
1141 /** @fn pthread_mutex_lock(pthread_mutex_t *mutex)
1143 @return If successful, pthread_mutex_lock will return zero, otherwise an error number will be returned to
1146 The pthread_mutex_lock function locks mutex .
1147 If the mutex is already locked, the calling thread will block until the
1148 mutex becomes available.
1152 pthread_mutex_t mutex;
1156 /* Initialize a mutex object with the default mutex attributes */
1157 if((rc=pthread_mutex_init(&mutex;,NULL)) != 0) {
1158 fprintf(stderr,"Error at pthread_mutex_init(), rc=%d
1162 /* Lock the mutex using pthread_mutex_lock() */
1163 if((rc=pthread_mutex_lock(&mutex;)) == 0) {
1164 printf(" lock is successful
1170 @see pthread_mutex_destroy()
1171 @see pthread_mutex_init()
1172 @see pthread_mutex_trylock()
1173 @see pthread_mutex_unlock()
1179 @externallyDefinedApi
1182 /** @fn pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
1185 @return If successful, pthread_mutex_timedlock will return zero, otherwise an error number will be returned to
1188 The pthread_mutex_timedlock function will lock mutex .
1189 If it is already locked the calling thread will block until
1190 the mutex becomes available or
1192 specified by abstime,
1194 The time of the timeout is an absolute time and
1195 is not relative to the current time.
1199 #include <sys/time.h>
1201 /* 3 seconds of timeout time for pthread_mutex_timedlock(). */
1202 struct timespec timeout;
1203 struct timeval currsec1;
1205 pthread_mutex_t mutex;
1208 /* Get the current time before the mutex locked. */
1209 gettimeofday(&currsec1;, NULL);
1210 /* Absolute time, not relative. */
1211 timeout.tv_sec = currsec1.tv_sec + TIMEOUT;
1212 timeout.tv_nsec = currsec1.tv_usec * 1000;
1213 printf("Timed mutex lock will block for %d seconds starting from: %ld.%06ld
1214 ", TIMEOUT, (long)currsec1.tv_sec, (long)currsec1.tv_usec);
1215 rc = pthread_mutex_timedlock(&mutex;, &timeout;);
1217 if (rc == ETIMEDOUT) {
1218 fprintf(stderr,"Thread stops waiting when time is out
1222 fprintf(stderr,"pthread_mutex_timedwait return %d
1226 fprintf(stderr,"Thread wakened up
1230 @see pthread_mutex_destroy()
1231 @see pthread_mutex_init()
1232 @see pthread_mutex_lock()
1233 @see pthread_mutex_trylock()
1234 @see pthread_mutex_unlock()
1240 @externallyDefinedApi
1243 /** @fn pthread_mutex_trylock(pthread_mutex_t *mutex)
1245 @return If successful, pthread_mutex_trylock will return zero, otherwise an error number will be returned to
1248 The pthread_mutex_trylock function locks mutex .
1249 If the mutex is already locked, pthread_mutex_trylock will not block waiting for the mutex, but will return an error condition.
1254 pthread_mutex_t mutex;
1255 rc = pthread_mutex_trylock(&mutex;);
1259 printf ("Locked successfully
1263 printf ("could not lock, try later.....
1269 @see pthread_mutex_destroy()
1270 @see pthread_mutex_init()
1271 @see pthread_mutex_lock()
1272 @see pthread_mutex_unlock()
1278 @externallyDefinedApi
1281 /** @fn pthread_mutex_unlock(pthread_mutex_t *mutex)
1283 @return If successful, pthread_mutex_unlock will return zero, otherwise an error number will be returned to
1286 If the current thread holds the lock on mutex ,
1287 then the pthread_mutex_unlock function unlocks mutex .
1291 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1295 /* Get the mutex using pthread_mutex_lock() */
1296 if((rc=pthread_mutex_lock(&mutex;)) != 0) {
1297 fprintf(stderr,"Error at pthread_mutex_lock(), rc=%d
1301 /* Release the mutex using pthread_mutex_unlock() */
1302 if((rc=pthread_mutex_unlock(&mutex;)) != 0) {
1303 printf("unlock failed
1309 @see pthread_mutex_destroy()
1310 @see pthread_mutex_init()
1311 @see pthread_mutex_lock()
1312 @see pthread_mutex_trylock()
1318 @externallyDefinedApi
1321 /** @fn pthread_condattr_init(pthread_condattr_t *)
1323 Note: This description also covers the following functions -
1324 pthread_condattr_destroy()
1326 @return If successful, these functions return 0.
1327 Otherwise, an error number is returned to indicate the error.
1329 Condition attribute objects are used to specify parameters to pthread_cond_init .
1330 Current implementation of conditional variables does not support any
1331 attributes, so these functions are not very useful.
1333 The pthread_condattr_init function initializes a condition attribute object with the default
1334 attributes.(As of now None)
1336 The pthread_condattr_destroy function destroys a condition attribute object.
1340 pthread_condattr_t condattr;
1342 /* Initialize a condition variable attributes object */
1343 if((rc=pthread_condattr_init(&condattr;)) != 0)
1345 fprintf(stderr,"Cannot initialize condition variable attributes object
1349 /* Destroy the condition variable attributes object */
1350 if(pthread_condattr_destroy(&condattr;) != 0)
1352 fprintf(stderr,"Error at pthread_condattr_destroy(), rc=%d
1358 @see pthread_cond_init()
1364 @externallyDefinedApi
1367 /** @fn pthread_condattr_destroy(pthread_condattr_t *)
1369 Refer pthread_condattr_init() for the documentation
1370 @see pthread_cond_init()
1376 @externallyDefinedApi
1379 /** @fn pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *)
1381 @param # represents the parameter attr
1382 @return If successful, the pthread_cond_init function will return zero and put the new condition variable id into cond ,
1383 otherwise an error number will be returned to indicate the error.
1385 The pthread_cond_init function creates a new condition variable referenced
1386 by cond with attributes referenced by attr. If attr is NULL, the default condition variable attributes will be used.
1387 Upon successful initialization, the state of the condition variable will be initialized.
1394 pthread_mutex_t mutex;
1395 pthread_cond_t cond;
1397 static struct testdata td;
1400 /* Initialize a condition variable object */
1401 if (pthread_cond_init(&td.cond;, NULL) != 0) {
1402 fprintf(stderr,"Fail to initialize cond
1406 /* ... Use it for wait, signal, broadcast */
1407 /* Destroy the condition variable object */
1408 if((rc=pthread_cond_destroy(&td.cond;)) != 0)
1410 fprintf(stderr,"Error at pthread_cond_destroy(), rc=%d
1417 @see pthread_cond_broadcast()
1418 @see pthread_cond_destroy()
1419 @see pthread_cond_signal()
1420 @see pthread_cond_timedwait()
1421 @see pthread_cond_wait()
1427 @externallyDefinedApi
1430 /** @fn pthread_cond_destroy(pthread_cond_t *cond)
1432 @return If successful, the pthread_cond_destroy function will return zero, otherwise an error number will be returned
1433 to indicate the error.
1435 The pthread_cond_destroy function frees the resources allocated by the condition variable cond .
1442 pthread_mutex_t mutex;
1443 pthread_cond_t cond;
1445 static struct testdata td;
1448 /* Initialize a condition variable object */
1449 if (pthread_cond_init(&td.cond;, NULL) != 0) {
1450 fprintf(stderr,"Fail to initialize cond
1454 /* ... Use it for wait, signal, broadcast */
1455 /* Destroy the condition variable object */
1456 if((rc=pthread_cond_destroy(&td.cond;)) != 0)
1458 fprintf(stderr,"Error at pthread_cond_destroy(), rc=%d
1465 @see pthread_cond_broadcast()
1466 @see pthread_cond_init()
1467 @see pthread_cond_signal()
1468 @see pthread_cond_timedwait()
1469 @see pthread_cond_wait()
1475 @externallyDefinedApi
1478 /** @fn pthread_cond_destroy(pthread_cond_t *cond)
1480 @return If successful, the pthread_cond_destroy function will return zero, otherwise an error number will be returned
1481 to indicate the error.
1483 The pthread_cond_destroy function frees the resources allocated by the condition variable cond .
1490 pthread_mutex_t mutex;
1491 pthread_cond_t cond;
1493 static struct testdata td;
1496 /* Initialize a condition variable object */
1497 if (pthread_cond_init(&td.cond;, NULL) != 0) {
1498 fprintf(stderr,"Fail to initialize cond
1502 /* ... Use it for wait, signal, broadcast */
1503 /* Destroy the condition variable object */
1504 if((rc=pthread_cond_destroy(&td.cond;)) != 0)
1506 fprintf(stderr,"Error at pthread_cond_destroy(), rc=%d
1513 @see pthread_cond_broadcast()
1514 @see pthread_cond_init()
1515 @see pthread_cond_signal()
1516 @see pthread_cond_timedwait()
1517 @see pthread_cond_wait()
1523 @externallyDefinedApi
1526 /** @fn pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
1529 @return If successful, the pthread_cond_wait function will return zero.
1530 Otherwise an error number will be returned to
1533 The pthread_cond_wait function atomically blocks the current thread waiting on the condition
1534 variable specified by cond ,
1535 and unblocks the mutex specified by mutex .
1536 The waiting thread unblocks only after another thread calls pthread_cond_signal ,
1537 or pthread_cond_broadcast with the same condition variable, and the current thread reacquires the lock
1544 pthread_mutex_t mutex;
1545 pthread_cond_t cond;
1547 static struct testdata td;
1549 /* mutex and conditional variable must be initialized */
1550 if (pthread_mutex_lock(&td.mutex;) != 0) {
1551 fprintf(stderr,"Thread failed to acquire mutex
1555 fprintf(stderr,"Thread started
1557 fprintf(stderr,"Thread is waiting for the cond
1559 rc = pthread_cond_wait(&td.cond;, &td.mutex;);
1561 fprintf(stderr,"pthread_cond_wait return %d
1565 fprintf(stderr,"Thread wakened
1567 pthread_mutex_unlock(&td.mutex;);
1570 @see pthread_cond_broadcast()
1571 @see pthread_cond_destroy()
1572 @see pthread_cond_init()
1573 @see pthread_cond_signal()
1574 @see pthread_cond_timedwait()
1580 @externallyDefinedApi
1583 /** @fn pthread_cond_signal(pthread_cond_t *cond)
1585 @return If successful, the pthread_cond_signal function will return zero, otherwise an error number will be returned
1586 to indicate the error.
1588 The pthread_cond_signal function unblocks one thread waiting for the condition variable cond .
1595 pthread_mutex_t mutex;
1596 pthread_cond_t cond;
1598 static struct testdata td;
1601 if (pthread_mutex_init(&td.mutex;, NULL) != 0) {
1602 fprintf(stderr,"Fail to initialize mutex
1606 if (pthread_cond_init(&td.cond;, NULL) != 0) {
1607 fprintf(stderr,"Fail to initialize cond
1611 /* ....... other thread wait for signal */
1612 /* Acquire the mutex to make sure that all waiters are currently
1613 blocked on pthread_cond_wait */
1614 if (pthread_mutex_lock(&td.mutex;) != 0) {
1615 fprintf(stderr,"Main: Fail to acquire mutex
1619 if (pthread_mutex_unlock(&td.mutex;) != 0) {
1620 fprintf(stderr,"Main: Fail to release mutex
1624 /* signal the condition to wake up all waiters */
1625 fprintf(stderr," signal the condition
1627 rc = pthread_cond_signal(&td.cond;);
1629 fprintf(stderr," failed to signal the condition
1636 @see pthread_cond_broadcast()
1637 @see pthread_cond_destroy()
1638 @see pthread_cond_init()
1639 @see pthread_cond_timedwait()
1640 @see pthread_cond_wait()
1646 @externallyDefinedApi
1649 /** @fn pthread_cond_broadcast(pthread_cond_t *cond)
1651 @return If successful, the pthread_cond_broadcast function will return zero, otherwise an error number will be returned
1652 to indicate the error.
1654 The pthread_cond_broadcast function unblocks all threads waiting for the condition variable cond .
1661 pthread_mutex_t mutex;
1662 pthread_cond_t cond;
1664 static struct testdata td;
1665 if (pthread_mutex_init(&td.mutex;, NULL) != 0) {
1666 fprintf(stderr,"Fail to initialize mutex
1670 if (pthread_cond_init(&td.cond;, NULL) != 0) {
1671 fprintf(stderr,"Fail to initialize cond
1675 /* ....... other thread wait for signal */
1676 /* Acquire the mutex to make sure that all waiters are currently
1677 blocked on pthread_cond_wait */
1678 if (pthread_mutex_lock(&td.mutex;) != 0) {
1679 fprintf(stderr,"Main: Fail to acquire mutex
1683 if (pthread_mutex_unlock(&td.mutex;) != 0) {
1684 fprintf(stderr,"Main: Fail to release mutex
1688 /* signal the condition to wake up all waiters */
1689 fprintf(stderr," signal the condition0);
1690 rc = pthread_cond_broadcast(&td.cond;);
1692 fprintf(stderr," failed to signal the condition
1698 @see pthread_cond_destroy()
1699 @see pthread_cond_init()
1700 @see pthread_cond_signal()
1701 @see pthread_cond_timedwait()
1702 @see pthread_cond_wait()
1708 @externallyDefinedApi
1711 /** @fn pthread_key_create(pthread_key_t *key, destructor_routine dest)
1714 @return If successful, the pthread_key_create function will store the newly created key value at the location specified by key and returns zero.
1715 Otherwise an error number will be returned to indicate
1718 The pthread_key_create function creates a thread-specific data key visible to all threads in the
1720 Key values provided by pthread_key_create are opaque objects used to locate thread-specific data.
1722 key value may be used by different threads, the values bound to the key
1723 by pthread_setspecific are maintained on a per-thread basis and persist for the life of the calling
1726 Upon key creation, the value NULL is associated with the new key in all
1728 Upon thread creation, the value NULL is associated with all
1729 defined keys in the new thread.
1731 An optional destructor function dest may be associated with each key value.
1733 thread exit, if a key value has a non-NULL destructor pointer, and the
1734 thread has a non-NULL value associated with the key, the function pointed
1735 to is called with the current associated value as its sole argument.
1737 order of destructor calls is unspecified if more than one destructor exists
1738 for a thread when it exits.
1740 If, after all the destructors have been called for all non-NULL values
1741 with associated destructors, there are still some non-NULL values with
1742 associated destructors, then the process is repeated.
1744 [PTHREAD_DESTRUCTOR_ITERATIONS] iterations of destructor calls for
1745 outstanding non-NULL values, there are still some non-NULL values with
1746 associated destructors, the implementation stops calling destructors.
1751 if(pthread_key_create(&keys;, NULL) != 0)
1753 printf("Error: pthread_key_create() failed
1759 @see pthread_getspecific()
1760 @see pthread_key_delete()
1761 @see pthread_setspecific()
1767 @externallyDefinedApi
1770 /** @fn pthread_key_delete(pthread_key_t key)
1772 @return If successful, the pthread_key_delete function will return zero.
1773 Otherwise an error number will be returned to
1776 The pthread_key_delete function deletes a thread-specific data key previously returned by pthread_key_create .
1777 The thread-specific data values associated with key need not be NULL at the time that pthread_key_delete is called.
1778 It is the responsibility of the application to free any
1779 application storage or perform any cleanup actions for data structures
1780 related to the deleted key or associated thread-specific data in any threads;
1781 this cleanup can be done either before or after pthread_key_delete is called.
1782 Any attempt to use key following the call to pthread_key_delete results in undefined behavior.
1784 The pthread_key_delete function is callable from within destructor functions.
1785 Destructor functions
1786 are not invoked by pthread_key_delete .
1787 Any destructor function that may have been associated with key will no longer be called upon thread exit.
1792 if(pthread_key_create(&keys;, NULL) != 0)
1794 printf("Error: pthread_key_create() failed
1798 if(pthread_key_delete(keys) != 0)
1800 printf("Error: pthread_key_delete() failed
1806 @see pthread_getspecific()
1807 @see pthread_key_create()
1808 @see pthread_setspecific()
1814 @externallyDefinedApi
1817 /** @fn pthread_setspecific(pthread_key_t key, const void *value)
1820 @return If successful, the pthread_setspecific function will return zero.
1821 Otherwise an error number will be returned to
1824 The pthread_setspecific function associates a thread-specific value with a key obtained via a previous call to pthread_key_create .
1825 Different threads can bind different values to the same key.
1827 typically pointers to blocks of dynamically allocated memory that have been
1828 reserved for use by the calling thread.
1830 The effect of calling pthread_setspecific with a key value not obtained from pthread_key_create or after key has been deleted with pthread_key_delete is undefined.
1832 The pthread_setspecific function may be called from a thread-specific data destructor function,
1833 however this may result in lost storage or infinite loops.
1839 if(pthread_key_create(&keys;, NULL) != 0)
1841 printf("Error: pthread_key_create() failed
1846 if(pthread_setspecific(keys, (void *)(long)(100)) != 0)
1848 printf("Error: pthread_setspecific() failed
1855 @see pthread_getspecific()
1856 @see pthread_key_create()
1857 @see pthread_key_delete()
1863 @externallyDefinedApi
1866 /** @fn pthread_getspecific(pthread_key_t key)
1868 @return The pthread_getspecific function will return the thread-specific data value associated with the given key .
1869 If no thread-specific data value is associated with key ,
1870 then the value NULL is returned.
1872 The pthread_getspecific function returns the value currently bound to the specified key on behalf of the calling thread.
1874 The effect of calling pthread_getspecific with a key value not obtained from pthread_key_create or after key has been deleted with pthread_key_delete is undefined.
1876 The pthread_getspecific function may be called from a thread-specific data destructor function.
1882 if(pthread_key_create(&keys;, NULL) != 0)
1884 printf("Error: pthread_key_create() failed0);
1888 if(pthread_setspecific(keys, (void *)(long)(100)) != 0)
1890 printf("Error: pthread_setspecific() failed
1895 rc = pthread_getspecific(keys);
1896 if(rc != (void *)(long)(100))
1898 printf("getspecific failed
1904 @see pthread_key_create()
1905 @see pthread_key_delete()
1906 @see pthread_setspecific()
1912 @externallyDefinedApi
1915 /** @fn pthread_attr_getscope(const pthread_attr_t *attrib, int *scope)
1918 Refer pthread_attr_init() for the documentation
1919 @see pthread_create()
1925 @externallyDefinedApi
1928 /** @fn pthread_attr_setscope(pthread_attr_t *attrib, int conscope)
1931 Refer pthread_attr_init() for the documentation
1932 @see pthread_create()
1938 @externallyDefinedApi
1941 /** @fn pthread_attr_setschedpolicy(pthread_attr_t *attrib, int policy)
1944 Refer pthread_attr_init() for the documentation
1945 @see pthread_create()
1951 @externallyDefinedApi
1954 /** @fn pthread_attr_getschedpolicy(const pthread_attr_t *attrib, int *policy)
1957 Refer pthread_attr_init() for the documentation
1958 @see pthread_create()
1964 @externallyDefinedApi
1967 /** @fn pthread_attr_getschedparam(const pthread_attr_t *attrib, struct sched_param *param)
1970 Refer pthread_attr_init() for the documentation
1971 @see pthread_create()
1977 @externallyDefinedApi
1980 /** @fn pthread_attr_setschedparam(pthread_attr_t *attrib, const struct sched_param *param)
1983 Refer pthread_attr_init() for the documentation
1984 @see pthread_create()
1990 @externallyDefinedApi
1993 /** @fn pthread_getschedparam(pthread_t thr, int *policy, struct sched_param *param)
1997 Refer pthread_setschedparam() for the documentation
2003 @externallyDefinedApi
2006 /** @fn pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
2010 Note: This description also covers the following functions -
2011 pthread_getschedparam()
2013 @return If successful, these functions return 0.
2014 Otherwise, an error number is returned to indicate the error.
2016 The pthread_setschedparam and pthread_getschedparam functions set and get the scheduling parameters of individual threads.
2017 The scheduling policy for a thread will be SCHED_RR (round-robin). ( SCHED_FIFO is not supported)
2018 The thread priority (accessed via param-\>sched_priority )
2019 must be at least PTHREAD_MIN_PRIORITY and no more than PTHREAD_MAX_PRIORITY(as returned by sched_get_priority_max() and sched_get_priority_min() APIs) .
2023 struct sched_param sparam;
2024 int policy, priority, policy_1;
2028 sparam.sched_priority = priority;
2029 rc = pthread_setschedparam(pthread_self(), policy, &sparam;);
2032 printf("Error at pthread_setschedparam: rc=%d
2040 struct sched_param param;
2041 pthread_t thread; // Thread which will be assigned the scheduling priority and the policy.
2042 pthread_t thread = pthread_self();
2043 param.sched_priority = 100;
2045 e = pthread_setschedparam(thread,SCHED_RR, & param);
2048 printf("setting scheduling policy and priority failed."));
2055 struct sched_param param;
2056 pthread_t thread; // Thread which will be assigned the scheduling priority and the policy.
2058 pthread_t thread = pthread_self();
2059 param.sched_priority = 100;
2061 e = pthread_setschedparam(thread,SCHED_RR, & param);
2064 printf("setting scheduling policy and priority failed."));
2067 e = pthread_getschedparam(thread,&policy;,& param);
2070 printf("getting scheduling policy and priority failed."));
2078 @externallyDefinedApi
2082 /** @fn pthread_cond_timedwait(pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec *abstime)
2086 @return If successful, the pthread_cond_timedwait function will return zero.
2087 Otherwise an error number will be returned to
2090 The pthread_cond_timedwait function atomically blocks the current thread waiting on the condition,
2091 specified by the cond,and unblocks the mutex specified by the mutex.
2093 The waiting thread unblocks only after another thread calls pthread_cond_signal,pthread_cond_broadcast with the same condition variable,
2094 or if the system time reaches the time specified in abstime,and the current thread reacquires the lock on the mutex.
2102 pthread_mutex mutex;
2103 pthread_cond_t cond;
2105 static struct testdata td;
2107 struct timespec timeout;
2108 struct timeval curtime;
2109 if(pthread_mutex_lock(&td.mutex)!=0)
2111 fprintf(stderr,"fail");
2114 if(gettimeofday(&curtime,NULL)!=0)
2116 fprintf(stderr,"fail");
2119 timeout.tv_sec=curtime.tv_sec;
2120 timeout.tv_nsec=curtime.tv_nsec;
2121 timeout.tv_sec+=TIMEOUT;
2122 fprintf(stderr,"thread is waiting on the cond");
2123 rc=pthread_cond_timedwait(&td.cond,&td.mutex,&timeout);
2128 fprintf(stderr,"thread stops when time is out");
2133 fprintf(stderr,"pthread_cond_timedwait return");
2137 fprintf(stderr,"thread wakened up");
2138 pthread_mutex_unlock(&td.mutex);
2147 @externallyDefinedApi