24
24
25
25
function Base. showerror (io:: IO , ex:: CTaskException )
26
26
println (io, " CTaskException:" )
27
- showerror (io, getproperty (ex. task, :exception ), getproperty (ex. task, :backtrace ))
27
+ bt = @static if VERSION < v " 1.6.0-DEV.1145"
28
+ ct. backtrace
29
+ else
30
+ ct. storage[:_libtask_bt ]
31
+ end
32
+ showerror (io, ex. task. exception, bt)
28
33
end
29
34
30
35
# Utility function for self-copying mechanism
@@ -37,8 +42,7 @@ function n_copies(t::Task)
37
42
end
38
43
39
44
function enable_stack_copying (t:: Task )
40
- state = getproperty (t, :state )
41
- if state != = :runnable && state != = :done
45
+ if istaskfailed (t)
42
46
error (" only runnable or finished tasks' stack can be copied." )
43
47
end
44
48
return ccall ((:jl_enable_stack_copying , libtask_julia), Any, (Any,), t):: Task
@@ -72,11 +76,12 @@ function task_wrapper(func)
72
76
ct = _current_task ()
73
77
@static if VERSION < v " 1.6.0-DEV.1145"
74
78
ct. exception = ex
79
+ ct. backtrace = catch_backtrace ()
75
80
else
76
81
ct. _isexception = true
82
+ ct. storage[:_libtask_bt ] = catch_backtrace ()
77
83
end
78
84
ct. result = ex
79
- ct. backtrace = catch_backtrace ()
80
85
ct. storage === nothing && (ct. storage = IdDict ())
81
86
ct. storage[:_libtask_state ] = :failed
82
87
wait ()
88
93
89
94
function Base. copy (ctask:: CTask )
90
95
task = ctask. task
91
- state = getproperty (task, :state )
92
- if state != = :runnable && state != = :done
96
+ if istaskfailed (task)
93
97
error (" only runnable or finished tasks can be copied." )
94
98
end
95
99
@@ -102,13 +106,6 @@ function Base.copy(ctask::CTask)
102
106
newtask. code = task. code
103
107
setstate! (newtask, getstate (task))
104
108
newtask. result = task. result
105
- @static if VERSION < v " 1.1"
106
- newtask. parent = task. parent
107
- end
108
-
109
- if isdefined (task, :last )
110
- newtask. last = nothing
111
- end
112
109
113
110
return CTask (newtask)
114
111
end
@@ -139,13 +136,9 @@ function produce(v)
139
136
end
140
137
141
138
# Internal check to make sure that it is possible to switch to the consumer.
142
- @assert getproperty (task, :state ) in ( :runnable , :queued )
139
+ @assert ! istaskdone (task) && ! istaskfailed (task )
143
140
144
- @static if VERSION < v " 1.1.9999"
145
- task. state === :queued && yield ()
146
- else
147
- task. queue != = nothing && yield ()
148
- end
141
+ task. queue != = nothing && yield ()
149
142
150
143
if empty
151
144
# Switch to the consumer.
@@ -203,7 +196,7 @@ function consume(ctask::CTask, values...)
203
196
push! (producer. storage[:consumers ]. waitq, ct)
204
197
end
205
198
206
- if getproperty (producer, :state ) === :runnable
199
+ if ! istaskdone (producer) && ! istaskfailed (producer)
207
200
# Switch to the producer.
208
201
schedule (producer)
209
202
yield ()
@@ -214,7 +207,7 @@ function consume(ctask::CTask, values...)
214
207
end
215
208
216
209
# If the task failed, throw an exception.
217
- _istaskfailed (producer) && throw (CTaskException (producer))
210
+ istaskfailed (producer) && throw (CTaskException (producer))
218
211
219
212
# If the task is done return the result.
220
213
istaskdone (producer) && return producer. result
@@ -223,14 +216,6 @@ function consume(ctask::CTask, values...)
223
216
wait ()
224
217
end
225
218
226
- function _istaskfailed (task:: Task )
227
- @static if VERSION < v " 1.3"
228
- return task. state === :failed
229
- else
230
- return Base. istaskfailed (task)
231
- end
232
- end
233
-
234
219
function getstate (task:: Task )
235
220
@static if VERSION < v " 1.6.0-DEV.618"
236
221
return task. state
@@ -254,4 +239,3 @@ function setstate!(task::Task, state)
254
239
end
255
240
end
256
241
end
257
-
0 commit comments