| 1 | /* Copyright (C) 2002-2021 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 | |
| 25 | /* Keeping track of currently used mappings. */ |
| 26 | struct inuse_sem |
| 27 | { |
| 28 | dev_t dev; |
| 29 | ino_t ino; |
| 30 | int refcnt; |
| 31 | sem_t *sem; |
| 32 | char name[]; |
| 33 | }; |
| 34 | |
| 35 | |
| 36 | /* The search tree for existing mappings. */ |
| 37 | extern void *__sem_mappings attribute_hidden; |
| 38 | |
| 39 | /* Lock to protect the search tree. */ |
| 40 | extern int __sem_mappings_lock attribute_hidden; |
| 41 | |
| 42 | |
| 43 | /* Comparison function for search in tree with existing mappings. */ |
| 44 | extern int __sem_search (const void *a, const void *b) attribute_hidden; |
| 45 | |
| 46 | static inline void __new_sem_open_init (struct new_sem *sem, unsigned value) |
| 47 | { |
| 48 | #if __HAVE_64B_ATOMICS |
| 49 | sem->data = value; |
| 50 | #else |
| 51 | sem->value = value << SEM_VALUE_SHIFT; |
| 52 | sem->nwaiters = 0; |
| 53 | #endif |
| 54 | /* pad is used as a mutex on pre-v9 sparc and ignored otherwise. */ |
| 55 | sem->pad = 0; |
| 56 | |
| 57 | /* This always is a shared semaphore. */ |
| 58 | sem->private = FUTEX_SHARED; |
| 59 | } |
| 60 | |
| 61 | /* Prototypes of functions with multiple interfaces. */ |
| 62 | extern int __new_sem_init (sem_t *sem, int pshared, unsigned int value); |
| 63 | extern int __old_sem_init (sem_t *sem, int pshared, unsigned int value); |
| 64 | extern int __new_sem_destroy (sem_t *sem); |
| 65 | extern int __new_sem_post (sem_t *sem); |
| 66 | extern int __new_sem_wait (sem_t *sem); |
| 67 | extern int __old_sem_wait (sem_t *sem); |
| 68 | extern int __new_sem_trywait (sem_t *sem); |
| 69 | extern int __new_sem_getvalue (sem_t *sem, int *sval); |
| 70 | |
| 71 | #if __TIMESIZE == 64 |
| 72 | # define __sem_clockwait64 __sem_clockwait |
| 73 | # define __sem_timedwait64 __sem_timedwait |
| 74 | #else |
| 75 | extern int |
| 76 | __sem_clockwait64 (sem_t *sem, clockid_t clockid, |
| 77 | const struct __timespec64 *abstime); |
| 78 | libpthread_hidden_proto (__sem_clockwait64) |
| 79 | extern int |
| 80 | __sem_timedwait64 (sem_t *sem, const struct __timespec64 *abstime); |
| 81 | libpthread_hidden_proto (__sem_timedwait64) |
| 82 | #endif |
| 83 | |