【发布时间】:2012-07-17 01:58:45
【问题描述】:
这是我的自旋锁实现,但它似乎无法保护关键代码。我的实现有问题吗?
static __inline__ int xchg_asm(int* lock, int val)
{
int ret;
__asm__ __volatile__(
LOCK "movl (%1),%%eax;
xchg (%1),%2;
movl %%eax, %0" :"=m" (ret) :"d"(lock), "c"(val)
);
return ret;
}
void spin_init(spinlock_t* sl)
{
sl->val = 0;
}
void spin_lock(spinlock_t* sl)
{
int ret;
do {
ret = xchg_asm(&(sl->val), 1);
} while ( ret==0 );
}
void spin_unlock(spinlock_t* sl)
{
xchg_asm(&(sl->val), 0);
}
【问题讨论】:
-
优秀;它可能不是错误,但对我来说,
spin_destroy()函数释放未由spin_init()分配的内存似乎很奇怪。 (也有spin_alloc()函数吗?) -
spin_destory 可能是多余的。
-
也许是过分了。但是提供一个匹配的
_init()和_destroy()函数 很好,特别是如果你想在将来修改任何东西。
标签: linux assembly linux-kernel x86 kernel