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> Tue Mar 13 09:25:10 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* common.mk (clear-installed-list): separated from install-prereq. * common.mk (clear-installed-list): separated from install-prereq.

View file

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