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