Permalink
Browse files

More queue fiddling

  • Loading branch information...
Seth Morabito
Seth Morabito committed Nov 11, 2008
1 parent c981c2e commit 02c6410f019d1fcfdce343ba901ea80e9e45c89f
Showing with 7 additions and 2 deletions.
  1. +7 −2 server/lib/bdrb_thread_pool.rb
@@ -21,8 +21,9 @@ def initialize(master,size,logger)
@logger = logger
@size = size
@threads = []
- @work_queue = Queue.new
+ @work_queue = []
@mutex = Monitor.new
+ @cv = @mutex.new_cond
@size.times { add_thread }
end
@@ -50,7 +51,9 @@ def defer(method_name,args = nil)
@mutex.synchronize do
job_key = Thread.current[:job_key]
persistent_job_id = Thread.current[:persistent_job_id]
- @work_queue << WorkData.new(args,job_key,method_name,persistent_job_id)
+ @cv.wait_while { @work_queue.size >= size }
+ @work_queue.push(WorkData.new(args,job_key,method_name,persistent_job_id))
+ @cv.broadcast
end
end
@@ -63,7 +66,9 @@ def add_thread
begin
task = nil
@mutex.synchronize do
+ @cv.wait_while { @work_queue.size == 0 }
task = @work_queue.pop
+ @cv.broadcast
end
if task
Thread.current[:job_key] = task.job_key

0 comments on commit 02c6410

Please sign in to comment.