Line 0
Link Here
|
0 |
- |
1 |
/* Copyright (C) 2002, 2010 Free Software Foundation, Inc. |
|
|
2 |
This file is part of the GNU C Library. |
3 |
Contributed by Darren Hart <dvhltc@us.ibm.com> |
4 |
Based on test-cond1.c by Ulrich Drepper <drepper@redhat.com> |
5 |
|
6 |
The GNU C Library is free software; you can redistribute it and/or |
7 |
modify it under the terms of the GNU Lesser General Public |
8 |
License as published by the Free Software Foundation; either |
9 |
version 2.1 of the License, or (at your option) any later version. |
10 |
|
11 |
The GNU C Library is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
Lesser General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU Lesser General Public |
17 |
License along with the GNU C Library; if not, write to the Free |
18 |
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
02111-1307 USA. */ |
20 |
|
21 |
#include <error.h> |
22 |
#include <pthread.h> |
23 |
#include <stdio.h> |
24 |
#include <stdlib.h> |
25 |
|
26 |
|
27 |
static pthread_cond_t cond; |
28 |
static pthread_mutex_t mut; |
29 |
|
30 |
|
31 |
static void * |
32 |
tf (void *p) |
33 |
{ |
34 |
int err; |
35 |
|
36 |
err = pthread_mutex_lock (&mut); |
37 |
if (err != 0) |
38 |
error (EXIT_FAILURE, err, "child: cannot get mutex"); |
39 |
|
40 |
puts ("child: got mutex; signalling"); |
41 |
|
42 |
pthread_cond_signal (&cond); |
43 |
|
44 |
puts ("child: unlock"); |
45 |
|
46 |
err = pthread_mutex_unlock (&mut); |
47 |
if (err != 0) |
48 |
error (EXIT_FAILURE, err, "child: cannot unlock"); |
49 |
|
50 |
puts ("child: done"); |
51 |
|
52 |
return NULL; |
53 |
} |
54 |
|
55 |
|
56 |
static int |
57 |
do_test (void) |
58 |
{ |
59 |
pthread_t th; |
60 |
int err; |
61 |
|
62 |
pthread_mutexattr_t ma; |
63 |
pthread_condattr_t ca; |
64 |
|
65 |
printf ("&cond = %p\n&mut = %p\n", &cond, &mut); |
66 |
|
67 |
puts ("parent: setup condvar"); |
68 |
|
69 |
err = pthread_condattr_init(&ca); |
70 |
if (err != 0) |
71 |
error (EXIT_FAILURE, err, "parent: failed to init condattr"); |
72 |
err = pthread_condattr_setprotocol_np(&ca, PTHREAD_PRIO_INHERIT); |
73 |
if (err != 0) |
74 |
error (EXIT_FAILURE, err, "parent: failed to set condattr protocol"); |
75 |
err = pthread_cond_init(&cond, &ca); |
76 |
if (err != 0) |
77 |
error (EXIT_FAILURE, err, "parent: failed to init condvar"); |
78 |
|
79 |
puts ("parent: setup mutex"); |
80 |
|
81 |
err = pthread_mutexattr_init(&ma); |
82 |
if (err != 0) |
83 |
error (EXIT_FAILURE, err, "parent: failed to init mutexattr"); |
84 |
err = pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT); |
85 |
if (err != 0) |
86 |
error (EXIT_FAILURE, err, "parent: failed to set mutexattr protocol"); |
87 |
err = pthread_mutex_init(&mut, &ma); |
88 |
if (err != 0) |
89 |
error (EXIT_FAILURE, err, "parent: failed to init mutex"); |
90 |
|
91 |
puts ("parent: get mutex"); |
92 |
|
93 |
err = pthread_mutex_lock (&mut); |
94 |
if (err != 0) |
95 |
error (EXIT_FAILURE, err, "parent: cannot get mutex"); |
96 |
|
97 |
puts ("parent: create child"); |
98 |
|
99 |
err = pthread_create (&th, NULL, tf, NULL); |
100 |
if (err != 0) |
101 |
error (EXIT_FAILURE, err, "parent: cannot create thread"); |
102 |
|
103 |
puts ("parent: wait for condition"); |
104 |
|
105 |
err = pthread_cond_wait (&cond, &mut); |
106 |
if (err != 0) |
107 |
error (EXIT_FAILURE, err, "parent: cannot wait fir signal"); |
108 |
|
109 |
puts ("parent: got signal"); |
110 |
|
111 |
err = pthread_join (th, NULL); |
112 |
if (err != 0) |
113 |
error (EXIT_FAILURE, err, "parent: failed to join"); |
114 |
|
115 |
puts ("done"); |
116 |
|
117 |
return 0; |
118 |
} |
119 |
|
120 |
|
121 |
#define TEST_FUNCTION do_test () |
122 |
#include "../test-skeleton.c" |