1 /* Post a semaphore. Generic version.
2 Copyright (C) 2005-2022 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
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, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <semaphore.h>
20 #include <assert.h>
21
22 #include <hurdlock.h>
23
24 #include <pt-internal.h>
25
26 int
__sem_post(sem_t * sem)27 __sem_post (sem_t *sem)
28 {
29 struct new_sem *isem = (struct new_sem *) sem;
30 int flags = isem->pshared ? GSYNC_SHARED : 0;
31
32 #if __HAVE_64B_ATOMICS
33 uint64_t d = atomic_load_relaxed (&isem->data);
34
35 do
36 {
37 if ((d & SEM_VALUE_MASK) == SEM_VALUE_MAX)
38 {
39 errno = EOVERFLOW;
40 return -1;
41 }
42 }
43 while (!atomic_compare_exchange_weak_release (&isem->data, &d, d + 1));
44
45 if ((d >> SEM_NWAITERS_SHIFT) != 0)
46 /* Wake one waiter. */
47 __lll_wake (((unsigned int *) &isem->data) + SEM_VALUE_OFFSET, flags);
48 #else
49 unsigned int v = atomic_load_relaxed (&isem->value);
50
51 do
52 {
53 if ((v >> SEM_VALUE_SHIFT) == SEM_VALUE_MAX)
54 {
55 errno = EOVERFLOW;
56 return -1;
57 }
58 }
59 while (!atomic_compare_exchange_weak_release
60 (&isem->value, &v, v + (1 << SEM_VALUE_SHIFT)));
61
62 if ((v & SEM_NWAITERS_MASK) != 0)
63 /* Wake one waiter. */
64 __lll_wake (&isem->value, flags);
65 #endif
66
67 return 0;
68 }
69 libpthread_hidden_def (__sem_post)
70 strong_alias (__sem_post, sem_post);
71