From: Kazuhiro NISHIYAMA Date: 2002-03-05T04:01:45+09:00 Subject: [ruby-dev:16211] pstore.rb and sync.rb 西山和広です。 PStoreがマルチプロセス対応だけどマルチスレッド対応じゃない という話があったので、対応させてみました。 そのときに1.7のsync.rbはsuperが必要になっててはまったので それを気づきやすくするためのパッチもつけてみました。 変更点は * lib/pstore.rb: * Sync_mを使ってマルチスレッド対応。 * read_onlyの時は読み書きオープンしないようにした。 * lib/sync.rb: * initializeでのsuper呼び忘れがわかりやすくなるように 例外をあげるようにした。 です。 -- |ZnZ(ゼット エヌ ゼット) |西山和広(Kazuhiro NISHIYAMA) Index: pstore.rb =================================================================== RCS file: /src/ruby/lib/pstore.rb,v retrieving revision 1.13 diff -u -r1.13 pstore.rb --- pstore.rb 2001/12/01 14:03:19 1.13 +++ pstore.rb 2002/03/04 18:51:28 @@ -14,12 +14,16 @@ require "ftools" require "digest/md5" +require "sync" if defined? Thread class PStore class Error < StandardError end + include Sync_m if defined? Thread + def initialize(file) + super() dir = File::dirname(file) unless File::directory? dir raise PStore::Error, format("directory %s does not exist", dir) @@ -86,12 +90,13 @@ value = nil backup = @filename+"~" begin - file = File::open(@filename, "rb+") + file = File::open(@filename, (read_only ? "rb" : "rb+")) orig = true rescue Errno::ENOENT raise if read_only file = File::open(@filename, "wb+") end + sync_lock(read_only ? Sync_m::SH : Sync_m::EX) if defined? Thread file.flock(read_only ? File::LOCK_SH : File::LOCK_EX) if read_only @table = Marshal::load(file) @@ -133,6 +138,7 @@ @table = nil @transaction = false file.close if file + sync_unlock(read_only ? Sync_m::SH : Sync_m::EX) if defined? Thread end value end Index: sync.rb =================================================================== RCS file: /src/ruby/lib/sync.rb,v retrieving revision 1.4 diff -u -r1.4 sync.rb --- sync.rb 2001/06/06 14:19:33 1.4 +++ sync.rb 2002/03/04 18:51:28 @@ -155,13 +156,16 @@ def sync_unlock(m = EX) Thread.critical = true + if sync_mode.nil? + raise ScriptError, "must call super in initialize" + end if sync_mode == UN Thread.critical = false Err::UnknownLocker.Fail(Thread.current) end - + m = sync_mode if m == EX and sync_mode == SH - + runnable = false case m when UN @@ -276,6 +280,8 @@ else ret = false end + else + raise ScriptError, "must call super in initialize" end when EX if sync_mode == UN or @@ -287,6 +293,8 @@ elsif sync_mode == EX && sync_ex_locker == Thread.current self.sync_ex_count = sync_ex_count + 1 ret = true + elsif sync_mode.nil? + raise ScriptError, "must call super in initialize" else ret = false end @@ -303,7 +311,7 @@ #Sync_m.extend_class self include Sync_m - def initialize + def initialize(*args) super end