Class: Green

Inherits:
Object
  • Object
show all
Includes:
GreenMethods
Defined in:
lib/green.rb,
lib/green-em.rb,
lib/green/hub.rb,
lib/green/zmq.rb,
lib/green/event.rb,
lib/green/group.rb,
lib/green/mysql2.rb,
lib/green/socket.rb,
lib/green/hub/nio4r.rb,
lib/green/semaphore.rb,
lib/green/activerecord.rb,
lib/green/connection_pool.rb,
lib/green/hub/em.rb

Direct Known Subclasses

ActiveRecord

Defined Under Namespace

Modules: EM, GreenMethods, Mysql2, ZMQ Classes: ActiveRecord, ConditionVariable, ConnectionPool, Event, GreenError, GreenKill, Group, Hub, Mutex, Pool, Proxy, Semaphore, Socket, SocketWaiter, TCPServer, TCPSocket, ThrowException

Constant Summary collapse

VERSION =
"0.1.1"
MAIN =
Fiber.current[:green] = Proxy.new
ERRORS =
Errno::constants.each_with_object({}) do |c, h|
  const = Errno.const_get(c)
  h[const::Errno] = const    
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GreenMethods

#[], #[]=, #kill, #locals, #schedule, #switch, #throw

Constructor Details

#initializeGreen

Returns a new instance of Green.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/green.rb', line 142

def initialize
  @callbacks = []
  @alive = true
  Green.list_hash[self] = self
  @f = Fiber.new do
    begin
      res = yield
    rescue GreenKill => e
    end
    @alive = false
    @callbacks.each { |c|
      c.call(res)
    }
    Green.list_hash.delete self
    Green.hub.switch
  end
  @f[:green] = self
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



141
142
143
# File 'lib/green.rb', line 141

def callbacks
  @callbacks
end

#fObject (readonly)

Returns the value of attribute f.



141
142
143
# File 'lib/green.rb', line 141

def f
  @f
end

Class Method Details

.currentObject



85
86
87
# File 'lib/green.rb', line 85

def current
  Fiber.current[:green] || Proxy.new
end

.hubObject



96
97
98
99
# File 'lib/green.rb', line 96

def hub
  # thread_locals[:hub] ||= make_hub
  @hub ||= make_hub
end

.initObject



123
124
125
# File 'lib/green.rb', line 123

def init
  hub
end

.listObject



119
120
121
# File 'lib/green.rb', line 119

def list
  list_hash.values
end

.list_hashObject



115
116
117
# File 'lib/green.rb', line 115

def list_hash
  @list_hash ||= {}
end

.mainObject



81
82
83
# File 'lib/green.rb', line 81

def main
  MAIN
end

.make_hubObject



89
90
91
92
93
94
# File 'lib/green.rb', line 89

def make_hub
  hub_name = ENV['GREEN_HUB'] || 'Nio4r'
  Hub.const_get(hub_name.to_sym).new
  # Hub::Nio4r.new
  # Hub::EM.new
end

.sleep(n) ⇒ Object



77
78
79
# File 'lib/green.rb', line 77

def sleep(n)
  Green.hub.sleep(n)
end

.spawn(&blk) ⇒ Object



101
102
103
# File 'lib/green.rb', line 101

def spawn(&blk)
  new(&blk).tap { |o| o.start }
end

.thread_localsObject



72
73
74
75
# File 'lib/green.rb', line 72

def thread_locals
  @thread_locals ||= {}
  @thread_locals[Thread.current] ||= {}
end

.timeout(n, &blk) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/green.rb', line 105

def timeout(n, &blk)
  g = current
  timer = hub.timer(n) do
    g.throw Timeout::Error.new
  end
  res = blk.call
  timer.cancel
  res
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/green.rb', line 161

def alive?
  @alive
end

#callback(cb = nil, &blk) ⇒ Object



169
170
171
172
173
174
175
176
# File 'lib/green.rb', line 169

def callback(cb=nil, &blk)
  cb ||= blk
  if alive?
    callbacks << cb
  else
    Green.hub.callback(cb)
  end
end

#joinObject



178
179
180
181
182
# File 'lib/green.rb', line 178

def join
  g = Green.current
  callback { |res| Green.hub.callback { g.switch(res) } }
  Green.hub.switch
end

#startObject



165
166
167
# File 'lib/green.rb', line 165

def start
  Green.hub.callback { self.switch }
end