Project

General

Profile

Feature #4483 » patch.diff

Glass_saga (Masaki Matsushita), 03/08/2011 02:36 PM

View differences:

lib/pstore.rb
119 119
  # PStore objects are always reentrant. But if _thread_safe_ is set to true,
120 120
  # then it will become thread-safe at the cost of a minor performance hit.
121 121
  #
122
  def initialize(file, thread_safe = false)
122
  def initialize(file)
123 123
    dir = File::dirname(file)
124 124
    unless File::directory? dir
125 125
      raise PStore::Error, format("directory %s does not exist", dir)
......
130 130
    @filename = file
131 131
    @abort = false
132 132
    @ultra_safe = false
133
    @thread_safe = thread_safe
134 133
    @lock = Mutex.new
135 134
  end
136 135

  
......
313 312
  #
314 313
  def transaction(read_only = false, &block)  # :yields:  pstore
315 314
    value = nil
316
    raise PStore::Error, "nested transaction" if !@thread_safe && @lock.locked?
317 315
    @lock.synchronize do
318 316
      @rdonly = read_only
319 317
      @abort = false
test/test_pstore.rb
73 73
  end
74 74

  
75 75
  def test_thread_safe
76
    assert_raise(PStore::Error) do
77
      flag = false
78
      Thread.new do
79
        @pstore.transaction do
80
          @pstore[:foo] = "bar"
81
          flag = true
82
          sleep 1
83
        end
84
      end
85
      until flag; end
86
      @pstore.transaction {}
87
    end
88 76
    assert_block do
89
      pstore = PStore.new("pstore.tmp2.#{Process.pid}",true)
77
      pstore = PStore.new("pstore.tmp2.#{Process.pid}")
90 78
      flag = false
91 79
      Thread.new do
92 80
        pstore.transaction do