-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheap.go
253 lines (201 loc) · 5.89 KB
/
heap.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package worker
import "sync"
// // taskHeap is a heap of tasks that implements the heap interface
// type taskHeap struct {
// tasks []Task
// numActive int
// mutex sync.Mutex
// waitersLock sync.Mutex
// waiters []*sync.Cond
// }
// func newTaskHeap() *taskHeap {
// th := &taskHeap{
// tasks: make([]Task, 0),
// numActive: 0,
// }
// th.waitersLock = sync.Mutex{}
// th.mutex = sync.Mutex{}
// th.waiters = make([]*sync.Cond, 0)
// return th
// }
// // Len returns the number of tasks in the heap
// func (th *taskHeap) Len() int {
// th.mutex.Lock()
// defer th.mutex.Unlock()
// return len(th.tasks)
// }
// // Less returns whether the task with index i should sort before the task with index j in the heap
// func (th *taskHeap) Less(i, j int) bool {
// th.mutex.Lock()
// defer th.mutex.Unlock()
// // sort by priority first
// if th.tasks[i].Priority != th.tasks[j].Priority {
// return th.tasks[i].Priority < th.tasks[j].Priority
// }
// // if priorities are the same, sort by ID
// return th.tasks[i].ID.String() < th.tasks[j].ID.String()
// }
// // Swap swaps the tasks with indexes i and j in the heap
// func (th *taskHeap) Swap(i, j int) {
// th.mutex.Lock()
// defer th.mutex.Unlock()
// th.tasks[i], th.tasks[j] = th.tasks[j], th.tasks[i]
// th.tasks[i].index = i
// th.tasks[j].index = j
// }
// // Push adds a task to the heap
// func (th *taskHeap) Push(x interface{}) {
// task := x.(Task)
// // Lock the mutex to access the tasks slice
// th.mutex.Lock()
// defer th.mutex.Unlock()
// // Add the task to the tasks slice
// th.tasks = append(th.tasks, task)
// // If there are no waiters, there's nothing else to do
// if len(th.waiters) == 0 {
// return
// }
// // Wake up the first waiter
// waiter := th.waiters[0]
// th.waiters = th.waiters[1:]
// waiter.Signal()
// }
// // Pop removes and returns the highest priority task from the heap
// func (th *taskHeap) Pop() interface{} {
// th.mutex.Lock()
// defer th.mutex.Unlock()
// if len(th.tasks) == 0 {
// return nil
// }
// // swap the first and last elements
// n := len(th.tasks)
// task := th.tasks[0]
// th.tasks[0], th.tasks[n-1] = th.tasks[n-1], th.tasks[0]
// th.tasks = th.tasks[:n-1]
// // notify waiting goroutines that a new task is available
// th.notifyWaiters()
// return task
// }
// // notifyWaiters notifies waiting goroutines that a new task is available
// func (th *taskHeap) notifyWaiters() {
// th.waitersLock.Lock()
// defer th.waitersLock.Unlock()
// for _, waiter := range th.waiters {
// waiter.Signal()
// }
// th.waiters = []*sync.Cond{}
// }
// // Wait waits for the task heap to be non-empty and returns a channel that signals when a task is added to the heap.
// func (th *taskHeap) Wait() *sync.Cond {
// // create a new condition variable for the waiters
// waiter := sync.NewCond(&th.waitersLock)
// // add the condition variable to the list of waiters
// th.waitersLock.Lock()
// th.waiters = append(th.waiters, waiter)
// th.waitersLock.Unlock()
// return waiter
// }
// // Unlock releases the lock.
// func (th *taskHeap) Unlock() {
// th.mutex.Unlock()
// }
// // Lock acquires the lock.
// func (th *taskHeap) Lock() {
// th.mutex.Lock()
// }
// taskHeap is a heap of tasks that implements the heap interface
type taskHeap struct {
tasks []Task
mutex sync.Mutex
waitersLock sync.RWMutex
waiters []*sync.Cond
}
func newTaskHeap() *taskHeap {
return &taskHeap{
tasks: make([]Task, 0),
}
}
// Len returns the number of tasks in the heap
func (th *taskHeap) Len() int {
th.mutex.Lock()
defer th.mutex.Unlock()
return len(th.tasks)
}
// Less returns whether the task with index i should sort before the task with index j in the heap
func (th *taskHeap) Less(i, j int) bool {
th.mutex.Lock()
defer th.mutex.Unlock()
// sort by priority first
if th.tasks[i].Priority != th.tasks[j].Priority {
return th.tasks[i].Priority < th.tasks[j].Priority
}
// if priorities are the same, sort by ID
return th.tasks[i].ID.String() < th.tasks[j].ID.String()
}
// Swap swaps the tasks with indexes i and j in the heap
func (th *taskHeap) Swap(i, j int) {
th.mutex.Lock()
defer th.mutex.Unlock()
th.tasks[i], th.tasks[j] = th.tasks[j], th.tasks[i]
th.tasks[i].index = i
th.tasks[j].index = j
}
// Push adds a task to the heap
func (th *taskHeap) Push(x interface{}) {
task := x.(Task)
// Lock the mutex to access the tasks slice
th.mutex.Lock()
defer th.mutex.Unlock()
// Add the task to the tasks slice
th.tasks = append(th.tasks, task)
// Wake up the first waiter
if len(th.waiters) > 0 {
waiter := th.waiters[0]
th.waiters = th.waiters[1:]
waiter.Signal()
}
}
// Pop removes and returns the highest priority task from the heap
func (th *taskHeap) Pop() interface{} {
th.mutex.Lock()
defer th.mutex.Unlock()
if len(th.tasks) == 0 {
return nil
}
// swap the first and last elements
n := len(th.tasks)
task := th.tasks[0]
th.tasks[0], th.tasks[n-1] = th.tasks[n-1], th.tasks[0]
th.tasks = th.tasks[:n-1]
// notify waiting goroutines that a new task is available
th.notifyWaiters()
return task
}
// notifyWaiters notifies waiting goroutines that a new task is available
func (th *taskHeap) notifyWaiters() {
th.waitersLock.Lock()
waiters := th.waiters
th.waiters = nil
th.waitersLock.Unlock()
for _, waiter := range waiters {
waiter.Signal()
}
}
// Wait waits for the task heap to be non-empty and returns a channel that signals when a task is added to the heap.
func (th *taskHeap) Wait() *sync.Cond {
// create a new condition variable for the waiters
waiter := sync.NewCond(&th.waitersLock)
// add the condition variable to the list of waiters
th.waitersLock.Lock()
th.waiters = append(th.waiters, waiter)
th.waitersLock.Unlock()
return waiter
}
// Unlock releases the lock.
func (th *taskHeap) Unlock() {
th.mutex.Unlock()
}
// Lock acquires the lock.
func (th *taskHeap) Lock() {
th.mutex.Lock()
}