t_barrier.c revision 1.1 1 1.1 jmmv /* $NetBSD: t_barrier.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1 jmmv * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * Redistribution and use in source and binary forms, with or without
8 1.1 jmmv * modification, are permitted provided that the following conditions
9 1.1 jmmv * are met:
10 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
11 1.1 jmmv * notice, this list of conditions and the following disclaimer.
12 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
14 1.1 jmmv * documentation and/or other materials provided with the distribution.
15 1.1 jmmv *
16 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmmv */
28 1.1 jmmv
29 1.1 jmmv #include <sys/cdefs.h>
30 1.1 jmmv __COPYRIGHT("@(#) Copyright (c) 2008\
31 1.1 jmmv The NetBSD Foundation, inc. All rights reserved.");
32 1.1 jmmv __RCSID("$NetBSD: t_barrier.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $");
33 1.1 jmmv
34 1.1 jmmv #include <pthread.h>
35 1.1 jmmv #include <stdio.h>
36 1.1 jmmv #include <unistd.h>
37 1.1 jmmv
38 1.1 jmmv #include <atf-c.h>
39 1.1 jmmv
40 1.1 jmmv #include "h_common.h"
41 1.1 jmmv
42 1.1 jmmv #define COUNT 5
43 1.1 jmmv
44 1.1 jmmv pthread_barrier_t barrier;
45 1.1 jmmv pthread_mutex_t mutex;
46 1.1 jmmv int serial_count;
47 1.1 jmmv int after_barrier_count;
48 1.1 jmmv
49 1.1 jmmv static void *
50 1.1 jmmv threadfunc(void *arg)
51 1.1 jmmv {
52 1.1 jmmv int which = (int)(long)arg;
53 1.1 jmmv int ret;
54 1.1 jmmv
55 1.1 jmmv printf("thread %d entering barrier\n", which);
56 1.1 jmmv ret = pthread_barrier_wait(&barrier);
57 1.1 jmmv printf("thread %d leaving barrier -> %d\n", which, ret);
58 1.1 jmmv
59 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
60 1.1 jmmv after_barrier_count++;
61 1.1 jmmv if (ret == PTHREAD_BARRIER_SERIAL_THREAD)
62 1.1 jmmv serial_count++;
63 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
64 1.1 jmmv
65 1.1 jmmv return NULL;
66 1.1 jmmv }
67 1.1 jmmv
68 1.1 jmmv ATF_TC(barrier);
69 1.1 jmmv ATF_TC_HEAD(barrier, tc)
70 1.1 jmmv {
71 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Checks barriers");
72 1.1 jmmv }
73 1.1 jmmv ATF_TC_BODY(barrier, tc)
74 1.1 jmmv {
75 1.1 jmmv int i;
76 1.1 jmmv pthread_t new[COUNT];
77 1.1 jmmv void *joinval;
78 1.1 jmmv
79 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
80 1.1 jmmv PTHREAD_REQUIRE(pthread_barrier_init(&barrier, NULL, COUNT));
81 1.1 jmmv
82 1.1 jmmv for (i = 0; i < COUNT; i++) {
83 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
84 1.1 jmmv ATF_REQUIRE_EQ(after_barrier_count, 0);
85 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
86 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new[i], NULL, threadfunc,
87 1.1 jmmv (void *)(long)i));
88 1.1 jmmv sleep(2);
89 1.1 jmmv }
90 1.1 jmmv
91 1.1 jmmv for (i = 0; i < COUNT; i++) {
92 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new[i], &joinval));
93 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
94 1.1 jmmv ATF_REQUIRE(after_barrier_count > i);
95 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
96 1.1 jmmv printf("main joined with thread %d\n", i);
97 1.1 jmmv }
98 1.1 jmmv
99 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
100 1.1 jmmv ATF_REQUIRE_EQ(after_barrier_count, COUNT);
101 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
102 1.1 jmmv ATF_REQUIRE_EQ(serial_count, 1);
103 1.1 jmmv }
104 1.1 jmmv
105 1.1 jmmv ATF_TP_ADD_TCS(tp)
106 1.1 jmmv {
107 1.1 jmmv ATF_TP_ADD_TC(tp, barrier);
108 1.1 jmmv
109 1.1 jmmv return atf_no_error();
110 1.1 jmmv }
111