[SOLVED] Concurrency: Lock and non-preemptible code
Posted: Wed Sep 21, 2016 11:01 am
Hi fellow osdevers,
I just realized that I had a huge problem with concurrency when a process is waiting for a lock and the lock is released from an interrupt handler (non-preemptible). This is typical case of deferred work from IRQ handlers.
More specifically, I have this particular case:
For this very particular problem (not for a general-purpose lock), I was thinking of using Compare-And-Swap to solve this issue rather than disable interrupts.
The pseudo-code would be something like this:
(This does not fully take preemption into account (preemption would have to play nice with DEFER))
It's harder to adapt for non-binary locks though.
What do you think of this code ? Could this kind of thing work ? Is it worth trying to make it work fully or is it simply better to disable IRQ at this point ?
Thanks
I just realized that I had a huge problem with concurrency when a process is waiting for a lock and the lock is released from an interrupt handler (non-preemptible). This is typical case of deferred work from IRQ handlers.
More specifically, I have this particular case:
- 1 thread is waiting on the lock: calling lock.wait() and the thread never change
- 1 IRQ handler is releasing the lock: calling lock.notify()
For this very particular problem (not for a general-purpose lock), I was thinking of using Compare-And-Swap to solve this issue rather than disable interrupts.
The pseudo-code would be something like this:
Code: Select all
//pid is the pid of the preemptible process
//value is the current value of the lock (INIT/WAIT/FREE)
void wait(){
state[pid] = DEFER;
if(!CAS(value, FREE, WAIT)){
if(CAS(state[pid], DEFER, BLOCKED)){
reschedule();
}
}
state[pid] = RUNNING;
}
void notify(){
if(value == WAIT){
state[pid] = READY;
}
value = FREE;
}
It's harder to adapt for non-binary locks though.
What do you think of this code ? Could this kind of thing work ? Is it worth trying to make it work fully or is it simply better to disable IRQ at this point ?
Thanks