From: Rich Felker Date: Fri, 11 Mar 2011 02:34:19 +0000 (-0500) Subject: fix sem_open and sem_close to obey posix semantics X-Git-Tag: v0.7.0~11 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=81af503610761a69476a3adbe8341fa8b6d078aa;p=oweals%2Fmusl.git fix sem_open and sem_close to obey posix semantics multiple opens of the same named semaphore must return the same pointer, and only the last close can unmap it. thus the ugly global state keeping track of mappings. the maximum number of distinct named semaphores that can be opened is limited sufficiently small that the linear searches take trivial time, especially compared to the syscall overhead of these functions. --- diff --git a/include/limits.h b/include/limits.h index e12819ba..55ad8231 100644 --- a/include/limits.h +++ b/include/limits.h @@ -30,6 +30,7 @@ #define PTHREAD_STACK_MIN PAGE_SIZE #define PTHREAD_DESTRUCTOR_ITERATIONS 4 #define SEM_VALUE_MAX 0x7fffffff +#define SEM_NSEMS_MAX 256 /* Arbitrary numbers... */ diff --git a/src/conf/sysconf.c b/src/conf/sysconf.c index cdaeb2a6..a660257d 100644 --- a/src/conf/sysconf.c +++ b/src/conf/sysconf.c @@ -40,8 +40,8 @@ long sysconf(int name) [_SC_VERSION] = VER, [_SC_PAGE_SIZE] = PAGE_SIZE, [_SC_RTSIG_MAX] = 63, /* ?? */ - [_SC_SEM_NSEMS_MAX] = _POSIX_SEM_NSEMS_MAX, - [_SC_SEM_VALUE_MAX] = _POSIX_SEM_VALUE_MAX, + [_SC_SEM_NSEMS_MAX] = SEM_NSEMS_MAX, + [_SC_SEM_VALUE_MAX] = OFLOW, [_SC_SIGQUEUE_MAX] = -1, [_SC_TIMER_MAX] = -1, [_SC_BC_BASE_MAX] = _POSIX2_BC_BASE_MAX, @@ -215,8 +215,8 @@ long sysconf(int name) } else if (values[name] == VER) { return _POSIX_VERSION; } else if (values[name] == OFLOW) { - return ARG_MAX; - } else { - return values[name]; + if (name == _SC_ARG_MAX) return ARG_MAX; + if (name == _SC_SEM_VALUE_MAX) return SEM_VALUE_MAX; } + return values[name]; } diff --git a/src/thread/sem_close.c b/src/thread/sem_close.c deleted file mode 100644 index 036ee540..00000000 --- a/src/thread/sem_close.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -int sem_close(sem_t *sem) -{ - return munmap(sem, sizeof *sem); -} diff --git a/src/thread/sem_open.c b/src/thread/sem_open.c index 55d5ef6b..6fff71a8 100644 --- a/src/thread/sem_open.c +++ b/src/thread/sem_open.c @@ -9,32 +9,32 @@ #include #include #include +#include +#include -static void *find_map(int fd) +static struct { + ino_t ino; + sem_t *sem; + int refcnt; +} *semtab; + +static int semcnt; +static pthread_spinlock_t lock; +static pthread_once_t once; + +static void init() { - char c; - struct stat st; - FILE *f; - void *addr; - unsigned long long off, ino; - char buf[100]; + semtab = calloc(sizeof *semtab, SEM_NSEMS_MAX); +} - if (fstat(fd, &st) < 0) return 0; - if (!(f = fopen("/proc/self/maps", "rb"))) return 0; - - while (fgets(buf, sizeof buf, f)) { - sscanf(buf, "%lx-%*lx %*s %llx %*x:%*x %llu /dev/shm%c", - (long *)&addr, &off, &ino, &c); - while (!strchr(buf, '\n') && fgets(buf, sizeof buf, f)); - if (c!='/') continue; - c = 0; - if (!off && st.st_ino == ino) { - fclose(f); - return addr; - } - } - fclose(f); - return 0; +static sem_t *find_map(ino_t ino) +{ + int i; + for (i=0; i