2

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.

7
  • 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. Commented Sep 30, 2017 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? Commented Sep 30, 2017 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 ... Commented Sep 30, 2017 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. Commented Sep 30, 2017 at 19:24
  • 2
    I think this is a great question, and it deserves an answer somewhere other than the comments, @dirkt :) Commented Feb 15, 2018 at 23:22

2 Answers 2

2

When a thread is called the sleep method, the thread will be added into a sleep queue. If the compute clock frequency is 100HZ, that means every 10ms the current running process will be interrupted. After reserve the current context of the thread, then it will decrease the value (-10ms) for each thread. When it comes to zero, the thread will move to "waiting for CPU" queue. When time slice comes to this thread, it will be running again. Also because this which not immediately become running, so the time actually sleeps is larger than the value it set.

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.