Sha256: c59ad51f9d356d06b75ecbc5a63ce9c52fc0a088ef636b0fc0376f5b30330a32

Contents?: true

Size: 1.78 KB

Versions: 6

Compression:

Stored size: 1.78 KB

Contents

module Concurrent
  module Synchronization

    if Concurrent.on_rbx?
      
      # @!visibility private
      # @!macro internal_implementation_note
      class RbxObject < AbstractObject
        def initialize
          @Waiters = []
          ensure_ivar_visibility!
        end

        protected

        def synchronize(&block)
          Rubinius.synchronize(self, &block)
        end

        def ns_wait(timeout = nil)
          wchan = Rubinius::Channel.new

          begin
            @Waiters.push wchan
            Rubinius.unlock(self)
            signaled = wchan.receive_timeout timeout
          ensure
            Rubinius.lock(self)

            if !signaled && !@Waiters.delete(wchan)
              # we timed out, but got signaled afterwards,
              # so pass that signal on to the next waiter
              @Waiters.shift << true unless @Waiters.empty?
            end
          end

          self
        end

        def ns_signal
          @Waiters.shift << true unless @Waiters.empty?
          self
        end

        def ns_broadcast
          @Waiters.shift << true until @Waiters.empty?
          self
        end

        def ensure_ivar_visibility!
          # Rubinius instance variables are not volatile so we need to insert barrier
          Rubinius.memory_barrier
        end

        def self.attr_volatile *names
          names.each do |name|
            ivar = :"@volatile_#{name}"
            class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def #{name}
              Rubinius.memory_barrier
              #{ivar}
            end

            def #{name}=(value)
              #{ivar} = value
              Rubinius.memory_barrier
            end
            RUBY
          end
          names.map { |n| [n, :"#{n}="] }.flatten
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
concurrent-ruby-0.9.0 lib/concurrent/synchronization/rbx_object.rb
concurrent-ruby-0.9.0-java lib/concurrent/synchronization/rbx_object.rb
concurrent-ruby-0.9.0.pre3-java lib/concurrent/synchronization/rbx_object.rb
concurrent-ruby-0.9.0.pre3 lib/concurrent/synchronization/rbx_object.rb
concurrent-ruby-0.9.0.pre2 lib/concurrent/synchronization/rbx_object.rb
concurrent-ruby-0.9.0.pre2-java lib/concurrent/synchronization/rbx_object.rb