cv_cancel1.c revision 164898
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: head/tools/regression/pthread/cv_cancel1/cv_cancel1.c 164898 2006-12-05 05:14:56Z davidxu $
27164898Sdavidxu *
28164898Sdavidxu */
29164898Sdavidxu#include <pthread.h>
30164898Sdavidxu#include <stdio.h>
31164898Sdavidxu
32164898Sdavidxu#define NLOOPS	10
33164898Sdavidxu
34164898Sdavidxupthread_mutex_t m;
35164898Sdavidxupthread_cond_t cv;
36164898Sdavidxu
37164898Sdavidxuint wake;
38164898Sdavidxuint stop;
39164898Sdavidxu
40164898Sdavidxuvoid *
41164898Sdavidxuthr_routine(void *arg)
42164898Sdavidxu{
43164898Sdavidxu	pthread_mutex_lock(&m);
44164898Sdavidxu	while (wake == 0)
45164898Sdavidxu		pthread_cond_wait(&cv, &m);
46164898Sdavidxu	pthread_mutex_unlock(&m);
47164898Sdavidxu
48164898Sdavidxu	while (stop == 0)
49164898Sdavidxu		pthread_yield();
50164898Sdavidxu	return (NULL);
51164898Sdavidxu}
52164898Sdavidxu
53164898Sdavidxuint main(int argc, char **argv)
54164898Sdavidxu{
55164898Sdavidxu	pthread_t td;
56164898Sdavidxu	int i;
57164898Sdavidxu	void *result;
58164898Sdavidxu
59164898Sdavidxu	pthread_setconcurrency(1);
60164898Sdavidxu	for (i = 0; i < NLOOPS; ++i) {
61164898Sdavidxu		stop = 0;
62164898Sdavidxu		wake = 0;
63164898Sdavidxu
64164898Sdavidxu		pthread_create(&td, NULL, thr_routine, NULL);
65164898Sdavidxu		sleep(1);
66164898Sdavidxu		printf("trying: %d\n", i);
67164898Sdavidxu		pthread_mutex_lock(&m);
68164898Sdavidxu		wake = 1;
69164898Sdavidxu		pthread_cond_signal(&cv);
70164898Sdavidxu		pthread_cancel(td);
71164898Sdavidxu		pthread_mutex_unlock(&m);
72164898Sdavidxu		stop = 1;
73164898Sdavidxu		result = NULL;
74164898Sdavidxu		pthread_join(td, &result);
75164898Sdavidxu		if (result == PTHREAD_CANCELED) {
76164898Sdavidxu			printf("the condition variable implemention does not\n"
77164898Sdavidxu			       "conform to SUSv3, a thread unblocked from\n"
78164898Sdavidxu			       "condition variable still can be canceled.\n");
79164898Sdavidxu			return (1);
80164898Sdavidxu		}
81164898Sdavidxu	}
82164898Sdavidxu
83164898Sdavidxu	printf("OK\n");
84164898Sdavidxu	return (0);
85164898Sdavidxu}
86