OLD | NEW |
1 // Copyright 2012, 2013 Canonical Ltd. | 1 // Copyright 2012, 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 // Package voyeur implements a concurrency-safe value that can be watched for | 4 // Package voyeur implements a concurrency-safe value that can be watched for |
5 // changes. | 5 // changes. |
6 package voyeur | 6 package voyeur |
7 | 7 |
8 import ( | 8 import ( |
9 "sync" | 9 "sync" |
10 ) | 10 ) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 // returns nil. | 57 // returns nil. |
58 func (v *Value) Close() error { | 58 func (v *Value) Close() error { |
59 v.mu.Lock() | 59 v.mu.Lock() |
60 v.init() | 60 v.init() |
61 v.closed = true | 61 v.closed = true |
62 v.mu.Unlock() | 62 v.mu.Unlock() |
63 v.wait.Broadcast() | 63 v.wait.Broadcast() |
64 return nil | 64 return nil |
65 } | 65 } |
66 | 66 |
67 // Get returns the current value. If the Value has been closed, ok will be | 67 // Closed reports whether the value has been closed. |
68 // false. | 68 func (v *Value) Closed() bool { |
69 func (v *Value) Get() (val interface{}, ok bool) { | |
70 v.mu.RLock() | 69 v.mu.RLock() |
71 defer v.mu.RUnlock() | 70 defer v.mu.RUnlock() |
72 » if v.closed { | 71 » return v.closed |
73 » » return v.val, false | 72 } |
74 » } | 73 |
75 » return v.val, true | 74 // Get returns the current value. |
| 75 func (v *Value) Get() interface{} { |
| 76 » v.mu.RLock() |
| 77 » defer v.mu.RUnlock() |
| 78 » return v.val |
76 } | 79 } |
77 | 80 |
78 // Watch returns a Watcher that can be used to watch for changes to the value. | 81 // Watch returns a Watcher that can be used to watch for changes to the value. |
79 func (v *Value) Watch() *Watcher { | 82 func (v *Value) Watch() *Watcher { |
80 return &Watcher{value: v} | 83 return &Watcher{value: v} |
81 } | 84 } |
82 | 85 |
83 // Watcher represents a single watcher of a shared value. | 86 // Watcher represents a single watcher of a shared value. |
84 type Watcher struct { | 87 type Watcher struct { |
85 value *Value | 88 value *Value |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 w.closed = true | 138 w.closed = true |
136 w.value.mu.Unlock() | 139 w.value.mu.Unlock() |
137 w.value.wait.Broadcast() | 140 w.value.wait.Broadcast() |
138 } | 141 } |
139 | 142 |
140 // Value returns the last value that was retrieved from the watched Value by | 143 // Value returns the last value that was retrieved from the watched Value by |
141 // Next. | 144 // Next. |
142 func (w *Watcher) Value() interface{} { | 145 func (w *Watcher) Value() interface{} { |
143 return w.current | 146 return w.current |
144 } | 147 } |
OLD | NEW |