Sha256: d06b9e2d4dfdbb0a4d4c319089fe4efe862c37f2289342edb55d92d54732a93b

Contents?: true

Size: 927 Bytes

Versions: 2

Compression:

Stored size: 927 Bytes

Contents

require 'monitor'

module Redistat
  module Synchronize

    class << self
      def included(base)
        base.send(:include, InstanceMethods)
      end

      def monitor
        @monitor ||= Monitor.new
      end

      def thread_safe
        monitor.synchronize do
          return @thread_safe unless @thread_safe.nil?
          @thread_safe = false
        end
      end

      def thread_safe=(value)
        monitor.synchronize do
          @thread_safe = value
        end
      end
    end # << self

    module InstanceMethods
      def thread_safe
        Synchronize.thread_safe
      end

      def thread_safe=(value)
        Synchronize.thread_safe = value
      end

      def monitor
        Synchronize.monitor
      end

      def synchronize(&block)
        if thread_safe
          monitor.synchronize(&block)
        else
          block.call
        end
      end
    end # InstanceMethods

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redistat-0.5.0 lib/redistat/mixins/synchronize.rb
redistat-0.4.0 lib/redistat/mixins/synchronize.rb