Page 1 of 1

Suspend a task that is not the current task

Posted: Thu Oct 24, 2013 7:12 pm
by rjp
I haven't been able to find anything that fits this, but is there a way to suspend a task that is not the currently executing task? I am porting from a different OS where we used that feature, and while we can work around it, it would probably be easier to implement the missing functionality.

On the surface, it looks like it would be a simple copy/paste of chSchGoSleepS with some changes to handle an argument (rather than currp), but it may cause problems if you attempt to suspend a task that is already sleeping on something else. Do you have any thoughts on how this may be implemented or is it more work than it's worth?

Re: Suspend a task that is not the current task

Posted: Thu Oct 24, 2013 7:37 pm
by Giovanni
Hi,

It is something that in ChibiOS is explicitly not allowed because it would open the door to many problems hard/impossible to handle. For example suspending a thread owning a critical section that would no more be released or be released in an unpredictable way (that in a RTOS is worse that simply hang the application). Also, the thread could be in any state, each state would require a different handling.

I recommend that the thread itself, it its loop, decides if to suspend by checking a condition then go wait on a semahore.

Code: Select all

  while (TRUE) {
    ...
    chSysLock();
    if (condition)
       chSemWait(&sem);
    chSysUnlock();
    ...
  }


To command a suspend:

Code: Select all

    condition = TRUE;


To wakeup the thread simply:

Code: Select all

    chSysLock();
    condition = FALSE;
    chSemResetS(&sem, 0);
    chSysUnlock();


Lock and unlocks are there to make the operations atomic. The semaphore is initialized to zero.

Giovanni

Re: Suspend a task that is not the current task

Posted: Thu Oct 24, 2013 7:46 pm
by rjp
This is what I was thinking I would have to do. We've already worked around the issue of owning locks when the tasks are suspended under the other OS, but it is done asynchronously. I will just change the structure to suspend synchronously (via message). The synchronous messages would probably work well for this, since the caller would be blocked until the task is successfully stopped.

Rather than wait on a semaphore, can I call chSchGoSleepS() with THD_STATE_SUSPENDED and treat it like a task that is created with chThreadCreateI. I would have the task suspend itself and then use chThdResume to wake it up. Would this be considered unsafe?

Re: Suspend a task that is not the current task

Posted: Thu Oct 24, 2013 7:50 pm
by Giovanni
It is OK, it is a generic suspend state not involving ant special processing. It is used in several places in that way, especially in the HAL. In 3.0 this role will be enhanced exactly in that direction adding specific API.

Synchronous messages are in general very efficient.

Giovanni

Re: Suspend a task that is not the current task

Posted: Thu Oct 24, 2013 8:41 pm
by rjp
Thanks for the super prompt replies.

I'm really interested in 3.0, and perhaps this is better left for another thread, but is there an unstable/test tree generally available for users who are willing to suffer issues in the short term to avoid porting 2.x->3.0 down the road?

Re: Suspend a task that is not the current task

Posted: Thu Oct 24, 2013 8:56 pm
by Giovanni
The 3.0 branch is already usable, not all platform have been ported over but the STM32 is there already. See the 3.0 thread in Development: viewtopic.php?f=3&t=817&start=50#p11279

Of course do not expect it to be final, it will be released in like 8-12 months.

Giovanni