1 /*	$OpenBSD: pthread_cond_timedwait.c,v 1.7 2013/12/21 05:39:21 guenther Exp $	*/
2 /*
3  * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4  * proven@mit.edu All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Chris Provenzano,
17  *	the University of California, Berkeley, and contributors.
18  * 4. Neither the name of Chris Provenzano, the University, nor the names of
19  *   contributors may be used to endorse or promote products derived
20  *   from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /* ==== test_pthread_cond.c =========================================
36  * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu
37  *
38  * Description : Test pthread_cond(). Run this after test_create()
39  *
40  *  1.23 94/05/04 proven
41  *      -Started coding this file.
42  */
43 
44 #include <pthread.h>
45 #include <stdio.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include "test.h"
50 
51 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
52 pthread_cond_t cond;
53 
54 static void *
55 thread_1(void * new_buf)
56 {
57 	CHECKr(pthread_mutex_lock(&mutex));
58 	CHECKr(pthread_cond_signal(&cond));
59 	CHECKr(pthread_mutex_unlock(&mutex));
60 	pthread_exit(NULL);
61 }
62 
63 static void *
64 thread_2(void * new_buf)
65 {
66 	sleep(1);
67 	CHECKr(pthread_mutex_lock(&mutex));
68 	CHECKr(pthread_cond_signal(&cond));
69 	CHECKr(pthread_mutex_unlock(&mutex));
70 	pthread_exit(NULL);
71 }
72 
73 int
74 main(int argc, char *argv[])
75 {
76 	struct timespec abstime;
77 	struct timespec begtime;
78 	struct timespec endtime;
79 	pthread_t thread;
80 	pthread_condattr_t condattr;
81 	int ret;
82 	int ix;
83 
84 	printf("pthread_cond_timedwait START\n");
85 
86 	CHECKr(pthread_condattr_init(&condattr));
87 	CHECKr(pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC));
88 	CHECKr(pthread_cond_init(&cond, &condattr));
89 
90 	CHECKr(pthread_mutex_lock(&mutex));
91 	CHECKe(clock_gettime(CLOCK_MONOTONIC, &begtime));
92 	abstime = begtime;
93 
94 	for (ix = 0; ix < 2; ix++) {
95 		abstime.tv_sec += 5;
96 
97 		/* Test a condition timeout */
98 		switch((ret = pthread_cond_timedwait(&cond, &mutex, &abstime))) {
99 		case 0:
100 			PANIC("pthread_cond_timedwait #0 failed to timeout");
101 			/* NOTREACHED */
102 		case ETIMEDOUT:
103 			/* expected behaviour */
104 			CHECKe(clock_gettime(CLOCK_MONOTONIC, &endtime));
105 			timespecsub(&endtime, &begtime, &endtime);
106 			printf("Got timeout %d in %lld.%09ld seconds\n", ix,
107 			       (long long)endtime.tv_sec, endtime.tv_nsec);
108 			break;
109 		default:
110 			DIE(ret, "pthread_cond_timedwait");
111 			/* NOTREACHED */
112 		}
113 	}
114 
115 	/* Test a normal condition signal */
116 	CHECKr(pthread_create(&thread, NULL, thread_1, NULL));
117 
118 	abstime.tv_sec += 5;
119 	CHECKr(pthread_cond_timedwait(&cond, &mutex, &abstime));
120 
121 	/* Test a normal condition signal after a sleep */
122 	CHECKr(pthread_create(&thread, NULL, thread_2, NULL));
123 
124 	pthread_yield();
125 
126 	CHECKr(pthread_cond_timedwait(&cond, &mutex, &abstime));
127 
128 	SUCCEED;
129 }
130