|
Revision 97, 1.1 kB
(checked in by anonymous, 3 years ago)
|
This commit was manufactured by cvs2svn to create tag
'release-candidate-0-9-3-rc2'.
|
- Property svn:eol-style set to
native
- Property svn:keywords set to
Author Date Id Revision
|
| Line | |
|---|
| 1 |
/*??? |
|---|
| 2 |
#ifdef HAVE_SCHED_H |
|---|
| 3 |
# include <sched.h> |
|---|
| 4 |
#endif |
|---|
| 5 |
*/ |
|---|
| 6 |
|
|---|
| 7 |
typedef struct { volatile unsigned int lock; |
|---|
| 8 |
volatile pid_t pid; |
|---|
| 9 |
volatile int locked; |
|---|
| 10 |
} spinlock_t; |
|---|
| 11 |
|
|---|
| 12 |
#define spinlock_init(rw) do { (rw)->lock = 0x00000001; (rw)->pid=-1; (rw)->locked=0;} while(0) |
|---|
| 13 |
|
|---|
| 14 |
#define spinlock_try_lock(rw) asm volatile("lock ; decl %0" :"=m" ((rw)->lock) : : "memory") |
|---|
| 15 |
#define _spinlock_unlock(rw) asm volatile("lock ; incl %0" :"=m" ((rw)->lock) : : "memory") |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
/*??? |
|---|
| 19 |
#ifdef HAVE_SCHED_YIELD |
|---|
| 20 |
# define yield sched_yield |
|---|
| 21 |
#else |
|---|
| 22 |
*/ |
|---|
| 23 |
static inline void yield() |
|---|
| 24 |
{ |
|---|
| 25 |
struct timeval t; |
|---|
| 26 |
|
|---|
| 27 |
t.tv_sec = 0; |
|---|
| 28 |
t.tv_usec = 100; |
|---|
| 29 |
select(0, NULL, NULL, NULL, &t); |
|---|
| 30 |
} |
|---|
| 31 |
/*??? |
|---|
| 32 |
#endif |
|---|
| 33 |
*/ |
|---|
| 34 |
static inline void spinlock_unlock(spinlock_t* rw) { |
|---|
| 35 |
if (rw->locked && (rw->pid == getpid())) { |
|---|
| 36 |
rw->pid = 0; |
|---|
| 37 |
rw->locked = 0; |
|---|
| 38 |
_spinlock_unlock(rw); |
|---|
| 39 |
} |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
static inline void spinlock_lock(spinlock_t* rw) |
|---|
| 43 |
{ |
|---|
| 44 |
while (1) { |
|---|
| 45 |
spinlock_try_lock(rw); |
|---|
| 46 |
if (rw->lock == 0) { |
|---|
| 47 |
rw->pid = getpid(); |
|---|
| 48 |
rw->locked = 1; |
|---|
| 49 |
return; |
|---|
| 50 |
} |
|---|
| 51 |
_spinlock_unlock(rw); |
|---|
| 52 |
yield(); |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|