summaryrefslogtreecommitdiff
path: root/lib/tempfile.rb
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-12-31 15:02:22 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-12-31 15:02:22 +0000
commita3e1b1ce7ed7e7ffac23015fc2fde56511b30681 (patch)
tree7b725552a9a4ded93849ca2faab1b257f7761790 /lib/tempfile.rb
parent3e7566d8fb5138bb9cd647e5fdefc54fc9803509 (diff)
* Merge YARV
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11439 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/tempfile.rb')
-rw-r--r--lib/tempfile.rb36
1 files changed, 17 insertions, 19 deletions
diff --git a/lib/tempfile.rb b/lib/tempfile.rb
index a033a5b..6a2d560 100644
--- a/lib/tempfile.rb
+++ b/lib/tempfile.rb
@@ -6,12 +6,14 @@
require 'delegate'
require 'tmpdir'
+require 'thread'
# A class for managing temporary files. This library is written to be
# thread safe.
class Tempfile < DelegateClass(File)
MAX_TRY = 10
@@cleanlist = []
+ @@lock = Mutex.new
# Creates a temporary file of mode 0600 in the temporary directory
# whose name is basename.pid.n and opens with mode "w+". A Tempfile
@@ -26,27 +28,23 @@ class Tempfile < DelegateClass(File)
tmpdir = '/tmp'
end
- lock = nil
+ lock = tmpname = nil
n = failure = 0
-
- begin
- Thread.critical = true
-
+ @@lock.synchronize {
begin
- tmpname = File.join(tmpdir, make_tmpname(basename, n))
- lock = tmpname + '.lock'
- n += 1
- end while @@cleanlist.include?(tmpname) or
- File.exist?(lock) or File.exist?(tmpname)
-
- Dir.mkdir(lock)
- rescue
- failure += 1
- retry if failure < MAX_TRY
- raise "cannot generate tempfile `%s'" % tmpname
- ensure
- Thread.critical = false
- end
+ begin
+ tmpname = File.join(tmpdir, make_tmpname(basename, n))
+ lock = tmpname + '.lock'
+ n += 1
+ end while @@cleanlist.include?(tmpname) or
+ File.exist?(lock) or File.exist?(tmpname)
+ Dir.mkdir(lock)
+ rescue
+ failure += 1
+ retry if failure < MAX_TRY
+ raise "cannot generate tempfile `%s'" % tmpname
+ end
+ }
@data = [tmpname]
@clean_proc = Tempfile.callback(@data)