Ticket #5693: spin-lock.patch
File spin-lock.patch, 3.3 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 SUNLOCKED 0 13 #define SLOCKED 1 14 15 16 17 int 18 pthread_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 29 int 30 pthread_spin_destroy(pthread_spinlock_t* slock) 31 { 32 return 0; 33 } 34 35 36 int 37 pthread_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 51 int 52 pthread_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 65 int 66 pthread_spin_unlock(pthread_spinlock_t* slock) 67 { 68 slock->lock = SUNLOCKED; 69 return 0; 70 } 71 -
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));