-
Notifications
You must be signed in to change notification settings - Fork 2
/
futures.lisp
177 lines (166 loc) · 6.65 KB
/
futures.lisp
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
(defpackage "FUTURE"
(:use "CL" "SB-EXT" "SB-THREAD")
(:export "FUTURE"
"DEPENDENTS"
"STATUS" "WAIT" "CANCEL"
"MARK-DEPENDENCIES" "THAW" "MARK-DONE"))
;;; Infrastructure for futures: lenient-evaluated values
;;;
;;; A future is a computation with a set of dependencies; whenever
;;; all the dependencies for a computation have been fully executed,
;;; it too is executed.
;;;
;;; In order to do so, each future also tracks its dependents
;;; in list of weak pointers. When the future is marked as done, the
;;; depcount (number of yet unfulfilled dependencies) of its
;;; dependents is decremented. When all the dependencies are
;;; fullfilled (depcount is zero), the future is recursively executed.
;;;
;;; For convenience, futures are also bulk-tasks, but this is irrelevant
;;; to the interface.
;;;
;;; Slots in a FUTURE:
;;; function: list designator of functions to be called on execution;
;;; they receive the future as their single argument.
;;; dependents: list of weak pointers to dependents, initialized
;;; to zero and updated on demand.
;;; dependencies: vector of dependencies
;;; depcount: number of dependencies yet to be fullfilled, updated on
;;; demand.
;;; %status: current status of the future. Upgraded to a slow, lock-ful
;;; representation as needed.
;;;
;;; A future goes through a few stages:
;;;
;;; :orphan is the initial stage. The future is initialized, but not
;;; yet linked to its dependencies.
;;; :frozen futures have been linked to their dependencies (via
;;; MARK-DEPENDENCIES), but not been marked for execution.
;;; :waiting futures have been marked for execution (via THAW),
;;; and will wait until all their dependencies are satisfied.
;;; :running futures have had all their dependencies satisfied
;;; :done futures have finished executing
;;; :cancelled futures have been cancalled
;;;
;;; STATUS and WAIT can be used to poll a future's current status or wait
;;; until it becomes equal to a value in a set of status.
;;;
;;; CANCEL marks a future as cancelled, unless it is already executing.
;;;
;;; Creating a future should follow this pattern:
;;; - Allocate a future
;;; - MARK-DEPENDENCIES
;;; - Maybe walk its DEPENDENTS list for analyses
;;; - THAW it
;;; - Maybe WAIT until :cancelled or :done
(in-package "FUTURE")
(deftype status ()
'(member :orphan :frozen :waiting :running :done :cancelled))
(defstruct (future
(:include work-stack:bulk-task)
(:constructor nil))
(function nil :type (or list symbol function))
(dependents nil :type (or list (member :done :cancelled)))
(dependencies nil :type simple-vector)
(depcount 0 :type word)
(%status :orphan :type (or status slow-status)))
(defun dependents (future)
(let ((dependents (future-dependents future)))
(and (listp dependents)
dependents)))
(status:define-status-type slow-status
(:fast-type future
:status-type status
:default-status :orphan
:final-states (:done :cancelled))
future-%status status wait status-upgrade)
(defun execute (future)
(unless (eql (status-upgrade future :running :waiting)
:waiting)
(return-from execute))
(let ((function (future-function future)))
(etypecase function
(null)
(cons
(dolist (function function)
(funcall function future)))
((or symbol function)
(funcall function future)))
(setf (future-function future) nil))
nil)
(defun cancel (future)
(declare (type future future))
(let ((status (status-upgrade future :cancelled :orphan :frozen :waiting)))
(when (member status '(:frozen :waiting))
;; recursively mark as cancelled?
(setf (future-dependents future) :cancelled))
status))
(defun thaw (future &key (recursive t))
(declare (type future future))
(labels ((rec (future)
(declare (type future future))
(case (status-upgrade future :waiting :frozen)
(:orphan (error "Thawing orphan future"))
(:frozen
(when recursive
(map nil #'rec (future-dependencies future)))
(when (zerop (future-depcount future))
(execute future))))))
(rec future))
future)
(defun mark-dependencies (future &key (thaw t) (recursive nil))
(declare (type future future))
(assert (eql (status-upgrade future :frozen :orphan) :orphan))
(let ((wp (make-weak-pointer future)))
(flet ((mark-dep (dep)
(declare (type future dep))
(ecase (status dep)
(:orphan
(if recursive
(mark-dependencies dep :thaw thaw :recursive t)
(error "Dependency is an orphan")))
((:frozen :waiting :running))
(:done
(return-from mark-dep))
(:cancelled
(error "Dependency cancelled")))
(let ((cons (list wp)))
(atomic-incf (future-depcount future))
(loop
(let ((dependents (future-dependents dep)))
(setf (cdr cons) dependents)
(cond ((eql dependents :done)
(atomic-decf (future-depcount future))
(return-from mark-dep))
((eql dependents :cancelled)
;; cancel self?
(atomic-decf (future-depcount future))
(error "Dependency cancelled"))
((eql (cas (future-dependents dep)
dependents cons)
dependents)
(return-from mark-dep))))))))
(declare (dynamic-extent #'mark-dep))
(map nil #'mark-dep (future-dependencies future))
(when thaw (thaw future)))))
(defun mark-done (future)
(declare (type future future))
(unless (eql :running (status-upgrade future :done :running))
(return-from mark-done))
(setf (future-dependencies future) #())
(let ((dependents
(loop
(let ((dependents (future-dependents future)))
(when (or (eql dependents :done)
(eql dependents :cancelled))
(return-from mark-done))
(when (eql (cas (future-dependents future)
dependents :done)
dependents)
(return dependents))))))
(dolist (wp dependents)
(let ((value (weak-pointer-value wp)))
(when (and value
(= 1 (atomic-decf (future-depcount value)))
(eql :waiting (status value)))
(execute value))))))