Can I interrupt a running sleep instance?

27 views
Skip to first unread message

Lance White

unread,
Sep 8, 2009, 10:06:33 AM9/8/09
to Sleep Developers
Subject says it all really - Is it possible to interrupt a running
sleep instance, whether it be in a loop, or a user defined function,
and if it is currently executing a user defined function, can that
function get notified that the script has been interrupted?

Cheers

Lance

Raphael Mudge

unread,
Sep 8, 2009, 10:53:31 AM9/8/09
to sleep-de...@googlegroups.com
Hi Lance,
Are you trying to create a mechanism for signaling a running script
to stop? I recommend creating your own mechanism using continuations
and having your scripts use it. Something like this:

inline checkSafe
{
if (whatever condition you want)
{
callcc {
warn("Function $1 is now paused");
# save $1 to a var or pass it to some function... $1is a
paused version of the parent function--call it to resume.
};
}
}

Then you can add periodic calls to checkSafe() in your code, places
where it's safe to pause the currently running function.

If you want advice on how this could be done at the Java level
(throwing a Sleep exception or flagging a return), let me know. I
think at the Java level might be dangerous as you can break out of a
function but I don't think you'd be able to resume it. Also the Java
way would have to deal with race conditions.

-- Raphael

grol...@gmail.com

unread,
Sep 9, 2009, 4:13:45 AM9/9/09
to sleep-de...@googlegroups.com
Sorry, I should have said the script was being called and interrupted from within Java.I haven't looked at continuations yet - I've only just started looking at Sleep, but looking at an earlier post on this list, what I ended up doing, was calling :

scriptInstance.getScriptEnvironment().flagReturn(null, ScriptEnvironment.FLOW_CONTROL_THROW);

in the controlling code, and checking :

script.getScriptEnvironment().isThrownValue()

inside any extra functions I write. This seems to work quite well. There would be no requirement to continue a script running from when it was interrupted, it just needs to stop, so I don't have to worry about this.

Cheers

Lance

Raphael Mudge

unread,
Sep 9, 2009, 8:36:08 AM9/9/09
to sleep-de...@googlegroups.com
Hi Lance,
I think that will work fine them, throwing a null exception is one
way to tell the Sleep interpreter to halt all action and just stop at
the next instruction. There is a race condition though where if an
instruction is about to call a Return happens, your thread gets
called where you do the flagging, the Return executes and flags a
normal Return, and then the interpreter keeps going. Something to be
aware of. If you just want a crude mechanism and you're ok with
sending the flag a few times (in a weird situation like I described)--
then you should be ok.

-- Raphael
Reply all
Reply to author
Forward
0 new messages