2011-03-17 00:05:44 +01:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package sync
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// Cond implements a condition variable, a rendezvous point
|
|
|
|
// for goroutines waiting for or announcing the occurrence
|
|
|
|
// of an event.
|
|
|
|
//
|
|
|
|
// Each Cond has an associated Locker L (often a *Mutex or *RWMutex),
|
|
|
|
// which must be held when changing the condition and
|
|
|
|
// when calling the Wait method.
|
2013-11-06 20:49:01 +01:00
|
|
|
//
|
|
|
|
// A Cond must not be copied after first use.
|
2011-03-17 00:05:44 +01:00
|
|
|
type Cond struct {
|
2016-07-22 20:15:38 +02:00
|
|
|
noCopy noCopy
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// L is held while observing or changing the condition
|
|
|
|
L Locker
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
notify notifyList
|
2013-11-06 20:49:01 +01:00
|
|
|
checker copyChecker
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCond returns a new Cond with Locker l.
|
|
|
|
func NewCond(l Locker) *Cond {
|
|
|
|
return &Cond{L: l}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait atomically unlocks c.L and suspends execution
|
2016-07-22 20:15:38 +02:00
|
|
|
// of the calling goroutine. After later resuming execution,
|
|
|
|
// Wait locks c.L before returning. Unlike in other systems,
|
2012-03-02 21:01:37 +01:00
|
|
|
// Wait cannot return unless awoken by Broadcast or Signal.
|
2011-03-17 00:05:44 +01:00
|
|
|
//
|
2012-03-02 21:01:37 +01:00
|
|
|
// Because c.L is not locked when Wait first resumes, the caller
|
2011-03-17 00:05:44 +01:00
|
|
|
// typically cannot assume that the condition is true when
|
2016-07-22 20:15:38 +02:00
|
|
|
// Wait returns. Instead, the caller should Wait in a loop:
|
2011-03-17 00:05:44 +01:00
|
|
|
//
|
|
|
|
// c.L.Lock()
|
|
|
|
// for !condition() {
|
|
|
|
// c.Wait()
|
|
|
|
// }
|
|
|
|
// ... make use of condition ...
|
|
|
|
// c.L.Unlock()
|
|
|
|
//
|
|
|
|
func (c *Cond) Wait() {
|
2013-11-06 20:49:01 +01:00
|
|
|
c.checker.check()
|
2016-07-22 20:15:38 +02:00
|
|
|
t := runtime_notifyListAdd(&c.notify)
|
2011-03-17 00:05:44 +01:00
|
|
|
c.L.Unlock()
|
2016-07-22 20:15:38 +02:00
|
|
|
runtime_notifyListWait(&c.notify, t)
|
2011-03-17 00:05:44 +01:00
|
|
|
c.L.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal wakes one goroutine waiting on c, if there is any.
|
|
|
|
//
|
|
|
|
// It is allowed but not required for the caller to hold c.L
|
|
|
|
// during the call.
|
|
|
|
func (c *Cond) Signal() {
|
2016-07-22 20:15:38 +02:00
|
|
|
c.checker.check()
|
|
|
|
runtime_notifyListNotifyOne(&c.notify)
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Broadcast wakes all goroutines waiting on c.
|
|
|
|
//
|
|
|
|
// It is allowed but not required for the caller to hold c.L
|
|
|
|
// during the call.
|
|
|
|
func (c *Cond) Broadcast() {
|
2013-11-06 20:49:01 +01:00
|
|
|
c.checker.check()
|
2016-07-22 20:15:38 +02:00
|
|
|
runtime_notifyListNotifyAll(&c.notify)
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// copyChecker holds back pointer to itself to detect object copying.
|
|
|
|
type copyChecker uintptr
|
|
|
|
|
|
|
|
func (c *copyChecker) check() {
|
|
|
|
if uintptr(*c) != uintptr(unsafe.Pointer(c)) &&
|
|
|
|
!atomic.CompareAndSwapUintptr((*uintptr)(c), 0, uintptr(unsafe.Pointer(c))) &&
|
|
|
|
uintptr(*c) != uintptr(unsafe.Pointer(c)) {
|
|
|
|
panic("sync.Cond is copied")
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
|
|
|
|
// noCopy may be embedded into structs which must not be copied
|
|
|
|
// after the first use.
|
|
|
|
//
|
2018-01-09 02:23:08 +01:00
|
|
|
// See https://golang.org/issues/8005#issuecomment-190753527
|
2016-07-22 20:15:38 +02:00
|
|
|
// for details.
|
|
|
|
type noCopy struct{}
|
|
|
|
|
|
|
|
// Lock is a no-op used by -copylocks checker from `go vet`.
|
2018-09-24 23:46:21 +02:00
|
|
|
func (*noCopy) Lock() {}
|
|
|
|
func (*noCopy) Unlock() {}
|