Page 1 of 1

Conditional Suspension/Resumption of Threads

Posted: Sat Aug 05, 2023 10:25 pm
by Stkyrdknmblz
Hello everyone,
I am still relatively new to operating systems and I am learning about POSIX with the intention of eventually developing my own OS from scratch. In POSIX, it seems there is an option to suspend a thread until some condition is met.

I designed a similar mechanism which allows for entire logical expressions to be evaluated rather than just one conditional variable. Furthermore, it can be used to both suspend the thread if the thread is active/running and resume the thread if it is stopped.

What are your thoughts? I thought this would be a good extension, but I would like to know your opinion.

Thanks!

Re: Conditional Suspension/Resumption of Threads

Posted: Wed Aug 09, 2023 6:10 am
by devc1
So from what I understand, you are suspending the thread, and creating an evaluation function that either returns true or false ? this function is executed each time the scheduler finds the thread suspended and if the functions returns true the thread is no longer suspended ?

Personnaly, for example with a disk driver I will just suspend the thread, wait for the interrupt and then the interrupt handler will resume the thread, the thread is looping SuspendThread() until the condition is met (which is probably some value or boolean set by the interrupt handler or a process waiting for IPC), if it's not (for e.g. some other thread tries to wake up the thread) it just suspends itself again. A second SuspendThread() could be dangerous though, It may result in a deadlock but I will fix it later.

Re: Conditional Suspension/Resumption of Threads

Posted: Wed Aug 09, 2023 6:24 am
by davmac314
Stkyrdknmblz wrote:Hello everyone,
In POSIX, it seems there is an option to suspend a thread until some condition is met.
If you mean condition variables, you are misunderstanding what they are, I think.
I designed a similar mechanism which allows for entire logical expressions to be evaluated rather than just one conditional variable.
A POSIX condition variable is just a synchronisation device. It does not reflect the actual condition itself. You can already tie "entire logical expressions" to condition variables in POSIX; an application does this by signalling the condition variable when it detects (by whatever means) that the logical expression has become true.
Furthermore, it can be used to both suspend the thread if the thread is active/running and resume the thread if it is stopped.
POSIX condition variables achieve both these goals. A thread suspends on the condition variable after checking the condition, determining that it is false, and deciding to wait until another thread makes it true. When another thread makes the condition true, it signals the condition variable and the waiting thread then resumes.

It sound like you plan to make this suspension/resumption automatic, rather than requiring explicit action by the application. As well as being difficult to implement (assuming it as general as you make it sound), I think this would be less useful since it makes it much more difficult to understand when the thread might become blocked. Also, you need some way of assigning which thread(s) should be blocked when the condition is false. That is normally explicit since a thread itself chooses to wait.