This is the last step to remove ticket support from librthreads.

Replace "struct _spinlock" with "_atomic_lock_t".


Index: lib/librthread/rthread.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread.c,v
retrieving revision 1.93
diff -u -p -r1.93 rthread.c
--- lib/librthread/rthread.c    3 Sep 2016 16:44:20 -0000       1.93
+++ lib/librthread/rthread.c    3 Sep 2016 19:41:54 -0000
@@ -63,15 +63,13 @@ REDIRECT_SYSCALL(thrkill);
 
 static int concurrency_level;  /* not used */
 
-struct _spinlock _SPINLOCK_UNLOCKED_ASSIGN = _SPINLOCK_UNLOCKED;
-
 int _threads_ready;
 size_t _thread_pagesize;
 struct listhead _thread_list = LIST_HEAD_INITIALIZER(_thread_list);
-struct _spinlock _thread_lock = _SPINLOCK_UNLOCKED;
+_atomic_lock_t _thread_lock = _SPINLOCK_UNLOCKED;
 static struct pthread_queue _thread_gc_list
     = TAILQ_HEAD_INITIALIZER(_thread_gc_list);
-static struct _spinlock _thread_gc_lock = _SPINLOCK_UNLOCKED;
+static _atomic_lock_t _thread_gc_lock = _SPINLOCK_UNLOCKED;
 static struct pthread _initial_thread;
 
 struct pthread_attr _rthread_attr_default = {
@@ -89,22 +87,22 @@ struct pthread_attr _rthread_attr_defaul
  * internal support functions
  */
 void
-_spinlock(volatile struct _spinlock *lock)
+_spinlock(volatile _atomic_lock_t *lock)
 {
-       while (_atomic_lock(&lock->ticket))
+       while (_atomic_lock(lock))
                sched_yield();
 }
 
 int
-_spinlocktry(volatile struct _spinlock *lock)
+_spinlocktry(volatile _atomic_lock_t *lock)
 {
-       return 0 == _atomic_lock(&lock->ticket);
+       return 0 == _atomic_lock(lock);
 }
 
 void
-_spinunlock(volatile struct _spinlock *lock)
+_spinunlock(volatile _atomic_lock_t *lock)
 {
-       lock->ticket = _ATOMIC_LOCK_UNLOCKED;
+       *lock = _ATOMIC_LOCK_UNLOCKED;
 }
 
 static void
@@ -187,9 +185,9 @@ _rthread_init(void)
        tib->tib_thread = thread;
        thread->tib = tib;
 
-       thread->donesem.lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       thread->donesem.lock = _SPINLOCK_UNLOCKED;
        tib->tib_thread_flags = TIB_THREAD_INITIAL_STACK;
-       thread->flags_lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       thread->flags_lock = _SPINLOCK_UNLOCKED;
        strlcpy(thread->name, "Main process", sizeof(thread->name));
        LIST_INSERT_HEAD(&_thread_list, thread, threads);
        _rthread_debug_init();
@@ -432,8 +430,8 @@ pthread_create(pthread_t *threadp, const
        thread = tib->tib_thread;
        memset(thread, 0, sizeof(*thread));
        thread->tib = tib;
-       thread->donesem.lock = _SPINLOCK_UNLOCKED_ASSIGN;
-       thread->flags_lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       thread->donesem.lock = _SPINLOCK_UNLOCKED;
+       thread->flags_lock = _SPINLOCK_UNLOCKED;
        thread->fn = start_routine;
        thread->arg = arg;
        tib->tib_tid = -1;
@@ -643,7 +641,7 @@ _thread_dump_info(void)
 void
 _rthread_dl_lock(int what)
 {
-       static struct _spinlock lock = _SPINLOCK_UNLOCKED;
+       static _atomic_lock_t lock = _SPINLOCK_UNLOCKED;
        static pthread_t owner = NULL;
        static struct pthread_queue lockers = TAILQ_HEAD_INITIALIZER(lockers);
        static int count = 0;
@@ -658,7 +656,7 @@ _rthread_dl_lock(int what)
                } else if (owner != self) {
                        TAILQ_INSERT_TAIL(&lockers, self, waiting);
                        while (owner != self) {
-                               __thrsleep(self, 0, NULL, &lock.ticket, NULL);
+                               __thrsleep(self, 0, NULL, &lock, NULL);
                                _spinlock(&lock);
                        }
                }
@@ -679,7 +677,7 @@ _rthread_dl_lock(int what)
                }
        } else {
                /* reinit: used in child after fork to clear the queue */
-               lock = _SPINLOCK_UNLOCKED_ASSIGN;
+               lock = _SPINLOCK_UNLOCKED;
                if (--count == 0)
                        owner = NULL;
                TAILQ_INIT(&lockers);
Index: lib/librthread/rthread.h
===================================================================
RCS file: /cvs/src/lib/librthread/rthread.h,v
retrieving revision 1.59
diff -u -p -r1.59 rthread.h
--- lib/librthread/rthread.h    3 Sep 2016 16:44:20 -0000       1.59
+++ lib/librthread/rthread.h    3 Sep 2016 19:41:54 -0000
@@ -20,7 +20,7 @@
  * Since only the thread library cares about their size or arrangement,
  * it should be possible to switch libraries without relinking.
  *
- * Do not reorder struct _spinlock and sem_t variables in the structs.
+ * Do not reorder _atomic_lock_t and sem_t variables in the structs.
  * This is due to alignment requirements of certain arches like hppa.
  * The current requirement is 16 bytes.
  *
@@ -37,17 +37,7 @@
 #define RTHREAD_STACK_SIZE_DEF (256 * 1024)
 #endif
 
-/*
- * tickets don't work yet? (or seem much slower, with lots of system time)
- * until then, keep the struct around to avoid excessive changes going
- * back and forth.
- */
-struct _spinlock {
-       _atomic_lock_t ticket;
-};
-
-#define        _SPINLOCK_UNLOCKED { _ATOMIC_LOCK_UNLOCKED }
-extern struct _spinlock _SPINLOCK_UNLOCKED_ASSIGN;
+#define        _SPINLOCK_UNLOCKED _ATOMIC_LOCK_UNLOCKED
 
 struct stack {
        SLIST_ENTRY(stack)      link;   /* link for free default stacks */
@@ -59,7 +49,7 @@ struct stack {
 };
 
 struct __sem {
-       struct _spinlock lock;
+       _atomic_lock_t lock;
        volatile int waitcount;
        volatile int value;
        int shared;
@@ -68,7 +58,7 @@ struct __sem {
 TAILQ_HEAD(pthread_queue, pthread);
 
 struct pthread_mutex {
-       struct _spinlock lock;
+       _atomic_lock_t lock;
        struct pthread_queue lockers;
        int type;
        pthread_t owner;
@@ -83,7 +73,7 @@ struct pthread_mutex_attr {
 };
 
 struct pthread_cond {
-       struct _spinlock lock;
+       _atomic_lock_t lock;
        struct pthread_queue waiters;
        struct pthread_mutex *mutex;
        clockid_t clock;
@@ -94,7 +84,7 @@ struct pthread_cond_attr {
 };
 
 struct pthread_rwlock {
-       struct _spinlock lock;
+       _atomic_lock_t lock;
        pthread_t owner;
        struct pthread_queue writers;
        int readers;
@@ -149,7 +139,7 @@ struct pthread_barrierattr {
 };
 
 struct pthread_spinlock {
-       struct _spinlock lock;
+       _atomic_lock_t lock;
        pthread_t owner;
 };
 
@@ -157,7 +147,7 @@ struct tib;
 struct pthread {
        struct __sem donesem;
        unsigned int flags;
-       struct _spinlock flags_lock;
+       _atomic_lock_t flags_lock;
        struct tib *tib;
        void *retval;
        void *(*fn)(void *);
@@ -191,9 +181,9 @@ struct pthread {
        (((size) + (_thread_pagesize - 1)) & ~(_thread_pagesize - 1))
 
 __BEGIN_HIDDEN_DECLS
-void   _spinlock(volatile struct _spinlock *);
-int    _spinlocktry(volatile struct _spinlock *);
-void   _spinunlock(volatile struct _spinlock *);
+void   _spinlock(volatile _atomic_lock_t *);
+int    _spinlocktry(volatile _atomic_lock_t *);
+void   _spinunlock(volatile _atomic_lock_t *);
 int    _sem_wait(sem_t, int, const struct timespec *, int *);
 int    _sem_post(sem_t);
 
@@ -212,7 +202,7 @@ void        _thread_malloc_reinit(void);
 extern int _threads_ready;
 extern size_t _thread_pagesize;
 extern LIST_HEAD(listhead, pthread) _thread_list;
-extern struct _spinlock _thread_lock;
+extern _atomic_lock_t _thread_lock;
 extern struct pthread_attr _rthread_attr_default;
 __END_HIDDEN_DECLS
 
Index: lib/librthread/rthread_file.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_file.c,v
retrieving revision 1.9
diff -u -p -r1.9 rthread_file.c
--- lib/librthread/rthread_file.c       3 Sep 2016 16:44:20 -0000       1.9
+++ lib/librthread/rthread_file.c       3 Sep 2016 19:41:54 -0000
@@ -87,7 +87,7 @@ static struct static_file_lock {
 } flh[NUM_HEADS];
 
 /* Lock for accesses to the hash table: */
-static struct _spinlock        hash_lock       = _SPINLOCK_UNLOCKED;
+static _atomic_lock_t  hash_lock       = _SPINLOCK_UNLOCKED;
 
 /*
  * Find a lock structure for a FILE, return NULL if the file is
@@ -205,7 +205,7 @@ _thread_flockfile(FILE * fp)
                 */
                TAILQ_INSERT_TAIL(&p->lockers,self,waiting);
                while (p->owner != self) {
-                       __thrsleep(self, 0, NULL, &hash_lock.ticket, NULL);
+                       __thrsleep(self, 0, NULL, &hash_lock, NULL);
                        _spinlock(&hash_lock);
                }
        }
@@ -301,4 +301,3 @@ _thread_funlockfile(FILE * fp)
        /* Unlock the hash table: */
        _spinunlock(&hash_lock);
 }
-
Index: lib/librthread/rthread_fork.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_fork.c,v
retrieving revision 1.18
diff -u -p -r1.18 rthread_fork.c
--- lib/librthread/rthread_fork.c       1 Sep 2016 10:41:02 -0000       1.18
+++ lib/librthread/rthread_fork.c       3 Sep 2016 19:41:54 -0000
@@ -100,13 +100,13 @@ _dofork(pid_t (*sys_fork)(void))
 #endif
                /* update this thread's structure */
                tib->tib_tid = getthrid();
-               me->donesem.lock = _SPINLOCK_UNLOCKED_ASSIGN;
-               me->flags_lock = _SPINLOCK_UNLOCKED_ASSIGN;
+               me->donesem.lock = _SPINLOCK_UNLOCKED;
+               me->flags_lock = _SPINLOCK_UNLOCKED;
 
                /* reinit the thread list */
                LIST_INIT(&_thread_list);
                LIST_INSERT_HEAD(&_thread_list, me, threads);
-               _thread_lock = _SPINLOCK_UNLOCKED_ASSIGN;
+               _thread_lock = _SPINLOCK_UNLOCKED;
 
                /* single threaded now */
                __isthreaded = 0;
Index: lib/librthread/rthread_libc.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_libc.c,v
retrieving revision 1.16
diff -u -p -r1.16 rthread_libc.c
--- lib/librthread/rthread_libc.c       1 Sep 2016 10:56:46 -0000       1.16
+++ lib/librthread/rthread_libc.c       3 Sep 2016 19:41:54 -0000
@@ -191,7 +191,7 @@ _thread_malloc_reinit(void)
        int i;
 
        for (i = 0; i < _MALLOC_MUTEXES; i++) {
-               malloc_lock[i].lock = _SPINLOCK_UNLOCKED_ASSIGN;
+               malloc_lock[i].lock = _SPINLOCK_UNLOCKED;
                TAILQ_INIT(&malloc_lock[i].lockers);
                malloc_lock[i].owner = NULL;
                malloc_lock[i].count = 0;
@@ -201,7 +201,7 @@ _thread_malloc_reinit(void)
 /*
  * atexit lock
  */
-static struct _spinlock atexit_lock = _SPINLOCK_UNLOCKED;
+static _atomic_lock_t atexit_lock = _SPINLOCK_UNLOCKED;
 
 void
 _thread_atexit_lock(void)
@@ -218,7 +218,7 @@ _thread_atexit_unlock(void)
 /*
  * atfork lock
  */
-static struct _spinlock atfork_lock = _SPINLOCK_UNLOCKED;
+static _atomic_lock_t atfork_lock = _SPINLOCK_UNLOCKED;
 
 void
 _thread_atfork_lock(void)
@@ -235,7 +235,7 @@ _thread_atfork_unlock(void)
 /*
  * arc4random lock
  */
-static struct _spinlock arc4_lock = _SPINLOCK_UNLOCKED;
+static _atomic_lock_t arc4_lock = _SPINLOCK_UNLOCKED;
 
 void
 _thread_arc4_lock(void)
@@ -248,4 +248,3 @@ _thread_arc4_unlock(void)
 {
        _spinunlock(&arc4_lock);
 }
-
Index: lib/librthread/rthread_rwlock.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_rwlock.c,v
retrieving revision 1.7
diff -u -p -r1.7 rthread_rwlock.c
--- lib/librthread/rthread_rwlock.c     3 Sep 2016 16:44:20 -0000       1.7
+++ lib/librthread/rthread_rwlock.c     3 Sep 2016 19:41:54 -0000
@@ -20,7 +20,6 @@
  * rwlocks
  */
 
-
 #include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -30,8 +29,7 @@
 
 #include "rthread.h"
 
-
-static struct _spinlock rwlock_init_lock = _SPINLOCK_UNLOCKED;
+static _atomic_lock_t rwlock_init_lock = _SPINLOCK_UNLOCKED;
 
 int
 pthread_rwlock_init(pthread_rwlock_t *lockp,
@@ -42,7 +40,7 @@ pthread_rwlock_init(pthread_rwlock_t *lo
        lock = calloc(1, sizeof(*lock));
        if (!lock)
                return (errno);
-       lock->lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       lock->lock = _SPINLOCK_UNLOCKED;
        TAILQ_INIT(&lock->writers);
 
        *lockp = lock;
@@ -118,7 +116,7 @@ _rthread_rwlock_rdlock(pthread_rwlock_t 
        else {
                do {
                        if (__thrsleep(lock, CLOCK_REALTIME, abstime,
-                           &lock->lock.ticket, NULL) == EWOULDBLOCK)
+                           &lock->lock, NULL) == EWOULDBLOCK)
                                return (ETIMEDOUT);
                        _spinlock(&lock->lock);
                } while (lock->owner != NULL || !TAILQ_EMPTY(&lock->writers));
@@ -181,7 +179,7 @@ _rthread_rwlock_wrlock(pthread_rwlock_t 
                TAILQ_INSERT_TAIL(&lock->writers, thread, waiting);
                do {
                        do_wait = __thrsleep(thread, CLOCK_REALTIME, abstime,
-                           &lock->lock.ticket, NULL) != EWOULDBLOCK;
+                           &lock->lock, NULL) != EWOULDBLOCK;
                        _spinlock(&lock->lock);
                } while (lock->owner != thread && do_wait);
 
Index: lib/librthread/rthread_sem.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_sem.c,v
retrieving revision 1.24
diff -u -p -r1.24 rthread_sem.c
--- lib/librthread/rthread_sem.c        3 Sep 2016 16:44:20 -0000       1.24
+++ lib/librthread/rthread_sem.c        3 Sep 2016 19:41:55 -0000
@@ -72,7 +72,7 @@ _sem_wait(sem_t sem, int tryonly, const 
                sem->waitcount++;
                do {
                        r = __thrsleep(ident, CLOCK_REALTIME, abstime,
-                           &sem->lock.ticket, delayed_cancel);
+                           &sem->lock, delayed_cancel);
                        _spinlock(&sem->lock);
                        /* ignore interruptions other than cancelation */
                        if (r == EINTR && (delayed_cancel == NULL ||
@@ -160,7 +160,7 @@ sem_init(sem_t *semp, int pshared, unsig
                errno = ENOSPC;
                return (-1);
        }
-       sem->lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       sem->lock = _SPINLOCK_UNLOCKED;
        sem->value = value;
        *semp = sem;
 
@@ -395,7 +395,7 @@ sem_open(const char *name, int oflag, ..
                return (SEM_FAILED);
        }
        if (created) {
-               sem->lock = _SPINLOCK_UNLOCKED_ASSIGN;
+               sem->lock = _SPINLOCK_UNLOCKED;
                sem->value = value;
                sem->shared = 1;
        }
Index: lib/librthread/rthread_spin_lock.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_spin_lock.c,v
retrieving revision 1.3
diff -u -p -r1.3 rthread_spin_lock.c
--- lib/librthread/rthread_spin_lock.c  1 Jun 2013 20:47:40 -0000       1.3
+++ lib/librthread/rthread_spin_lock.c  3 Sep 2016 19:41:55 -0000
@@ -37,7 +37,7 @@ pthread_spin_init(pthread_spinlock_t *lo
        if (l == NULL)
                return (ENOMEM);
 
-       l->lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       l->lock = _SPINLOCK_UNLOCKED;
        *lock = l;
        return (0);
 }
Index: lib/librthread/rthread_stack.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_stack.c,v
retrieving revision 1.15
diff -u -p -r1.15 rthread_stack.c
--- lib/librthread/rthread_stack.c      1 Sep 2016 10:56:46 -0000       1.15
+++ lib/librthread/rthread_stack.c      3 Sep 2016 19:41:55 -0000
@@ -18,7 +18,7 @@
  * attributes for possible reuse.
  */
 static SLIST_HEAD(, stack) def_stacks = SLIST_HEAD_INITIALIZER(head);
-static struct _spinlock def_stacks_lock = _SPINLOCK_UNLOCKED;
+static _atomic_lock_t def_stacks_lock = _SPINLOCK_UNLOCKED;
 
 struct stack *
 _rthread_alloc_stack(pthread_t thread)
@@ -134,4 +134,3 @@ _rthread_free_stack(struct stack *stack)
                free(stack);
        }
 }
-
Index: lib/librthread/rthread_sync.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_sync.c,v
retrieving revision 1.43
diff -u -p -r1.43 rthread_sync.c
--- lib/librthread/rthread_sync.c       3 Sep 2016 16:44:20 -0000       1.43
+++ lib/librthread/rthread_sync.c       3 Sep 2016 19:41:55 -0000
@@ -20,7 +20,6 @@
  * Mutexes and conditions - synchronization functions.
  */
 
-
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
@@ -32,7 +31,7 @@
 #include "rthread.h"
 #include "cancel.h"            /* in libc/include */
 
-static struct _spinlock static_init_lock = _SPINLOCK_UNLOCKED;
+static _atomic_lock_t static_init_lock = _SPINLOCK_UNLOCKED;
 
 /*
  * mutexen
@@ -45,7 +44,7 @@ pthread_mutex_init(pthread_mutex_t *mute
        mutex = calloc(1, sizeof(*mutex));
        if (!mutex)
                return (errno);
-       mutex->lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       mutex->lock = _SPINLOCK_UNLOCKED;
        TAILQ_INIT(&mutex->lockers);
        if (attr == NULL) {
                mutex->type = PTHREAD_MUTEX_DEFAULT;
@@ -131,7 +130,7 @@ _rthread_mutex_lock(pthread_mutex_t *mut
 
                        /* self-deadlock, possibly until timeout */
                        while (__thrsleep(self, CLOCK_REALTIME, abstime,
-                           &mutex->lock.ticket, NULL) != EWOULDBLOCK)
+                           &mutex->lock, NULL) != EWOULDBLOCK)
                                _spinlock(&mutex->lock);
                        return (ETIMEDOUT);
                }
@@ -148,7 +147,7 @@ _rthread_mutex_lock(pthread_mutex_t *mut
                TAILQ_INSERT_TAIL(&mutex->lockers, self, waiting);
                while (mutex->owner != self) {
                        ret = __thrsleep(self, CLOCK_REALTIME, abstime,
-                           &mutex->lock.ticket, NULL);
+                           &mutex->lock, NULL);
                        _spinlock(&mutex->lock);
                        assert(mutex->owner != NULL);
                        if (ret == EWOULDBLOCK) {
@@ -250,7 +249,7 @@ pthread_cond_init(pthread_cond_t *condp,
        cond = calloc(1, sizeof(*cond));
        if (!cond)
                return (errno);
-       cond->lock = _SPINLOCK_UNLOCKED_ASSIGN;
+       cond->lock = _SPINLOCK_UNLOCKED;
        TAILQ_INIT(&cond->waiters);
        if (attr == NULL)
                cond->clock = CLOCK_REALTIME;
@@ -360,7 +359,7 @@ pthread_cond_timedwait(pthread_cond_t *c
        /* wait until we're the owner of the mutex again */
        while (mutex->owner != self) {
                error = __thrsleep(self, cond->clock, abstime,
-                   &mutex->lock.ticket, &self->delayed_cancel);
+                   &mutex->lock, &self->delayed_cancel);
 
                /*
                 * If abstime == NULL, then we're definitely waiting
@@ -509,7 +508,7 @@ pthread_cond_wait(pthread_cond_t *condp,
 
        /* wait until we're the owner of the mutex again */
        while (mutex->owner != self) {
-               error = __thrsleep(self, 0, NULL, &mutex->lock.ticket,
+               error = __thrsleep(self, 0, NULL, &mutex->lock,
                    &self->delayed_cancel);
 
                /*
Index: lib/librthread/rthread_tls.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread_tls.c,v
retrieving revision 1.17
diff -u -p -r1.17 rthread_tls.c
--- lib/librthread/rthread_tls.c        2 Apr 2016 19:56:53 -0000       1.17
+++ lib/librthread/rthread_tls.c        3 Sep 2016 19:41:55 -0000
@@ -27,7 +27,7 @@
 #include "rthread.h"
 
 static struct rthread_key rkeys[PTHREAD_KEYS_MAX];
-static struct _spinlock rkeyslock = _SPINLOCK_UNLOCKED;
+static _atomic_lock_t rkeyslock = _SPINLOCK_UNLOCKED;
 
 int
 pthread_key_create(pthread_key_t *key, void (*destructor)(void*))

-- 
Michal Mazurek

Reply via email to