aboutsummaryrefslogtreecommitdiffstats
path: root/event.go
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2018-05-05 02:20:52 +0200
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2018-05-05 02:20:52 +0200
commit6db41d5a269c79bd04b18dbfa171cc241a6cdcc9 (patch)
tree7cfd9a8461b5c66dd971971e3706ada4c8b0484a /event.go
parentAdd missing locks and fix debug output, and try to flush queues (diff)
downloadwireguard-go-6db41d5a269c79bd04b18dbfa171cc241a6cdcc9.tar.xz
wireguard-go-6db41d5a269c79bd04b18dbfa171cc241a6cdcc9.zip
Initial version of migration to new event model
- Begin move away from global timer state. - Made logging format more consistent
Diffstat (limited to 'event.go')
-rw-r--r--event.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/event.go b/event.go
new file mode 100644
index 0000000..d238834
--- /dev/null
+++ b/event.go
@@ -0,0 +1,44 @@
+package main
+
+import (
+ "sync/atomic"
+ "time"
+)
+
+type Event struct {
+ guard int32
+ next time.Time
+ interval time.Duration
+ C chan struct{}
+}
+
+func newEvent(interval time.Duration) *Event {
+ return &Event{
+ guard: 0,
+ next: time.Now(),
+ interval: interval,
+ C: make(chan struct{}, 1),
+ }
+}
+
+func (e *Event) Clear() {
+ select {
+ case <-e.C:
+ default:
+ }
+}
+
+func (e *Event) Fire() {
+ if e == nil || atomic.SwapInt32(&e.guard, 1) != 0 {
+ return
+ }
+ now := time.Now()
+ if e.next.After(now) {
+ select {
+ case e.C <- struct{}{}:
+ default:
+ }
+ e.next = now.Add(e.interval)
+ }
+ atomic.StoreInt32(&e.guard, 0)
+}