OSDN Git Service

Replaced mongrel with thin
[redminele/redminele.git] / ruby / lib / ruby / gems / 1.8 / gems / eventmachine-0.12.10-x86-mswin32-60 / lib / em / spawnable.rb
1 #--\r
2 #\r
3 # Author:: Francis Cianfrocca (gmail: blackhedd)\r
4 # Homepage::  http://rubyeventmachine.com\r
5 # Date:: 25 Aug 2007\r
6\r
7 # See EventMachine and EventMachine::Connection for documentation and\r
8 # usage examples.\r
9 #\r
10 #----------------------------------------------------------------------------\r
11 #\r
12 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.\r
13 # Gmail: blackhedd\r
14\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
19\r
20 # See the file COPYING for complete licensing information.\r
21 #\r
22 #---------------------------------------------------------------------------\r
23 #\r
24 #\r
25 \r
26 module EventMachine\r
27   # Support for Erlang-style processes.\r
28   #\r
29   class SpawnedProcess\r
30     # Send a message to the spawned process\r
31     def notify *x\r
32       me = self\r
33       EM.next_tick {\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
37         #\r
38         y = me.call(*x)\r
39         if y and y.respond_to?(:pull_out_yield_block)\r
40           a,b = y.pull_out_yield_block\r
41           set_receiver a\r
42           self.notify if b\r
43         end\r
44       }\r
45     end\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
49 \r
50     #--\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
55       class << self\r
56         define_method :call, $em______tmpglobal.dup\r
57       end\r
58     end\r
59 \r
60   end\r
61 \r
62   class YieldBlockFromSpawnedProcess # :nodoc:\r
63     def initialize block, notify\r
64       @block = [block,notify]\r
65     end\r
66     def pull_out_yield_block\r
67       @block\r
68     end\r
69   end\r
70 \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
75     s\r
76   end\r
77 \r
78   def self.yield &block # :nodoc:\r
79     return YieldBlockFromSpawnedProcess.new( block, false )\r
80   end\r
81 \r
82   def self.yield_and_notify &block # :nodoc:\r
83     return YieldBlockFromSpawnedProcess.new( block, true )\r
84   end\r
85 end\r