Module: EMRPC::SinglethreadedClient

Included in:
Client::BlockingPid
Defined in:
lib/emrpc/blocking_api/singlethreaded_client.rb

Overview

Blocks the current thread around #send_from method and #on_return/#on_raise callbacks.

Example:

class BlockingPid
  include Pid
  include SinglethreadedClient
end

Constant Summary collapse

MBOX =
'SinglethreadedClient::MBOX'
FINISH_ACCEPTOR =
Object.new.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object

Initialization method-2



22
23
24
# File 'lib/emrpc/blocking_api/singlethreaded_client.rb', line 22

def self.extended(obj)
  obj.initialize_singlethreaded_client
end

Instance Method Details

#blocking_send(*args) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/emrpc/blocking_api/singlethreaded_client.rb', line 43

def blocking_send(*args)
  @outbox.push([:send, self, *args])
  mbox = @inbox
  if mbox.shift == :return
    return mbox.shift
  else
    raise mbox.shift
  end
end

#initialize(*args, &blk) ⇒ Object

Initialization method-1



16
17
18
19
# File 'lib/emrpc/blocking_api/singlethreaded_client.rb', line 16

def initialize(*args, &blk)
  super(*args, &blk)
  initialize_singlethreaded_client
end

#initialize_singlethreaded_clientObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/emrpc/blocking_api/singlethreaded_client.rb', line 26

def initialize_singlethreaded_client
  @outbox = Queue.new
  @inbox = Queue.new
  @acceptor = Thread.new(self, @outbox, @inbox) do |rcvr, obox, ibox|
    while 1
      args = obox.pop
      break if args == FINISH_ACCEPTOR
      rcvr.send(*args)
    end
  end
end

#on_raise(pid, exception) ⇒ Object



58
59
60
61
# File 'lib/emrpc/blocking_api/singlethreaded_client.rb', line 58

def on_raise(pid, exception)
  @inbox.push(:raise)
  @inbox.push(exception)
end

#on_return(pid, result) ⇒ Object



53
54
55
56
# File 'lib/emrpc/blocking_api/singlethreaded_client.rb', line 53

def on_return(pid, result)
  @inbox.push(:return)
  @inbox.push(result)
end

#pid_class_nameObject



63
64
65
# File 'lib/emrpc/blocking_api/singlethreaded_client.rb', line 63

def pid_class_name
  "SinglethreadedClient"
end

#stopObject



39
40
41
# File 'lib/emrpc/blocking_api/singlethreaded_client.rb', line 39

def stop
  @outbox.push(FINISH_ACCEPTOR)
end