1

I am studying the locking mechanisms in an OS and came across these POSIX functions:

pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m);
pthread_cond_signal(pthread_cond_t *c);

I fully understand the idea behind the sleep/wake up here. But I am not sure how is this done in HW and how is it affecting scheduling...etc

My understanding that when a thread executes: pthread_cond_wait() it goes to sleep (blocks), but what does this actually mean? Yes it gets de-scheduled and moved into a blocked state somewhere in a privileged queue, But when another process executes pthread_cond_signal(), how does the CPU wakes up the blocked thread? Is it during the timer interrupt that the kernel goes and checks all condition variables and then decides to wake up a thread associated with one that got freed up?

I couldn't find a detailed explanation on this anywhere online, or maybe I am not looking correctly.

Any help is highly appreciated.

  • Nothing of this is done in hardware (except for the regular interrupt that the kernel uses to end the thread/process time slices). All of this is done by the kernel, manipulating kernel data structures. – dirkt Sep 30 '17 at 18:59
  • Thanks for your response. Ok so it is done in the kernel. How does the kernel wake up a process? Is there an array of condition variables that the OS keep checking when it runs the tick interrupt? – SFbay007 Sep 30 '17 at 19:01
  • 2
    A process/thread is woken up but inserting it in the queue of processes/threads to be scheduled. If you look at the source of signal, there is a user-space list of threads waiting on the conditional variable, and one of those gets woken with a futex system call. Checking an array of condition variables every tick would be very slow ... – dirkt Sep 30 '17 at 19:18
  • 1
    Threads are implemented as a mix of userspace and kernel space. Just like a thread/process waiting on I/O is put on a kernel-space list to be rescheduled when that particular I/O operation completes, a thread waiting on a condition variable is put on a user-space list of this condition variable. So the answer to your question is "yes and no". :-) And the current thread implementation has been optimized over the years, so don't get hung up when the implementation doesn't match the (more simple) principles. – dirkt Sep 30 '17 at 19:24
  • 2
    I think this is a great question, and it deserves an answer somewhere other than the comments, @dirkt :) – theMayer Feb 15 '18 at 23:22
1

A process/thread is woken up by inserting it in the queue of processes/threads to be scheduled. If you look at the source of signal, there is a user-space list of threads waiting on the conditional variable, and one of those gets woken with a futex system call. Checking an array of condition variables every tick would be very slow ...

So when they describe the thread being blocked, it doesn't mean it is being put in a blocked queue, like threads waiting on I/O?

Threads are implemented as a mix of userspace and kernel space. Just like a thread/process waiting on I/O is put on a kernel-space list to be rescheduled when that particular I/O operation completes, a thread waiting on a condition variable is put on a user-space list of this condition variable. So the answer to your question is "yes and no". :-)

And the current thread implementation has been optimized over the years, so don't get hung up when the implementation doesn't match the (more simple) principles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.