Sha256: 1a97adfe56cf0c6c299fae8340175db8cc26fbb098df5166890a196e0109fdb1

Contents?: true

Size: 928 Bytes

Versions: 1

Compression:

Stored size: 928 Bytes

Contents

require 'monitor'

module Redisrank
  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

1 entries across 1 versions & 1 rubygems

Version Path
redisrank-0.1.0 lib/redisrank/mixins/synchronize.rb