1164898Sdavidxu/*-
2164898Sdavidxu * Copyright (c) 2006, David Xu <davidxu@freebsd.org>
3164898Sdavidxu * All rights reserved.
4164898Sdavidxu *
5164898Sdavidxu * Redistribution and use in source and binary forms, with or without
6164898Sdavidxu * modification, are permitted provided that the following conditions
7164898Sdavidxu * are met:
8164898Sdavidxu * 1. Redistributions of source code must retain the above copyright
9164898Sdavidxu *    notice unmodified, this list of conditions, and the following
10164898Sdavidxu *    disclaimer.
11164898Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright
12164898Sdavidxu *    notice, this list of conditions and the following disclaimer in the
13164898Sdavidxu *    documentation and/or other materials provided with the distribution.
14164898Sdavidxu *
15164898Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16164898Sdavidxu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17164898Sdavidxu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18164898Sdavidxu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19164898Sdavidxu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20164898Sdavidxu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21164898Sdavidxu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22164898Sdavidxu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23164898Sdavidxu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24164898Sdavidxu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25164898Sdavidxu *
26164898Sdavidxu * $FreeBSD$
27164898Sdavidxu *
28164898Sdavidxu */
29164898Sdavidxu#include <pthread.h>
30164898Sdavidxu#include <stdio.h>
31253384Skevlo#include <unistd.h>
32164898Sdavidxu
33164898Sdavidxu#define NLOOPS	10
34164898Sdavidxu
35164928Sdavidxupthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
36164928Sdavidxupthread_cond_t cv = PTHREAD_COND_INITIALIZER;
37164898Sdavidxu
38164898Sdavidxuint wake;
39164898Sdavidxuint stop;
40164898Sdavidxu
41164898Sdavidxuvoid *
42164898Sdavidxuthr_routine(void *arg)
43164898Sdavidxu{
44164898Sdavidxu	pthread_mutex_lock(&m);
45164898Sdavidxu	while (wake == 0)
46164898Sdavidxu		pthread_cond_wait(&cv, &m);
47164898Sdavidxu	pthread_mutex_unlock(&m);
48164898Sdavidxu
49164898Sdavidxu	while (stop == 0)
50164898Sdavidxu		pthread_yield();
51164898Sdavidxu	return (NULL);
52164898Sdavidxu}
53164898Sdavidxu
54164898Sdavidxuint main(int argc, char **argv)
55164898Sdavidxu{
56164898Sdavidxu	pthread_t td;
57164898Sdavidxu	int i;
58164898Sdavidxu	void *result;
59164898Sdavidxu
60164898Sdavidxu	pthread_setconcurrency(1);
61164898Sdavidxu	for (i = 0; i < NLOOPS; ++i) {
62164898Sdavidxu		stop = 0;
63164898Sdavidxu		wake = 0;
64164898Sdavidxu
65164898Sdavidxu		pthread_create(&td, NULL, thr_routine, NULL);
66164898Sdavidxu		sleep(1);
67164898Sdavidxu		printf("trying: %d\n", i);
68164898Sdavidxu		pthread_mutex_lock(&m);
69164898Sdavidxu		wake = 1;
70164898Sdavidxu		pthread_cond_signal(&cv);
71164898Sdavidxu		pthread_cancel(td);
72164898Sdavidxu		pthread_mutex_unlock(&m);
73164898Sdavidxu		stop = 1;
74164898Sdavidxu		result = NULL;
75164898Sdavidxu		pthread_join(td, &result);
76164898Sdavidxu		if (result == PTHREAD_CANCELED) {
77164899Sdavidxu			printf("the condition variable implementation does not\n"
78164898Sdavidxu			       "conform to SUSv3, a thread unblocked from\n"
79164898Sdavidxu			       "condition variable still can be canceled.\n");
80164898Sdavidxu			return (1);
81164898Sdavidxu		}
82164898Sdavidxu	}
83164898Sdavidxu
84164898Sdavidxu	printf("OK\n");
85164898Sdavidxu	return (0);
86164898Sdavidxu}
87