Re: Re: Trying to make kernel thread sleep

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Dinesh...

> Sorry for being late in replying back. I have used
> schedule_timeout and it is working fine. But curious
> to know what wrong is going on while using wait
> queues.
> I am working on kernel 2.4.20
> Here is full code:
[deleted]...
> 	set_current_state(TASK_INTERRUPTIBLE);

I think this is the problem. You set a running task into sleeping state, 
but didn't quickly follow it with re-scheduling function (that 
implicitly use wait queue too). What happen here was, your kernel 
thread was declared in sleeping state, time slice was running out, your 
thread was kicked out from CPU but wasn't runnable anymore.

Actually, you don't need it, because interruptible_sleep_time_out do it 
all for your.

I'll give another example how it is really done if you want to do it on 
very low level way. Take a look on wait_for_completion(). Comments are 
added inline:
void wait_for_completion(struct completion *x)
{
        spin_lock_irq(&x->wait.lock);
        if (!x->done) {
// [Mulyadi]: declare a wait queue 
                DECLARE_WAITQUEUE(wait, current);

                wait.flags |= WQ_FLAG_EXCLUSIVE;
// [Mulyadi]:  add to the wait queue first
                __add_wait_queue_tail(&x->wait, &wait);
                do {
// [Mulyadi]:  and then mark the current task as interruptible
                        __set_current_state(TASK_UNINTERRUPTIBLE);
                        spin_unlock_irq(&x->wait.lock);
// [Mulyadi]: feel free to reschedule
                        schedule();
                        spin_lock_irq(&x->wait.lock);
                } while (!x->done);
// [Mulyadi]:  if you are done with it, remove the task from wait queue
                __remove_wait_queue(&x->wait, &wait);
        }
        x->done--;
        spin_unlock_irq(&x->wait.lock);
}

That way, the kernel knows that you're asking to be woken up when a 
signal is delivered, an event happens or simply because of timeout.

So the conclusion is, as far as I know, declare a waitqueue, insert the 
task to waitqueue, and set the task state. Don't mess this order...

regards,

Mulyadi


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux