Re: set_current_state() vs current->state

From: Andrea Arcangeli (andrea@suse.de)
Date: Sun Oct 01 2000 - 09:03:01 EST


On Sun, Oct 01, 2000 at 03:04:31AM -0400, Frank Davis wrote:
> Hello all,
> I've been going through the drivers/block code (almost done) and noticed the use of, eg.:
> set_current_state(TASK_RUNNING); and also
> current->state = TASK_RUNNING;
>
> Which one is most recent, and is there a suggestion on which one to use?

`__set_current_state(x)' is completly equivalent to `current->state = x'.

set_current_state(x) instead means:

        current->state = x
        mb()

It puts a CPU memory barrier _after_ the assignment. This is necessary in most
cases while using the wait-event interface to avoid SMP races.

In short you need set_current_state(x) when you do something that relies on the
ordering like:

        set_current_state(TASK_UNINTERRUPTIBLE)
        if (event_happened_meanwhile)
                break;
        schedule();

You must make sure not to read `event_happened_meanwhile' before making visible
the change of current->state to avoid missing one event and to hang in
UNINTERRUPTIBLE mode in schedule.

You don't need the mb() version (so you can use __set_current_state(x)) when
you do something that doesn't relies on the ordering like:

        [..]
        remove_wait_queue(...);
        __set_current_state(TASK_RUNNING);
        return;

In the l-k archives you should find further details of such SMP race conditions.

Andrea
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sat Oct 07 2000 - 21:00:07 EST