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 27struct testdata 28{ 29 pthread_mutex_t mutex; 30 pthread_cond_t cond; 31} td; 32 33int t1_start = 0; 34 35void *t1_func(void *arg) 36{ 37 int rc; 38 struct timeval curtime; 39 struct timespec timeout; 40 41 if (pthread_mutex_lock(&td.mutex) != 0) { 42 fprintf(stderr,"Thread1 failed to acquire the mutex\n"); 43 exit(PTS_UNRESOLVED); 44 } 45 fprintf(stderr,"Thread1 started\n"); 46 t1_start = 1; /* let main thread continue */ 47 48 if (gettimeofday(&curtime, NULL) !=0 ) { 49 fprintf(stderr,"Fail to get current time\n"); 50 exit(PTS_UNRESOLVED); 51 } 52 timeout.tv_sec = curtime.tv_sec; 53 timeout.tv_nsec = 0; 54 55 fprintf(stderr,"Thread1 is waiting for the cond\n"); 56 rc = pthread_cond_timedwait(&td.cond, &td.mutex, &timeout); 57 if (rc == ETIMEDOUT) { 58 fprintf(stderr,"Thread1 stops waiting when time is out\n"); 59 pthread_exit((void*)PTS_PASS); 60 } 61 else { 62 fprintf(stderr,"pthread_cond_timedwait return %d instead of ETIMEDOUT\n", rc); 63 exit(PTS_FAIL); 64 } 65 66 return NULL; 67} 68 69int main() 70{ 71 pthread_t thread1; 72 int rc, th_ret; 73 74 if (pthread_mutex_init(&td.mutex, NULL) != 0) { 75 fprintf(stderr,"Fail to initialize mutex\n"); 76 return PTS_UNRESOLVED; 77 } 78 if (pthread_cond_init(&td.cond, NULL) != 0) { 79 fprintf(stderr,"Fail to initialize cond\n"); 80 return PTS_UNRESOLVED; 81 } 82 83 if (pthread_create(&thread1, NULL, t1_func, NULL) != 0) { 84 fprintf(stderr,"Fail to create thread 1\n"); 85 return PTS_UNRESOLVED; 86 } 87 88 /* If the thread hasn't ended in 5 seconds, then most probably 89 * pthread_cond_timedwait is failing to function correctly. */ 90 alarm(5); 91 92 /* Wait for thread to end execution. */ 93 if(pthread_join(thread1, (void*)&th_ret) != 0) 94 { 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,"Test FAILED: Did not re-acquire mutex after timedout out call to pthread_cond_timedwait\n"); 104 return PTS_FAIL; 105 } 106 107 if(th_ret == PTS_PASS) 108 { 109 printf("Test PASSED\n"); 110 return PTS_PASS; 111 } 112 else if(th_ret == PTS_FAIL) 113 { 114 printf("Test FAILED\n"); 115 return PTS_FAIL; 116 } else 117 return PTS_UNRESOLVED; 118} 119 120