This patch fixes problems in KDE4, that relies on sharing (process-shared)
semaphores via mmap(). This feature is used in the wild, so if we claim
that we support process-shared semaphores, we have to implement it, too.

This changes the sem_t definition: it becomes a synonim to struct __sem
instead of being a pointer to it. This way we allow apps to have sem_t in
shared memory via sem_init() and thus share it between processes (by,
e.g., mmap()'ing the same file). Of course, this forces a major library
version bump.

Dirty games with "lock" member required to avoid exposing extra structure
(struct _spinlock) in public header. On the bright side, this diff removes
more stuff than it adds, so it have to be a good diff. :)

Tested fine on i386 with KDE 4.11.

Comments? Okays?
--
zhuk@


Index: libpthread/include/semaphore.h
===================================================================
RCS file: /cvs/src/lib/libpthread/include/semaphore.h,v
retrieving revision 1.9
diff -u -p -r1.9 semaphore.h
--- libpthread/include/semaphore.h      26 Mar 2013 14:38:08 -0000      1.9
+++ libpthread/include/semaphore.h      7 Dec 2013 09:55:23 -0000
@@ -38,10 +38,21 @@
 #define _SEMAPHORE_H_
 
 #include <sys/cdefs.h>
+#include <machine/spinlock.h>
+
+/*
+ * Could not use opaque type definition due to shared semaphores.
+ *
+ * Should contain same items at the beginning as _spinlock in rthread.h.
+ */
+struct __sem {
+       _atomic_lock_t lock;
+       volatile int waitcount;
+       volatile int value;
+       int shared;
+};
+typedef struct __sem sem_t;
 
-/* Opaque type definition. */
-struct __sem;
-typedef struct __sem *sem_t;
 struct timespec;
 
 #define SEM_FAILED      ((sem_t *)0)
Index: librthread/rthread.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread.c,v
retrieving revision 1.75
diff -u -p -r1.75 rthread.c
--- librthread/rthread.c        29 Nov 2013 16:27:40 -0000      1.75
+++ librthread/rthread.c        7 Dec 2013 09:55:23 -0000
@@ -170,7 +170,7 @@ _rthread_init(void)
        struct sigaction sa;
 
        thread->tid = getthrid();
-       thread->donesem.lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       thread->donesem.lock = _ATOMIC_LOCK_UNLOCKED;
        thread->flags |= THREAD_CANCEL_ENABLE | THREAD_CANCEL_DEFERRED |
            THREAD_ORIGINAL;
        thread->flags_lock = _SPINLOCK_UNLOCKED_ASSIGN;
