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
27
# File 'lib/knj/threadhandler.rb', line 4

def initialize(args = {})
  require "#{$knjpath}errors"
  require "tsafe" if !Kernel.const_defined?(:Tsafe)
  
  @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



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

def check_inactive
  raise "Destroyed Knj::Threadhandler." if !@mutex
  cur_time = Time.now.to_i - @args[:timeout]
  
  @mutex.synchronize do
    @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



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

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/knj/threadhandler.rb', line 137

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



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
132
133
134
135
# File 'lib/knj/threadhandler.rb', line 82

def get_and_lock
  raise "Destroyed Knj::Threadhandler." if !@mutex
  newobj = nil
  
  begin
    retdata = nil
    
    @mutex.synchronize do
      @objects.each do |data|
        if data[:free]
          retdata = data
          break
        end
      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
      @mutex.synchronize do
        @objects << Tsafe::MonHash.new.merge(
          :free => false,
          :object => newobj
        )
      end
      STDOUT.print "Spawned db and locked new.\n" if @args[:debug]
    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



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

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

#on_inactive(&block) ⇒ Object



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

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

#on_spawn_new(&block) ⇒ Object



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

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.



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/knj/threadhandler.rb', line 156

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