blob: 103ca1ed6fb057cde94d6d065d89665c3cad1b15 (
plain)
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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package vsphere
import (
"sync"
"time"
)
func newTask(doWork func(), doEvery time.Duration) *task {
task := task{
done: make(chan struct{}),
running: make(chan struct{}),
}
go func() {
t := time.NewTicker(doEvery)
defer func() {
t.Stop()
close(task.running)
}()
for {
select {
case <-task.done:
return
case <-t.C:
doWork()
}
}
}()
return &task
}
type task struct {
once sync.Once
done chan struct{}
running chan struct{}
}
func (t *task) stop() {
t.once.Do(func() { close(t.done) })
}
func (t *task) isStopped() bool {
select {
case <-t.done:
return true
default:
return false
}
}
func (t *task) isRunning() bool {
select {
case <-t.running:
return false
default:
return true
}
}
|