Sha256: cb09f1457ecd175a62325186a2e05139691eae05a97a1c597da9d62eedd13add

Contents?: true

Size: 953 Bytes

Versions: 9

Compression:

Stored size: 953 Bytes

Contents

module Mutant
  module Actor
    # Receiver side of an actor
    class Receiver
      include Adamantium::Flat, Concord.new(:condition_variable, :mutex, :messages)

      # Receives a message, blocking
      #
      # @return [Object]
      #
      # @api private
      #
      def call
        2.times do
          message = try_blocking_receive
          return message unless message.equal?(Undefined)
        end
        fail ProtocolError
      end

    private

      # Try a blocking receive
      #
      # @return [Undefined]
      #   if there is no message yet
      #
      # @return [Object]
      #   if there is a message
      #
      # @api private
      #
      def try_blocking_receive
        mutex.synchronize do
          if messages.empty?
            condition_variable.wait(mutex)
            Undefined
          else
            messages.shift
          end
        end
      end

    end # Receiver
  end # Actor
end # Mutant

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mutant-0.8.0 lib/mutant/actor/receiver.rb
mutant-0.7.9 lib/mutant/actor/receiver.rb
mutant-0.7.8 lib/mutant/actor/receiver.rb
mutant-0.7.7 lib/mutant/actor/receiver.rb
mutant-0.7.6 lib/mutant/actor/receiver.rb
mutant-0.7.5 lib/mutant/actor/receiver.rb
mutant-0.7.4 lib/mutant/actor/receiver.rb
mutant-0.7.3 lib/mutant/actor/receiver.rb
mutant-0.7.2 lib/mutant/actor/receiver.rb