Class: CWG::CWThreads

Inherits:
Object
  • Object
show all
Defined in:
/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CWThreads) initialize(context, processes)

Returns a new instance of CWThreads



9
10
11
12
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 9

def initialize context, processes
  @context = context
  @processes = processes
end

Instance Attribute Details

- (Object) threads (readonly)

Returns the value of attribute threads



7
8
9
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 7

def threads
  @threads
end

Instance Method Details

- (Boolean) any_thread_open?

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 102

def any_thread_open?
  @threads.each do |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

- (Object) close_threads



124
125
126
127
128
129
130
131
132
133
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 124

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 >= 10)
  end
end

- (Object) force_kill



115
116
117
118
119
120
121
122
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 115

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

- (Object) kill_monitor_keys_thread_maybe(thread)



98
99
100
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 98

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

- (Object) kill_open_threads



52
53
54
55
56
57
58
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 52

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

- (Object) kill_thread(thread)



48
49
50
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 48

def kill_thread thread
  thread[:thread].kill
end

- (Object) monitor_threads



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 25

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


91
92
93
94
95
96
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 91

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

- (Object) run



135
136
137
138
139
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 135

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

- (Object) start_threads



14
15
16
17
18
19
20
21
22
23
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 14

def start_threads
  @threads = @processes.collect do |th|
    {:thread =>
     Thread.new do
       @context.send th
     end,
     :name => th
    }
  end
end

- (Boolean) thread_false_or_nil?(th)

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 60

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

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

- (Object) wait_for_threads



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_threads.rb', line 70

def wait_for_threads
  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