From 81af503610761a69476a3adbe8341fa8b6d078aa Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Thu, 10 Mar 2011 21:34:19 -0500 Subject: 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. --- src/thread/sem_open.c | 106 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 80 insertions(+), 26 deletions(-) (limited to 'src/thread/sem_open.c') 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