Coder Social home page Coder Social logo

futex's Introduction

EO principles respected here DevOps By Rultor.com We recommend RubyMine

Build Status Build status Gem Version Maintainability Yard Docs

Hits-of-Code License

Sometimes you need to synchronize your block of code, but Mutex is too coarse-grained, because it always locks, no matter what objects your code accesses. The Futex (from "file mutex") is more fine-grained and uses a file as an entrance lock to your code.

First, install it:

$ gem install futex

Then, use it like this:

require 'futex'
Futex.new('/tmp/my-file.txt').open do |f|
  IO.write(f, 'Hello, world!')
end

The file /tmp/my-file.txt.lock will be created and used as an entrance lock. It will won't be deleted afterwards.

If you are not planning to write to the file, it is recommended to get a non-exclusive/shared access to it, by providing false to the method open():

require 'futex'
Futex.new('/tmp/my-file.txt').open(false) do |f|
  IO.read(f)
end

For better traceability you can provide a few arguments to the constructor of the Futex class, including:

  • log: an object that implements debug() method, which will receive supplementary messages from the locking mechanism;

  • logging: set it to true if you want to see logs;

  • timeout: the number of seconds to wait for the lock availability (Futex::CantLock exception is raised when the wait is expired);

  • sleep: the number of seconds to wait between attempts to acquire the lock file (the smaller the number, the more responsive is the software, but the higher the load for the file system and the CPU);

  • lock: the absolute path of the lock file;

That's it.

How to contribute

Read these guidelines. Make sure you build is green before you contribute your pull request. You will need to have Ruby 2.3+ and Bundler installed. Then:

$ bundle update
$ bundle exec rake

If it's clean and you don't see any error messages, submit your pull request.

futex's People

Contributors

dapi avatar renovate[bot] avatar tsvayer avatar yegor256 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

dapi tsvayer

futex's Issues

Errno::EACCES: Permission denied @ fptr_finalize (in Windows)

https://ci.appveyor.com/project/yegor256/futex/build/job/1wmn2jfi5wce1f96

Errno::EACCES: Permission denied @ fptr_finalize - C:/Users/appveyor/AppData/Local/Temp/1/d20181109-2536-mxashs/a/b/c/file.txt.lock
	C:/projects/futex/lib/futex.rb:89:in `write'
	C:/projects/futex/lib/futex.rb:89:in `block in open'
	C:/projects/futex/lib/futex.rb:70:in `open'
	C:/projects/futex/lib/futex.rb:70:in `open'
	C:/projects/futex/test/test_futex.rb:52:in `block (2 levels) in test_syncs_access_to_file_in_slow_motion'
	C:/projects/futex/vendor/bundle/ruby/2.3.0/gems/threads-0.3.0/lib/threads.rb:57:in `block (3 levels) in assert'
	C:/projects/futex/vendor/bundle/ruby/2.3.0/gems/threads-0.3.0/lib/threads.rb:53:in `loop'
	C:/projects/futex/vendor/bundle/ruby/2.3.0/gems/threads-0.3.0/lib/threads.rb:53:in `block (2 levels) in assert'
	C:/projects/futex/vendor/bundle/ruby/2.3.0/gems/concurrent-ruby-1.1.3/lib/concurrent/executor/ruby_thread_pool_executor.rb:348:in `run_task'
	C:/projects/futex/vendor/bundle/ruby/2.3.0/gems/concurrent-ruby-1.1.3/lib/concurrent/executor/ruby_thread_pool_executor.rb:337:in `block (3 levels) in create_worker'
	C:/projects/futex/vendor/bundle/ruby/2.3.0/gems/concurrent-ruby-1.1.3/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `loop'
	C:/projects/futex/vendor/bundle/ruby/2.3.0/gems/concurrent-ruby-1.1.3/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `block (2 levels) in create_worker'
	C:/projects/futex/vendor/bundle/ruby/2.3.0/gems/concurrent-ruby-1.1.3/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `catch'
	C:/projects/futex/vendor/bundle/ruby/2.3.0/gems/concurrent-ruby-1.1.3/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `block in create_worker'

relative file names

Let's print relative file names, instead of absolute ones (make sure this feature is configurable):

require 'futex'
Futex.new('/tmp/my-file.txt', absolute_file_names: true).open |f|
  IO.read(f)
end

By default this should be set to false.

This is what I see now in logs:

#83543/ can't get exclusive access to the file /private/var/folders/vl/633jwjvn2jvbj9zfg1sgglhw0000gp/T/d20181120-83431-1gu0e5m/cb19b5d996fe7f19.z because of the lock at /private/var/folders/vl/633jwjvn2jvbj9zfg1sgglhw0000gp/T/d20181120-83431-1gu0e5m/cb19b5d996fe7f19.z.lock, after 16000ms of waiting: #83543/

I want to see this:

#83543/ can't get exclusive access to the file cb19b5d996fe7f19.z because of the lock at cb19b5d996fe7f19.z.lock, after 16000ms of waiting: #83543/

NoMethodError: undefined method `-' for nil:NilClass

NoMethodError: undefined method `-' for nil:NilClass
   	/Users/yegor/.rvm/gems/ruby-2.6.0/gems/futex-0.8.4/lib/futex.rb:180:in `block in open_synchronized'
   	/Users/yegor/.rvm/gems/ruby-2.6.0/gems/futex-0.8.4/lib/futex.rb:192:in `block in synchronized'
   	/Users/yegor/.rvm/gems/ruby-2.6.0/gems/futex-0.8.4/lib/futex.rb:190:in `open'
   	/Users/yegor/.rvm/gems/ruby-2.6.0/gems/futex-0.8.4/lib/futex.rb:190:in `synchronized'
   	/Users/yegor/.rvm/gems/ruby-2.6.0/gems/futex-0.8.4/lib/futex.rb:177:in `ensure in open_synchronized'
   	/Users/yegor/.rvm/gems/ruby-2.6.0/gems/futex-0.8.4/lib/futex.rb:177:in `open_synchronized'
   	/Users/yegor/.rvm/gems/ruby-2.6.0/gems/futex-0.8.4/lib/futex.rb:103:in `open'

NoMethodError - undefined method `close' for nil:NilClass

NoMethodError - undefined method `close' for nil:NilClass:
	/Users/yegor/.rvm/gems/ruby-2.5.1/gems/futex-0.8.3/lib/futex.rb:178:in `block in open_synchronized'
	/Users/yegor/.rvm/gems/ruby-2.5.1/gems/futex-0.8.3/lib/futex.rb:192:in `block in synchronized'
	/Users/yegor/.rvm/gems/ruby-2.5.1/gems/futex-0.8.3/lib/futex.rb:190:in `open'
	/Users/yegor/.rvm/gems/ruby-2.5.1/gems/futex-0.8.3/lib/futex.rb:190:in `synchronized'
	/Users/yegor/.rvm/gems/ruby-2.5.1/gems/futex-0.8.3/lib/futex.rb:177:in `ensure in open_synchronized'
	/Users/yegor/.rvm/gems/ruby-2.5.1/gems/futex-0.8.3/lib/futex.rb:177:in `open_synchronized'
	/Users/yegor/.rvm/gems/ruby-2.5.1/gems/futex-0.8.3/lib/futex.rb:103:in `open'

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

bundler
Gemfile
  • ruby ~>2.3

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.