1*4f1223e8SApple OSS Distributions# Atomic Condition Variables for Thread Synchronization 2*4f1223e8SApple OSS Distributions 3*4f1223e8SApple OSS DistributionsQuickly synchronizing when multiple threads could send wakeups. 4*4f1223e8SApple OSS Distributions 5*4f1223e8SApple OSS Distributions## Overview 6*4f1223e8SApple OSS Distributions 7*4f1223e8SApple OSS Distributions`sched_cond_*` (see `sched_prim.h`) provides a means of optimized wake/sleep 8*4f1223e8SApple OSS Distributionssynchronization on kernel threads. Specifically, it provides a wrapper for 9*4f1223e8SApple OSS Distributions`assert_wait`/`thread_block` & `thread_wake` patterns with fast paths. 10*4f1223e8SApple OSS Distributions 11*4f1223e8SApple OSS Distributions## Interfaces 12*4f1223e8SApple OSS Distributions* `sched_cond_t` / `sched_cond_atomic_t` - Atomic condition variable type to synchronize on 13*4f1223e8SApple OSS Distributions* `sched_cond_init(sched_cond_t *cond)` - Initialize the atomic condition var 14*4f1223e8SApple OSS Distributions* `sched_cond_wait(sched_cond_t *cond, ...)` - Set state to inactive and wait for a wakeup on cond 15*4f1223e8SApple OSS Distributions* `sched_cond_signal(sched_cond_t *cond, ...)` - Issue a wakeup on cond for the specified thread 16*4f1223e8SApple OSS Distributions* `sched_cond_ack(sched_cond_t *cond)` - Acknowledge the wakeup on cond and set state to active 17*4f1223e8SApple OSS Distributions 18*4f1223e8SApple OSS Distributions## Limitations of Existing Interfaces 19*4f1223e8SApple OSS Distributions 20*4f1223e8SApple OSS DistributionsConsider the following example of a producer-consumer relationship. 21*4f1223e8SApple OSS Distributions 22*4f1223e8SApple OSS Distributions### Producer Thread 23*4f1223e8SApple OSS Distributions```c 24*4f1223e8SApple OSS Distributionswhile(1) { 25*4f1223e8SApple OSS Distributions ... 26*4f1223e8SApple OSS Distributions thread_wake_thread(..., consumer_thread); // (A) 27*4f1223e8SApple OSS Distributions} 28*4f1223e8SApple OSS Distributions``` 29*4f1223e8SApple OSS Distributions### Consumer Thread 30*4f1223e8SApple OSS Distributions```c 31*4f1223e8SApple OSS Distributionsvoid work_loop_continuation() 32*4f1223e8SApple OSS Distributions{ 33*4f1223e8SApple OSS Distributions // (B) 34*4f1223e8SApple OSS Distributions ... 35*4f1223e8SApple OSS Distributions assert_wait(...); // (C) 36*4f1223e8SApple OSS Distributions thread_block(..., work_loop_continuation); // (D) 37*4f1223e8SApple OSS Distributions} 38*4f1223e8SApple OSS Distributions``` 39*4f1223e8SApple OSS Distributions 40*4f1223e8SApple OSS DistributionsThis scheme has two key inefficiences: 41*4f1223e8SApple OSS Distributions1. Multiple calls to wake the consumer thread (A) may be made before the consumer_thread has awoken. 42*4f1223e8SApple OSS Distributions This results in precious CPU cycles being spent in (A) to wake the thread despite the fact that 43*4f1223e8SApple OSS Distributions it has already been queued. 44*4f1223e8SApple OSS Distributions2. If in the time since waking (B) and blocking (D), the consumer thread has been sent a wakeup (A), 45*4f1223e8SApple OSS Distributions the thread will still yield (D), thus spending precious CPU cycles setting itself up to block only 46*4f1223e8SApple OSS Distributions to be immediately queued once more. 47*4f1223e8SApple OSS Distributions 48*4f1223e8SApple OSS Distributions 49*4f1223e8SApple OSS Distributions## Example Usage 50*4f1223e8SApple OSS Distributions 51*4f1223e8SApple OSS Distributions`sched_cond_t` and its functions provide fast paths for (1) and (2) by wrapping `thread_wake_thread` and 52*4f1223e8SApple OSS Distributions`assert_wait/thread_block` with atomic bit operations. 53*4f1223e8SApple OSS Distributions 54*4f1223e8SApple OSS DistributionsUsing these enhancements, the previous example can be revised to: 55*4f1223e8SApple OSS Distributions 56*4f1223e8SApple OSS Distributions### Producer Thread 57*4f1223e8SApple OSS Distributions```c 58*4f1223e8SApple OSS Distributionswhile(1) { 59*4f1223e8SApple OSS Distributions ... 60*4f1223e8SApple OSS Distributions sched_cond_signal(&my_cond, ..., consumer_thread); // (E) 61*4f1223e8SApple OSS Distributions} 62*4f1223e8SApple OSS Distributions``` 63*4f1223e8SApple OSS Distributions### Consumer Thread 64*4f1223e8SApple OSS Distributions```c 65*4f1223e8SApple OSS Distributionsvoid work_loop_continuation() 66*4f1223e8SApple OSS Distributions{ 67*4f1223e8SApple OSS Distributions sched_cond_ack(&my_cond); // (F) 68*4f1223e8SApple OSS Distributions while (1) { 69*4f1223e8SApple OSS Distributions ... 70*4f1223e8SApple OSS Distributions sched_cond_wait(&my_cond, ..., work_loop_continuation); // (G) 71*4f1223e8SApple OSS Distributions } 72*4f1223e8SApple OSS Distributions} 73*4f1223e8SApple OSS Distributions``` 74*4f1223e8SApple OSS Distributions 75*4f1223e8SApple OSS DistributionsIn this example, the producer thread signals the consumer (E), resulting in an explicit wake (A) iff the consumer is 76*4f1223e8SApple OSS Distributionsnot awake and has not already been issued an un-acked wakeup. Conversely, the consumer acks the wakeup (F) once awake, 77*4f1223e8SApple OSS Distributionssignalling that it is active and clearing the queued wakeup. Once done with its consumption it attempts to wait on the 78*4f1223e8SApple OSS Distributionscond (G), signalling that it is inactive and checking for any wakeups that have been issued since the last ack (F). 79*4f1223e8SApple OSS DistributionsIf a wakeup has been issued, the consumer immediately acks the wakeup and returns to re-enter the work loop. Else, 80*4f1223e8SApple OSS Distributionsit will block as in (D). 81*4f1223e8SApple OSS Distributions 82*4f1223e8SApple OSS Distributions### On acknowledging wakeups 83*4f1223e8SApple OSS Distributions 84*4f1223e8SApple OSS DistributionsOne may note that the adoption of `sched_cond_*` involves adding an additional step (ack) to the consumers work loop. This 85*4f1223e8SApple OSS Distributionsstep is critical for two reasons. 86*4f1223e8SApple OSS Distributions 87*4f1223e8SApple OSS Distributions1. Wakeups can be coalesced without potential loss of data. By ack-ing the wakeup *prior* to doing work, wakeups 88*4f1223e8SApple OSS Distributions that are issued while the thread is active are guaranteed to be observed because the consumer will check for wakeups since the 89*4f1223e8SApple OSS Distributions last ack before giong to sleep. 90*4f1223e8SApple OSS Distributions2. Wakeups need not explicitly `thread_wake` the consumer thread if it is already awake. This is because the consumer thread will not 91*4f1223e8SApple OSS Distributions block if it observes a wakeup has been issued while it was awake. 92*4f1223e8SApple OSS Distributions 93