Ticket #5693: spin-lock.patch

File spin-lock.patch, 3.3 KB (added by lucian, 14 years ago)

generic-spin-lock-implementation

  • src/system/libroot/posix/pthread/Jamfile

     
    1818    pthread_mutexattr.c
    1919    pthread_once.cpp
    2020    pthread_rwlock.cpp
     21    pthread_spinlock.c
    2122;
    2223
  • src/system/libroot/posix/pthread/pthread_spinlock.c

     
     1/*
     2 * Copyright 2010, Lucian Adrian Grijincu, lucian.grijincu@gmail.com.
     3 * Distributed under the terms of the MIT License.
     4 */
     5
     6
     7#include <pthread.h>
     8#include "pthread_private.h"
     9
     10
     11
     12#define SUNLOCKED   0
     13#define SLOCKED     1
     14
     15
     16
     17int
     18pthread_spin_init(pthread_spinlock_t* slock, int pshared)
     19{
     20    // this implementation of spinlocks doesn't differentiate
     21    // between spin locks used by threads in the same process or
     22    // between of different processes.
     23
     24    slock->lock = SUNLOCKED;
     25    return 0;
     26}
     27
     28
     29int
     30pthread_spin_destroy(pthread_spinlock_t* slock)
     31{
     32    return 0;
     33}
     34
     35
     36int
     37pthread_spin_lock(pthread_spinlock_t* slock)
     38{
     39    // test and test-and-set semantics:
     40    //  loop with low overhead reads until the spinlock seems unlocked
     41    //  and then use the high-overhead test-and-set operation to try to
     42    //  acquire the spinlock
     43
     44    while (slock->lock == SLOCKED
     45            || atomic_test_and_set(&slock->lock, SLOCKED, SUNLOCKED) == SLOCKED)
     46        ; // spin
     47    return 0;
     48}
     49
     50
     51int
     52pthread_spin_trylock(pthread_spinlock_t* slock)
     53{
     54    // implemented with test and test-and-set semantics
     55    // as in pthread_spin_lock
     56
     57    if (slock->lock == SLOCKED)
     58        return EBUSY;
     59    if (atomic_test_and_set(&slock->lock, SLOCKED, SUNLOCKED) == SLOCKED)
     60        return EBUSY;
     61    return 0;
     62}
     63
     64
     65int
     66pthread_spin_unlock(pthread_spinlock_t* slock)
     67{
     68    slock->lock = SUNLOCKED;
     69    return 0;
     70}
     71
  • headers/posix/pthread.h

     
    2121typedef struct  _pthread_once       pthread_once_t;
    2222typedef struct  _pthread_rwlock     pthread_rwlock_t;
    2323typedef struct  _pthread_rwlockattr *pthread_rwlockattr_t;
     24typedef struct  _pthread_spinlock   pthread_spinlock_t;
    2425/*
    2526typedef struct  _pthread_barrier    *pthread_barrier_t;
    2627typedef struct  _pthread_barrierattr *pthread_barrierattr_t;
    27 typedef struct  _pthread_spinlock   *pthread_spinlock_t;
    2828*/
    2929
    3030struct _pthread_mutex {
     
    6464    };
    6565};
    6666
     67struct _pthread_spinlock {
     68    int32_t     lock;
     69};
     70
    6771#define PTHREAD_MUTEX_DEFAULT       0
    6872#define PTHREAD_MUTEX_NORMAL        1
    6973#define PTHREAD_MUTEX_ERRORCHECK    2
     
    206210extern int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr,
    207211    int shared);
    208212
     213/* spinlock functions */
     214extern int pthread_spin_init(pthread_spinlock_t* spinlock, int pshared);
     215extern int pthread_spin_destroy(pthread_spinlock_t* spinlock);
     216extern int pthread_spin_lock(pthread_spinlock_t* spinlock);
     217extern int pthread_spin_trylock(pthread_spinlock_t* spinlock);
     218extern int pthread_spin_unlock(pthread_spinlock_t* spinlock);
     219
    209220/* misc. functions */
    210221extern int pthread_atfork(void (*prepare)(void), void (*parent)(void),
    211222    void (*child)(void));