whimsical-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [whimsy] branch master updated: Encapsulate file open as well
Date Tue, 03 Jul 2018 17:04:20 GMT
This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new 21a1348  Encapsulate file open as well
21a1348 is described below

commit 21a1348f7d0709999906bf0b62d9707e0e91b15e
Author: Sebb <sebb@apache.org>
AuthorDate: Tue Jul 3 18:04:18 2018 +0100

    Encapsulate file open as well
---
 lib/whimsy/lockfile.rb | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/lib/whimsy/lockfile.rb b/lib/whimsy/lockfile.rb
index b8497fe..57d5515 100644
--- a/lib/whimsy/lockfile.rb
+++ b/lib/whimsy/lockfile.rb
@@ -19,7 +19,7 @@ module LockFile
     return verbose ?  [err==nil, err] :  err==nil
   end
 
-  # lock a file and ensure it gets unlocked
+  # lock an open file and ensure it gets unlocked
   def self.flock(file, mode)
     ok = file.flock(mode)
     if ok
@@ -32,6 +32,14 @@ module LockFile
     ok
   end
 
+  # open a file and lock it
+  def self.lockfile(filename, mode, lockmode)
+    open(filename, mode) do |f|
+      self.flock(f, lockmode) { |g|
+        yield g
+      }
+    end
+  end
 end
 
 
@@ -47,6 +55,14 @@ if __FILE__ == $0
     ret = LockFile.create_ex(name) {|f| f << text}
   when 'createShow'
     ret = LockFile.create_ex(name, true) {|f| f << text}
+  when 'opena'
+    puts "#{Time.now} Wait lock"
+    ret = LockFile.lockfile(name, 'a', File::LOCK_EX) do |f|
+      puts "#{Time.now} Got lock"
+      f << text
+      puts "#{Time.now} Sleep"
+      sleep(5)
+    end
   when 'locka'
     open(name,'a') do |f|
       puts "#{Time.now} Wait lock"


Mime
View raw message