Skip to content

Commit 0d9815d

Browse files
committed
Fix bug in interrupting not yet started process
1 parent cc8b16c commit 0d9815d

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

docs/src/examples/ross.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,13 @@ const G = Exponential(MU)
4949
end
5050
5151
@resumable function start_sim(env::Environment, repair_facility::Resource, spares::Store{Process})
52-
procs = Process[]
53-
for i in 1:N
54-
push!(procs, @process machine(env, repair_facility, spares))
55-
end
56-
@yield timeout(env)
57-
for proc in procs
52+
for i in 1:N
53+
proc = @process machine(env, repair_facility, spares)
5854
@yield interrupt(proc)
5955
end
60-
for i in 1:S
61-
@yield put(spares, @process machine(env, repair_facility, spares))
56+
for i in 1:S
57+
proc = @process machine(env, repair_facility, spares)
58+
@yield put(spares, proc)
6259
end
6360
end
6461

src/events.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ end
2828
struct Initialize <: AbstractEvent
2929
bev :: BaseEvent
3030
function Initialize(env::Environment)
31-
schedule(new(BaseEvent(env)))
31+
new(BaseEvent(env))
3232
end
3333
end
3434

src/old/processes.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mutable struct OldProcess <: DiscreteProcess
2323
proc = new()
2424
proc.bev = BaseEvent(env)
2525
proc.task = @task func(env, args...)
26-
proc.target = timeout(env)
26+
proc.target = schedule(Initialize(env))
2727
proc.resume = @callback execute(proc.target, proc)
2828
return proc
2929
end
@@ -58,10 +58,17 @@ function execute(ev::AbstractEvent, proc::OldProcess)
5858
end
5959
end
6060

61+
function execute_interrupt(ev::Interrupt, proc::OldProcess)
62+
remove_callback(proc.resume, proc.target)
63+
execute(ev, proc)
64+
end
65+
6166
function interrupt(proc::OldProcess, cause::Any=nothing)
67+
env = environment(proc)
6268
if !istaskdone(proc.task)
63-
remove_callback(proc.resume, proc.target)
64-
proc.target = schedule(Interrupt(environment(proc)); priority=typemax(Int8), value=InterruptException(proc, cause))
65-
proc.resume = @callback execute(proc.target, proc)
69+
proc.target isa Initialize && schedule(proc.target; priority=typemax(Int8))
70+
target = schedule(Interrupt(env); priority=typemax(Int8), value=InterruptException(active_process(env), cause))
71+
@callback execute_interrupt(target, proc)
6672
end
73+
timeout(env; priority=typemax(Int8))
6774
end

src/processes.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ mutable struct Process <: DiscreteProcess
44
target :: AbstractEvent
55
resume :: Function
66
function Process(func::Function, env::Environment, args::Any...)
7-
cor = new()
8-
cor.bev = BaseEvent(env)
9-
cor.fsmi = func(env, args...)
10-
cor.target = Initialize(env)
11-
cor.resume = @callback execute(cor.target, cor)
12-
cor
7+
proc = new()
8+
proc.bev = BaseEvent(env)
9+
proc.fsmi = func(env, args...)
10+
proc.target = schedule(Initialize(env))
11+
proc.resume = @callback execute(proc.target, proc)
12+
proc
1313
end
1414
end
1515

@@ -35,12 +35,17 @@ function execute(ev::AbstractEvent, proc::Process)
3535
end
3636
end
3737

38+
function execute_interrupt(ev::Interrupt, proc::Process)
39+
remove_callback(proc.resume, proc.target)
40+
execute(ev, proc)
41+
end
42+
3843
function interrupt(proc::Process, cause::Any=nothing)
3944
env = environment(proc)
4045
if !done(proc.fsmi)
41-
remove_callback(proc.resume, proc.target)
42-
proc.target = schedule(Interrupt(env); priority=typemax(Int8), value=InterruptException(proc, cause))
43-
proc.resume = @callback execute(proc.target, proc)
46+
proc.target isa Initialize && schedule(proc.target; priority=typemax(Int8))
47+
target = schedule(Interrupt(env); priority=typemax(Int8), value=InterruptException(active_process(env), cause))
48+
@callback execute_interrupt(target, proc)
4449
end
4550
timeout(env; priority=typemax(Int8))
4651
end

0 commit comments

Comments
 (0)