Ticket #5693: spin-lock-no-double-test.patch

File spin-lock-no-double-test.patch, 3.0 KB (added by lucian, 14 years ago)
  • 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 UNLOCKED    0
     13#define LOCKED      1
     14
     15
     16
     17int
     18pthread_spin_init(pthread_spinlock_t* lock, 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 threads of different processes.
     23
     24    lock->lock = UNLOCKED;
     25    return 0;
     26}
     27
     28
     29int
     30pthread_spin_destroy(pthread_spinlock_t* lock)
     31{
     32    return 0;
     33}
     34
     35
     36int
     37pthread_spin_lock(pthread_spinlock_t* lock)
     38{
     39    while (atomic_test_and_set(&lock->lock, LOCKED, UNLOCKED) == LOCKED)
     40        ; // spin
     41    return 0;
     42}
     43
     44
     45int
     46pthread_spin_trylock(pthread_spinlock_t* lock)
     47{
     48    if (atomic_test_and_set(&lock->lock, LOCKED, UNLOCKED) == LOCKED)
     49        return EBUSY;
     50    return 0;
     51}
     52
     53
     54int
     55pthread_spin_unlock(pthread_spinlock_t* lock)
     56{
     57    lock->lock = UNLOCKED;
     58    return 0;
     59}
     60
  • 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));