common Rainbows.accept method
[rainbows.git] / lib / rainbows / actor_spawn.rb
blob7603734547ce758f4856d02826c5f47a3daa4d96
1 # -*- encoding: binary -*-
3 require 'actor'
4 module Rainbows
6   # Actor concurrency model for Rubinius.  We can't seem to get message
7   # passing working right, so we're throwing a Mutex into the mix for
8   # now.  Hopefully somebody can fix things for us.
9   #
10   # This is different from the Revactor one which is not prone to race
11   # conditions at all (since it uses Fibers).
12   module ActorSpawn
13     include Base
15     # runs inside each forked worker, this sits around and waits
16     # for connections and doesn't die until the parent dies (or is
17     # given a INT, QUIT, or TERM signal)
18     def worker_loop(worker)
19       init_worker_process(worker)
20       limit = worker_connections
21       nr = 0
23       # can't seem to get the message passing to work right at the moment :<
24       lock = Mutex.new
26       begin
27         ret = IO.select(LISTENERS, nil, nil, 1) and ret.first.each do |l|
28           lock.synchronize { nr >= limit } and break sleep(0.01)
29           c = Rainbows.accept(l) and Actor.spawn do
30             lock.synchronize { nr += 1 }
31             begin
32               process_client(c)
33             ensure
34               lock.synchronize { nr -= 1 }
35             end
36           end
37         end
38       rescue => e
39         Error.listen_loop(e)
40       end while G.tick || lock.synchronize { nr > 0 }
41     end
42   end
43 end