OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [sync/] [cond.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
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
 
20
        // We must be careful to make sure that when Signal
21
        // releases a semaphore, the corresponding acquire is
22
        // executed by a goroutine that was already waiting at
23
        // the time of the call to Signal, not one that arrived later.
24
        // To ensure this, we segment waiting goroutines into
25
        // generations punctuated by calls to Signal.  Each call to
26
        // Signal begins another generation if there are no goroutines
27
        // left in older generations for it to wake.  Because of this
28
        // optimization (only begin another generation if there
29
        // are no older goroutines left), we only need to keep track
30
        // of the two most recent generations, which we call old
31
        // and new.
32
        oldWaiters int     // number of waiters in old generation...
33
        oldSema    *uint32 // ... waiting on this semaphore
34
 
35
        newWaiters int     // number of waiters in new generation...
36
        newSema    *uint32 // ... waiting on this semaphore
37
}
38
 
39
// NewCond returns a new Cond with Locker l.
40
func NewCond(l Locker) *Cond {
41
        return &Cond{L: l}
42
}
43
 
44
// Wait atomically unlocks c.L and suspends execution
45
// of the calling goroutine.  After later resuming execution,
46
// Wait locks c.L before returning.
47
//
48
// Because L is not locked when Wait first resumes, the caller
49
// typically cannot assume that the condition is true when
50
// Wait returns.  Instead, the caller should Wait in a loop:
51
//
52
//    c.L.Lock()
53
//    for !condition() {
54
//        c.Wait()
55
//    }
56
//    ... make use of condition ...
57
//    c.L.Unlock()
58
//
59
func (c *Cond) Wait() {
60
        c.m.Lock()
61
        if c.newSema == nil {
62
                c.newSema = new(uint32)
63
        }
64
        s := c.newSema
65
        c.newWaiters++
66
        c.m.Unlock()
67
        c.L.Unlock()
68
        runtime.Semacquire(s)
69
        c.L.Lock()
70
}
71
 
72
// Signal wakes one goroutine waiting on c, if there is any.
73
//
74
// It is allowed but not required for the caller to hold c.L
75
// during the call.
76
func (c *Cond) Signal() {
77
        c.m.Lock()
78
        if c.oldWaiters == 0 && c.newWaiters > 0 {
79
                // Retire old generation; rename new to old.
80
                c.oldWaiters = c.newWaiters
81
                c.oldSema = c.newSema
82
                c.newWaiters = 0
83
                c.newSema = nil
84
        }
85
        if c.oldWaiters > 0 {
86
                c.oldWaiters--
87
                runtime.Semrelease(c.oldSema)
88
        }
89
        c.m.Unlock()
90
}
91
 
92
// Broadcast wakes all goroutines waiting on c.
93
//
94
// It is allowed but not required for the caller to hold c.L
95
// during the call.
96
func (c *Cond) Broadcast() {
97
        c.m.Lock()
98
        // Wake both generations.
99
        if c.oldWaiters > 0 {
100
                for i := 0; i < c.oldWaiters; i++ {
101
                        runtime.Semrelease(c.oldSema)
102
                }
103
                c.oldWaiters = 0
104
        }
105
        if c.newWaiters > 0 {
106
                for i := 0; i < c.newWaiters; i++ {
107
                        runtime.Semrelease(c.newSema)
108
                }
109
                c.newWaiters = 0
110
                c.newSema = nil
111
        }
112
        c.m.Unlock()
113
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.