2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2006-12-31 18:02:22 +03:00
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# mutex_m.rb -
|
2011-05-19 04:07:25 +04:00
|
|
|
# $Release Version: 3.0$
|
|
|
|
# $Revision: 1.7 $
|
1998-01-16 15:13:05 +03:00
|
|
|
# Original from mutex.rb
|
2011-05-19 04:07:25 +04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
2001-06-06 13:13:14 +04:00
|
|
|
# modified by matz
|
|
|
|
# patched by akira yamada
|
2006-08-04 22:05:50 +04:00
|
|
|
#
|
2006-12-31 18:02:22 +03:00
|
|
|
# --
|
2013-02-20 06:51:51 +04:00
|
|
|
|
|
|
|
# = 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
|
2018-10-21 17:02:00 +03:00
|
|
|
# methods --- remember to call super() in your class initialize method.
|
2013-02-20 06:51:51 +04:00
|
|
|
#
|
|
|
|
# class Foo
|
|
|
|
# include Mutex_m
|
2018-10-21 17:02:00 +03:00
|
|
|
# def initialize
|
|
|
|
# # ...
|
2018-10-21 10:04:20 +03:00
|
|
|
# super()
|
|
|
|
# end
|
2013-02-20 06:51:51 +04:00
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
# obj = Foo.new
|
|
|
|
# # this obj can be handled like Mutex
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
module Mutex_m
|
2018-07-25 15:28:17 +03:00
|
|
|
|
|
|
|
VERSION = "0.1.0"
|
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
def Mutex_m.define_aliases(cl) # :nodoc:
|
2001-06-06 18:19:33 +04:00
|
|
|
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
|
|
|
|
}
|
2009-03-06 06:56:38 +03:00
|
|
|
end
|
2001-06-06 18:19:33 +04:00
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
def Mutex_m.append_features(cl) # :nodoc:
|
2001-06-06 13:13:14 +04:00
|
|
|
super
|
2001-06-06 18:19:33 +04:00
|
|
|
define_aliases(cl) unless cl.instance_of?(Module)
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
def Mutex_m.extend_object(obj) # :nodoc:
|
1999-08-13 09:45:20 +04:00
|
|
|
super
|
|
|
|
obj.mu_extended
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
def mu_extended # :nodoc:
|
1998-01-16 15:13:05 +03:00
|
|
|
unless (defined? locked? and
|
2011-05-19 01:19:18 +04:00
|
|
|
defined? lock and
|
|
|
|
defined? unlock and
|
|
|
|
defined? try_lock and
|
|
|
|
defined? synchronize)
|
2011-05-19 04:07:25 +04:00
|
|
|
Mutex_m.define_aliases(singleton_class)
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2001-06-06 13:13:14 +04:00
|
|
|
mu_initialize
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
# See Mutex#synchronize
|
2006-12-31 18:02:22 +03:00
|
|
|
def mu_synchronize(&block)
|
|
|
|
@_mutex.synchronize(&block)
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
# See Mutex#locked?
|
1999-08-13 09:45:20 +04:00
|
|
|
def mu_locked?
|
2006-12-31 18:02:22 +03:00
|
|
|
@_mutex.locked?
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
# See Mutex#try_lock
|
1999-08-13 09:45:20 +04:00
|
|
|
def mu_try_lock
|
2006-12-31 18:02:22 +03:00
|
|
|
@_mutex.try_lock
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
# See Mutex#lock
|
1999-08-13 09:45:20 +04:00
|
|
|
def mu_lock
|
2006-12-31 18:02:22 +03:00
|
|
|
@_mutex.lock
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
# See Mutex#unlock
|
1999-08-13 09:45:20 +04:00
|
|
|
def mu_unlock
|
2006-12-31 18:02:22 +03:00
|
|
|
@_mutex.unlock
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
# See Mutex#sleep
|
2010-07-23 11:14:54 +04:00
|
|
|
def sleep(timeout = nil)
|
|
|
|
@_mutex.sleep(timeout)
|
|
|
|
end
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
private
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
def mu_initialize # :nodoc:
|
2016-08-30 09:22:30 +03:00
|
|
|
@_mutex = Thread::Mutex.new
|
2001-06-06 13:13:14 +04:00
|
|
|
end
|
|
|
|
|
2013-02-20 06:51:51 +04:00
|
|
|
def initialize(*args) # :nodoc:
|
2001-06-06 13:13:14 +04:00
|
|
|
mu_initialize
|
|
|
|
super
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|