File: state.rb

package info (click to toggle)
libinnate-ruby 2010.07-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 812 kB
  • ctags: 621
  • sloc: ruby: 4,242; makefile: 2
file content (27 lines) | stat: -rw-r--r-- 659 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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