123456789_123456789_123456789_123456789_123456789_

Class: Monitor

Relationships & Source Files
Inherits: Object
Defined in: ext/monitor/lib/monitor.rb,
ext/monitor/monitor.c

Overview

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

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#mon_locked?Boolean (readonly)

internal methods for ::MonitorMixin

[ GitHub ]

  
# File 'ext/monitor/monitor.c', line 115

static VALUE
monitor_locked_p(VALUE monitor)
{
    struct rb_monitor *mc = monitor_ptr(monitor);
    return rb_mutex_locked_p(mc->mutex);
}

#mon_owned?Boolean (readonly)

[ GitHub ]

  
# File 'ext/monitor/monitor.c', line 122

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;
}

Instance Method Details

#enter Also known as: #mon_enter

[ GitHub ]

  
# File 'ext/monitor/monitor.c', line 75

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;
}

#exit Also known as: #mon_exit

[ GitHub ]

  
# File 'ext/monitor/monitor.c', line 98

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;
}

#mon_check_owner

[ GitHub ]

  
# File 'ext/monitor/monitor.c', line 88

static VALUE
monitor_check_owner(VALUE monitor)
{
    struct rb_monitor *mc = monitor_ptr(monitor);
    if (!mc_owner_p(mc)) {
        rb_raise(rb_eThreadError, "current fiber not owner");
    }
    return Qnil;
}

#mon_enter

Alias for #enter.

[ GitHub ]

  
# File 'ext/monitor/lib/monitor.rb', line 265

alias mon_enter enter

#mon_exit

Alias for #exit.

[ GitHub ]

  
# File 'ext/monitor/lib/monitor.rb', line 266

alias mon_exit exit

#mon_synchronize

Alias for #synchronize.

[ GitHub ]

  
# File 'ext/monitor/lib/monitor.rb', line 267

alias mon_synchronize synchronize

#mon_try_enter

Alias for #try_enter.

[ GitHub ]

  
# File 'ext/monitor/lib/monitor.rb', line 264

alias mon_try_enter try_enter

#new_cond

[ GitHub ]

  
# File 'ext/monitor/lib/monitor.rb', line 258

def new_cond
  ::MonitorMixin::ConditionVariable.new(self)
end

#synchronize Also known as: #mon_synchronize

[ GitHub ]

  
# File 'ext/monitor/monitor.c', line 196

static VALUE
monitor_synchronize(VALUE monitor)
{
    monitor_enter(monitor);
    return rb_ensure(monitor_sync_body, monitor, monitor_sync_ensure, monitor);
}

#try_enter Also known as: #try_mon_enter, #mon_try_enter

[ GitHub ]

  
# File 'ext/monitor/monitor.c', line 59

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;
}

#try_mon_enter

Alias for #try_enter.

[ GitHub ]

  
# File 'ext/monitor/lib/monitor.rb', line 263

alias try_mon_enter try_enter

#wait_for_cond(cond, timeout)

internal methods for MonitorMixin::ConditionalVariable

[ GitHub ]

  
# File 'ext/monitor/monitor.c', line 169

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);
}