Module: MonitorMixin

Defined in:
lib/ext_monitor.rb

Defined Under Namespace

Classes: ConditionVariable

Instance Method Summary collapse

Instance Method Details

#mon_check_ownerObject



90
91
92
# File 'lib/ext_monitor.rb', line 90

def mon_check_owner
  (defined?(@mon_data) ? @mon_data : use_monitor_core).check_owner
end

#mon_enterObject

Enters exclusive section.



40
41
42
# File 'lib/ext_monitor.rb', line 40

def mon_enter
  (defined?(@mon_data) ? @mon_data : use_monitor_core).enter
end

#mon_enter_for_cond(count) ⇒ Object



95
96
97
# File 'lib/ext_monitor.rb', line 95

def mon_enter_for_cond(count)
  (defined?(@mon_data) ? @mon_data : use_monitor_core).enter_for_cond(Thread.current, count)
end

#mon_exitObject

Leaves exclusive section.



47
48
49
# File 'lib/ext_monitor.rb', line 47

def mon_exit
  (defined?(@mon_data) ? @mon_data : use_monitor_core).exit
end

#mon_exit_for_condObject



99
100
101
# File 'lib/ext_monitor.rb', line 99

def mon_exit_for_cond
  (defined?(@mon_data) ? @mon_data : use_monitor_core).exit_for_cond
end

#mon_initializeObject

Initializes the MonitorMixin after being included in a class or when an object has been extended with the MonitorMixin



82
83
84
85
86
87
88
# File 'lib/ext_monitor.rb', line 82

def mon_initialize
  if defined?(@mon_data) && @mon_data_owner_object_id == self.object_id
    raise ThreadError, "already initialized"
  end
  @mon_data = ::Thread::MonitorCore.new
  @mon_data_owner_object_id = self.object_id
end

#mon_locked?Boolean

Returns true if this monitor is locked by any thread



54
55
56
# File 'lib/ext_monitor.rb', line 54

def mon_locked?
  (defined?(@mon_data) ? @mon_data : use_monitor_core).locked?
end

#mon_owned?Boolean

Returns true if this monitor is locked by current thread.



61
62
63
# File 'lib/ext_monitor.rb', line 61

def mon_owned?
  (defined?(@mon_data) ? @mon_data : use_monitor_core).owned?
end

#mon_synchronize(&b) ⇒ Object

Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin.



70
71
72
73
74
75
76
77
78
# File 'lib/ext_monitor.rb', line 70

def mon_synchronize(&b)
  mon_data = defined?(@mon_data) ? @mon_data : use_monitor_core
  mon_data.enter
  begin
    yield
  ensure
    mon_data.exit
  end
end

#mon_try_enterObject

Attempts to enter exclusive section. Returns false if lock fails.



34
35
36
# File 'lib/ext_monitor.rb', line 34

def mon_try_enter
  (defined?(@mon_data) ? @mon_data : use_monitor_core).try_enter
end

#use_monitor_coreObject



103
104
105
106
107
108
109
110
111
# File 'lib/ext_monitor.rb', line 103

def use_monitor_core
  # below doesn't call RUBY_VM_CHECK_INTS
  @mon_data = ::Thread::MonitorCore.new(@mon_mutex, @mon_owner, @mon_count)
  @mon_data_owner_object_id = self.object_id
  remove_instance_variable(:@mon_mutex)
  remove_instance_variable(:@mon_owner)
  remove_instance_variable(:@mon_count)
  @mon_data
end