nexos wrote:
Hello,
I am trying to make a thread sleeping system. It will work with timer and IO. I was wondering what the best way to make it is.
Thank you for your help
I use condition variables in general.
Then your interrupt handler signals the condition variable, it'll wake up your thread that is waiting for the interrupt.
So long as your condition variable and mutex are properly implemented, you should never lose any interrupt signals, and will be a good test of your synchronization primitives to ensure they work in an interrupt environment.
My timer is a bit simpler, using just a spinlock to protect timer structures and running callbacks in the interrupt context. But even there, for a thread sleep, I still use a condition variable to communicate between the timer callback and the thread that is sleeping. That way, you can also interrupt the sleep by signalling the condition variable early before the timer expires.