Welcome! Log In Create A New Profile


Data structure for waking thread from interrupt handler (binary semaphore?)

Posted by Jacob Feder 
Hi all,
I'm writing a device driver for an IP core in a Zynq (Xilinx FPGA SoC). I have what I think would be a fairly standard concurrency/locking requirement, but can't seem to find the right data structure for the task.

I have a write() function which tries to write to a buffer. If the buffer is full, it should sleep until an interrupt occurs. An interrupt occurs when some data has exited the buffer. This does not guarantee that the writer now has enough room to write to the buffer, but should wake up the writer so it can check.

I currently implemented this using wait_event() in the writer and wake_up() in the interrupt. The problem is that it hangs (I think...) in the case:
-> writer checks to see if there is room in buffer, but there is no room
-> interrupts occur until the buffer is empty
-> writer sleeps, waiting indefinitely for interrupts to occur and wake it

I think a binary semaphore is what I need here (I used this in FreeRTOS and it seemed to work well), but it doesn't appear that linux has one.

Any ideas?

(if you want to take a look at the code: GitHub)


I'll answer this one for myself smiling smiley

I was on the right track - but there is a function called wait_event_interruptible_lock_irq_timeout(). In order to prevent the trouble case you can use this function and call spin_lock_irq() before in order to block interrupts while wait_event is checking the condition variable.


Your Email:


Spam prevention:
Please, solve the mathematical question and enter the answer in the input field below. This is for blocking bots that try to post this form automatically.
Question: how much is 21 plus 17?