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.rb"

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.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(cl) ⇒ Object

:nodoc:



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

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

.define_aliases(cl) ⇒ Object

:nodoc:



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

def Mutex_m.define_aliases(cl) # :nodoc:
  cl.module_eval %q{
    alias locked? mu_locked?
    alias lock mu_lock
    alias unlock mu_unlock
    alias try_lock mu_try_lock
    alias synchronize mu_synchronize
  }
end

.extend_object(obj) ⇒ Object

:nodoc:



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

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

Instance Method Details

#mu_extendedObject

:nodoc:



65
66
67
68
69
70
71
72
73
74
# File 'lib/mutex_m.rb', line 65

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 Mutex#lock



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

def mu_lock
  @_mutex.lock
end

#mu_locked?Boolean

See Mutex#locked?

Returns:

  • (Boolean)


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

def mu_locked?
  @_mutex.locked?
end

#mu_synchronize(&block) ⇒ Object

See Mutex#synchronize



77
78
79
# File 'lib/mutex_m.rb', line 77

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

#mu_try_lockObject

See Mutex#try_lock



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

def mu_try_lock
  @_mutex.try_lock
end

#mu_unlockObject

See Mutex#unlock



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

def mu_unlock
  @_mutex.unlock
end

#sleep(timeout = nil) ⇒ Object

See Mutex#sleep



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

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