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() click to toggle source
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_thread_current());
        mc->count = 0;
    }
    mc->count++;
    return Qnil;
}
Also aliased as: mon_enter
exit() click to toggle source
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\n", (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
mon_check_owner() click to toggle source
static VALUE
monitor_check_owner(VALUE monitor)
{
    struct rb_monitor *mc = monitor_ptr(monitor);
    if (!mc_owner_p(mc)) {
        rb_raise(rb_eThreadError, "current thread not owner");
    }
    return Qnil;
}
mon_enter()
Alias for: enter
mon_exit()
Alias for: exit
mon_locked?() click to toggle source
static VALUE
monitor_locked_p(VALUE monitor)
{
    struct rb_monitor *mc = monitor_ptr(monitor);
    return rb_mutex_locked_p(mc->mutex);
}
mon_owned?() click to toggle source
static VALUE
monitor_owned_p(VALUE monitor)
{
    struct rb_monitor *mc = monitor_ptr(monitor);
    return (rb_mutex_locked_p(mc->mutex) && mc_owner_p(mc)) ? Qtrue : Qfalse;
}
mon_synchronize()
Alias for: synchronize
mon_try_enter()
Alias for: try_enter
new_cond() click to toggle source
# File monitor/lib/monitor.rb, line 258
def new_cond
  ::MonitorMixin::ConditionVariable.new(self)
end
synchronize() click to toggle source
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() click to toggle source
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_thread_current());
        mc->count = 0;
    }
    mc->count += 1;
    return Qtrue;
}
Also aliased as: try_mon_enter, mon_try_enter
try_mon_enter()

for compatibility

Alias for: try_enter
wait_for_cond(p1, p2) click to toggle source
static VALUE
monitor_wait_for_cond(VALUE monitor, VALUE cond, VALUE timeout)
{
    VALUE count = monitor_exit_for_cond(monitor);
    struct wait_for_cond_data data = {
        monitor,
        cond,
        timeout,
        count,
    };

    return rb_ensure(monitor_wait_for_cond_body, (VALUE)&data,
                     monitor_enter_for_cond, (VALUE)&data);
}