1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
# mutex_m.rb -
|
2001-06-06 13:13:14 +04:00
|
|
|
# $Release Version: 3.0$
|
1999-01-20 07:59:39 +03:00
|
|
|
# $Revision: 1.7 $
|
|
|
|
# $Date: 1998/02/27 04:28:57 $
|
1998-01-16 15:13:05 +03:00
|
|
|
# Original from mutex.rb
|
2001-06-06 13:13:14 +04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
|
|
|
# modified by matz
|
|
|
|
# patched by akira yamada
|
1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
# Usage:
|
|
|
|
# require "mutex_m.rb"
|
|
|
|
# obj = Object.new
|
|
|
|
# obj.extend Mutex_m
|
|
|
|
# ...
|
1999-08-13 09:45:20 +04:00
|
|
|
# extended object can be handled like Mutex
|
2001-06-06 13:13:14 +04:00
|
|
|
# or
|
|
|
|
# class Foo
|
|
|
|
# include Mutex_m
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
# obj = Foo.new
|
|
|
|
# this obj can be handled like Mutex
|
1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
|
|
|
|
module Mutex_m
|
2001-06-06 18: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
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2001-06-06 13:13:14 +04:00
|
|
|
def Mutex_m.append_features(cl)
|
|
|
|
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
|
|
|
|
|
|
|
|
def Mutex_m.extend_object(obj)
|
1999-08-13 09:45:20 +04:00
|
|
|
super
|
|
|
|
obj.mu_extended
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
def mu_extended
|
|
|
|
unless (defined? locked? and
|
|
|
|
defined? lock and
|
|
|
|
defined? unlock and
|
|
|
|
defined? try_lock and
|
|
|
|
defined? synchronize)
|
2001-06-06 18:19:33 +04:00
|
|
|
Mutex_m.define_aliases(class<<self;self;end)
|
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
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
# locking
|
1998-01-16 15:13:05 +03:00
|
|
|
def mu_synchronize
|
|
|
|
begin
|
|
|
|
mu_lock
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
mu_unlock
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
def mu_locked?
|
|
|
|
@mu_locked
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
def mu_try_lock
|
|
|
|
result = false
|
|
|
|
Thread.critical = true
|
|
|
|
unless @mu_locked
|
|
|
|
@mu_locked = true
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
Thread.critical = false
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def mu_lock
|
|
|
|
while (Thread.critical = true; @mu_locked)
|
|
|
|
@mu_waiting.push Thread.current
|
|
|
|
Thread.stop
|
|
|
|
end
|
|
|
|
@mu_locked = true
|
|
|
|
Thread.critical = false
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def mu_unlock
|
|
|
|
return unless @mu_locked
|
|
|
|
Thread.critical = true
|
|
|
|
wait = @mu_waiting
|
|
|
|
@mu_waiting = []
|
|
|
|
@mu_locked = false
|
|
|
|
Thread.critical = false
|
|
|
|
for w in wait
|
|
|
|
w.run
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2001-06-06 13:13:14 +04:00
|
|
|
def mu_initialize
|
1999-08-13 09:45:20 +04:00
|
|
|
@mu_waiting = []
|
|
|
|
@mu_locked = false;
|
2001-06-06 13:13:14 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
mu_initialize
|
|
|
|
super
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|