Sha256: 5d49c3bf3c7068c59a6141dce8de14863940426b280d85113df2b7b565571967

Contents?: true

Size: 659 Bytes

Versions: 16

Compression:

Stored size: 659 Bytes

Contents

require 'thread'

module Innate
  SEMAPHORE = Mutex.new

  module SingletonMethods
    # Use this method to achieve thread-safety for sensitive operations.
    #
    # This should be of most use when manipulating files to prevent other
    # threads from doing the same, no other code will be scheduled during
    # execution of this method.
    #
    # @param [Proc] block the things you want to execute
    def sync(&block)
      SEMAPHORE.synchronize(&block)
    end

    def defer
      outer = ::Thread.current
      ::Thread.new{
        inner = ::Thread.current
        outer.keys.each{|k| inner[k] = outer[k] }
        yield
      }
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
innate-2023.01.06 lib/innate/state.rb
innate-2015.10.28 lib/innate/state.rb
innate-2013.02.21 lib/innate/state.rb
innate-2013.02 lib/innate/state.rb
innate-2012.12 lib/innate/state.rb
innate-2012.03 lib/innate/state.rb
innate-2011.12 lib/innate/state.rb
innate-2011.10 lib/innate/state.rb
innate-2011.04 lib/innate/state.rb
innate-2011.01 lib/innate/state.rb
innate-2010.07 lib/innate/state.rb
innate-2010.06.18 lib/innate/state.rb
innate-2010.04 lib/innate/state.rb
innate-2010.03 lib/innate/state.rb
innate-2010.01 lib/innate/state.rb
innate-2009.10 lib/innate/state.rb