Module: Celluloid::InternalPool

Defined in:
lib/celluloid/internal_pool.rb

Overview

Maintain a thread pool FOR SPEED!!

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.max_idleObject

Returns the value of attribute max_idle.



13
14
15
# File 'lib/celluloid/internal_pool.rb', line 13

def max_idle
  @max_idle
end

Class Method Details

.createObject

Create a new thread with an associated queue of procs to run



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/celluloid/internal_pool.rb', line 43

def create
  queue = Queue.new
  thread = Thread.new do
    while proc = queue.pop
      begin
        proc.call
      rescue => ex
        Logger.crash("thread crashed", ex)
      end

      put thread
    end
  end

  thread[:queue] = queue
  thread
end

.get(&block) ⇒ Object

Get a thread from the pool, running the given block



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/celluloid/internal_pool.rb', line 16

def get(&block)
  @mutex.synchronize do
    begin
      if @pool.empty?
        thread = create
      else
        thread = @pool.shift
      end
    end until thread.status # handle crashed threads

    thread[:queue] << block
    thread
  end
end

.put(thread) ⇒ Object

Return a thread to the pool



32
33
34
35
36
37
38
39
40
# File 'lib/celluloid/internal_pool.rb', line 32

def put(thread)
  @mutex.synchronize do
    if @pool.size >= @max_idle
      thread[:queue] << nil
    else
      @pool << thread
    end
  end
end