]>
Commit | Line | Data |
---|---|---|
33574c17 ST |
1 | /* Test condition attributes and pthread_cond_timedwait. |
2 | Copyright (C) 2000-2018 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
ad2b41bf ST |
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. | |
33574c17 ST |
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 | |
ad2b41bf | 13 | Lesser General Public License for more details. |
33574c17 | 14 | |
ad2b41bf ST |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
33574c17 ST |
18 | |
19 | #define _GNU_SOURCE | |
20 | ||
21 | #include <pthread.h> | |
22 | #include <stdio.h> | |
23 | #include <assert.h> | |
24 | #include <error.h> | |
25 | #include <errno.h> | |
26 | #include <sys/time.h> | |
27 | ||
28 | int | |
29 | main (int argc, char **argv) | |
30 | { | |
31 | error_t err; | |
32 | int i; | |
33 | pthread_condattr_t attr; | |
34 | pthread_cond_t cond; | |
35 | struct timespec ts; | |
36 | pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; | |
37 | struct timeval before, after; | |
38 | int diff; | |
39 | ||
40 | err = pthread_condattr_init (&attr); | |
41 | if (err) | |
42 | error (1, err, "pthread_condattr_init"); | |
43 | ||
44 | err = pthread_condattr_getpshared (&attr, &i); | |
45 | if (err) | |
46 | error (1, err, "pthread_condattr_getpshared"); | |
47 | assert (i == PTHREAD_PROCESS_PRIVATE); | |
48 | ||
49 | err = pthread_condattr_setpshared (&attr, PTHREAD_PROCESS_PRIVATE); | |
50 | assert (err == 0); | |
51 | ||
52 | err = pthread_cond_init (&cond, &attr); | |
53 | if (err) | |
54 | error (1, err, "pthread_cond_init"); | |
55 | ||
56 | err = pthread_condattr_destroy (&attr); | |
57 | if (err) | |
58 | error (1, err, "pthread_condattr_destroy"); | |
59 | ||
60 | gettimeofday (&before, 0); | |
61 | ts.tv_sec = before.tv_sec + 1; | |
62 | ts.tv_nsec = before.tv_usec * 1000; | |
63 | ||
64 | printf ("Starting wait @ %d\n", (int) before.tv_sec); | |
65 | ||
66 | pthread_mutex_lock (&m); | |
67 | err = pthread_cond_timedwait (&cond, &m, &ts); | |
68 | ||
69 | gettimeofday (&after, 0); | |
70 | ||
71 | printf ("End wait @ %d (err = %d)\n", (int) after.tv_sec, err); | |
72 | ||
73 | assert (err == ETIMEDOUT); | |
74 | ||
75 | diff = after.tv_sec * 1000000 + after.tv_usec | |
76 | - before.tv_sec * 1000000 - before.tv_usec; | |
77 | ||
78 | if (diff < 900000 || diff > 1100000) | |
79 | error (1, EGRATUITOUS, "pthread_cond_timedwait waited %d us", diff); | |
80 | ||
81 | return 0; | |
82 | } |