9
9
4
5
5
6
3 1
(a)
9
8
4
5
5
4
6
2
9
7
4
5
5
6 1
(b)
(c)
9
6
4
5
5
5
(d)
9 5
4
5
5
4
6
9
8 4
4
5
5
3
(e)
(f)
H
T
H
T
H
T
H
T
H
T
H
T
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4
71
7
2
7
3
7
7
12
Figure 1. Work Stealing: An Illustration
tion strategy known as fast/slow clones, and a distinct solu-
tion for locking [16]. Both are out of the scope of this paper.
Deque Management One natural consequence of placing
continuations onto the deque is that the order of tasks on
the deque reflects the immediacy of processing these items
as defined by the work-first principle: the earlier the item is
placed, the less immediate it is. For example, if the control
flow of a worker reaches L10, two tasks are placed on the
deque, the program counter for L3 (when the spawn in L2 is
executed) and the program counter for L7 (when the spawn
in L6 is executed). In a serial execution, L3 will only be
encountered after L7.
With this observation, deque is designed as a data struc-
ture that can be manipulated on both ends. Let us call the
head of the deque as the earliest item placed on the deque by
the worker, whereas the tail of the deque as the latest. When
a worker becomes idle, it always retrieves from the tail of
Algorithm 2.1 Worker
w : WORKER
procedure SCHEDULE(w)
loop
t ← POP(w)
if t==null then
v = SELECT()
t ← STEAL(v)
if t==null then
YIELD(w)
else
WORK(w, t)
end if
else
WORK(w, t)
end if
end loop
end procedure
Structures
structure WORKER
DQ // deque (array)
H // head index
T // tail index
end structure
structure TASK
... // program counter, etc
end structure
Other Definitions
procedure WORK(w, t)
// worker w runs task t
procedure SELECT()
// select and return a victim
procedure YIELD(w)
// yield worker w
procedure LOCK(w)
procedure UNLOCK(w)
// lock/unlock w
Algorithm 2.2 Push
w : WORKER
t : TASK
procedure PUSH(w,t)
w.T++
w.DQ[w.T] ← t
end procedure
Algorithm 2.3 Pop
w : WORKER
procedure POP(w)
w.T– –
if w.H > w.T then
w.T++
LOCK(w)
w.T−−
if w.H > w.T then
w.T++
UNLOCK(w)
return null
end if
end if
UNLOCK(w)
return w.DQ[w.T]
end procedure
Algorithm 2.4 Steal
v : WORKER // victim
procedure STEAL(v)
LOCK(v)
v.H++
if v.H > v.T then
v.H– –
UNLOCK(v)
return null
end if
UNLOCK(v)
return v.DQ[v.H]
end procedure
Figure 2. Work Stealing Algorithm
its own deque, i.e., the most immediate task. On the other
hand, when a thief attempts to steal from a worker, it always
retrieves from the head of that worker’s deque, i.e., the least
immediate task. From now on, we call the worker placing a
task to its own deque a push, while removing a task from its
own deque a pop. We continue to use term steal to refer to a
worker removing a task from another worker’s deque.