Class: Celluloid::InternalPool

### Home Classes Methods

### In Files

- lib/celluloid/internal_pool.rb

### Parent

Object

### Methods

- ::new
- #active?
- #assert_inactive
- #assert_running
- #busy_size
- #each
- #get
- #idle_size
- #kill
- #put
- #running?
- #shutdown
- #to_a

### Class/Module Index

Quicksearch

- Celluloid
- Celluloid::AbortError
- Celluloid::AbstractProxy
- Celluloid::Actor
- Celluloid::Actor::Sleeper
- Celluloid::ActorProxy
- Celluloid::ActorSystem
- Celluloid::AsyncCall
- Celluloid::AsyncProxy
- Celluloid::BlockCall
- Celluloid::BlockProxy
- Celluloid::BlockResponse
- Celluloid::CPUCounter
- Celluloid::Call
- Celluloid::CallChain
- Celluloid::Cell
- Celluloid::Cell::ExitHandler
- Celluloid::CellProxy
- Celluloid::ClassMethods
- Celluloid::Condition
- Celluloid::Condition::Waiter
- Celluloid::ConditionError
- Celluloid::DeadTaskError
- Celluloid::ErrorResponse
- Celluloid::EventedMailbox
- Celluloid::ExitEvent
- Celluloid::FSM
- Celluloid::FSM::ClassMethods
- Celluloid::FSM::State
- Celluloid::FSM::UnattachedError
- Celluloid::FiberStackError
- Celluloid::Future
- Celluloid::Future::Result
- Celluloid::FutureProxy
- Celluloid::Handler
- Celluloid::Handlers
- Celluloid::Incident
- Celluloid::IncidentLogger
- Celluloid::IncidentLogger::Severity
- Celluloid::IncidentReporter
- Celluloid::IncidentReporter::Formatter
- Celluloid::InstanceMethods
- Celluloid::InternalPool
- Celluloid::LinkingRequest
- Celluloid::LinkingResponse
- Celluloid::Links
- Celluloid::LogEvent
- Celluloid::Logger
- Celluloid::Logger::WithBacktrace
- Celluloid::Mailbox
- Celluloid::MailboxDead
- Celluloid::MailboxShutdown
- Celluloid::Method
- Celluloid::NamingRequest
- Celluloid::NotTaskError
- Celluloid::Notifications
- Celluloid::Notifications::Fanout
- Celluloid::Notifications::Subscriber
- Celluloid::PoolManager
- Celluloid::Probe
- Celluloid::Properties
- Celluloid::Receiver
- Celluloid::Receivers
- Celluloid::Registry
- Celluloid::Response
- Celluloid::ResumableError
- Celluloid::RingBuffer
- Celluloid::SignalConditionRequest
- Celluloid::Signals
- Celluloid::StackDump
- Celluloid::StackDump::ActorState
- Celluloid::StackDump::CellState
- Celluloid::StackDump::DisplayBacktrace
- Celluloid::StackDump::TaskState
- Celluloid::StackDump::ThreadState
- Celluloid::SuccessResponse
- Celluloid::SupervisionGroup
- Celluloid::SupervisionGroup::Member
- Celluloid::Supervisor
- Celluloid::SyncCall
- Celluloid::SyncProxy
- Celluloid::SystemEvent
- Celluloid::Task
- Celluloid::Task::TerminatedError
- Celluloid::Task::TimeoutError
- Celluloid::TaskFiber
- Celluloid::TaskSet
- Celluloid::TaskThread
- Celluloid::TerminationRequest
- Celluloid::Thread
- Celluloid::ThreadHandle
- Celluloid::UUID
- org
- org::jruby
- org::jruby::ext
- org::jruby::ext::fiber
- org::jruby::ext::fiber::ThreadFiber
- Bar
- Baz
- Blocker
- ExampleActorClass
- ExampleCrash
- ExampleMailbox
- ExampleProxy
- ExampleTask
- Fiber
- Foo
- MockActor
- Object
- Thread

No matching classes.

# Celluloid::InternalPool

Maintain a thread pool FOR SPEED!!

### Attributes

max_idle[RW]

### Public Class Methods

new() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 8
    def initialize
      @mutex = Mutex.new
      @idle_threads = []
      @all_threads  = []
      @busy_size = 0
      @idle_size = 0

      # TODO: should really adjust this based on usage
      @max_idle = 16
      @running = true
    end

### Public Instance Methods

active?() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 47
    def active?
      busy_size + idle_size > 0
    end

assert_inactive() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 32
    def assert_inactive
      if active?
        message = "Thread pool is still active"
        if defined?(JRUBY_VERSION)
          Celluloid.logger.warn message
        else
          raise Error, message
        end
      end
    end

assert_running() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 28
    def assert_running
      raise Error, "Thread pool is not running" unless running?
    end

busy_size() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 20
    def busy_size
      @busy_size
    end

each() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 51
    def each
      to_a.each {|thread| yield thread }
    end

get(&block) click to toggle source

Get a thread from the pool, running the given block

    # File lib/celluloid/internal_pool.rb, line 60
    def get(&block)
      @mutex.synchronize do
        assert_running

        begin
          if @idle_threads.empty?
            thread = create
          else
            thread = @idle_threads.pop
            @idle_size = @idle_threads.length
          end
        end until thread.status # handle crashed threads

        thread.busy = true
        @busy_size += 1
        thread[:celluloid_queue] << block
        thread
      end
    end

idle_size() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 24
    def idle_size
      @idle_size
    end

kill() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 110
    def kill
      @mutex.synchronize do
        finalize
        @running = false

        @all_threads.shift.kill until @all_threads.empty?
        @idle_threads.clear
        @busy_size = 0
        @idle_size = 0
      end
    end

put(thread) click to toggle source

Return a thread to the pool

    # File lib/celluloid/internal_pool.rb, line 81
    def put(thread)
      @mutex.synchronize do
        thread.busy = false
        if idle_size + 1 >= @max_idle
          thread[:celluloid_queue] << nil
          @busy_size -= 1
          @all_threads.delete(thread)
        else
          @idle_threads.push thread
          @busy_size -= 1
          @idle_size = @idle_threads.length
          clean_thread_locals(thread)
        end
      end
    end

running?() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 43
    def running?
      @running
    end

shutdown() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 97
    def shutdown
      @mutex.synchronize do
        finalize
        @all_threads.each do |thread|
          thread[:celluloid_queue] << nil
        end
        @all_threads.clear
        @idle_threads.clear
        @busy_size = 0
        @idle_size = 0
      end
    end

to_a() click to toggle source

    # File lib/celluloid/internal_pool.rb, line 55
    def to_a
      @mutex.synchronize { @all_threads.dup }
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.