Guru Prasad
Guru Prasad

Reputation: 4233

Prevent task from being scheduled

I have a sysfs interface which when written to should prevent the writing task from executing until certain conditions are met.

The condition for unblocking is of the form:

if(tsk->wait_time > MAX_WAIT_PERIOD || tsk->condition_met) {
    // Unblock task and let it run again
}

Because of the nature of the condition, I don't think schedule_timeout is the right mechanism to use; or at least, I haven't figured out how to use it and then cancel the wait time / appropriately signal the process.

I have also tried manually doing the following on the sysfs write:

...
__set_current_state(TASK_INTERRUPTIBLE);
schedule();
...

And then when the condition is satisfied:

if(tsk->wait_time > MAX_WAIT_PERIOD || tsk->condition_met) {
    set_task_state(tsk, TASK_RUNNING);
}

This results in: BUG: scheduling while atomic: dummyThread/2622/0x00000002
Given how, schedule_timeout does almost the exact same logic, I suspect that it would also cause the same BUG: scheduling while atomic: ... error.

I have also tried the deactivate_task and activate_task methods, but they cause a kernel panic in the scheduler's pick_next_task chain. If required, I will re-implement this and post the stack trace.

What is the right way to prevent a task from running until certain conditions are met?

Upvotes: 2

Views: 499

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66298

schedule_timeout() is good low-level function for timed wait.

But set_task_state() is bad choice for wake up: this function is mainly for current thread. Instead, use wake_up_process or wake_up_state.

Upvotes: 1

Related Questions