Skip to contentSkip to conversation

questions on wait_event ...

DISCUSSION Robert Hancock 3 participants 5 messages Dec 23, 2005 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 14 lines · Alexey Shinkin

Hi , all !
Could anyone please clarify one thing in that old well known wait_event_... code (taken from 2.6.5 wait.h ):

#define __wait_event_interruptible(wq, condition, ret) \

do {                                                                    \
       wait_queue_t __wait;                                     \
       init_waitqueue_entry(&__wait, current);           \
                                                                       \
       add_wait_queue(&wq, &__wait);                   \
       for (;;) {                                                      \
               set_current_state(TASK_INTERRUPTIBLE);    \
               if (condition)                                          \
                       break;
Quoted 6 lines

........................................

Is it possible that scheduling happen after set_current_state() but before checking the condition ?
If yes - even if we will have condition==TRUE by this moment - the scheduler will make the process to sleep anyway , right ?

Yes, but since the condition would then have changed after we were put into the wait queue, they would have woken up the queue and we should be woken up again.

Signature · 3 lines
Robert Hancock      Saskatoon, SK, Canada
To email, remove "nospam" from [email protected]
Home Page: http://www.roberthancock.com/
#2 lore raw
Quoted 24 lines
Quoted 14 lines · Alexey Shinkin

Hi , all !
Could anyone please clarify one thing in that old well known wait_event_... code (taken from 2.6.5 wait.h ):

#define __wait_event_interruptible(wq, condition, ret) \

do {                                                                    \
       wait_queue_t __wait;                                     \
       init_waitqueue_entry(&__wait, current);           \
                                                                       \
       add_wait_queue(&wq, &__wait);                   \
       for (;;) {                                                      \
               set_current_state(TASK_INTERRUPTIBLE);    \
               if (condition)                                          \
                       break;
Quoted 6 lines

........................................

Is it possible that scheduling happen after set_current_state() but before checking the condition ?
If yes - even if we will have condition==TRUE by this moment - the scheduler will make the process to sleep anyway , right ?

Quoted 7 lines

Yes, but since the condition would then have changed after we were put into the wait queue, they would have woken up the queue and we should be woken up again.

--
Robert Hancock Saskatoon, SK, Canada
To email, remove "nospam" from [email protected] Home Page: http://www.roberthancock.com/

And what if the condition have changed after we have checked it in wait_event() but before calling __wait_event() and before putting the process into the wait queue ? The process could not be woken up "in advance" , right ?

#define wait_event(wq, condition) \

do {                                                   \
       if (condition)                                \
               break;                                 \
  /* and here we have condition changed  ???? */
       __wait_event(wq, condition);        \
} while (0)

Regards
Alexey Shinkin

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! http://search.msn.com/

#3 lore raw
Quoted 6 lines · Alexey Shinkin

And what if the condition have changed after we have checked it in wait_event() but
before calling __wait_event() and before putting the process into the wait queue ?
The process could not be woken up "in advance" , right ?

Lets add the other part of this too (the __wait_event)

#define __wait_event(wq, condition) \

do {									\
	DEFINE_WAIT(__wait);						\
									\
	for (;;) {							\
		prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);	\
		if (condition)						\
			break;						\
		schedule();						\
	}								\
	finish_wait(&wq, &__wait);					\
} while (0)
Quoted 10 lines

#define wait_event(wq, condition) \

do {                                                   \
        if (condition)                                \
                break;                                 \
   /* and here we have condition changed  ???? */
        __wait_event(wq, condition);        \
} while (0)

So if the condition happens there, it will be checked again up above in __wait_event.

-- Steve

#4 lore raw

Look:

We call wait_event() , condition is FALSE at the moment :

   do {
        if (condition)
                break;

/* and here we have condition changed to TRUE */ /* process is NOT in any wait queue yet */
/* then unroll __wait_event(wq, condition); */

    do {							DEFINE_WAIT(__wait);					for (;;) {
	prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);

         /* at this point condition is TRUE , process is in a wait queue 

and its state is
TASK_UNINTERRUPTIBLE. If rescheduling happens now the
process will asleep,
despite of condition is TRUE . And will not be woken up until
next wake_up happens

             Is that correct ?  */

	if (condition)						     break;
                   schedule();
       }

finish_wait(&wq, &__wait);
} while (0)
/* end of unroll __wait_event*/

} while (0)

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

#5 lore raw

Please include CCs of people who respond to you or you might not ever get a response. There's too much traffic on the LKML, your email may get lost in the noise.

Quoted 15 lines · Alexey Shinkin

Look:

We call wait_event() , condition is FALSE at the moment :

    do {
         if (condition)
                 break;
    /* and here we have condition changed  to TRUE  */
    /*  process is NOT in any wait queue yet  */
    /*  then  unroll     __wait_event(wq, condition);        */

     do {							DEFINE_WAIT(__wait);					for (;;) {
	prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);

          /* at this point condition is TRUE , process is in a wait queue 
Quoted 6 lines

and its state is
TASK_UNINTERRUPTIBLE. If rescheduling happens now the
process will asleep,
despite of condition is TRUE . And will not be woken up until
next wake_up happens
Is that correct ? */

OHHH! Your question is about __preemption__!!!

That's a completely different story, because if a process gets preempted, it will _not_ be taken off the runqueue even if it's state is in TASK_UNINTERRUPTIBLE. Otherwise, there would be lots of places in the kernel that is broken.

from schedule in sched.c:

/* when preempted, the preempt_count gets "PREEMPT_ACTIVE"
so the following if will not be entered */

	if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
		switch_count = &prev->nvcsw;
		if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
				unlikely(signal_pending(prev))))
			prev->state = TASK_RUNNING;
		else {
			if (prev->state == TASK_UNINTERRUPTIBLE)
				rq->nr_uninterruptible++;

/* Here we would have taken off the task from the runqueue
but we don't, so the task _will_ wake up again when it is scheduled back in. */

			deactivate_task(prev, rq);
		}
	}

-- Steve

Quoted 18 lines
	if (condition)						     break;
                    schedule();
        }

finish_wait(&wq, &__wait);
} while (0)
/* end of unroll __wait_event*/

} while (0)

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/