Module: Mutex_m

Defined in:
lib/mutex_m.rb

Overview

mutex_m.rb

When ‘mutex_m’ is required, any object that extends or includes Mutex_m will be treated like a Mutex.

Start by requiring the standard library Mutex_m:

require "mutex_m"

From here you can extend an object with Mutex instance methods:

obj = Object.new
obj.extend Mutex_m

Or mixin Mutex_m into your module to your class inherit Mutex instance methods — remember to call super() in your class initialize method.

class Foo
  include Mutex_m
  def initialize
    # ...
    super()
  end
  # ...
end
obj = Foo.new
# this obj can be handled like Mutex

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(cl) ⇒ Object

:nodoc:



59
60
61
62
# File 'lib/mutex_m.rb', line 59

def Mutex_m.append_features(cl) # :nodoc:
  super
  define_aliases(cl) unless cl.instance_of?(Module)
end

.define_aliases(cl) ⇒ Object

:nodoc:



46
47
48
49
50
51
52
# File 'lib/mutex_m.rb', line 46

def Mutex_m.define_aliases(cl) # :nodoc:
  cl.alias_method(:locked?, :mu_locked?)
  cl.alias_method(:lock, :mu_lock)
  cl.alias_method(:unlock, :mu_unlock)
  cl.alias_method(:try_lock, :mu_try_lock)
  cl.alias_method(:synchronize, :mu_synchronize)
end

.extend_object(obj) ⇒ Object

:nodoc:



64
65
66
67
# File 'lib/mutex_m.rb', line 64

def Mutex_m.extend_object(obj) # :nodoc:
  super
  obj.mu_extended
end

.prepend_features(cl) ⇒ Object

:nodoc:



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

def Mutex_m.prepend_features(cl) # :nodoc:
  super
  define_aliases(cl) unless cl.instance_of?(Module)
end

Instance Method Details

#mu_extendedObject

:nodoc:



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

def mu_extended # :nodoc:
  unless (defined? locked? and
          defined? lock and
          defined? unlock and
          defined? try_lock and
          defined? synchronize)
    Mutex_m.define_aliases(singleton_class)
  end
  mu_initialize
end

#mu_lockObject

See Thread::Mutex#lock



96
97
98
# File 'lib/mutex_m.rb', line 96

def mu_lock
  @_mutex.lock
end

#mu_locked?Boolean

See Thread::Mutex#locked?

Returns:

  • (Boolean)


86
87
88
# File 'lib/mutex_m.rb', line 86

def mu_locked?
  @_mutex.locked?
end

#mu_synchronize(&block) ⇒ Object

See Thread::Mutex#synchronize



81
82
83
# File 'lib/mutex_m.rb', line 81

def mu_synchronize(&block)
  @_mutex.synchronize(&block)
end

#mu_try_lockObject

See Thread::Mutex#try_lock



91
92
93
# File 'lib/mutex_m.rb', line 91

def mu_try_lock
  @_mutex.try_lock
end

#mu_unlockObject

See Thread::Mutex#unlock



101
102
103
# File 'lib/mutex_m.rb', line 101

def mu_unlock
  @_mutex.unlock
end

#sleep(timeout = nil) ⇒ Object

See Thread::Mutex#sleep



106
107
108
# File 'lib/mutex_m.rb', line 106

def sleep(timeout = nil)
  @_mutex.sleep(timeout)
end