1 /* Post a semaphore. Generic version.
2 Copyright (C) 2005-2023 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>
24 #include <pt-internal.h>
27 __sem_post (sem_t
*sem
)
29 struct new_sem
*isem
= (struct new_sem
*) sem
;
30 int flags
= isem
->pshared
? GSYNC_SHARED
: 0;
32 #if __HAVE_64B_ATOMICS
33 uint64_t d
= atomic_load_relaxed (&isem
->data
);
37 if ((d
& SEM_VALUE_MASK
) == SEM_VALUE_MAX
)
43 while (!atomic_compare_exchange_weak_release (&isem
->data
, &d
, d
+ 1));
45 if ((d
>> SEM_NWAITERS_SHIFT
) != 0)
46 /* Wake one waiter. */
47 __lll_wake (((unsigned int *) &isem
->data
) + SEM_VALUE_OFFSET
, flags
);
49 unsigned int v
= atomic_load_relaxed (&isem
->value
);
53 if ((v
>> SEM_VALUE_SHIFT
) == SEM_VALUE_MAX
)
59 while (!atomic_compare_exchange_weak_release
60 (&isem
->value
, &v
, v
+ (1 << SEM_VALUE_SHIFT
)));
62 if ((v
& SEM_NWAITERS_MASK
) != 0)
63 /* Wake one waiter. */
64 __lll_wake (&isem
->value
, flags
);
69 libpthread_hidden_def (__sem_post
)
70 strong_alias (__sem_post
, sem_post
);