1 /* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * Created by: bing.wei.liu 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_cond_timedwait() 9 * shall be equivalent to pthread_cond_wait(), except that an error is returned 10 * if the absolute time specified by abstime has already been passed at the time 11 * of the call. 12 * 13 */ 14 15 #define _XOPEN_SOURCE 600 16 17 #include <pthread.h> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <unistd.h> 21 #include <sys/time.h> 22 #include <errno.h> 23 #include "posixtest.h" 24 25 #define INTERVAL 2 26 27 struct testdata { 28 pthread_mutex_t mutex; 29 pthread_cond_t cond; 30 } td; 31 32 int t1_start = 0; 33 34 void *t1_func(void *arg) 35 { 36 int rc; 37 struct timeval curtime; 38 struct timespec timeout; 39 40 (void) arg; 41 42 if (pthread_mutex_lock(&td.mutex) != 0) { 43 fprintf(stderr, "Thread1 failed to acquire the mutex\n"); 44 exit(PTS_UNRESOLVED); 45 } 46 fprintf(stderr, "Thread1 started\n"); 47 t1_start = 1; /* let main thread continue */ 48 49 if (gettimeofday(&curtime, NULL) != 0) { 50 fprintf(stderr, "Fail to get current time\n"); 51 exit(PTS_UNRESOLVED); 52 } 53 timeout.tv_sec = curtime.tv_sec; 54 timeout.tv_nsec = 0; 55 56 fprintf(stderr, "Thread1 is waiting for the cond\n"); 57 rc = pthread_cond_timedwait(&td.cond, &td.mutex, &timeout); 58 if (rc == ETIMEDOUT) { 59 fprintf(stderr, "Thread1 stops waiting when time is out\n"); 60 pthread_exit((void *)PTS_PASS); 61 } else { 62 fprintf(stderr, 63 "pthread_cond_timedwait return %d instead of ETIMEDOUT\n", 64 rc); 65 exit(PTS_FAIL); 66 } 67 } 68 69 int main(void) 70 { 71 pthread_t thread1; 72 int rc; 73 void *th_ret; 74 75 if (pthread_mutex_init(&td.mutex, NULL) != 0) { 76 fprintf(stderr, "Fail to initialize mutex\n"); 77 return PTS_UNRESOLVED; 78 } 79 if (pthread_cond_init(&td.cond, NULL) != 0) { 80 fprintf(stderr, "Fail to initialize cond\n"); 81 return PTS_UNRESOLVED; 82 } 83 84 if (pthread_create(&thread1, NULL, t1_func, NULL) != 0) { 85 fprintf(stderr, "Fail to create thread 1\n"); 86 return PTS_UNRESOLVED; 87 } 88 89 /* If the thread hasn't ended in 5 seconds, then most probably 90 * pthread_cond_timedwait is failing to function correctly. */ 91 alarm(5); 92 93 /* Wait for thread to end execution. */ 94 if (pthread_join(thread1, (void *)&th_ret) != 0) { 95 fprintf(stderr, "Could not join the thread. \n"); 96 return PTS_UNRESOLVED; 97 } 98 99 /* Make sure pthread_cond_timedwait released and re-acquired the mutex 100 * as it should. */ 101 rc = pthread_mutex_trylock(&td.mutex); 102 if (rc == 0) { 103 fprintf(stderr, 104 "Test FAILED: Did not re-acquire mutex after timedout out call to pthread_cond_timedwait\n"); 105 return PTS_FAIL; 106 } 107 108 if (pthread_mutex_unlock(&td.mutex) != 0) { 109 fprintf(stderr, "Main failed to release mutex\n"); 110 return PTS_UNRESOLVED; 111 } 112 113 switch ((long)th_ret) { 114 case PTS_PASS: 115 printf("Test PASSED\n"); 116 return PTS_PASS; 117 case PTS_FAIL: 118 printf("Test FAILED\n"); 119 return PTS_FAIL; 120 default: 121 return PTS_UNRESOLVED; 122 } 123 } 124