class Fiber
Memcopy = Win32API.new('kernel32','RtlMoveMemory','pll','l')
CREATING = 0
RUNNING = 1
TERMINATED = 2
def status
buf = "\0"*4
Memcopy.call(buf,self.object_id*2+16,4)
objPtr = buf.unpack("L").first
return 0 if objPtr == 0
Memcopy.call(buf,objPtr+480,4)
return buf.unpack("L").first
end
def alive?
return self.status != TERMINATE
end
end
class Fiber
Memcopy = Win32API.new('kernel32','RtlMoveMemory','pll','l')
CREATING = 0
RUNNING = 1
TERMINATED = 2
def status
buf = "\0"*4
Memcopy.call(buf,self.object_id*2+16,4)
objPtr = buf.unpack("L").first
return 0 if objPtr == 0
Memcopy.call(buf,objPtr+480,4)
return buf.unpack("L").first
end
def alive?
return self.status != TERMINATE
end
end
class Fiber
@@call_stack = []
attr_accessor :alive
attr_accessor :children
alias old_initialize initialize
def initialize(&block)
old_initialize(&block)
@alive = true
@children = []
end
def self.current
@@call_stack.last
end
alias old_resume resume
def resume
@@call_stack.push(self)
@children.each { |child|
child.resume
}
@children.delete_if { |child|
!child.alive
}
begin
old_resume
rescue FiberError => e
@alive = false
end
@@call_stack.pop
end
def self.fork(&block)
if Fiber.current
child = Fiber.new(&block)
Fiber.current.children << child
child.resume
end
end
end
class Fiber
@@call_stack = []
attr_accessor :alive
attr_accessor :children
alias old_initialize initialize
def initialize(&block)
old_initialize(&block)
@alive = true
@children = []
end
def self.current
@@call_stack.last
end
alias old_resume resume
def resume
@@call_stack.push(self)
@children.each { |child|
child.resume
}
@children.delete_if { |child|
!child.alive
}
begin
old_resume
rescue FiberError => e
@alive = false
end
@@call_stack.pop
end
def self.fork(&block)
if Fiber.current
child = Fiber.new(&block)
Fiber.current.children << child
child.resume
end
end
end