2016-05-13 06:48:53 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-01-25 06:19:16 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:41:22 +04:00
|
|
|
|
2015-11-18 07:54:10 +03:00
|
|
|
// Package wait provides utility functions for polling, listening using Go
|
|
|
|
// channel.
|
2014-08-20 02:54:59 +04:00
|
|
|
package wait
|
|
|
|
|
2014-08-29 03:41:42 +04:00
|
|
|
import (
|
2016-02-04 20:48:21 +03:00
|
|
|
"log"
|
2014-08-29 03:41:42 +04:00
|
|
|
"sync"
|
|
|
|
)
|
2014-08-20 02:54:59 +04:00
|
|
|
|
2014-09-11 04:12:58 +04:00
|
|
|
type Wait interface {
|
2014-10-12 04:35:58 +04:00
|
|
|
Register(id uint64) <-chan interface{}
|
|
|
|
Trigger(id uint64, x interface{})
|
2016-06-16 07:31:07 +03:00
|
|
|
IsRegistered(id uint64) bool
|
2014-09-11 04:12:58 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 05:39:02 +04:00
|
|
|
type List struct {
|
2014-08-20 02:54:59 +04:00
|
|
|
l sync.Mutex
|
2014-10-12 04:35:58 +04:00
|
|
|
m map[uint64]chan interface{}
|
2014-08-20 02:54:59 +04:00
|
|
|
}
|
|
|
|
|
2014-08-29 03:41:42 +04:00
|
|
|
func New() *List {
|
2014-10-12 04:35:58 +04:00
|
|
|
return &List{m: make(map[uint64]chan interface{})}
|
2014-08-20 02:54:59 +04:00
|
|
|
}
|
|
|
|
|
2014-10-12 04:35:58 +04:00
|
|
|
func (w *List) Register(id uint64) <-chan interface{} {
|
2014-08-20 02:54:59 +04:00
|
|
|
w.l.Lock()
|
|
|
|
defer w.l.Unlock()
|
|
|
|
ch := w.m[id]
|
|
|
|
if ch == nil {
|
|
|
|
ch = make(chan interface{}, 1)
|
|
|
|
w.m[id] = ch
|
2016-02-04 20:48:21 +03:00
|
|
|
} else {
|
|
|
|
log.Panicf("dup id %x", id)
|
2014-08-20 02:54:59 +04:00
|
|
|
}
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2014-10-12 04:35:58 +04:00
|
|
|
func (w *List) Trigger(id uint64, x interface{}) {
|
2014-08-20 02:54:59 +04:00
|
|
|
w.l.Lock()
|
|
|
|
ch := w.m[id]
|
|
|
|
delete(w.m, id)
|
|
|
|
w.l.Unlock()
|
|
|
|
if ch != nil {
|
|
|
|
ch <- x
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
}
|
2016-01-04 22:24:49 +03:00
|
|
|
|
2016-06-16 07:31:07 +03:00
|
|
|
func (w *List) IsRegistered(id uint64) bool {
|
|
|
|
w.l.Lock()
|
|
|
|
defer w.l.Unlock()
|
|
|
|
_, ok := w.m[id]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-01-04 22:24:49 +03:00
|
|
|
type waitWithResponse struct {
|
|
|
|
ch <-chan interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWithResponse(ch <-chan interface{}) Wait {
|
|
|
|
return &waitWithResponse{ch: ch}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *waitWithResponse) Register(id uint64) <-chan interface{} {
|
|
|
|
return w.ch
|
|
|
|
}
|
|
|
|
func (w *waitWithResponse) Trigger(id uint64, x interface{}) {}
|
2016-06-16 07:31:07 +03:00
|
|
|
func (w *waitWithResponse) IsRegistered(id uint64) bool {
|
|
|
|
panic("waitWithResponse.IsRegistered() shouldn't be called")
|
|
|
|
}
|