class Cassandra::Executors::ThreadPool

Public Class Methods

new(size) click to toggle source
   # File lib/cassandra/executors.rb
40 def initialize(size)
41   mon_initialize
42 
43   @cond    = new_cond
44   @tasks   = ::Array.new
45   @waiting = 0
46   @term    = false
47   @pool    = ::Array.new(size, &method(:spawn_thread))
48 end

Public Instance Methods

execute(*args, &block) click to toggle source
   # File lib/cassandra/executors.rb
50 def execute(*args, &block)
51   synchronize do
52     return if @term
53 
54     @tasks << Task.new(*args, &block)
55     @cond.signal if @waiting > 0
56   end
57 
58   nil
59 end
shutdown() click to toggle source
   # File lib/cassandra/executors.rb
61 def shutdown
62   execute do
63     synchronize do
64       @term = true
65       @cond.broadcast if @waiting > 0
66     end
67   end
68 
69   nil
70 end

Private Instance Methods

run() click to toggle source
   # File lib/cassandra/executors.rb
78 def run
79   Thread.current.abort_on_exception = true
80 
81   loop do
82     tasks = nil
83 
84     synchronize do
85       @waiting += 1
86       @cond.wait while !@term && @tasks.empty?
87       @waiting -= 1
88 
89       return if @tasks.empty?
90 
91       tasks  = @tasks
92       @tasks = ::Array.new
93     end
94 
95     tasks.each(&:run).clear
96   end
97 end
spawn_thread(i) click to toggle source
   # File lib/cassandra/executors.rb
74 def spawn_thread(i)
75   Thread.new(&method(:run))
76 end