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.
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.
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.
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/>. */
19 #include <semaphore.h>
25 #include <pt-internal.h>
28 __sem_post (sem_t
*sem
)
30 struct new_sem
*isem
= (struct new_sem
*) sem
;
31 int flags
= isem
->pshared
? GSYNC_SHARED
: 0;
33 #if __HAVE_64B_ATOMICS
34 uint64_t d
= atomic_load_relaxed (&isem
->data
);
38 if ((d
& SEM_VALUE_MASK
) == SEM_VALUE_MAX
)
39 return __hurd_fail (EOVERFLOW
);
41 while (!atomic_compare_exchange_weak_release (&isem
->data
, &d
, d
+ 1));
43 if ((d
>> SEM_NWAITERS_SHIFT
) != 0)
44 /* Wake one waiter. */
45 __lll_wake (((unsigned int *) &isem
->data
) + SEM_VALUE_OFFSET
, flags
);
47 unsigned int v
= atomic_load_relaxed (&isem
->value
);
51 if ((v
>> SEM_VALUE_SHIFT
) == SEM_VALUE_MAX
)
52 return __hurd_fail (EOVERFLOW
);
54 while (!atomic_compare_exchange_weak_release
55 (&isem
->value
, &v
, v
+ (1 << SEM_VALUE_SHIFT
)));
57 if ((v
& SEM_NWAITERS_MASK
) != 0)
58 /* Wake one waiter. */
59 __lll_wake (&isem
->value
, flags
);
64 libpthread_hidden_def (__sem_post
)
65 strong_alias (__sem_post
, sem_post
);