2006-12-31 10:02:22 -05:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# mutex_m.rb -
|
2001-06-06 05:13:14 -04:00
|
|
|
# $Release Version: 3.0$
|
1999-01-19 23:59:39 -05:00
|
|
|
# $Revision: 1.7 $
|
1998-01-16 07:13:05 -05:00
|
|
|
# Original from mutex.rb
|
2001-06-06 05:13:14 -04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
|
|
|
# modified by matz
|
|
|
|
# patched by akira yamada
|
2006-08-04 14:05:50 -04:00
|
|
|
#
|
2006-12-31 10:02:22 -05:00
|
|
|
# --
|
|
|
|
# Usage:
|
|
|
|
# require "mutex_m.rb"
|
|
|
|
# obj = Object.new
|
|
|
|
# obj.extend Mutex_m
|
|
|
|
# ...
|
|
|
|
# extended object can be handled like Mutex
|
|
|
|
# or
|
|
|
|
# class Foo
|
|
|
|
# include Mutex_m
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
# obj = Foo.new
|
|
|
|
# this obj can be handled like Mutex
|
1998-01-16 07:13:05 -05:00
|
|
|
#
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
require 'thread'
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
module Mutex_m
|
2001-06-06 10:19:33 -04:00
|
|
|
def Mutex_m.define_aliases(cl)
|
|
|
|
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-05 22:56:38 -05:00
|
|
|
end
|
2001-06-06 10:19:33 -04:00
|
|
|
|
2001-06-06 05:13:14 -04:00
|
|
|
def Mutex_m.append_features(cl)
|
|
|
|
super
|
2001-06-06 10:19:33 -04:00
|
|
|
define_aliases(cl) unless cl.instance_of?(Module)
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def Mutex_m.extend_object(obj)
|
1999-08-13 01:45:20 -04:00
|
|
|
super
|
|
|
|
obj.mu_extended
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def mu_extended
|
|
|
|
unless (defined? locked? and
|
|
|
|
defined? lock and
|
|
|
|
defined? unlock and
|
|
|
|
defined? try_lock and
|
|
|
|
defined? synchronize)
|
2010-05-23 04:48:44 -04:00
|
|
|
Mutex_m.define_aliases(singleton_class)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2001-06-06 05:13:14 -04:00
|
|
|
mu_initialize
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
|
|
# locking
|
2006-12-31 10:02:22 -05:00
|
|
|
def mu_synchronize(&block)
|
|
|
|
@_mutex.synchronize(&block)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
def mu_locked?
|
2006-12-31 10:02:22 -05:00
|
|
|
@_mutex.locked?
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
def mu_try_lock
|
2006-12-31 10:02:22 -05:00
|
|
|
@_mutex.try_lock
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
def mu_lock
|
2006-12-31 10:02:22 -05:00
|
|
|
@_mutex.lock
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
def mu_unlock
|
2006-12-31 10:02:22 -05:00
|
|
|
@_mutex.unlock
|
1999-08-13 01:45:20 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
private
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2001-06-06 05:13:14 -04:00
|
|
|
def mu_initialize
|
2006-12-31 10:02:22 -05:00
|
|
|
@_mutex = Mutex.new
|
2001-06-06 05:13:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
mu_initialize
|
|
|
|
super
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|