Basic question about chSchGoSleepS

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
dismirlian
Posts: 65
Joined: Fri Dec 20, 2013 3:59 pm
Has thanked: 1 time
Been thanked: 16 times

Basic question about chSchGoSleepS

Post by dismirlian »

Hello, maybe this is a dumb question, but I still don't know its answer :):

Assume I have one thread running, which never enables/disables interrupts (no chSysLock/Unlock). At one point, a higher priority thread preempts it, and runs the following code (taken from the ChibiOS Wiki):

Code: Select all

chSysLock();
tp = chThdSelf();
chSchGoSleepS(THD_STATE_SUSPENDED);
msg = chThdSelf()->p_u.rdymsg;  /* Retrieving the message, optional.*/
chSysUnlock();


If I get this right, when the code hits chSchGoSleepS, a preemption takes place, which is done with interrupts disabled. Then, the lower priority thread continues running. My question is, if the lower priority thread never does a chSysUnlock, who enables interrupts again? I can't seem to find the code that does this in the context switch routine... (I'm working on an STM32F3).

Thank you for your help!!

Diego
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Basic question about chSchGoSleepS

Post by Giovanni »

Hi,

The context switch operation is always performed between a lock and an unlock, so after a context switch it is the woken thread that performs the unlock. For example:

- lock
- switch
- unlock

It is not immediately apparent but the shown unlock is never the one that re-enables the interrupts, the unlock operation is performed "on the other side".

Giovanni
dismirlian
Posts: 65
Joined: Fri Dec 20, 2013 3:59 pm
Has thanked: 1 time
Been thanked: 16 times

Re: Basic question about chSchGoSleepS

Post by dismirlian »

Hi Giovanni, thanks for your answer!

Yes, I supposed that the woken thread is the one which performs the unlock. But what if the woken thread never calls chSysUnlock()? For example:

Low priority thread:

Code: Select all

volatile int a;
msg_t ThreadLowPrio(void *p) {
   for(;;) {
      a++;
   }
}


High priority thread (from the Wiki, except for the added chThdSleepMilliseconds):

Code: Select all

static Thread *tp = NULL;
void ThreadHighPrio(void *p) {
 
  while (TRUE) {
    msg_t msg;

    chThdSleepMilliseconds(100);
 
    /* Waiting for the IRQ to happen.*/
    chSysLock();
    tp = chThdSelf();
    chSchGoSleepS(THD_STATE_SUSPENDED);
    msg = chThdSelf()->p_u.rdymsg;  /* Retrieving the message, optional.*/
    chSysUnlock();
    /* Perform processing here.*/
  }
}


IRQ (directly from the Wiki):

Code: Select all

CH_IRQ_HANDLER(myIRQ) {
  CH_IRQ_PROLOGUE();
 
  /* Wakes up the thread.*/
  chSysLockFromIsr();
  if (tp != NULL) {
    tp->p_u.rdymsg = (msg_t)55;     /* Sending the message, optional.*/
    chSchReadyI(tp);
    tp = NULL;
  }
  chSysUnlockFromIsr().
 
  CH_IRQ_EPILOGUE();
}


Suppose also that Priority(_idle_thread) < Priority(ThreadLowPrio) < Priority(ThreadHighPrio).

If I understand correctly, the sequence of actions is:
1) ThreadHighPrio goes to sleep for 100ms (chThdSleepMilliseconds).
2) ThreadHighPrio wakes up.
3) ThreadHighPrio goes to sleep (chSchGoSleepS), and the scheduler immediately wakes up the ThreadLowPrio.
4) ThreadLowPrio never does a chSysUnlock().
5) The interrupt takes place, but the interrupts are still disabled :? (forever!).

If the ThreadLowPrio never yields execution (because its priority is higher than the only other thread ready to run - _idle_thread), then the system is locked forever? Even if its timeslot expires, the scheduler will not find a thread ready to run with higher priority...

Am I missing something?

Thanks for your help!
Diego.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Basic question about chSchGoSleepS

Post by Giovanni »

The low priority thread would perform its unlock because preempted threads are always in the ISR epilogue code that does exactly that.

Giovanni
dismirlian
Posts: 65
Joined: Fri Dec 20, 2013 3:59 pm
Has thanked: 1 time
Been thanked: 16 times

Re: Basic question about chSchGoSleepS

Post by dismirlian »

Whew! Now I get it. Thanks Giovanni.

Diego.
egarcia
Posts: 2
Joined: Fri May 29, 2015 11:17 pm

Re: Basic question about chSchGoSleepS

Post by egarcia »

Hi Giovanni. I'm fairly new to ChibiOS, so sorry if this question doesn't make any sense.

MCU: STM32

I think I understand the concept here of lock -> switch -> unlock. To my understanding, the order of steps is:

1. Thread1 locks.
2. Thread1 calls chSchGoSleepS(THD_STATE_SUSPENDED)
2a. chSchGoSleepS puts Thread1 to sleep and keeps it there in a suspended state.
2b. chSchGoSleepS switches the active thread to Thread2
3. Thread2 unlocks.

Is this correct?

My question is, what if no threads have been created yet. For example, before calling "chThdCreateStatic" I do some initialization. Such as enabling the I2C1 module. Then I do some I2C writes to a peripheral on my board. The function chSchGoSleepS(THD_STATE_SUSPENDED) will be called within the i2c_lld. But there is no thread created yet, so how will it unlock?

Thanks for your help. Enjoying your RTOS!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Basic question about chSchGoSleepS

Post by Giovanni »

Hi,

Your understanding of the context switch protocol is correct.

About the other question, after invoking chSysInit() there are already two threads: Idle and Main. The main() function is a full thread after exiting chSysInit().

Giovanni
Post Reply