Feature #4483 » patch.diff
lib/pstore.rb | ||
---|---|---|
# PStore objects are always reentrant. But if _thread_safe_ is set to true,
|
||
# then it will become thread-safe at the cost of a minor performance hit.
|
||
#
|
||
def initialize(file, thread_safe = false)
|
||
def initialize(file)
|
||
dir = File::dirname(file)
|
||
unless File::directory? dir
|
||
raise PStore::Error, format("directory %s does not exist", dir)
|
||
... | ... | |
@filename = file
|
||
@abort = false
|
||
@ultra_safe = false
|
||
@thread_safe = thread_safe
|
||
@lock = Mutex.new
|
||
end
|
||
... | ... | |
#
|
||
def transaction(read_only = false, &block) # :yields: pstore
|
||
value = nil
|
||
raise PStore::Error, "nested transaction" if !@thread_safe && @lock.locked?
|
||
@lock.synchronize do
|
||
@rdonly = read_only
|
||
@abort = false
|
test/test_pstore.rb | ||
---|---|---|
end
|
||
def test_thread_safe
|
||
assert_raise(PStore::Error) do
|
||
flag = false
|
||
Thread.new do
|
||
@pstore.transaction do
|
||
@pstore[:foo] = "bar"
|
||
flag = true
|
||
sleep 1
|
||
end
|
||
end
|
||
until flag; end
|
||
@pstore.transaction {}
|
||
end
|
||
assert_block do
|
||
pstore = PStore.new("pstore.tmp2.#{Process.pid}",true)
|
||
pstore = PStore.new("pstore.tmp2.#{Process.pid}")
|
||
flag = false
|
||
Thread.new do
|
||
pstore.transaction do
|