Sha256: e3e56174eccaacc31b77ad31ff253dc11834dde37f265b35d4e3c080c34df509

Contents?: true

Size: 910 Bytes

Versions: 8

Compression:

Stored size: 910 Bytes

Contents

# frozen_string_literal: true

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]
      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
      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

8 entries across 8 versions & 1 rubygems

Version Path
mutant-0.8.24 lib/mutant/actor/receiver.rb
mutant-0.8.23 lib/mutant/actor/receiver.rb
mutant-0.8.22 lib/mutant/actor/receiver.rb
mutant-0.8.21 lib/mutant/actor/receiver.rb
mutant-0.8.20 lib/mutant/actor/receiver.rb
mutant-0.8.19 lib/mutant/actor/receiver.rb
mutant-0.8.18 lib/mutant/actor/receiver.rb
mutant-0.8.17 lib/mutant/actor/receiver.rb