Class: Monitor
- Inherits:
-
Object
- Object
- Monitor
- Defined in:
- lib/monitor.rb,
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 Method Summary collapse
- #enter ⇒ Object (also: #mon_enter)
- #exit ⇒ Object (also: #mon_exit)
- #mon_check_owner ⇒ Object
-
#mon_locked? ⇒ Boolean
internal methods for MonitorMixin.
- #mon_owned? ⇒ Boolean
- #new_cond ⇒ Object
- #synchronize ⇒ Object (also: #mon_synchronize)
- #try_enter ⇒ Object (also: #try_mon_enter, #mon_try_enter)
-
#wait_for_cond(cond, timeout) ⇒ Object
internal methods for MonitorMixin::ConditionalVariable.
Instance Method Details
#enter ⇒ Object Also known as: mon_enter
75 76 77 78 79 80 81 82 83 84 85 86 |
# File '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_thread_current());
mc->count = 0;
}
mc->count++;
return Qnil;
}
|
#exit ⇒ Object Also known as: mon_exit
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File '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 ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File '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 thread not owner");
}
return Qnil;
}
|
#mon_locked? ⇒ Boolean
internal methods for MonitorMixin
115 116 117 118 119 120 |
# File '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
122 123 124 125 126 127 |
# File '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;
}
|
#new_cond ⇒ Object
258 259 260 |
# File 'lib/monitor.rb', line 258 def new_cond ::MonitorMixin::ConditionVariable.new(self) end |
#synchronize ⇒ Object Also known as: mon_synchronize
196 197 198 199 200 201 |
# File '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 ⇒ Object Also known as: try_mon_enter, mon_try_enter
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File '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_thread_current());
mc->count = 0;
}
mc->count += 1;
return Qtrue;
}
|
#wait_for_cond(cond, timeout) ⇒ Object
internal methods for MonitorMixin::ConditionalVariable
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File '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);
}
|