AsyncSourceAsyncWorkerPoolWorker

class Worker

A background worker thread.

Definitions

def initialize

Create a new worker.

Implementation

def initialize
	@work = ::Thread::Queue.new
	@thread = ::Thread.new(&method(:run))
end

def run

Execute work until the queue is closed.

Implementation

def run
	while work = @work.pop
		work.call
	end
end

def close

Close the worker thread.

Implementation

def close
	if thread = @thread
		@thread = nil
		thread.kill
	end
end

def call(work)

Call the work and notify the scheduler when it is done.

Implementation

def call(work)
	promise = Promise.new(work)
	
	@work.push(promise)
	
	begin
		return promise.wait
	ensure
		promise.cancel
	end
end

Discussion