- 論壇徽章:
- 0
|
回復(fù) 5# bruceteen
謝謝,我看內(nèi)核里面也有許多類似的應(yīng)用
/**
* 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了,說明一般情況下應(yīng)該是都能成功的對(duì)吧!那應(yīng)該是不會(huì)出現(xiàn)死循環(huán)的,即使有兩個(gè)線程在操作這個(gè)原子變量,應(yīng)該
不可能剛好大家都在死循環(huán)來修改這個(gè)值吧! |
|