1 /* Copyright (C) 2002-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Ulrich Drepper <drepper@redhat.com>, 2002.
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 <futex-internal.h>
21 #include "pthreadP.h"
22 
23 #define SEM_SHM_PREFIX  "sem."
24 
__new_sem_open_init(struct new_sem * sem,unsigned value)25 static inline void __new_sem_open_init (struct new_sem *sem, unsigned value)
26 {
27 #if __HAVE_64B_ATOMICS
28   sem->data = value;
29 #else
30   sem->value = value << SEM_VALUE_SHIFT;
31   sem->nwaiters = 0;
32 #endif
33   /* pad is used as a mutex on pre-v9 sparc and ignored otherwise.  */
34   sem->pad = 0;
35 
36   /* This always is a shared semaphore.  */
37   sem->private = FUTEX_SHARED;
38 }
39 
40 /* Prototypes of functions with multiple interfaces.  */
41 extern int __new_sem_init (sem_t *sem, int pshared, unsigned int value);
42 extern int __old_sem_init (sem_t *sem, int pshared, unsigned int value);
43 extern int __new_sem_destroy (sem_t *sem);
44 extern int __new_sem_post (sem_t *sem);
45 extern int __new_sem_wait (sem_t *sem);
46 extern int __old_sem_wait (sem_t *sem);
47 extern int __new_sem_trywait (sem_t *sem);
48 extern int __new_sem_getvalue (sem_t *sem, int *sval);
49 
50 #if __TIMESIZE == 64
51 # define __sem_clockwait64 __sem_clockwait
52 # define __sem_timedwait64 __sem_timedwait
53 #else
54 extern int
55 __sem_clockwait64 (sem_t *sem, clockid_t clockid,
56                    const struct __timespec64 *abstime);
57 libc_hidden_proto (__sem_clockwait64)
58 extern int
59 __sem_timedwait64 (sem_t *sem, const struct __timespec64 *abstime);
60 libc_hidden_proto (__sem_timedwait64)
61 #endif
62