-
Notifications
You must be signed in to change notification settings - Fork 21
/
jobmap.go
86 lines (72 loc) · 1.34 KB
/
jobmap.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
package core
import (
//"fmt"
)
type JobMap struct {
RWMutex
_map map[string]*Job
}
func NewJobMap() (t *JobMap) {
t = &JobMap{
_map: make(map[string]*Job),
}
t.RWMutex.Init("JobMap")
return t
}
//--------job accessor methods-------
func (jm *JobMap) Len() (length int, err error) {
read_lock, err := jm.RLockNamed("Len")
if err != nil {
return
}
defer jm.RUnlockNamed(read_lock)
length = len(jm._map)
return
}
func (jm *JobMap) Get(jobid string, lock bool) (job *Job, ok bool, err error) {
if lock {
read_lock, xerr := jm.RLockNamed("Get")
if xerr != nil {
err = xerr
return
}
defer jm.RUnlockNamed(read_lock)
}
job, ok = jm._map[jobid]
return
}
func (jm *JobMap) Add(job *Job) (err error) {
err = jm.LockNamed("Add")
if err != nil {
return
}
defer jm.Unlock()
jm._map[job.Id] = job // TODO prevent overwriting
return
}
func (jm *JobMap) Delete(jobid string, lock bool) (err error) {
if lock {
err = jm.LockNamed("Delete")
if err != nil {
return
}
defer jm.Unlock()
}
delete(jm._map, jobid)
return
}
func (jm *JobMap) Get_List(lock bool) (jobs []*Job, err error) {
if lock {
var read_lock ReadLock
read_lock, err = jm.RLockNamed("Get_List")
if err != nil {
return
}
defer jm.RUnlockNamed(read_lock)
}
jobs = []*Job{}
for _, job := range jm._map {
jobs = append(jobs, job)
}
return
}