1/* Copyright (C) 2002-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <errno.h>
19#include <semaphore.h>
20#include <stdio.h>
21#include <unistd.h>
22
23
24static int
25do_test (void)
26{
27 sem_t s;
28
29 if (sem_init (sem: &s, pshared: 0, value: 1) == -1)
30 {
31 puts (s: "init failed");
32 return 1;
33 }
34
35 if (TEMP_FAILURE_RETRY (sem_wait (&s)) == -1)
36 {
37 puts (s: "1st wait failed");
38 return 1;
39 }
40
41 if (sem_post (sem: &s) == -1)
42 {
43 puts (s: "1st post failed");
44 return 1;
45 }
46
47 if (TEMP_FAILURE_RETRY (sem_trywait (&s)) == -1)
48 {
49 puts (s: "1st trywait failed");
50 return 1;
51 }
52
53 errno = 0;
54 if (TEMP_FAILURE_RETRY (sem_trywait (&s)) != -1)
55 {
56 puts (s: "2nd trywait succeeded");
57 return 1;
58 }
59 else if (errno != EAGAIN)
60 {
61 puts (s: "2nd trywait did not set errno to EAGAIN");
62 return 1;
63 }
64
65 if (sem_post (sem: &s) == -1)
66 {
67 puts (s: "2nd post failed");
68 return 1;
69 }
70
71 if (TEMP_FAILURE_RETRY (sem_wait (&s)) == -1)
72 {
73 puts (s: "2nd wait failed");
74 return 1;
75 }
76
77 if (sem_destroy (sem: &s) == -1)
78 {
79 puts (s: "destroy failed");
80 return 1;
81 }
82
83 return 0;
84}
85
86#define TEST_FUNCTION do_test ()
87#include "../test-skeleton.c"
88

source code of glibc/sysdeps/pthread/tst-sem1.c