forked from canonical/iot-agent
/
taskset.go
44 lines (35 loc) · 911 Bytes
/
taskset.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
package legacy
import "sync"
type taskSet struct {
tasks map[SubscribeAction]bool
mut sync.Mutex
}
func NewTaskSet() *taskSet {
t := &taskSet{}
t.tasks = make(map[SubscribeAction]bool)
return t
}
// remove id values from a copy of subscribe action so that
// they can be useful within a task set (The ids are unique on a per
// message basis so otherwise everything will be unique)
func (t *taskSet) homogenizeAction(act SubscribeAction) SubscribeAction {
act.Id = ""
return act
}
func (t *taskSet) Add(task TaskInput) {
act := t.homogenizeAction(*task.input)
t.mut.Lock()
t.tasks[act] = true
t.mut.Unlock()
}
func (t *taskSet) Remove(task TaskInput) {
act := t.homogenizeAction(*task.input)
t.mut.Lock()
delete(t.tasks, act)
t.mut.Unlock()
}
func (t *taskSet) Contains(tasks TaskInput) bool {
act := t.homogenizeAction(*tasks.input)
_, contained := t.tasks[act]
return contained
}