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.

class Foo
  include Mutex_m
  # ...
end
obj = Foo.new
# this obj can be handled like Mutex

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(cl) ⇒ Object

:nodoc:



50
51
52
53
# File 'lib/mutex_m.rb', line 50

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

.define_aliases(cl) ⇒ Object

:nodoc:



40
41
42
43
44
45
46
47
48
# File 'lib/mutex_m.rb', line 40

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:



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

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

Instance Method Details

#mu_extendedObject

:nodoc:



60
61
62
63
64
65
66
67
68
69
# File 'lib/mutex_m.rb', line 60

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



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

def mu_lock
  @_mutex.lock
end

#mu_locked?Boolean

See Mutex#locked?

Returns:

  • (Boolean)


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

def mu_locked?
  @_mutex.locked?
end

#mu_synchronize(&block) ⇒ Object

See Mutex#synchronize



72
73
74
# File 'lib/mutex_m.rb', line 72

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

#mu_try_lockObject

See Mutex#try_lock



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

def mu_try_lock
  @_mutex.try_lock
end

#mu_unlockObject

See Mutex#unlock



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

def mu_unlock
  @_mutex.unlock
end

#sleep(timeout = nil) ⇒ Object

See Mutex#sleep



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

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