在 Linux 2.6.11.12 中,rwlock_t定义如下:
typedef struct {
volatile unsigned int lock;
#ifdef CONFIG_DEBUG_SPINLOCK
unsigned magic;
#endif
#ifdef CONFIG_PREEMPT
unsigned int break_lock;
#endif
} rwlock_t;
在 的定义中rwlock_t,lock字段是unsigned int。当我们要获得读锁时,read_lock()最终会调用_raw_read_trylock()
static inline int _raw_read_trylock(rwlock_t *lock)
{
atomic_t *count = (atomic_t *)lock;
atomic_dec(count);
if (atomic_read(count) >= 0)
return 1;
atomic_inc(count);
return 0;
}
在这个函数中,我们调用atomic_dec()减少lock并检查它是否大于或等于零。但由于lock是一个unsigned int,lock将永远大于 0!也就是说,此函数将始终返回 1。
我猜想atomic_read()将结果转换为 a int,但在 i386 拱门中,它被定义为
#define atomic_read(v) ((v)->counter)
而且我不知道如何unsigned int lock工作。