- 论坛徽章:
- 0
|
回复 5# bruceteen
谢谢,我看内核里面也有许多类似的应用
/**
* sysfs_get_active - get an active reference to sysfs_dirent
* @sd: sysfs_dirent to get an active reference to
*
* Get an active reference of @sd. This function is noop if @sd
* is NULL.
*
* RETURNS:
* Pointer to @sd on success, NULL on failure.
*/
static struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
{
if (unlikely(!sd))
return NULL;
while (1) {
int v, t;
v = atomic_read(&sd->s_active);
if (unlikely(v < 0))
return NULL;
t = atomic_cmpxchg(&sd->s_active, v, v + 1);
if (likely(t == v))
return sd;
if (t < 0)
return NULL;
cpu_relax();
}
}
可以看到if (likely(t == v)),既然都likely了,说明一般情况下应该是都能成功的对吧!那应该是不会出现死循环的,即使有两个线程在操作这个原子变量,应该
不可能刚好大家都在死循环来修改这个值吧! |
|