summaryrefslogtreecommitdiff
path: root/lib/mutex_m.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-08-13 05:45:20 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-08-13 05:45:20 +0000
commit65a5162550f58047974793cdc8067a970b2435c0 (patch)
tree082bb7d5568f3b2e36e3fe166e9f3039394fcf44 /lib/mutex_m.rb
parentfcd020c83028f5610d382e85a2df00223e12bd7e (diff)
1.4.0
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@520 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/mutex_m.rb')
-rw-r--r--lib/mutex_m.rb226
1 files changed, 59 insertions, 167 deletions
diff --git a/lib/mutex_m.rb b/lib/mutex_m.rb
index 4b8d644..e0fcf0f 100644
--- a/lib/mutex_m.rb
+++ b/lib/mutex_m.rb
@@ -12,54 +12,27 @@
# obj = Object.new
# obj.extend Mutex_m
# ...
-# 後はMutexと同じ使い方
+# extended object can be handled like Mutex
#
-require "finalize"
-
module Mutex_m
- def Mutex_m.extendable_module(obj)
- if Fixnum === obj or TRUE === obj or FALSE === obj or nil == obj
- raise TypeError, "Mutex_m can't extend to this class(#{obj.type})"
- else
- begin
- obj.instance_eval "@mu_locked"
- For_general_object
- rescue TypeError
- For_primitive_object
- end
+ def Mutex_m.append_features(cl)
+ super
+ unless cl.instance_of?(Module)
+ 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
- end
-
- def Mutex_m.includable_module(cl)
- begin
- dummy = cl.new
- Mutex_m.extendable_module(dummy)
- rescue NameError
- # newが定義されていない時は, DATAとみなす.
- For_primitive_object
- end
- end
-
- def Mutex_m.extend_class(cl)
- return super if cl.instance_of?(Module)
-
- # モジュールの時は何もしない. クラスの場合, 適切なモジュールの決定
- # とaliasを行う.
- real = includable_module(cl)
- cl.module_eval %q{
- include real
-
- alias locked? mu_locked?
- alias lock mu_lock
- alias unlock mu_unlock
- alias try_lock mu_try_lock
- alias synchronize mu_synchronize
- }
+ return self
end
def Mutex_m.extend_object(obj)
- obj.extend(Mutex_m.extendable_module(obj))
+ super
+ obj.mu_extended
end
def mu_extended
@@ -76,6 +49,7 @@ module Mutex_m
alias synchronize mu_synchronize
end"
end
+ initialize
end
# locking
@@ -88,132 +62,50 @@ module Mutex_m
end
end
- # internal class
- module For_general_object
- include Mutex_m
-
- def For_general_object.extend_object(obj)
- super
- obj.mu_extended
- end
-
- def mu_extended
- super
- @mu_waiting = []
- @mu_locked = FALSE;
- end
-
- def mu_locked?
- @mu_locked
- end
-
- 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
-
+ def mu_locked?
+ @mu_locked
end
-
- module For_primitive_object
- include Mutex_m
- Mu_Locked = Hash.new
-
- def For_primitive_object.extend_object(obj)
- super
-
- obj.mu_extended
- Finalizer.add(obj, For_primitive_object, :mu_finalize)
- end
-
- def mu_extended
- super
- initialize
- end
-
- def For_primitive_object.mu_finalize(id)
- Thread.critical = TRUE
- if wait = Mu_Locked.delete(id)
- # wait == [] ときだけ GCされるので, for w in wait は意味なし.
- Thread.critical = FALSE
- for w in wait
- w.run
- end
- else
- Thread.critical = FALSE
- end
- self
- end
-
- def mu_locked?
- Mu_Locked.key?(self.id)
- end
-
- def mu_try_lock
- Thread.critical = TRUE
- if Mu_Locked.key?(self.id)
- ret = FALSE
- else
- Mu_Locked[self.id] = []
- Finalizer.add(self, For_primitive_object, :mu_finalize)
- ret = TRUE
- end
- Thread.critical = FALSE
- ret
- end
-
- def mu_lock
- while (Thread.critical = TRUE; w = Mu_Locked[self.id])
- w.push Thread.current
- Thread.stop
- end
- Mu_Locked[self.id] = []
- Finalizer.add(self, For_primitive_object, :mu_finalize)
- Thread.critical = FALSE
- self
- end
-
- def mu_unlock
- Thread.critical = TRUE
- if wait = Mu_Locked.delete(self.id)
- Finalizer.delete(self, For_primitive_object, :mu_finalize)
- Thread.critical = FALSE
- for w in wait
- w.run
- end
- else
- Thread.critical = FALSE
- end
- self
- end
+
+ 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
+
+ def initialize(*args)
+ ret = super
+ @mu_waiting = []
+ @mu_locked = false;
+ return ret
end
end
-