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:



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

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

.define_aliases(cl) ⇒ Object

:nodoc:



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

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:



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

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

Instance Method Details

#mu_extendedObject

:nodoc:



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

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



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

def mu_lock
  @_mutex.lock
end

#mu_locked?Boolean

See Mutex#locked?

Returns:

  • (Boolean)


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

def mu_locked?
  @_mutex.locked?
end

#mu_synchronize(&block) ⇒ Object

See Mutex#synchronize



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

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

#mu_try_lockObject

See Mutex#try_lock



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

def mu_try_lock
  @_mutex.try_lock
end

#mu_unlockObject

See Mutex#unlock



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

def mu_unlock
  @_mutex.unlock
end

#sleep(timeout = nil) ⇒ Object

See Mutex#sleep



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

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