diff options
author | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2021-02-03 13:50:21 -0300 |
---|---|---|
committer | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2021-02-08 14:10:42 -0300 |
commit | da4aea0b5e60ec2351367b0facee24e6035a7129 (patch) | |
tree | 7892242d802abdadf07a9e27c7af8292940630a3 /sysdeps/pthread/sem_close.c | |
parent | e9fed2438a1ff475821864f906286dc58907f06b (diff) | |
download | glibc-da4aea0b5e60ec2351367b0facee24e6035a7129.tar.gz glibc-da4aea0b5e60ec2351367b0facee24e6035a7129.tar.xz glibc-da4aea0b5e60ec2351367b0facee24e6035a7129.zip |
pthread: Refactor semaphore code
The internal semaphore list code is moved to a specific file, sem_routine.c, and the internal usage is simplified to only two functions (one to insert a new semaphore and one to remove it from the internal list). There is no need to expose the internal locking, neither how the semaphore mapping is implemented. No functional or semantic change is expected, tested on x86_64-linux-gnu.
Diffstat (limited to 'sysdeps/pthread/sem_close.c')
-rw-r--r-- | sysdeps/pthread/sem_close.c | 56 |
1 files changed, 4 insertions, 52 deletions
diff --git a/sysdeps/pthread/sem_close.c b/sysdeps/pthread/sem_close.c index 8b6c9c6dc5..6649196cac 100644 --- a/sysdeps/pthread/sem_close.c +++ b/sysdeps/pthread/sem_close.c @@ -17,65 +17,17 @@ <https://www.gnu.org/licenses/>. */ #include <errno.h> -#include <search.h> -#include <sys/mman.h> #include "semaphoreP.h" - -struct walk_closure -{ - sem_t *the_sem; - struct inuse_sem *rec; -}; - -static void -walker (const void *inodep, VISIT which, void *closure0) -{ - struct walk_closure *closure = closure0; - struct inuse_sem *nodep = *(struct inuse_sem **) inodep; - - if (nodep->sem == closure->the_sem) - closure->rec = nodep; -} - +#include <sem_routines.h> int sem_close (sem_t *sem) { - int result = 0; - - /* Get the lock. */ - lll_lock (__sem_mappings_lock, LLL_PRIVATE); - - /* Locate the entry for the mapping the caller provided. */ - struct inuse_sem *rec; - { - struct walk_closure closure = { .the_sem = sem, .rec = NULL }; - __twalk_r (__sem_mappings, walker, &closure); - rec = closure.rec; - } - if (rec != NULL) + if (!__sem_remove_mapping (sem)) { - /* Check the reference counter. If it is going to be zero, free - all the resources. */ - if (--rec->refcnt == 0) - { - /* Remove the record from the tree. */ - (void) __tdelete (rec, &__sem_mappings, __sem_search); - - result = munmap (rec->sem, sizeof (sem_t)); - - free (rec); - } - } - else - { - /* This is no valid semaphore. */ - result = -1; __set_errno (EINVAL); + return -1; } - /* Release the lock. */ - lll_unlock (__sem_mappings_lock, LLL_PRIVATE); - - return result; + return 0; } |