[#15707] Schedule for the 1.8.7 release — "Akinori MUSHA" <knu@...>

Hi, developers,

21 messages 2008/03/01

[#15740] Copy-on-write friendly garbage collector — Hongli Lai <hongli@...99.net>

Hi.

31 messages 2008/03/03
[#15742] Re: Copy-on-write friendly garbage collector — Yukihiro Matsumoto <matz@...> 2008/03/03

Hi,

[#15829] Re: Copy-on-write friendly garbage collector — Daniel DeLorme <dan-ml@...42.com> 2008/03/08

Yukihiro Matsumoto wrote:

[#15756] embedding Ruby 1.9.0 inside pthread — "Suraj Kurapati" <sunaku@...>

Hello,

18 messages 2008/03/03
[#15759] Re: embedding Ruby 1.9.0 inside pthread — Nobuyoshi Nakada <nobu@...> 2008/03/04

Hi,

[#15760] Re: embedding Ruby 1.9.0 inside pthread — Yukihiro Matsumoto <matz@...> 2008/03/04

Hi,

[#15762] Re: embedding Ruby 1.9.0 inside pthread — "Suraj N. Kurapati" <sunaku@...> 2008/03/04

Yukihiro Matsumoto wrote:

[#15783] Adding startup and shutdown to Test::Unit — Daniel Berger <Daniel.Berger@...>

Hi all,

15 messages 2008/03/04

[#15835] TimeoutError in core, timeouts for ConditionVariable#wait — MenTaLguY <mental@...>

I've been reworking JRuby's stdlib to improve performance and fix

10 messages 2008/03/09

[#15990] Recent changes in Range#step behavior — "Vladimir Sizikov" <vsizikov@...>

Hi,

35 messages 2008/03/23
[#15991] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/23

[#15993] Re: Recent changes in Range#step behavior — "Vladimir Sizikov" <vsizikov@...> 2008/03/23

Hi Dave,

[#15997] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/23

[#16024] Re: Recent changes in Range#step behavior — "Vladimir Sizikov" <vsizikov@...> 2008/03/26

Hi Dave,

[#16025] Re: Recent changes in Range#step behavior — Yukihiro Matsumoto <matz@...> 2008/03/26

Hi,

[#16026] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/26

[#16027] Re: Recent changes in Range#step behavior — Yukihiro Matsumoto <matz@...> 2008/03/26

Hi,

[#16029] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/26

[#16030] Re: Recent changes in Range#step behavior — Yukihiro Matsumoto <matz@...> 2008/03/26

Hi,

[#16031] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/26

[#16032] Re: Recent changes in Range#step behavior — "Vladimir Sizikov" <vsizikov@...> 2008/03/26

On Wed, Mar 26, 2008 at 7:01 PM, Dave Thomas <dave@pragprog.com> wrote:

[#16033] Re: Recent changes in Range#step behavior — Dave Thomas <dave@...> 2008/03/26

[#16041] Re: Recent changes in Range#step behavior — David Flanagan <david@...> 2008/03/26

Dave Thomas wrote:

[PATCH] timeouts for ConditionVariable#wait

From: MenTaLguY <mental@...>
Date: 2008-03-10 18:58:07 UTC
List: ruby-core #15847
On Sun, 2008-03-09 at 12:13 +0900, MenTaLguY wrote:
>  1. I added a second optional argument to ConditionVariable#wait which
> specifies a timeout in seconds.  If no second argument is provided,
> #wait will wait indefinitely, as it did before.  If a specified timeout
> elapses before the waiting thread is signaled, #wait raises
> TimeoutError.  Timeouts <=3D 0 raise TimeoutError immediately.

Since this first change seems to be uncontroversial, I've attached a
patch which implements this in 1.9 using the timeout argument of
Mutex#sleep.

There doesn't seem to be a consensus about whether to move TimeoutError
into core, so I left TimeoutError alone for now.  This patch simply
requires timeout.rb to get it.

Please let me know if there are any problems with the patch.  Thanks,

-mental

Attachments (2)

condition-variable-wait-timeout.diff (1.14 KB, text/x-diff)
Index: lib/thread.rb
===================================================================
--- lib/thread.rb	(revision 15741)
+++ lib/thread.rb	(working copy)
@@ -20,6 +20,8 @@
   Thread.abort_on_exception = true
 end
 
+require 'timeout'
+
 # 
 # ConditionVariable objects augment class Mutex. Using condition variables,
 # it is possible to suspend while in the middle of a critical section until a
@@ -59,14 +61,24 @@
   #
   # Releases the lock held in +mutex+ and waits; reacquires the lock on wakeup.
   #
-  def wait(mutex)
+  def wait(mutex, timeout=nil)
     begin
       # TODO: mutex should not be used
       @waiters_mutex.synchronize do
         @waiters.push(Thread.current)
       end
-      mutex.sleep
+      if timeout
+        elapsed = mutex.sleep timeout if timeout > 0.0
+        unless timeout > 0.0 and elapsed < timeout
+          t = @waiters_mutex.synchronize { @waiters.delete Thread.current }
+          signal unless t # if we got notified, pass it along
+          raise TimeoutError, "wait timed out"
+        end
+      else
+        mutex.sleep
+      end
     end
+    nil
   end
   
   #
signature.asc (189 Bytes, application/pgp-signature)

In This Thread