Class: OpenC3::ThreadManager

Inherits:
Object
  • Object
show all
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 Method Summary collapse

Constructor Details

#initializeThreadManager

Returns a new instance of ThreadManager.



40
41
42
43
# File 'lib/openc3/utilities/thread_manager.rb', line 40

def initialize
  @threads = []
  @shutdown_started = false
end

Class Method Details

.instanceObject

Get the singleton instance of ThreadManager



30
31
32
33
34
35
36
37
38
# File 'lib/openc3/utilities/thread_manager.rb', line 30

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

#joinObject



77
78
79
80
81
# File 'lib/openc3/utilities/thread_manager.rb', line 77

def join
  @threads.each do |thread, _, _|
    thread.join
  end
end

#monitorObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/openc3/utilities/thread_manager.rb', line 49

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



45
46
47
# File 'lib/openc3/utilities/thread_manager.rb', line 45

def register(thread, stop_object: nil, shutdown_object: nil)
  @threads << [thread, stop_object, shutdown_object]
end

#shutdownObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/openc3/utilities/thread_manager.rb', line 60

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