Class: CW::Threads

Inherits:
Object
  • Object
show all
Defined in:
lib/cw/threads.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, processes) ⇒ Threads

Returns a new instance of Threads.



8
9
10
11
12
13
# File 'lib/cw/threads.rb', line 8

def initialize context, processes
  Thread.abort_on_exception = true
  @context = context
  @processes = processes
  @threads = []
end

Instance Attribute Details

#threadsObject (readonly)

Returns the value of attribute threads.



6
7
8
# File 'lib/cw/threads.rb', line 6

def threads
  @threads
end

Instance Method Details

#add(context, th) ⇒ Object



51
52
53
# File 'lib/cw/threads.rb', line 51

def add context, th
  @threads << start_thread(context, th)
end

#any_thread_open?Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cw/threads.rb', line 130

def any_thread_open?
  @threads.each do |thread|
#        print "status "
#            p thread
    if(thread[:name] == :monitor_keys_thread)
      kill_monitor_keys_thread_maybe thread
    else
      unless thread_false_or_nil?(thread)
        return true
      end
    end
  end
  nil
end

#close_threadsObject



155
156
157
158
159
160
161
162
163
164
# File 'lib/cw/threads.rb', line 155

def close_threads
  await_termination_count = 0
  loop do
    sleep 0.1
    break unless any_thread_open?()
#         print_threads_status
    await_termination_count += 1
    force_kill if(await_termination_count >= 30)
  end
end

#force_killObject



145
146
147
148
149
150
151
152
153
# File 'lib/cw/threads.rb', line 145

def force_kill
  puts "exiting"
#      puts "Forcing kill!\r"
  kill_open_threads
  # print_threads_status
  system("stty -raw echo")
  sleep 0.2
  exit(1)
end

#join(x) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/cw/threads.rb', line 43

def join x
  @threads.each do |th|
    if th[:name] == x
      th[:thread].join
    end
  end
end

#kill_monitor_keys_thread_maybe(thread) ⇒ Object



126
127
128
# File 'lib/cw/threads.rb', line 126

def kill_monitor_keys_thread_maybe thread
  kill_thread(thread) unless thread_false_or_nil?(thread)
end

#kill_open_threadsObject



79
80
81
82
83
84
85
# File 'lib/cw/threads.rb', line 79

def kill_open_threads
  @threads.each do |thread|
    unless thread_false_or_nil?(thread)
      kill_thread thread
    end
  end
end

#kill_thread(thread) ⇒ Object



30
31
32
# File 'lib/cw/threads.rb', line 30

def kill_thread thread
  thread[:thread].kill
end

#kill_thread_x(x) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/cw/threads.rb', line 34

def kill_thread_x x
  @threads.each_with_index do |th,idx|
    if th[:name] == x
      kill_thread th
      @threads.delete_at idx
    end
  end
end

#monitor_threadsObject



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

def monitor_threads
  exiting = false
  loop do
    sleep 0.5
#        @threads.each do |th|
#          if thread_false_or_nil?(th)
##todo            exiting = true
#            unless Cfg.config["exit"]
##              puts "** #{th[:name].to_s.gsub('_',' ')} quit unexpectedly!**"
#              if th[:thread].backtrace
#                STDERR.puts th[:thread].backtrace.join("\n    \\_ ")
#              end
#            end
#          end
    # print_threads_status
    exiting = true if(Cfg.config["exit"])
    break if exiting
  end
  @threads.each do |th|
    th[:thread].kill.join
  end
  #close_threads if exiting
end


118
119
120
121
122
123
124
# File 'lib/cw/threads.rb', line 118

def print_threads_status
  @threads.each do |thread|
    puts "\r"
    print "#{thread[:name]} = "
    #        p thread[:thread].status
  end
end

#runObject



166
167
168
169
170
171
# File 'lib/cw/threads.rb', line 166

def run
  start_threads
  monitor_threads
  system("stty -raw echo")
  puts "\r"
end

#start_thread(context, th) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/cw/threads.rb', line 21

def start_thread context, th
  {
    :thread => Thread.new do
      context.send th
    end,
    :name => th
  }
end

#start_threadsObject



15
16
17
18
19
# File 'lib/cw/threads.rb', line 15

def start_threads
  @processes.collect do |th|
    @threads << start_thread(@context, th)
  end
end

#thread_false_or_nil?(th) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
# File 'lib/cw/threads.rb', line 87

def thread_false_or_nil? th
  if(th[:thread].status == false)
    return true
  end

  if(th[:thread].status.nil?)
    return true
  end
end

#wait_for_threadsObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cw/threads.rb', line 97

def wait_for_threads
  Cfg.config.params["exit"] = false
  loop do
    alive = false
    sleep 0.1
    @threads.each { |th|
      if  thread_false_or_nil? th
      elsif th[:name] != :monitor_keys_thread
        alive = true
      end
    }
    break unless alive
  end
  threads.each {|th|
    if(th[:name] == :monitor_keys_thread)
      kill_thread th
    end
    sleep 0.1
  }
end