Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a mutex cannot be released from an ISR

Vxworks states that mutual exculsion semaphores : Cannot be given inside ISR, while the condition is vaild for binary and counting semaphore.

I am not able to understand the reason out the same.

Thanks, Zaks.

like image 473
Zaks Avatar asked Nov 01 '25 15:11

Zaks


1 Answers

Remember that a Mutex must first be acquired/taken then released/given. In addition, the task that acquires the mutex owns it. This prevents another task from releasing a mutex it doesn't own.

With that being the case, it becomes clear that since an ISR cannot acquire a mutex (or any semaphore for that matter - it's a blocking operation), then it follows that it can't give the mutex.

It is quite possible for an ISR to do give a Binary or Counting semaphore to signal a task that something happens. But mutexes are always a take/give pair.

To clarify a point. In VxWorks, the ISR context is not the same as the context of a task!
The following scenario is invalid:

  Task A            ISR
  semTake(mutex)
   ....
                    semGive(mutex)

Task A owns the mutex. When the ISR runs, it executes in a totaly different context. Most current processors have a separate ISR stack. Since Task A owns the mutex, how could the ISR give it up? In fact, what guarantee do you have that the ISR will fire while A has the mutex.
Even assuming you "could" give a mutex in an ISR, how would you handle the following scenario:

 Task A                       Task B                 ISR
  semTake(mutex)
  ...
  <context switch happens>
                              <B runs>
                                                  semGive(mutex)

Task A gets switched out due to a call unrelated to the mutex, and Task B runs. The ISR now executes while B was running. Would it still be valid for the ISR to be given?

Regardless of all this, the simple fact is that a mutex is always used in a pair of Get/Set. I fail to see a use case where you would have an isolated semGive.

Is there a specific situation you have in mind that would require a semGive from an ISR context?

like image 146
Benoit Avatar answered Nov 04 '25 13:11

Benoit