-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.go
152 lines (129 loc) · 3.02 KB
/
timer.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//go:build !windows
// +build !windows
package rtc
import (
"fmt"
"sync/atomic"
"syscall"
"time"
)
type Alarm struct {
Time time.Time
}
type Timer struct {
done chan struct{}
rtc *RTC
fired atomic.Bool
C <-chan Alarm
}
// NewTimerAt creates a new Timer that will send an Alarm on its channel after the given time.
func NewTimerAt(dev string, t time.Time) (*Timer, error) {
c, err := NewRTC(dev)
if err != nil {
return nil, err
}
if err := c.SetAlarm(t); err != nil {
_ = c.Close()
return nil, err
}
if err := c.SetAlarmInterrupt(true); err != nil {
_ = c.Close()
return nil, err
}
// Give the channel a 1-element time buffer.
// If the client falls behind while reading, we drop ticks
// on the floor until the client catches up.
ch := make(chan Alarm, 1)
timer := &Timer{
done: make(chan struct{}),
rtc: c,
C: ch,
}
go func() {
buf := make([]byte, 4)
_, err := syscall.Read(c.fd, buf)
if err != nil {
fmt.Printf("got error reading interrupt, returning\n")
return
}
select {
case <-timer.done:
// Don't send alarm if Stop() has been called
default:
timer.fired.Store(true)
}
// buf[0] = bit mask encoding the types of interrupt that occurred.
// buf[1:3] = number of interrupts since last read
//r := binary.LittleEndian.Uint32(buf)
//irqTypes := r & 0x000000FF
//fmt.Printf("r: 0x%X, types: 0x%X\n", r, irqTypes)
//cnt := r >> 8
ch <- Alarm{
Time: time.Now(),
}
}()
return timer, nil
}
// NewTimer creates a new Timer that will send an Alarm with the current time on its channel after at least duration d.
func NewTimer(dev string, d time.Duration) (*Timer, error) {
c, err := NewRTC(dev)
if err != nil {
return nil, err
}
t, err := c.GetTime()
if err != nil {
return nil, err
}
if err := c.SetAlarm(t.Add(d)); err != nil {
_ = c.Close()
return nil, err
}
if err := c.SetAlarmInterrupt(true); err != nil {
_ = c.Close()
return nil, err
}
ch := make(chan Alarm, 1)
buf := make([]byte, 4)
timer := &Timer{
done: make(chan struct{}),
rtc: c,
C: ch,
}
go func() {
_, err := syscall.Read(c.fd, buf)
if err != nil {
fmt.Printf("got error reading interrupt, returning: %v\n", err)
return
}
select {
case <-timer.done:
// Don't send alarm if Stop() has been called
default:
}
ch <- Alarm{
Time: time.Now(),
}
}()
return timer, nil
}
// Stop prevents the Timer from firing.
// It returns true if the call stops the timer, false if the timer has already
// expired or been stopped.
// Stop does not close the channel, to prevent a read from the channel succeeding
// incorrectly.
//
// To ensure the channel is empty after a call to Stop, check the
// return value and drain the channel.
// For example, assuming the program has not received from t.C already:
//
// if !t.Stop() {
// <-t.C
// }
//
// This cannot be done concurrent to other receives from the Timer's
// channel or other calls to the Timer's Stop method.
func (t *Timer) Stop() bool {
close(t.done)
_ = t.rtc.Close()
return t.fired.Load()
}