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 return ETIMEDOUT if the time specified by 'abstime' has passed.
10 *
11 */
12
13#define _XOPEN_SOURCE 600
14
15#include <pthread.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <sys/time.h>
20#include <errno.h>
21#include "posixtest.h"
22
23#define TIMEOUT   3
24
25struct testdata
26{
27	pthread_mutex_t mutex;
28	pthread_cond_t  cond;
29} td;
30
31void *t1_func(void *arg)
32{
33	int rc;
34	struct timespec timeout;
35	struct timeval  curtime;
36
37	if (pthread_mutex_lock(&td.mutex) != 0) {
38		fprintf(stderr,"Thread1 failed to acquire the mutex\n");
39		exit(PTS_UNRESOLVED);
40	}
41	fprintf(stderr,"Thread1 started\n");
42
43	if (gettimeofday(&curtime, NULL) !=0 ) {
44		fprintf(stderr,"Fail to get current time\n");
45		exit(PTS_UNRESOLVED);
46	}
47	timeout.tv_sec = curtime.tv_sec + TIMEOUT;
48	timeout.tv_nsec = curtime.tv_usec * 1000;
49
50	fprintf(stderr,"Thread1 is waiting for the cond for %d seconds\n", TIMEOUT);
51	rc = pthread_cond_timedwait(&td.cond, &td.mutex, &timeout);
52	if (rc == ETIMEDOUT) {
53		fprintf(stderr,"Thread1 stops waiting when time is out\n");
54		printf("Test PASSED\n");
55		exit(PTS_PASS);
56	}
57	else {
58		fprintf(stderr,"pthread_cond_timedwait return %d instead of ETIMEDOUT\n", rc);
59                printf("Test FAILED\n");
60		exit(PTS_FAIL);
61        }
62}
63
64int main()
65{
66	pthread_t  thread1;
67	int th_ret;
68
69	if (pthread_mutex_init(&td.mutex, NULL) != 0) {
70		fprintf(stderr,"Fail to initialize mutex\n");
71		return PTS_UNRESOLVED;
72	}
73	if (pthread_cond_init(&td.cond, NULL) != 0) {
74		fprintf(stderr,"Fail to initialize cond\n");
75		return PTS_UNRESOLVED;
76	}
77
78	if (pthread_create(&thread1, NULL, t1_func, NULL) != 0) {
79		fprintf(stderr,"Fail to create thread 1\n");
80		return PTS_UNRESOLVED;
81	}
82
83	fprintf(stderr,"Main: no condition is going to be met\n");
84
85	pthread_join(thread1, (void*)&th_ret);
86	return th_ret;
87}
88