1 /* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * Created by: rolla.n.selbak REMOVE-THIS AT intel DOT com 4 * This file is licensed under the GPL license. For the full content 5 * of this license, see the COPYING file at the top level of this 6 * source tree. 7 8 * Test that pthread_once() 9 * 10 * Dynamic package installation. Tihe first call to pthread_once() by any 11 * thread in a process, with a given 'once_control', shall call the 12 * 'init_routine' with no arguments. Subsequent calls of pthread_once() 13 * with the same once_control shall not call the 'init_routine'. The 14 * 'once_control' paramter shall determine whether the associated 15 * initialization routine has been called. 16 * 17 * STEPS: 18 * 1.Initialize a pthread_once object 19 * 2.Call pthread_once passing it this object 20 * 3.Call pthread_once again, this time, it shouldn't execute the function 21 * passed to it. If it does, the test fails. 22 */ 23 24 #include <pthread.h> 25 #include <stdio.h> 26 #include "posixtest.h" 27 28 /* Keeps track of how many times the init function has been called. */ 29 static int init_flag; 30 31 /* The init function that pthread_once calls */ 32 void an_init_func(void) 33 { 34 init_flag++; 35 } 36 37 int main(void) 38 { 39 int ret; 40 41 pthread_once_t once_control = PTHREAD_ONCE_INIT; 42 43 init_flag = 0; 44 45 /* Call pthread_once, passing it the once_control */ 46 ret = pthread_once(&once_control, an_init_func); 47 if (ret != 0) { 48 printf("pthread_once failed\n"); 49 return PTS_UNRESOLVED; 50 } 51 /* Call pthread_once again. The init function should not be 52 * called. */ 53 ret = pthread_once(&once_control, an_init_func); 54 if (ret != 0) { 55 printf("pthread_once failed\n"); 56 return PTS_UNRESOLVED; 57 } 58 59 if (init_flag != 1) { 60 printf("Test FAILED\n"); 61 return PTS_FAIL; 62 } 63 64 printf("Test PASSED\n"); 65 return PTS_PASS; 66 67 } 68