[前][次][番号順一覧][スレッド一覧]

ruby-changes:19012

From: sorah <ko1@a...>
Date: Mon, 7 Mar 2011 23:03:51 +0900 (JST)
Subject: [ruby-changes:19012] Ruby:r31050 (trunk): * lib/pstore.rb: Delete variable @transaction and fix #4474. Patch by

sorah	2011-03-07 23:03:41 +0900 (Mon, 07 Mar 2011)

  New Revision: 31050

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=31050

  Log:
    * lib/pstore.rb: Delete variable @transaction and fix #4474. Patch by
      Masaki Matsushita (Glass_saga).
    
    * test/test_pstore.rb(test_thread_safe): Add test for #4474.

  Modified files:
    trunk/ChangeLog
    trunk/lib/pstore.rb
    trunk/test/test_pstore.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 31049)
+++ ChangeLog	(revision 31050)
@@ -1,3 +1,10 @@
+Mon Mar  7 22:59:39 2011  Shota Fukumori  <sorah@t...>
+
+	* lib/pstore.rb: Delete variable @transaction and fix #4474. Patch by
+	  Masaki Matsushita (Glass_saga).
+
+	* test/test_pstore.rb(test_thread_safe): Add test for #4474.
+
 Mon Mar  7 21:31:38 2011  KOSAKI Motohiro  <kosaki.motohiro@g...>
 
 	* process.c (proc_setgroups): replace getgrnam() with getgrnam_r()
Index: lib/pstore.rb
===================================================================
--- lib/pstore.rb	(revision 31049)
+++ lib/pstore.rb	(revision 31050)
@@ -127,21 +127,16 @@
     if File::exist? file and not File::readable? file
       raise PStore::Error, format("file %s not readable", file)
     end
-    @transaction = false
     @filename = file
     @abort = false
     @ultra_safe = false
     @thread_safe = thread_safe
-    if @thread_safe
-      @lock = Mutex.new
-    else
-      @lock = DummyMutex.new
-    end
+    @lock = Mutex.new
   end
 
   # Raises PStore::Error if the calling code is not in a PStore#transaction.
   def in_transaction
-    raise PStore::Error, "not in transaction" unless @transaction
+    raise PStore::Error, "not in transaction" unless @lock.locked?
   end
   #
   # Raises PStore::Error if the calling code is not in a PStore#transaction or
@@ -318,10 +313,9 @@
   #
   def transaction(read_only = false, &block)  # :yields:  pstore
     value = nil
-    raise PStore::Error, "nested transaction" if @transaction
+    raise PStore::Error, "nested transaction" if !@thread_safe && @lock.locked?
     @lock.synchronize do
       @rdonly = read_only
-      @transaction = true
       @abort = false
       file = open_and_lock_file(@filename, read_only)
       if file
@@ -347,8 +341,6 @@
       end
     end
     value
-  ensure
-    @transaction = false
   end
 
   private
@@ -357,12 +349,6 @@
   EMPTY_MARSHAL_DATA = Marshal.dump({})
   EMPTY_MARSHAL_CHECKSUM = Digest::MD5.digest(EMPTY_MARSHAL_DATA)
 
-  class DummyMutex
-    def synchronize
-      yield
-    end
-  end
-
   #
   # Open the specified filename (either in read-only mode or in
   # read-write mode) and lock it for reading or writing.
Index: test/test_pstore.rb
===================================================================
--- test/test_pstore.rb	(revision 31049)
+++ test/test_pstore.rb	(revision 31050)
@@ -71,4 +71,33 @@
       end
     end
   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)
+      flag = false
+      Thread.new do
+        pstore.transaction do
+          pstore[:foo] = "bar"
+          flag = true
+          sleep 1
+        end
+      end
+      until flag; end
+      pstore.transaction { pstore[:foo] == "bar" }
+      File.unlink("pstore.tmp2.#{Process.pid}") rescue nil
+    end
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]