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:
parent
140fb1bae3
commit
f6f313379b
2 changed files with 89 additions and 88 deletions
|
@ -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.
|
||||||
|
|
173
lib/sync.rb
173
lib/sync.rb
|
@ -23,9 +23,8 @@
|
||||||
# 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
|
||||||
#
|
#
|
||||||
# Sync#mode
|
# Sync#mode
|
||||||
|
@ -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 for Classes.
|
||||||
# make aliases and include the proper module.
|
define_aliases(cl) unless cl.instance_of?(Module)
|
||||||
define_aliases(cl)
|
self
|
||||||
end
|
|
||||||
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,102 +128,101 @@ 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)
|
end
|
||||||
Thread.critical = false
|
|
||||||
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
|
||||||
if sync_sh_locker[Thread.current]
|
@sync_mutex.synchronize do
|
||||||
sync_upgrade_waiting.push [Thread.current, sync_sh_locker[Thread.current]]
|
if sync_try_lock_sub(m)
|
||||||
sync_sh_locker.delete(Thread.current)
|
return self
|
||||||
else
|
else
|
||||||
sync_waiting.push Thread.current
|
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
|
||||||
|
@sync_mutex.sleep
|
||||||
|
end
|
||||||
end
|
end
|
||||||
Thread.stop
|
|
||||||
end
|
end
|
||||||
Thread.critical = false
|
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def sync_unlock(m = EX)
|
def sync_unlock(m = EX)
|
||||||
Thread.critical = true
|
wakeup_threads = []
|
||||||
if sync_mode == UN
|
@sync_mutex.synchronize do
|
||||||
Thread.critical = false
|
if sync_mode == UN
|
||||||
Err::UnknownLocker.Fail(Thread.current)
|
|
||||||
end
|
|
||||||
|
|
||||||
m = sync_mode if m == EX and sync_mode == SH
|
|
||||||
|
|
||||||
runnable = false
|
|
||||||
case m
|
|
||||||
when UN
|
|
||||||
Thread.critical = false
|
|
||||||
Err::UnknownLocker.Fail(Thread.current)
|
|
||||||
|
|
||||||
when EX
|
|
||||||
if sync_ex_locker == Thread.current
|
|
||||||
if (self.sync_ex_count = sync_ex_count - 1) == 0
|
|
||||||
self.sync_ex_locker = nil
|
|
||||||
if sync_sh_locker.include?(Thread.current)
|
|
||||||
self.sync_mode = SH
|
|
||||||
else
|
|
||||||
self.sync_mode = UN
|
|
||||||
end
|
|
||||||
runnable = true
|
|
||||||
end
|
|
||||||
else
|
|
||||||
Err::UnknownLocker.Fail(Thread.current)
|
Err::UnknownLocker.Fail(Thread.current)
|
||||||
end
|
end
|
||||||
|
|
||||||
when SH
|
m = sync_mode if m == EX and sync_mode == SH
|
||||||
if (count = sync_sh_locker[Thread.current]).nil?
|
|
||||||
|
runnable = false
|
||||||
|
case m
|
||||||
|
when UN
|
||||||
Err::UnknownLocker.Fail(Thread.current)
|
Err::UnknownLocker.Fail(Thread.current)
|
||||||
else
|
|
||||||
if (sync_sh_locker[Thread.current] = count - 1) == 0
|
when EX
|
||||||
sync_sh_locker.delete(Thread.current)
|
if sync_ex_locker == Thread.current
|
||||||
if sync_sh_locker.empty? and sync_ex_count == 0
|
if (self.sync_ex_count = sync_ex_count - 1) == 0
|
||||||
self.sync_mode = UN
|
self.sync_ex_locker = nil
|
||||||
|
if sync_sh_locker.include?(Thread.current)
|
||||||
|
self.sync_mode = SH
|
||||||
|
else
|
||||||
|
self.sync_mode = UN
|
||||||
|
end
|
||||||
runnable = true
|
runnable = true
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
Err::UnknownLocker.Fail(Thread.current)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
when SH
|
||||||
w.run
|
if (count = sync_sh_locker[Thread.current]).nil?
|
||||||
|
Err::UnknownLocker.Fail(Thread.current)
|
||||||
|
else
|
||||||
|
if (sync_sh_locker[Thread.current] = count - 1) == 0
|
||||||
|
sync_sh_locker.delete(Thread.current)
|
||||||
|
if sync_sh_locker.empty? and sync_ex_count == 0
|
||||||
|
self.sync_mode = UN
|
||||||
|
runnable = true
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
else
|
end
|
||||||
wait = sync_waiting
|
|
||||||
self.sync_waiting = []
|
if runnable
|
||||||
Thread.critical = false
|
if sync_upgrade_waiting.size > 0
|
||||||
for w in wait
|
th, count = sync_upgrade_waiting.shift
|
||||||
w.run
|
sync_sh_locker[th] = count
|
||||||
|
th.wakeup
|
||||||
|
wakeup_threads.push th
|
||||||
|
else
|
||||||
|
wait = sync_waiting
|
||||||
|
self.sync_waiting = []
|
||||||
|
for th in wait
|
||||||
|
th.wakeup
|
||||||
|
wakeup_threads.push th
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
for th in wakeup_threads
|
||||||
Thread.critical = false
|
th.run
|
||||||
|
end
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def sync_synchronize(mode = EX)
|
def sync_synchronize(mode = EX)
|
||||||
|
sync_lock(mode)
|
||||||
begin
|
begin
|
||||||
sync_lock(mode)
|
|
||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
sync_unlock
|
sync_unlock
|
||||||
|
@ -239,7 +236,12 @@ module Sync_m
|
||||||
attr_accessor :sync_sh_locker
|
attr_accessor :sync_sh_locker
|
||||||
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)
|
||||||
|
@ -279,7 +283,7 @@ module Sync_m
|
||||||
end
|
end
|
||||||
when EX
|
when EX
|
||||||
if sync_mode == UN or
|
if sync_mode == UN or
|
||||||
sync_mode == SH && sync_sh_locker.size == 1 && sync_sh_locker.include?(Thread.current)
|
sync_mode == SH && sync_sh_locker.size == 1 && sync_sh_locker.include?(Thread.current)
|
||||||
self.sync_mode = m
|
self.sync_mode = m
|
||||||
self.sync_ex_locker = Thread.current
|
self.sync_ex_locker = Thread.current
|
||||||
self.sync_ex_count = 1
|
self.sync_ex_count = 1
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue