Sha256: 67897a0e8e3ed5ec22b61ae96ed3a49f25d549cc2fd4496b9fb78294710bc9a0

Contents?: true

Size: 880 Bytes

Versions: 8

Compression:

Stored size: 880 Bytes

Contents

module Mutest
  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 # Mutest

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mutest-0.0.9 lib/mutest/actor/receiver.rb
mutest-0.0.8 lib/mutest/actor/receiver.rb
mutest-0.0.7 lib/mutest/actor/receiver.rb
mutest-0.0.6 lib/mutest/actor/receiver.rb
mutest-0.0.5 lib/mutest/actor/receiver.rb
mutest-0.0.4 lib/mutest/actor/receiver.rb
mutest-0.0.3 lib/mutest/actor/receiver.rb
mutest-0.0.2 lib/mutest/actor/receiver.rb