@@ -404,7 +404,7 @@ pthread_create(pthread_t *threadp, const
        thread = calloc(1, sizeof(*thread));
        if (!thread)
                return (errno);
-       thread->donesem.lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       thread->donesem.lock = _ATOMIC_LOCK_UNLOCKED;
        thread->flags_lock = _SPINLOCK_UNLOCKED_ASSIGN;
        thread->fn = start_routine;
        thread->arg = arg;
Index: librthread/rthread.h
===================================================================
RCS file: /cvs/src/lib/librthread/rthread.h,v
retrieving revision 1.48
diff -u -p -r1.48 rthread.h
--- librthread/rthread.h        29 Nov 2013 16:27:40 -0000      1.48
+++ librthread/rthread.h        7 Dec 2013 09:55:23 -0000
@@ -60,13 +60,6 @@ struct stack {
        size_t  len;                    /* total size of allocated stack */
 };
 
-struct __sem {
-       struct _spinlock lock;
-       volatile int waitcount;
-       volatile int value;
-       int shared;
-};
-
 TAILQ_HEAD(pthread_queue, pthread);
 
 struct pthread_mutex {
@@ -209,8 +202,8 @@ extern struct pthread_attr _rthread_attr
 void   _spinlock(volatile struct _spinlock *);
 int    _spinlocktry(volatile struct _spinlock *);
 void   _spinunlock(volatile struct _spinlock *);
-int    _sem_wait(sem_t, int, const struct timespec *, int *);
-int    _sem_post(sem_t);
+int    _sem_wait(sem_t *, int, const struct timespec *, int *);
+int    _sem_post(sem_t *);
 
 int    _rthread_init(void);
 void   _rthread_setflag(pthread_t, int);
Index: librthread/rthread_fork.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_fork.c,v
retrieving revision 1.10
diff -u -p -r1.10 rthread_fork.c
--- librthread/rthread_fork.c   29 Nov 2013 16:27:40 -0000      1.10
+++ librthread/rthread_fork.c   7 Dec 2013 09:55:23 -0000
@@ -125,7 +125,7 @@ _dofork(int is_vfork)
        if (newid == 0) {
                /* update this thread's structure */
                me->tid = getthrid();
-               me->donesem.lock = _SPINLOCK_UNLOCKED_ASSIGN;
+               me->donesem.lock = _ATOMIC_LOCK_UNLOCKED;
                me->flags &= ~THREAD_DETACHED;
                me->flags_lock = _SPINLOCK_UNLOCKED_ASSIGN;
 
Index: librthread/rthread_sem.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_sem.c,v
retrieving revision 1.16
diff -u -p -r1.16 rthread_sem.c
--- librthread/rthread_sem.c    26 Nov 2013 11:24:43 -0000      1.16
+++ librthread/rthread_sem.c    7 Dec 2013 09:55:23 -0000
@@ -49,10 +49,16 @@
 #define SEM_MMAP_SIZE  getpagesize()
 
 /*
+ * Scary try to avoid exposing struct _spinlock to the world.
+ */
+#define sem_spinlock(l) _spinlock((struct _spinlock *)(l))
+#define sem_spinunlock(l) _spinunlock((struct _spinlock *)(l))
+
+/*
  * Internal implementation of semaphores
  */
 int
-_sem_wait(sem_t sem, int tryonly, const struct timespec *abstime,
+_sem_wait(sem_t *sem, int tryonly, const struct timespec *abstime,
     int *delayed_cancel)
 {
        void *ident = (void *)&sem->waitcount;
@@ -61,7 +67,7 @@ _sem_wait(sem_t sem, int tryonly, const 
        if (sem->shared)
                ident = SHARED_IDENT;
 
-       _spinlock(&sem->lock);
+       sem_spinlock(&sem->lock);
        if (sem->value) {
                sem->value--;
                r = 0;
@@ -71,9 +77,9 @@ _sem_wait(sem_t sem, int tryonly, const 
                sem->waitcount++;
                do {
                        r = __thrsleep(ident, CLOCK_REALTIME |
-                           _USING_TICKETS, abstime, &sem->lock.ticket,
+                           _USING_TICKETS, abstime, &sem->lock,
                            delayed_cancel);
-                       _spinlock(&sem->lock);
+                       sem_spinlock(&sem->lock);
                        /* ignore interruptions other than cancelation */
                        if (r == EINTR && (delayed_cancel == NULL ||
                            *delayed_cancel == 0))
@@ -83,13 +89,13 @@ _sem_wait(sem_t sem, int tryonly, const 
                if (r == 0)
                        sem->value--;
        }
-       _spinunlock(&sem->lock);
+       sem_spinunlock(&sem->lock);
        return (r);
 }
 
 /* always increment count */
 int
-_sem_post(sem_t sem)
+_sem_post(sem_t *sem)
 {
        void *ident = (void *)&sem->waitcount;
        int rv = 0;
@@ -97,13 +103,13 @@ _sem_post(sem_t sem)
        if (sem->shared)
                ident = SHARED_IDENT;
 
-       _spinlock(&sem->lock);
+       sem_spinlock(&sem->lock);
        sem->value++;
        if (sem->waitcount) {
                __thrwakeup(ident, 1);
                rv = 1;
        }
-       _spinunlock(&sem->lock);
+       sem_spinunlock(&sem->lock);
        return (rv);
 }
 
@@ -111,10 +117,10 @@ _sem_post(sem_t sem)
  * exported semaphores
  */
 int
-sem_init(sem_t *semp, int pshared, unsigned int value)
+sem_init(sem_t *sem, int pshared, unsigned int value)
 {
        char name[SEM_RANDOM_NAME_LEN];
-       sem_t sem, *sempshared;
+       sem_t *semshared;
        int i;
 
        if (value > SEM_VALUE_MAX) {
@@ -127,8 +133,8 @@ sem_init(sem_t *semp, int pshared, unsig
                        for (i = 0; i < SEM_RANDOM_NAME_LEN - 1; i++)
                                name[i] = arc4random_uniform(255) + 1;
                        name[SEM_RANDOM_NAME_LEN - 1] = '\0';
-                       sempshared = sem_open(name, O_CREAT | O_EXCL, 0, value);
-                       if (sempshared != SEM_FAILED)
+                       semshared = sem_open(name, O_CREAT | O_EXCL, 0, value);
+                       if (semshared != SEM_FAILED)
                                break;
                        if (errno == EEXIST)
                                continue;
@@ -139,34 +145,28 @@ sem_init(sem_t *semp, int pshared, unsig
 
                /* unnamed semaphore should not be opened twice */
                if (sem_unlink(name) == -1) {
-                       sem_close(sempshared);
+                       sem_close(semshared);
                        errno = ENOSPC;
                        return (-1);
                }
 
-               *semp = *sempshared;
-               free(sempshared);
+               memcpy(sem, semshared, sizeof(sem_t));
+               munmap(semshared, SEM_MMAP_SIZE);
+
                return (0);
        }
 
-       sem = calloc(1, sizeof(*sem));
-       if (!sem) {
-               errno = ENOSPC;
-               return (-1);
-       }
-       sem->lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       memset(sem, 0, sizeof(sem));
+       sem->lock = _ATOMIC_LOCK_UNLOCKED;
        sem->value = value;
-       *semp = sem;
 
        return (0);
 }
 
 int
-sem_destroy(sem_t *semp)
+sem_destroy(sem_t *sem)
 {
-       sem_t sem;
-
-       if (!semp || !(sem = *semp)) {
+       if (!sem) {
                errno = EINVAL;
                return (-1);
        }
@@ -179,7 +179,6 @@ sem_destroy(sem_t *semp)
                return (-1);
        }
 
-       *semp = NULL;
        if (sem->shared)
                munmap(sem, SEM_MMAP_SIZE);
        else
@@ -189,28 +188,24 @@ sem_destroy(sem_t *semp)
 }
 
 int
-sem_getvalue(sem_t *semp, int *sval)
+sem_getvalue(sem_t *sem, int *sval)
 {
-       sem_t sem;
-
-       if (!semp || !(sem = *semp)) {
+       if (!sem) {
                errno = EINVAL;
                return (-1);
        }
 
-       _spinlock(&sem->lock);
+       sem_spinlock(&sem->lock);
        *sval = sem->value;
-       _spinunlock(&sem->lock);
+       sem_spinunlock(&sem->lock);
 
        return (0);
 }
 
 int
-sem_post(sem_t *semp)
+sem_post(sem_t *sem)
 {
-       sem_t sem;
-
-       if (!semp || !(sem = *semp)) {
+       if (!sem) {
                errno = EINVAL;
                return (-1);
        }
@@ -221,13 +216,12 @@ sem_post(sem_t *semp)
 }
 
 int
-sem_wait(sem_t *semp)
+sem_wait(sem_t *sem)
 {
        pthread_t self = pthread_self();
-       sem_t sem;
        int r;
 
-       if (!semp || !(sem = *semp)) {
+       if (!sem) {
                errno = EINVAL;
                return (-1);
        }
@@ -245,13 +239,12 @@ sem_wait(sem_t *semp)
 }
 
 int
-sem_timedwait(sem_t *semp, const struct timespec *abstime)
+sem_timedwait(sem_t *sem, const struct timespec *abstime)
 {
        pthread_t self = pthread_self();
-       sem_t sem;
        int r;
 
-       if (!semp || !(sem = *semp)) {
+       if (!sem) {
                errno = EINVAL;
                return (-1);
        }
@@ -269,12 +262,11 @@ sem_timedwait(sem_t *semp, const struct 
 }
 
 int
-sem_trywait(sem_t *semp)
+sem_trywait(sem_t *sem)
 {
-       sem_t sem;
        int r;
 
-       if (!semp || !(sem = *semp)) {
+       if (!sem) {
                errno = EINVAL;
                return (-1);
        }
@@ -304,7 +296,7 @@ sem_open(const char *name, int oflag, ..
 {
        char sempath[SEM_PATH_SIZE];
        struct stat sb;
-       sem_t sem, *semp;
+       sem_t *sem;
        unsigned int value = 0;
        int created = 0, fd;
 
@@ -367,35 +359,24 @@ sem_open(const char *name, int oflag, ..
                errno = EINVAL;
                return (SEM_FAILED);
        }
-       semp = malloc(sizeof(*semp));
-       if (!semp) {
-               munmap(sem, SEM_MMAP_SIZE);
-               errno = ENOSPC;
-               return (SEM_FAILED);
-       }
        if (created) {
-               sem->lock = _SPINLOCK_UNLOCKED_ASSIGN;
+               sem->lock = _ATOMIC_LOCK_UNLOCKED;
                sem->value = value;
                sem->shared = 1;
        }
-       *semp = sem;
 
-       return (semp);
+       return (sem);
 }
 
 int
-sem_close(sem_t *semp)
+sem_close(sem_t *sem)
 {
-       sem_t sem;
-
-       if (!semp || !(sem = *semp) || !sem->shared) {
+       if (!sem || !sem->shared) {
                errno = EINVAL;
                return (-1);
        }
 
-       *semp = NULL;
        munmap(sem, SEM_MMAP_SIZE);
-       free(semp);
 
        return (0);
 }
Index: librthread/shlib_version
===================================================================
RCS file: /cvs/src/lib/librthread/shlib_version,v
retrieving revision 1.17
diff -u -p -r1.17 shlib_version
--- librthread/shlib_version    13 Aug 2013 05:52:15 -0000      1.17
+++ librthread/shlib_version    7 Dec 2013 09:55:23 -0000
@@ -1,2 +1,2 @@
-major=18
+major=19
 minor=0

Reply via email to