summaryrefslogtreecommitdiff
path: root/thread_sync.rb
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2025-12-12 08:55:45 +0100
committerJean Boussier <jean.boussier@gmail.com>2025-12-12 10:07:39 +0100
commit7e7a1db579dad504a26e42d5f3efa5b2968389af (patch)
treeaa4b5cd43a269243a1af34292543945f7e51a07d /thread_sync.rb
parentcf97a14c78cf18bc98ff49b6d4b6bd337daeab60 (diff)
Define Thread::ConditionVariable in thread_sync.rb
It's more consistent with Mutex, but also the `#wait` method benefits from receiving the execution context instead of having to call `GET_EC`.
Diffstat (limited to 'thread_sync.rb')
-rw-r--r--thread_sync.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/thread_sync.rb b/thread_sync.rb
index 28c70b1e9c..a722b7ec1a 100644
--- a/thread_sync.rb
+++ b/thread_sync.rb
@@ -148,4 +148,48 @@ class Thread
Primitive.rb_mut_sleep(timeout)
end
end
+
+ class ConditionVariable
+ # Document-method: ConditionVariable::new
+ #
+ # Creates a new condition variable instance.
+ def initialize
+ end
+
+ undef_method :initialize_copy
+
+ # :nodoc:
+ def marshal_dump
+ raise TypeError, "can't dump #{self.class}"
+ end
+
+ # Document-method: Thread::ConditionVariable#signal
+ #
+ # Wakes up the first thread in line waiting for this lock.
+ def signal
+ Primitive.rb_condvar_signal
+ end
+
+ # Document-method: Thread::ConditionVariable#broadcast
+ #
+ # Wakes up all threads waiting for this lock.
+ def broadcast
+ Primitive.rb_condvar_broadcast
+ end
+
+ # Document-method: Thread::ConditionVariable#wait
+ # call-seq: wait(mutex, timeout=nil)
+ #
+ # Releases the lock held in +mutex+ and waits; reacquires the lock on wakeup.
+ #
+ # If +timeout+ is given, this method returns after +timeout+ seconds passed,
+ # even if no other thread doesn't signal.
+ #
+ # This method may wake up spuriously due to underlying implementation details.
+ #
+ # Returns the slept result on +mutex+.
+ def wait(mutex, timeout=nil)
+ Primitive.rb_condvar_wait(mutex, timeout)
+ end
+ end
end