Ticket #5693: spin-lock-no-double-test.patch
File spin-lock-no-double-test.patch, 3.0 KB (added by , 14 years ago) |
---|
-
src/system/libroot/posix/pthread/Jamfile
18 18 pthread_mutexattr.c 19 19 pthread_once.cpp 20 20 pthread_rwlock.cpp 21 pthread_spinlock.c 21 22 ; 22 23 -
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 17 int 18 pthread_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 29 int 30 pthread_spin_destroy(pthread_spinlock_t* lock) 31 { 32 return 0; 33 } 34 35 36 int 37 pthread_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 45 int 46 pthread_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 54 int 55 pthread_spin_unlock(pthread_spinlock_t* lock) 56 { 57 lock->lock = UNLOCKED; 58 return 0; 59 } 60 -
headers/posix/pthread.h
21 21 typedef struct _pthread_once pthread_once_t; 22 22 typedef struct _pthread_rwlock pthread_rwlock_t; 23 23 typedef struct _pthread_rwlockattr *pthread_rwlockattr_t; 24 typedef struct _pthread_spinlock pthread_spinlock_t; 24 25 /* 25 26 typedef struct _pthread_barrier *pthread_barrier_t; 26 27 typedef struct _pthread_barrierattr *pthread_barrierattr_t; 27 typedef struct _pthread_spinlock *pthread_spinlock_t;28 28 */ 29 29 30 30 struct _pthread_mutex { … … 64 64 }; 65 65 }; 66 66 67 struct _pthread_spinlock { 68 int32_t lock; 69 }; 70 67 71 #define PTHREAD_MUTEX_DEFAULT 0 68 72 #define PTHREAD_MUTEX_NORMAL 1 69 73 #define PTHREAD_MUTEX_ERRORCHECK 2 … … 206 210 extern int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, 207 211 int shared); 208 212 213 /* spinlock functions */ 214 extern int pthread_spin_init(pthread_spinlock_t* spinlock, int pshared); 215 extern int pthread_spin_destroy(pthread_spinlock_t* spinlock); 216 extern int pthread_spin_lock(pthread_spinlock_t* spinlock); 217 extern int pthread_spin_trylock(pthread_spinlock_t* spinlock); 218 extern int pthread_spin_unlock(pthread_spinlock_t* spinlock); 219 209 220 /* misc. functions */ 210 221 extern int pthread_atfork(void (*prepare)(void), void (*parent)(void), 211 222 void (*child)(void));