From: Eric Wong <normalperson-rMlxZR9MS24@public.gmane.org>
To: rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org
Subject: Re: [PATCH] thread_timeout: document additional caveats
Date: Fri, 20 Jul 2012 02:40:17 +0000 [thread overview]
Message-ID: <20120720024017.GA6512@dcvr.yhbt.net> (raw)
In-Reply-To: <20120719201403.GA11657-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
Eric Wong <normalperson-rMlxZR9MS24@public.gmane.org> wrote:
> Again, for the one thousandth time, timing out threads is very
> tricky business :<
On a related note, it looks like Thread.control_interrupt
landed in Ruby trunk (http://svn.ruby-lang.org/repos/ruby/trunk r36470)
Perhaps the future for interrupting long-running threads is
less bleak.
+/*
+ * call-seq:
+ * Thread.control_interrupt(hash) { ... } -> result of the block
+ *
+ * Thread.control_interrupt controls interrupt timing.
+ *
+ * _interrupt_ means asynchronous event and corresponding procedure
+ * by Thread#raise, Thread#kill, signal trap (not supported yet)
+ * and main thread termination (if main thread terminates, then all
+ * other thread will be killed).
+ *
+ * _hash_ has pairs of ExceptionClass and TimingSymbol. TimingSymbol
+ * is one of them:
+ * - :immediate Invoke interrupt immediately.
+ * - :on_blocking Invoke interrupt while _BlockingOperation_.
+ * - :never Never invoke interrupt.
+ *
+ * _BlockingOperation_ means that the operation will block the calling thread,
+ * such as read and write. On CRuby implementation, _BlockingOperation_ is
+ * operation executed without GVL.
+ *
+ * Masked interrupts are delayed until they are enabled.
+ * This method is similar to sigprocmask(3).
+ *
+ * TODO (DOC): control_interrupt is stacked.
+ * TODO (DOC): check ancestors.
+ * TODO (DOC): to prevent all interrupt, {Object => :never} works.
+ *
+ * NOTE: Asynchronous interrupts are difficult to use.
+ * If you need to communicate between threads,
+ * please consider to use another way such as Queue.
+ * Or use them with deep understanding about this method.
+ *
+ *
+ * # example: Guard from Thread#raise
+ * th = Thread.new do
+ * Thead.control_interrupt(RuntimeError => :never) {
+ * begin
+ * # Thread#raise doesn't interrupt here.
+ * # You can write resource allocation code safely.
+ * Thread.control_interrupt(RuntimeError => :immediate) {
+ * # ...
+ * # It is possible to be interrupted by Thread#raise.
+ * }
+ * ensure
+ * # Thread#raise doesn't interrupt here.
+ * # You can write resource dealocation code safely.
+ * end
+ * }
+ * end
+ * Thread.pass
+ * # ...
+ * th.raise "stop"
+ *
+ * # example: Guard from TimeoutError
+ * require 'timeout'
+ * Thread.control_interrupt(TimeoutError => :never) {
+ * timeout(10){
+ * # TimeoutError doesn't occur here
+ * Thread.control_interrupt(TimeoutError => :on_blocking) {
+ * # possible to be killed by TimeoutError
+ * # while blocking operation
+ * }
+ * # TimeoutError doesn't occur here
+ * }
+ * }
+ *
+ * # example: Stack control settings
+ * Thread.control_interrupt(FooError => :never) {
+ * Thread.control_interrupt(BarError => :never) {
+ * # FooError and BarError are prohibited.
+ * }
+ * }
+ *
+ * # example: check ancestors
+ * Thread.control_interrupt(Exception => :never) {
+ * # all exceptions inherited from Exception are prohibited.
+ * }
+ *
+ */
+
+/*
+ * call-seq:
+ * Thread.check_interrupt() -> nil
+ *
+ * Check queued interrupts.
+ *
+ * If there are queued interrupts, process respective procedures.
+ *
+ * This method can be defined as the following Ruby code:
+ *
+ * def Thread.check_interrupt
+ * Thread.control_interrupt(Object => :immediate) {
+ * Thread.pass
+ * }
+ * end
+ *
+ * Examples:
+ *
+ * th = Thread.new{
+ * Thread.control_interrupt(RuntimeError => :on_blocking){
+ * while true
+ * ...
+ * # reach safe point to invoke interrupt
+ * Thread.check_interrupt
+ * ...
+ * end
+ * }
+ * }
+ * ...
+ * th.raise # stop thread
+ *
+ * NOTE: This example can be described by the another code.
+ * You need to keep to avoid asynchronous interrupts.
+ *
+ * flag = true
+ * th = Thread.new{
+ * Thread.control_interrupt(RuntimeError => :on_blocking){
+ * while true
+ * ...
+ * # reach safe point to invoke interrupt
+ * break if flag == false
+ * ...
+ * end
+ * }
+ * }
+ * ...
+ * flag = false # stop thread
+ */
_______________________________________________
Rainbows! mailing list - rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org
http://rubyforge.org/mailman/listinfo/rainbows-talk
Do not quote signatures (like this one) or top post when replying
prev parent reply other threads:[~2012-07-20 2:40 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-07-19 20:14 [PATCH] thread_timeout: document additional caveats Eric Wong
[not found] ` <20120719201403.GA11657-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
2012-07-20 2:40 ` Eric Wong [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://yhbt.net/rainbows/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120720024017.GA6512@dcvr.yhbt.net \
--to=normalperson-rmlxzr9ms24@public.gmane.org \
--cc=rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhbt.net/rainbows.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).