forked from Safecast/safestream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
47 lines (40 loc) · 1015 Bytes
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2021 safecast. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package main
import (
"time"
)
// Event is our timeout-enabled event waiter abstraction
type Event struct {
eq chan struct{}
}
// EventNew allocates a new semaphore, initially unsignalled
func eventNew() *Event {
evt := Event{}
evt.eq = make(chan struct{}, 1)
return &evt
}
// IsSignalled will return true if the event has been signalled
func (evt *Event) IsSignalled() bool {
return len(evt.eq) != 0
}
// Wait waits for the event until the specified timeout, and
// returns true if acquired, and false if timeout
func (evt *Event) Wait(timeout time.Duration) bool {
select {
case <-evt.eq:
return true
case <-time.After(timeout):
return false
}
}
// Signal ensures that the waiter becomes unblocked
func (evt *Event) Signal() bool {
select {
case evt.eq <- struct{}{}:
return true
default:
return false
}
}