1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/sync.rb: support for ruby 1.9(YARV) thread model.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12064 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
keiju 2007-03-13 10:13:41 +00:00
parent 140fb1bae3
commit f6f313379b
2 changed files with 89 additions and 88 deletions

View file

@ -1,3 +1,7 @@
Tue Mar 13 19:04:30 2007 Keiju Ishitsuka <keiju@ruby-lang.org>
* lib/sync.rb: support for ruby 1.9(YARV) thread model.
Tue Mar 13 09:25:10 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* common.mk (clear-installed-list): separated from install-prereq.

View file

@ -23,8 +23,7 @@
# Sync_m#sync_lock, lock
# Sync_m#sync_unlock, unlock
#
# Sync, Synchronicer:
# include Sync_m
# Sync, Synchronizer:
# Usage:
# sync = Sync.new
#
@ -89,19 +88,18 @@ module Sync_m
def Sync_m.append_features(cl)
super
unless cl.instance_of?(Module)
# do nothing for Modules
# make aliases and include the proper module.
define_aliases(cl)
end
# make aliases for Classes.
define_aliases(cl) unless cl.instance_of?(Module)
self
end
def Sync_m.extend_object(obj)
super
obj.sync_extended
obj.sync_extend
end
def sync_extended
def sync_extend
unless (defined? locked? and
defined? shared? and
defined? exclusive? and
@ -130,33 +128,37 @@ module Sync_m
# locking methods.
def sync_try_lock(mode = EX)
return unlock if sync_mode == UN
Thread.critical = true
@sync_mutex.synchronize do
ret = sync_try_lock_sub(sync_mode)
Thread.critical = false
end
ret
end
def sync_lock(m = EX)
return unlock if m == UN
until (Thread.critical = true; sync_try_lock_sub(m))
while true
@sync_mutex.synchronize do
if sync_try_lock_sub(m)
return self
else
if sync_sh_locker[Thread.current]
sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]]
sync_sh_locker.delete(Thread.current)
else
sync_waiting.push Thread.current
end
Thread.stop
@sync_mutex.sleep
end
end
end
Thread.critical = false
self
end
def sync_unlock(m = EX)
Thread.critical = true
wakeup_threads = []
@sync_mutex.synchronize do
if sync_mode == UN
Thread.critical = false
Err::UnknownLocker.Fail(Thread.current)
end
@ -165,7 +167,6 @@ module Sync_m
runnable = false
case m
when UN
Thread.critical = false
Err::UnknownLocker.Fail(Thread.current)
when EX
@ -199,33 +200,29 @@ module Sync_m
if runnable
if sync_upgrade_waiting.size > 0
for k, v in sync_upgrade_waiting
sync_sh_locker[k] = v
end
wait = sync_upgrade_waiting
self.sync_upgrade_waiting = []
Thread.critical = false
for w, v in wait
w.run
end
th, count = sync_upgrade_waiting.shift
sync_sh_locker[th] = count
th.wakeup
wakeup_threads.push th
else
wait = sync_waiting
self.sync_waiting = []
Thread.critical = false
for w in wait
w.run
for th in wait
th.wakeup
wakeup_threads.push th
end
end
end
Thread.critical = false
end
for th in wakeup_threads
th.run
end
self
end
def sync_synchronize(mode = EX)
begin
sync_lock(mode)
begin
yield
ensure
sync_unlock
@ -240,6 +237,11 @@ module Sync_m
attr_accessor :sync_ex_locker
attr_accessor :sync_ex_count
def sync_inspect
sync_iv = instance_variables.select{|iv| /^@sync_/ =~ iv.id2name}.collect{|iv| iv.id2name + '=' + instance_eval(iv.id2name).inspect}.join(",")
print "<#{self.class}.extend Sync_m: #{inspect}, <Sync_m: #{sync_iv}>"
end
private
def sync_initialize
@ -249,11 +251,13 @@ module Sync_m
@sync_sh_locker = Hash.new
@sync_ex_locker = nil
@sync_ex_count = 0
@sync_mutex = Mutex.new
end
def initialize(*args)
sync_initialize
super
sync_initialize
end
def sync_try_lock_sub(m)
@ -291,7 +295,6 @@ module Sync_m
ret = false
end
else
Thread.critical = false
Err::LockModeFailer.Fail mode
end
return ret
@ -300,12 +303,6 @@ end
Synchronizer_m = Sync_m
class Sync
#Sync_m.extend_class self
include Sync_m
def initialize
super
end
end
Synchronizer = Sync