blob: b3d758d13abbb4c733c28470150f259bfd6a1f3f [file] [log] [blame]
Garrett Cooper2c282152010-12-16 00:55:50 -08001/*
robbiew0dc07652005-06-03 16:29:48 +00002 * 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
Garrett Cooper2c282152010-12-16 00:55:50 -08005 * of this license, see the COPYING file at the top level of this
robbiew0dc07652005-06-03 16:29:48 +00006 * source tree.
7
8 * Test that pthread_cond_timedwait()
9 * shall block on a condition variable. It shall be called with mutex locked
10 * by the calling thread or undefined behavior results.
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 5
24
25struct testdata
26{
27 pthread_mutex_t mutex;
28 pthread_cond_t cond;
29} td;
30
31int t1_start = 0;
32int signaled = 0;
33
34void *t1_func(void *arg)
35{
36 int rc;
37 struct timespec timeout;
38 struct timeval curtime;
39
40 if (pthread_mutex_lock(&td.mutex) != 0) {
41 fprintf(stderr,"Thread1: Fail to acquire mutex\n");
42 exit(PTS_UNRESOLVED);
43 }
44 fprintf(stderr,"Thread1 started\n");
45 t1_start = 1; /* let main thread continue */
Garrett Cooper2c282152010-12-16 00:55:50 -080046
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -080047 if (gettimeofday(&curtime, NULL) !=0) {
robbiew0dc07652005-06-03 16:29:48 +000048 fprintf(stderr,"Fail to get current time\n");
49 exit(PTS_UNRESOLVED);
50 }
51 timeout.tv_sec = curtime.tv_sec;
52 timeout.tv_nsec = curtime.tv_usec * 1000;
53 timeout.tv_sec += TIMEOUT;
54
55 fprintf(stderr,"Thread1 is waiting for the cond\n");
56 rc = pthread_cond_timedwait(&td.cond, &td.mutex, &timeout);
Garrett Cooperdf3eb162010-11-28 22:44:32 -080057 if (rc != 0) {
robbiew0dc07652005-06-03 16:29:48 +000058 if (rc == ETIMEDOUT) {
59 fprintf(stderr,"Thread1 stops waiting when time is out\n");
60 exit(PTS_UNRESOLVED);
61 }
62 else {
63 fprintf(stderr,"pthread_cond_timedwait return %d\n", rc);
64 exit(PTS_UNRESOLVED);
65 }
66 }
Garrett Cooper2c282152010-12-16 00:55:50 -080067
robbiew0dc07652005-06-03 16:29:48 +000068 fprintf(stderr,"Thread1 wakened up\n");
Garrett Cooperdf3eb162010-11-28 22:44:32 -080069 if (signaled == 0) {
robbiew0dc07652005-06-03 16:29:48 +000070 fprintf(stderr,"Thread1 did not block on the cond at all\n");
71 printf("Test FAILED\n");
72 exit(PTS_FAIL);
73 }
74 pthread_mutex_unlock(&td.mutex);
75 return NULL;
76}
77
78int main()
79{
80 pthread_t thread1;
81
82 if (pthread_mutex_init(&td.mutex, NULL) != 0) {
83 fprintf(stderr,"Fail to initialize mutex\n");
84 return PTS_UNRESOLVED;
85 }
86 if (pthread_cond_init(&td.cond, NULL) != 0) {
87 fprintf(stderr,"Fail to initialize cond\n");
88 return PTS_UNRESOLVED;
89 }
90
91 if (pthread_create(&thread1, NULL, t1_func, NULL) != 0) {
92 fprintf(stderr,"Fail to create thread 1\n");
93 return PTS_UNRESOLVED;
94 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -080095 while (!t1_start) /* wait for thread1 started */
robbiew0dc07652005-06-03 16:29:48 +000096 usleep(100);
Garrett Cooper2c282152010-12-16 00:55:50 -080097
robbiew0dc07652005-06-03 16:29:48 +000098 /* acquire the mutex released by pthread_cond_wait() within thread 1 */
Garrett Cooper2c282152010-12-16 00:55:50 -080099 if (pthread_mutex_lock(&td.mutex) != 0) {
robbiew0dc07652005-06-03 16:29:48 +0000100 fprintf(stderr,"Main: Fail to acquire mutex\n");
101 return PTS_UNRESOLVED;
102 }
103 if (pthread_mutex_unlock(&td.mutex) != 0) {
104 fprintf(stderr,"Main: Fail to release mutex\n");
105 return PTS_UNRESOLVED;
106 }
107 sleep(1);
Garrett Cooper2c282152010-12-16 00:55:50 -0800108
robbiew0dc07652005-06-03 16:29:48 +0000109 fprintf(stderr,"Time to wake up thread1 by signaling a condition\n");
110 signaled = 1;
111 if (pthread_cond_signal(&td.cond) != 0) {
112 fprintf(stderr,"Main: Fail to signal cond\n");
113 return PTS_UNRESOLVED;
114 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800115
robbiew0dc07652005-06-03 16:29:48 +0000116 pthread_join(thread1, NULL);
117 printf("Test PASSED\n");
118 return PTS_PASS;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700119}