Class: Knj::Threadhandler

Inherits:
Object show all
Defined in:
lib/knj/threadhandler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Threadhandler

Returns a new instance of Threadhandler.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/knj/threadhandler.rb', line 4

def initialize(args = {})
  require "#{$knjpath}errors"
  
  @args = args
  @objects = []
  @args[:timeout] = 5 if !@args[:timeout]
  @args[:max] = 50 if !@args[:max]
  @inactive_blocks = []
  @activate_blocks = []
  @mutex = Mutex.new
  
  @thread_timeout = Thread.new do
    begin
      loop do
        sleep @args[:timeout]
        break if !@mutex
        check_inactive
      end
    rescue => e
      STDOUT.print Knj::Errors.error_str(e)
    end
  end
end

Instance Attribute Details

#activate_blocksObject (readonly)

Returns the value of attribute activate_blocks.



2
3
4
# File 'lib/knj/threadhandler.rb', line 2

def activate_blocks
  @activate_blocks
end

#argsObject (readonly)

Returns the value of attribute args.



2
3
4
# File 'lib/knj/threadhandler.rb', line 2

def args
  @args
end

#inactive_blocksObject (readonly)

Returns the value of attribute inactive_blocks.



2
3
4
# File 'lib/knj/threadhandler.rb', line 2

def inactive_blocks
  @inactive_blocks
end

#mutexObject (readonly)

Returns the value of attribute mutex.



2
3
4
# File 'lib/knj/threadhandler.rb', line 2

def mutex
  @mutex
end

#objectsObject (readonly)

Returns the value of attribute objects.



2
3
4
# File 'lib/knj/threadhandler.rb', line 2

def objects
  @objects
end

Instance Method Details

#check_inactiveObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/knj/threadhandler.rb', line 65

def check_inactive
  raise "Destroyed Knj::Threadhandler." if !@mutex
  @mutex.synchronize do
    cur_time = Time.now.to_i - @args[:timeout]
    @objects.each do |data|
      if data[:free] and !data[:inactive] and data[:free] < cur_time
        @inactive_blocks.each do |block|
          data[:inactive] = true
          block.call(:obj => data[:object])
        end
      end
    end
  end
end

#destroyObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/knj/threadhandler.rb', line 43

def destroy
  return false if !@mutex
  
  @thread_timeout.kill if @thread_timeout and @thread_timeout.alive?
  @thread_timeout = nil
  
  @mutex.synchronize do
    @objects.each do |data|
      @inactive_blocks.each do |block|
        data[:inactive] = true
        block.call(:obj => data[:object])
      end
    end
  end
  
  @args = nil
  @objects = nil
  @inactive_blocks = nil
  @activate_blocks = nil
  @mutex = nil
end

#free(obj) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/knj/threadhandler.rb', line 133

def free(obj)
  @mutex.synchronize do
    return false if !@mutex or !@objects #something is trying to free and object, but the handler is destroyed. Dont crash but return false.
    
    freedata = false
    @objects.each do |data|
      if data[:object] == obj
        freedata = data
        break
      end
    end
    
    raise "Could not find that object in list." if !freedata
    STDOUT.print "Freed one.\n" if @args[:debug]
    freedata[:free] = Time.now.to_i
  end
end

#get_and_lockObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/knj/threadhandler.rb', line 80

def get_and_lock
  raise "Destroyed Knj::Threadhandler." if !@mutex
  newobj = nil
  sleep_do = false
  
  begin
    @mutex.synchronize do
      retdata = false
      @objects.each do |data|
        if data[:free]
          retdata = data
          break
        end
      end
      
      if retdata
        #Test if object is still free - if not, try again - knj.
        return get_and_lock if !retdata[:free]
        retdata[:free] = false
        
        if retdata[:inactive]
          @activate_blocks.each do |block|
            block.call(:obj => retdata[:object])
          end
          
          retdata.delete(:inactive)
        end
        
        return retdata[:object]
      end
      
      if @objects.length >= @args[:max]
        #The maximum amount of objects has already been spawned... Sleep 0.1 sec and try to lock an object again...
        raise Knj::Errors::Retry
      else
        #No free objects, but we can spawn a new one and use that...
        newobj = @spawn_new_block.call
        @objects << {
          :free => false,
          :object => newobj
        }
        STDOUT.print "Spawned db and locked new.\n" if @args[:debug]
      end
    end
    
    return newobj
  rescue Knj::Errors::Retry
    STDOUT.print "All objects was taken - sleeping 0.1 sec and tries again.\n" #if @args[:debug]
    sleep 0.1
    retry
  end
end

#on_activate(&block) ⇒ Object



38
39
40
41
# File 'lib/knj/threadhandler.rb', line 38

def on_activate(&block)
  raise "Destroyed Knj::Threadhandler." if !@mutex
  @activate_blocks << block
end

#on_inactive(&block) ⇒ Object



33
34
35
36
# File 'lib/knj/threadhandler.rb', line 33

def on_inactive(&block)
  raise "Destroyed Knj::Threadhandler." if !@mutex
  @inactive_blocks << block
end

#on_spawn_new(&block) ⇒ Object



28
29
30
31
# File 'lib/knj/threadhandler.rb', line 28

def on_spawn_new(&block)
  raise "Destroyed Knj::Threadhandler." if !@mutex
  @spawn_new_block = block
end

#useObject

Executes the given block with an element and then frees it.



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/knj/threadhandler.rb', line 152

def use
  raise "No block was given." if !block_given?
  
  obj = self.get_and_lock
  
  begin
    yield(obj)
  ensure
    self.free(obj)
  end
end