2424
2525function Base. showerror (io:: IO , ex:: CTaskException )
2626 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)
2833end
2934
3035# Utility function for self-copying mechanism
@@ -37,8 +42,7 @@ function n_copies(t::Task)
3742end
3843
3944function enable_stack_copying (t:: Task )
40- state = getproperty (t, :state )
41- if state != = :runnable && state != = :done
45+ if istaskfailed (t)
4246 error (" only runnable or finished tasks' stack can be copied." )
4347 end
4448 return ccall ((:jl_enable_stack_copying , libtask_julia), Any, (Any,), t):: Task
@@ -72,11 +76,12 @@ function task_wrapper(func)
7276 ct = _current_task ()
7377 @static if VERSION < v " 1.6.0-DEV.1145"
7478 ct. exception = ex
79+ ct. backtrace = catch_backtrace ()
7580 else
7681 ct. _isexception = true
82+ ct. storage[:_libtask_bt ] = catch_backtrace ()
7783 end
7884 ct. result = ex
79- ct. backtrace = catch_backtrace ()
8085 ct. storage === nothing && (ct. storage = IdDict ())
8186 ct. storage[:_libtask_state ] = :failed
8287 wait ()
8893
8994function Base. copy (ctask:: CTask )
9095 task = ctask. task
91- state = getproperty (task, :state )
92- if state != = :runnable && state != = :done
96+ if istaskfailed (task)
9397 error (" only runnable or finished tasks can be copied." )
9498 end
9599
@@ -102,13 +106,6 @@ function Base.copy(ctask::CTask)
102106 newtask. code = task. code
103107 setstate! (newtask, getstate (task))
104108 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
112109
113110 return CTask (newtask)
114111end
@@ -139,13 +136,9 @@ function produce(v)
139136 end
140137
141138 # 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 )
143140
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 ()
149142
150143 if empty
151144 # Switch to the consumer.
@@ -203,7 +196,7 @@ function consume(ctask::CTask, values...)
203196 push! (producer. storage[:consumers ]. waitq, ct)
204197 end
205198
206- if getproperty (producer, :state ) === :runnable
199+ if ! istaskdone (producer) && ! istaskfailed (producer)
207200 # Switch to the producer.
208201 schedule (producer)
209202 yield ()
@@ -214,7 +207,7 @@ function consume(ctask::CTask, values...)
214207 end
215208
216209 # If the task failed, throw an exception.
217- _istaskfailed (producer) && throw (CTaskException (producer))
210+ istaskfailed (producer) && throw (CTaskException (producer))
218211
219212 # If the task is done return the result.
220213 istaskdone (producer) && return producer. result
@@ -223,14 +216,6 @@ function consume(ctask::CTask, values...)
223216 wait ()
224217end
225218
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-
234219function getstate (task:: Task )
235220 @static if VERSION < v " 1.6.0-DEV.618"
236221 return task. state
@@ -254,4 +239,3 @@ function setstate!(task::Task, state)
254239 end
255240 end
256241end
257-
0 commit comments