標題: 請問一個驅動代碼邏輯問題,高手請進 [打印本頁] 作者: jiangsheng84 時間: 2012-06-04 15:16 標題: 請問一個驅動代碼邏輯問題,高手請進 /**
* srpt_set_cmd_state() - Set the state of a SCSI command.
* @new: New state to be set.
*
* Does not modify the state of aborted commands. Returns the previous command
* state.
*/
enum srpt_command_state srpt_set_cmd_state(struct srpt_ioctx * ioctx,
enum srpt_command_state new)
{
enum srpt_command_state previous;
BUG_ON(!ioctx);
WARN_ON(new == SRPT_STATE_NEW);
do
{
previous = atomic_read(&ioctx->state);
} while ((previous != SRPT_STATE_DONE)
&& atomic_cmpxchg(&ioctx->state, previous, new) != previous);
謝謝,我看內核里面也有許多類似的應用
/**
* 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;