source: vendor/glibc-tests/glibc/nptl/tst-cond9.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1/* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <errno.h>
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <time.h>
25#include <sys/time.h>
26
27
28static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
29static pthread_mutex_t mut = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
30
31
32static void *
33tf (void *arg)
34{
35 int err = pthread_cond_wait (&cond, &mut);
36 if (err == 0)
37 {
38 puts ("cond_wait did not fail");
39 exit (1);
40 }
41
42 if (err != EPERM)
43 {
44 printf ("cond_wait didn't return EPERM but %d\n", err);
45 exit (1);
46 }
47
48
49 /* Current time. */
50 struct timeval tv;
51 (void) gettimeofday (&tv, NULL);
52 /* +1000 seconds in correct format. */
53 struct timespec ts;
54 TIMEVAL_TO_TIMESPEC (&tv, &ts);
55 ts.tv_sec += 1000;
56
57 err = pthread_cond_timedwait (&cond, &mut, &ts);
58 if (err == 0)
59 {
60 puts ("cond_timedwait did not fail");
61 exit (1);
62 }
63
64 if (err != EPERM)
65 {
66 printf ("cond_timedwait didn't return EPERM but %d\n", err);
67 exit (1);
68 }
69
70 return (void *) 1l;
71}
72
73
74static int
75do_test (void)
76{
77 pthread_t th;
78 int err;
79
80 printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
81
82 err = pthread_cond_wait (&cond, &mut);
83 if (err == 0)
84 {
85 puts ("cond_wait did not fail");
86 exit (1);
87 }
88
89 if (err != EPERM)
90 {
91 printf ("cond_wait didn't return EPERM but %d\n", err);
92 exit (1);
93 }
94
95
96 /* Current time. */
97 struct timeval tv;
98 (void) gettimeofday (&tv, NULL);
99 /* +1000 seconds in correct format. */
100 struct timespec ts;
101 TIMEVAL_TO_TIMESPEC (&tv, &ts);
102 ts.tv_sec += 1000;
103
104 err = pthread_cond_timedwait (&cond, &mut, &ts);
105 if (err == 0)
106 {
107 puts ("cond_timedwait did not fail");
108 exit (1);
109 }
110
111 if (err != EPERM)
112 {
113 printf ("cond_timedwait didn't return EPERM but %d\n", err);
114 exit (1);
115 }
116
117 if (pthread_mutex_lock (&mut) != 0)
118 {
119 puts ("parent: mutex_lock failed");
120 exit (1);
121 }
122
123 puts ("creating thread");
124
125 if (pthread_create (&th, NULL, tf, NULL) != 0)
126 {
127 puts ("create failed");
128 exit (1);
129 }
130
131 void *r;
132 if (pthread_join (th, &r) != 0)
133 {
134 puts ("join failed");
135 exit (1);
136 }
137 if (r != (void *) 1l)
138 {
139 puts ("thread has wrong return value");
140 exit (1);
141 }
142
143 puts ("done");
144
145 return 0;
146}
147
148
149#define TEST_FUNCTION do_test ()
150#include "../test-skeleton.c"
Note: See TracBrowser for help on using the repository browser.