Class: OpenC3::ThreadManager
- Defined in:
- lib/openc3/utilities/thread_manager.rb
Constant Summary collapse
- MONITOR_SLEEP_SECONDS =
0.25- @@instance =
Variable that holds the singleton instance
nil- @@instance_mutex =
Mutex used to ensure that only one instance of is created
Mutex.new
Class Method Summary collapse
-
.instance ⇒ Object
Get the singleton instance of ThreadManager.
Instance Method Summary collapse
-
#initialize ⇒ ThreadManager
constructor
A new instance of ThreadManager.
- #join ⇒ Object
- #monitor ⇒ Object
- #register(thread, stop_object: nil, shutdown_object: nil) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize ⇒ ThreadManager
Returns a new instance of ThreadManager.
35 36 37 38 |
# File 'lib/openc3/utilities/thread_manager.rb', line 35 def initialize @threads = [] @shutdown_started = false end |
Class Method Details
.instance ⇒ Object
Get the singleton instance of ThreadManager
25 26 27 28 29 30 31 32 33 |
# File 'lib/openc3/utilities/thread_manager.rb', line 25 def self.instance return @@instance if @@instance @@instance_mutex.synchronize do return @@instance if @@instance @@instance ||= self.new return @@instance end end |
Instance Method Details
#join ⇒ Object
72 73 74 75 76 |
# File 'lib/openc3/utilities/thread_manager.rb', line 72 def join @threads.each do |thread, _, _| thread.join end end |
#monitor ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/openc3/utilities/thread_manager.rb', line 44 def monitor while true @threads.each do |thread, _, _| if !thread.alive? return end end sleep(MONITOR_SLEEP_SECONDS) end end |
#register(thread, stop_object: nil, shutdown_object: nil) ⇒ Object
40 41 42 |
# File 'lib/openc3/utilities/thread_manager.rb', line 40 def register(thread, stop_object: nil, shutdown_object: nil) @threads << [thread, stop_object, shutdown_object] end |
#shutdown ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/openc3/utilities/thread_manager.rb', line 55 def shutdown @@instance_mutex.synchronize do return if @shutdown_started @shutdown_started = true end @threads.each do |thread, stop_object, shutdown_object| if thread.alive? if stop_object stop_object.stop end if shutdown_object shutdown_object.shutdown end end end end |