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