3 # Author:: Francis Cianfrocca (gmail: blackhedd)
\r
4 # Homepage:: http://rubyeventmachine.com
\r
7 # See EventMachine and EventMachine::Connection for documentation and
\r
10 #----------------------------------------------------------------------------
\r
12 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
\r
15 # This program is free software; you can redistribute it and/or modify
\r
16 # it under the terms of either: 1) the GNU General Public License
\r
17 # as published by the Free Software Foundation; either version 2 of the
\r
18 # License, or (at your option) any later version; or 2) Ruby's License.
\r
20 # See the file COPYING for complete licensing information.
\r
22 #---------------------------------------------------------------------------
\r
27 # Support for Erlang-style processes.
\r
29 class SpawnedProcess
\r
30 # Send a message to the spawned process
\r
34 # A notification executes in the context of this
\r
35 # SpawnedProcess object. That makes self and notify
\r
36 # work as one would expect.
\r
39 if y and y.respond_to?(:pull_out_yield_block)
\r
40 a,b = y.pull_out_yield_block
\r
46 alias_method :resume, :notify
\r
47 alias_method :run, :notify # for formulations like (EM.spawn {xxx}).run
\r
48 #attr_accessor :receiver
\r
51 # I know I'm missing something stupid, but the inside of class << s
\r
52 # can't see locally-bound values. It can see globals, though.
\r
53 def set_receiver blk
\r
54 $em______tmpglobal = blk
\r
56 define_method :call, $em______tmpglobal.dup
\r
62 class YieldBlockFromSpawnedProcess # :nodoc:
\r
63 def initialize block, notify
\r
64 @block = [block,notify]
\r
66 def pull_out_yield_block
\r
71 # Spawn an erlang-style process
\r
72 def self.spawn &block
\r
73 s = SpawnedProcess.new
\r
74 s.set_receiver block
\r
78 def self.yield &block # :nodoc:
\r
79 return YieldBlockFromSpawnedProcess.new( block, false )
\r
82 def self.yield_and_notify &block # :nodoc:
\r
83 return YieldBlockFromSpawnedProcess.new( block, true )
\r