Class: MySQLMutex
- Inherits:
-
DistributedMutex
- Object
- Mutex
- DistributedMutex
- MySQLMutex
- Defined in:
- lib/mysql_mutex.rb
Constant Summary collapse
- @@thread_locks =
Accounting for nested locks.
Hash.new { |h,k| h[k] = Hash.new(0) }
Constants inherited from DistributedMutex
DistributedMutex::DEFAULT_EXCEPTION_ON_TIMEOUT, DistributedMutex::DEFAULT_TIMEOUT
Instance Attribute Summary
Attributes inherited from DistributedMutex
#exception_on_timeout, #key, #timeout
Class Method Summary collapse
- .active_locks ⇒ Object
- .synchronize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_TIMEOUT, con = ActiveRecord::Base.connection, &block) ⇒ Object
Instance Method Summary collapse
-
#initialize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT, connection = ActiveRecord::Base.connection) ⇒ MySQLMutex
constructor
A new instance of MySQLMutex.
Methods inherited from DistributedMutex
#excluse_unlock, #lock, #locked?, #synchronize, #try_lock, #unlock
Constructor Details
#initialize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT, connection = ActiveRecord::Base.connection) ⇒ MySQLMutex
Returns a new instance of MySQLMutex.
8 9 10 11 12 13 14 |
# File 'lib/mysql_mutex.rb', line 8 def initialize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_EXCEPTION_ON_TIMEOUT, connection = ActiveRecord::Base.connection) super(key, timeout, exception_on_timeout) @connection = connection @connection_id = connection.show_variable('pseudo_thread_id') @get_sql = ActiveRecord::Base.send(:sanitize_sql_array,["SELECT GET_LOCK(?,?)", key, timeout]) @release_sql = ActiveRecord::Base.send(:sanitize_sql_array,["SELECT RELEASE_LOCK(?)", key]) end |
Class Method Details
.active_locks ⇒ Object
21 22 23 |
# File 'lib/mysql_mutex.rb', line 21 def self.active_locks @@thread_locks end |
.synchronize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_TIMEOUT, con = ActiveRecord::Base.connection, &block) ⇒ Object
16 17 18 19 |
# File 'lib/mysql_mutex.rb', line 16 def self.synchronize(key, timeout = DEFAULT_TIMEOUT, exception_on_timeout = DEFAULT_TIMEOUT, con = ActiveRecord::Base.connection, &block) mutex = new(key, timeout, exception_on_timeout, con) mutex.synchronize(&block) end |