Module: FiberedMysql2::FiberedMonitorMixin

Included in:
FiberedDatabaseConnectionPool
Defined in:
lib/fibered_mysql2/fibered_database_connection_pool.rb

Overview

From Ruby’s MonitorMixin, with all occurrences of Thread changed to Fiber

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extend_object(obj) ⇒ Object



90
91
92
93
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 90

def self.extend_object(obj)
  super
  obj.__send__(:mon_initialize)
end

Instance Method Details

#mon_check_ownerObject



166
167
168
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 166

def mon_check_owner
  @mon_owner == Fiber.current or raise FiberError, "current fiber not owner"
end

#mon_enterObject

Enters exclusive section.



111
112
113
114
115
116
117
118
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 111

def mon_enter
  if @mon_owner != Fiber.current
    @mon_mutex.lock
    @mon_owner = Fiber.current
    @mon_count = 0
  end
  @mon_count += 1
end

#mon_exitObject

Leaves exclusive section.



123
124
125
126
127
128
129
130
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 123

def mon_exit
  mon_check_owner
  @mon_count -= 1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end

#mon_initializeObject

Initializes the FiberedMonitorMixin after being included in a class



160
161
162
163
164
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 160

def mon_initialize
  @mon_owner = nil
  @mon_count = 0
  @mon_mutex = EM::Synchrony::Thread::Mutex.new
end

#mon_synchronizeObject Also known as: synchronize

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



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 137

def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    begin
      mon_exit
    rescue => ex
      ActiveRecord::Base.logger.error("Exception occurred while executing mon_exit: #{ex}")
    end
  end
end

#mon_try_enterObject

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



98
99
100
101
102
103
104
105
106
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 98

def mon_try_enter
  if @mon_owner != Fiber.current
    @mon_mutex.try_lock or return false
    @mon_owner = Fiber.current
    @mon_count = 0
  end
  @mon_count += 1
  true
end

#new_condObject

Creates a new FiberedConditionVariable associated with the receiver.



155
156
157
# File 'lib/fibered_mysql2/fibered_database_connection_pool.rb', line 155

def new_cond
  FiberedConditionVariable.new(self)
end