Gustavo Niemeyer | 05b1dbd | 2011-02-16 14:11:07 -0500 | [diff] [blame] | 1 | // Copyright 2011 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
Robert Griesemer | 9fc0f15 | 2011-02-23 10:12:38 -0800 | [diff] [blame^] | 4 | |
Gustavo Niemeyer | 05b1dbd | 2011-02-16 14:11:07 -0500 | [diff] [blame] | 5 | package sync |
| 6 | |
| 7 | import "runtime" |
| 8 | |
| 9 | // Cond implements a condition variable, a rendezvous point |
| 10 | // for goroutines waiting for or announcing the occurrence |
| 11 | // of an event. |
| 12 | // |
| 13 | // Each Cond has an associated Locker L (often a *Mutex or *RWMutex), |
| 14 | // which must be held when changing the condition and |
| 15 | // when calling the Wait method. |
| 16 | type Cond struct { |
| 17 | L Locker // held while observing or changing the condition |
| 18 | m Mutex // held to avoid internal races |
| 19 | waiters int // number of goroutines blocked on Wait |
| 20 | sema *uint32 |
| 21 | } |
| 22 | |
| 23 | // NewCond returns a new Cond with Locker l. |
| 24 | func NewCond(l Locker) *Cond { |
| 25 | return &Cond{L: l} |
| 26 | } |
| 27 | |
| 28 | // Wait atomically unlocks c.L and suspends execution |
| 29 | // of the calling goroutine. After later resuming execution, |
| 30 | // Wait locks c.L before returning. |
| 31 | // |
| 32 | // Because L is not locked when Wait first resumes, the caller |
| 33 | // typically cannot assume that the condition is true when |
| 34 | // Wait returns. Instead, the caller should Wait in a loop: |
| 35 | // |
| 36 | // c.L.Lock() |
| 37 | // for !condition() { |
| 38 | // c.Wait() |
| 39 | // } |
| 40 | // ... make use of condition ... |
| 41 | // c.L.Unlock() |
| 42 | // |
| 43 | func (c *Cond) Wait() { |
| 44 | c.m.Lock() |
| 45 | if c.sema == nil { |
| 46 | c.sema = new(uint32) |
| 47 | } |
| 48 | s := c.sema |
| 49 | c.waiters++ |
| 50 | c.m.Unlock() |
| 51 | c.L.Unlock() |
| 52 | runtime.Semacquire(s) |
| 53 | c.L.Lock() |
| 54 | } |
| 55 | |
| 56 | // Signal wakes one goroutine waiting on c, if there is any. |
| 57 | // |
| 58 | // It is allowed but not required for the caller to hold c.L |
| 59 | // during the call. |
| 60 | func (c *Cond) Signal() { |
| 61 | c.m.Lock() |
| 62 | if c.waiters > 0 { |
| 63 | c.waiters-- |
| 64 | runtime.Semrelease(c.sema) |
| 65 | } |
| 66 | c.m.Unlock() |
| 67 | } |
| 68 | |
| 69 | // Broadcast wakes all goroutines waiting on c. |
| 70 | // |
| 71 | // It is allowed but not required for the caller to hold c.L |
| 72 | // during the call. |
| 73 | func (c *Cond) Broadcast() { |
| 74 | c.m.Lock() |
| 75 | if c.waiters > 0 { |
| 76 | s := c.sema |
| 77 | n := c.waiters |
| 78 | for i := 0; i < n; i++ { |
| 79 | runtime.Semrelease(s) |
| 80 | } |
| 81 | // We just issued n wakeups via the semaphore s. |
| 82 | // To ensure that they wake up the existing waiters |
| 83 | // and not waiters that arrive after Broadcast returns, |
| 84 | // clear c.sema. The next operation will allocate |
| 85 | // a new one. |
| 86 | c.sema = nil |
| 87 | c.waiters = 0 |
| 88 | } |
| 89 | c.m.Unlock() |
| 90 | } |