class Monitor
Use the Monitor
class when you want to have a lock object for blocks with mutual exclusion.
require 'monitor' lock = Monitor.new lock.synchronize do # exclusive access end
Public Instance Methods
enter → nil
click to toggle source
Enters exclusive section.
static VALUE monitor_enter(VALUE monitor) { struct rb_monitor *mc = monitor_ptr(monitor); if (!mc_owner_p(mc)) { rb_mutex_lock(mc->mutex); RB_OBJ_WRITE(monitor, &mc->owner, rb_fiber_current()); mc->count = 0; } mc->count++; return Qnil; }
Also aliased as: mon_enter
exit → nil
click to toggle source
Leaves exclusive section.
static VALUE monitor_exit(VALUE monitor) { monitor_check_owner(monitor); struct rb_monitor *mc = monitor_ptr(monitor); if (mc->count <= 0) rb_bug("monitor_exit: count:%d", (int)mc->count); mc->count--; if (mc->count == 0) { RB_OBJ_WRITE(monitor, &mc->owner, Qnil); rb_mutex_unlock(mc->mutex); } return Qnil; }
Also aliased as: mon_exit
new_cond()
click to toggle source
Creates a new MonitorMixin::ConditionVariable
associated with the Monitor
object.
# File monitor/lib/monitor.rb, line 263 def new_cond ::MonitorMixin::ConditionVariable.new(self) end
synchronize { } → result of the block
click to toggle source
Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin
.
static VALUE monitor_synchronize(VALUE monitor) { monitor_enter(monitor); return rb_ensure(monitor_sync_body, monitor, monitor_sync_ensure, monitor); }
Also aliased as: mon_synchronize
try_enter → true or false
click to toggle source
Attempts to enter exclusive section. Returns false
if lock fails.
static VALUE monitor_try_enter(VALUE monitor) { struct rb_monitor *mc = monitor_ptr(monitor); if (!mc_owner_p(mc)) { if (!rb_mutex_trylock(mc->mutex)) { return Qfalse; } RB_OBJ_WRITE(monitor, &mc->owner, rb_fiber_current()); mc->count = 0; } mc->count += 1; return Qtrue; }
Also aliased as: try_mon_enter, mon_try_enter