File: thread_store.rb

package info (click to toggle)
ruby-xmpp4r 0.5.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,384 kB
  • sloc: ruby: 17,382; xml: 74; sh: 12; makefile: 4
file content (65 lines) | stat: -rw-r--r-- 1,727 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# =XMPP4R - XMPP Library for Ruby
#
# This file's copyright (c) 2009 by Pablo Lorenzzoni <pablo@propus.com.br>
# License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
# Website::http://xmpp4r.github.io
#
# Class ThreadStore
class ThreadStore
  attr_reader :cycles, :max

  # Create a new ThreadStore.
  #
  # max:: max number of threads to store (when exhausted, older threads will
  # just be killed and discarded). Default is 100. If 0 or negative no
  # threads will be discarded until #keep is called.
  def initialize(max = 100)
    @store  = []
    @max    = max > 0 ? max : 0
    @cycles = 0
    @killer_thread = Thread.new do
      loop do
        sleep 2 while @store.empty?
        sleep 1
        @store.each_with_index do |thread, i|
          th = @store.delete_at(i) if thread.nil? or ! thread.alive?
          th = nil
        end
        @cycles += 1
      end
    end
  end

  def inspect # :nodoc:
    sprintf("#<%s:0x%x @max=%d, @size=%d @cycles=%d>", self.class.name, __id__, @max, size, @cycles)
  end

  # Add a new thread to the ThreadStore
  def add(thread)
    if thread.instance_of?(Thread) and thread.respond_to?(:alive?)
      @store << thread
      keep(@max) if @max > 0
    end
  end

  # Keep only the number passed of threads
  #
  # n:: number of threads to keep (default to @max if @max > 0)
  def keep(n = nil)
    if n.nil?
      raise ArgumentError, "You need to pass the number of threads to keep!" if @max == 0
      n = @max
    end
    @store.shift.kill while @store.length > n
  end

  # Kill all threads
  def kill!
    @store.shift.kill while @store.length > 0
  end

  # How long is our ThreadStore
  def size; @store.length; end

end # of class ThreadStore