Class: HyperThread::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/hyper_thread/pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max: 2) ⇒ Pool

Returns a new instance of Pool.



5
6
7
8
9
10
# File 'lib/hyper_thread/pool.rb', line 5

def initialize(max: 2)
  @threads = []
  @mutex   = Mutex.new  
  @queue   = Queue.new  
  @max     = max.to_i   
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



3
4
5
# File 'lib/hyper_thread/pool.rb', line 3

def queue
  @queue
end

Instance Method Details

#<<(thread) ⇒ Object



118
119
120
121
122
123
# File 'lib/hyper_thread/pool.rb', line 118

def <<(thread)
  nsync do
    return false if @threads.count == max
    @threads << thread
  end
end

#async(forever: false, count: 1, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hyper_thread/pool.rb', line 55

def async(forever: false, count: 1, &block)
  raise "Required block syntax" unless block_given?
  count.times do
    if @threads.count == max
      @queue << block
      next
    end
    self << Thread.new do
      if forever
        loop do
          nsync do 
            block.call
          end
        end
      else
        nsync do 
          block.call
        end
      end
    end
  end
  return @queue if @threads.count == max
  @threads
end

#maxObject



23
24
25
# File 'lib/hyper_thread/pool.rb', line 23

def max
  @max
end

#max=(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/hyper_thread/pool.rb', line 12

def max=(value)
  nsync do
    if value > @max
      dead = @threads.sample(value).map(&:exit)
      @threads -= dead
    end
    @max = value
    return true
  end
end

#nsync(&block) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/hyper_thread/pool.rb', line 47

def nsync(&block)
  @mutex.synchronize do
    break if @shutdown
    block.call
  end
  return if @shutdown
end

#qsync(count: 1, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hyper_thread/pool.rb', line 99

def qsync(count: 1, &block)
  nsync do 
    raise "Required block syntax" unless block_given?
    Thread.new do
      count.times do
        @queue << block
      end
    end
    true
  end
end

#reapObject



40
41
42
43
44
45
# File 'lib/hyper_thread/pool.rb', line 40

def reap
  dead = @threads.reject(&:alive?)
  dead.map(&:kill)
  @threads -= dead
  return true
end

#shutdownObject



34
35
36
37
38
# File 'lib/hyper_thread/pool.rb', line 34

def shutdown
  @shutdown = true
  dead = @threads.map(&:exit)
  @threads -= dead
end

#threadsObject



27
28
29
30
31
32
# File 'lib/hyper_thread/pool.rb', line 27

def threads
  return @threads unless block_given?
  @threads.each do |thread|
    yield thread
  end
end

#todo(forever: false, count: false) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hyper_thread/pool.rb', line 80

def todo(forever: false, count: false)
  raise "Required block syntax" unless block_given?
  if count
    count.times do
      yield @queue.pop
    end
  else
    if forever
      loop do  
        while @queue.size > 0
          yield @queue.pop
        end
      end
    else
      yield @queue.pop
    end
  end
end

#todo?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
# File 'lib/hyper_thread/pool.rb', line 111

def todo?
  nsync do 
    return true if @queue.size > 0
    false
  end
end