]> sourceware.org Git - glibc.git/blame - nptl/tst-once3.c
* Makefile (nptl-version): Change regexp so case sensitivity is ok.
[glibc.git] / nptl / tst-once3.c
CommitLineData
2a8a8a84
UD
1/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <time.h>
24
25
26#define N 100
27
28static pthread_once_t once = PTHREAD_ONCE_INIT;
29
30static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
31static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
32
33static pthread_barrier_t bar;
34
35static int global;
36
37static void
38once_handler1 (void)
39{
40 if (pthread_mutex_lock (&mut) != 0)
41 {
42 puts ("once_handler1: mutex_lock failed");
43 exit (1);
44 }
45
46 int r = pthread_barrier_wait (&bar);
47 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
48 {
49 puts ("once_handler1: barrier_wait failed");
50 exit (1);
51 }
52
53 pthread_cond_wait (&cond, &mut);
54
55 /* We should never get here. */
56}
57
58static void
59once_handler2 (void)
60{
61 global = 1;
62}
63
64
65static void *
66tf (void *arg)
67{
68 pthread_once (&once, once_handler1);
69
70 /* We should never get here. */
71 puts ("pthread_once in tf returned");
72 exit (1);
73}
74
75
76static int
77do_test (void)
78{
79 pthread_t th;
80
81 if (pthread_barrier_init (&bar, NULL, 2) != 0)
82 {
83 puts ("barrier_init failed");
84 exit (1);
85 }
86
87 if (pthread_create (&th, NULL, tf, NULL) != 0)
88 {
89 puts ("first create failed");
90 exit (1);
91 }
92
93 int r = pthread_barrier_wait (&bar);
94 if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
95 {
96 puts ("barrier_wait failed");
97 exit (1);
98 }
99
100 if (pthread_mutex_lock (&mut) != 0)
101 {
102 puts ("mutex_lock failed");
103 exit (1);
104 }
105 /* We unlock the mutex so that we catch the case where the pthread_cond_wait
106 call incorrectly resumes and tries to get the mutex. */
107 if (pthread_mutex_unlock (&mut) != 0)
108 {
109 puts ("mutex_unlock failed");
110 exit (1);
111 }
112
113 /* Cancel the thread. */
114 if (pthread_cancel (th) != 0)
115 {
116 puts ("cancel failed");
117 exit (1);
118 }
119
120 void *result;
121 pthread_join (th, &result);
122 if (result != PTHREAD_CANCELED)
123 {
124 puts ("join didn't return PTHREAD_CANCELED");
125 exit (1);
126 }
127
128 pthread_once (&once, once_handler2);
129
130 if (global != 1)
131 {
132 puts ("global still 0");
133 exit (1);
134 }
135
136 return 0;
137}
138
139#define TEST_FUNCTION do_test ()
140#define TIMEOUT 4
141#include "../test-skeleton.c"
This page took 0.047902 seconds and 5 git commands to generate.