Home | History | Annotate | Download | only in pthread_cond_timedwait
      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  *   Upon successful completion, a value of zero shall be returned.
     10  */
     11 
     12 #define _XOPEN_SOURCE 600
     13 
     14 #include <pthread.h>
     15 #include <stdio.h>
     16 #include <stdlib.h>
     17 #include <unistd.h>
     18 #include <sys/time.h>
     19 #include <errno.h>
     20 #include "posixtest.h"
     21 
     22 #define INTERVAL  1
     23 #define TIMEOUT   5
     24 
     25 struct testdata {
     26 	pthread_mutex_t mutex;
     27 	pthread_cond_t cond;
     28 } td;
     29 
     30 int t1_start = 0;
     31 int signaled = 0;
     32 
     33 void *t1_func(void *arg)
     34 {
     35 	int rc;
     36 	struct timespec timeout;
     37 	struct timeval curtime;
     38 
     39 	(void) arg;
     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 + TIMEOUT;
     53 	timeout.tv_nsec = curtime.tv_usec * 1000;
     54 
     55 	fprintf(stderr, "Thread1 is waiting for the cond\n");
     56 	rc = pthread_cond_timedwait(&td.cond, &td.mutex, &timeout);
     57 	if (rc != 0) {
     58 		if (rc == ETIMEDOUT) {
     59 			fprintf(stderr,
     60 				"Thread1 stops waiting when time is out\n");
     61 			exit(PTS_UNRESOLVED);
     62 		} else {
     63 			fprintf(stderr, "pthread_cond_timedwait return %d\n",
     64 				rc);
     65 			printf("Test FAILED\n");
     66 			exit(PTS_FAIL);
     67 		}
     68 	}
     69 
     70 	if (signaled == 0) {
     71 		fprintf(stderr, "Thread1 did not block on the cond at all\n");
     72 		exit(PTS_UNRESOLVED);
     73 	}
     74 	fprintf(stderr, "Thread1 wakened and got returned value 0\n");
     75 
     76 	if (pthread_mutex_unlock(&td.mutex) != 0) {
     77 		fprintf(stderr, "Thread1 failed to release the mutex\n");
     78 		exit(PTS_UNRESOLVED);
     79 	}
     80 	fprintf(stderr, "Thread1 released the mutex\n");
     81 	return NULL;
     82 }
     83 
     84 int main(void)
     85 {
     86 	pthread_t thread1;
     87 
     88 	if (pthread_mutex_init(&td.mutex, NULL) != 0) {
     89 		fprintf(stderr, "Fail to initialize mutex\n");
     90 		return PTS_UNRESOLVED;
     91 	}
     92 	if (pthread_cond_init(&td.cond, NULL) != 0) {
     93 		fprintf(stderr, "Fail to initialize cond\n");
     94 		return PTS_UNRESOLVED;
     95 	}
     96 
     97 	if (pthread_create(&thread1, NULL, t1_func, NULL) != 0) {
     98 		fprintf(stderr, "Fail to create thread 1\n");
     99 		return PTS_UNRESOLVED;
    100 	}
    101 	while (!t1_start)	/* wait for thread1 started */
    102 		usleep(100);
    103 
    104 	/* acquire the mutex released by pthread_cond_wait() within thread 1 */
    105 	if (pthread_mutex_lock(&td.mutex) != 0) {
    106 		fprintf(stderr, "Main failed to acquire mutex\n");
    107 		return PTS_UNRESOLVED;
    108 	}
    109 	if (pthread_mutex_unlock(&td.mutex) != 0) {
    110 		fprintf(stderr, "Main failed to release mutex\n");
    111 		return PTS_UNRESOLVED;
    112 	}
    113 	sleep(INTERVAL);
    114 
    115 	fprintf(stderr, "Time to wake up thread1 by signaling a condition\n");
    116 	signaled = 1;
    117 	if (pthread_cond_signal(&td.cond) != 0) {
    118 		fprintf(stderr, "Main failed to signal the condition\n");
    119 		return PTS_UNRESOLVED;
    120 	}
    121 
    122 	pthread_join(thread1, NULL);
    123 	printf("Test PASSED\n");
    124 	return PTS_PASS;
    125 }
    126