Diag-Client-Lib
diagnostic_manager.cpp
Go to the documentation of this file.
1 /* Diagnostic Client library
2  * Copyright (C) 2024 Avijit Dey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
9 
10 namespace diag {
11 namespace client {
12 namespace common {
13 
14 DiagnosticManager::DiagnosticManager() noexcept : exit_requested_{false}, cond_var_{}, mutex_{} {}
15 
17  {
18  std::lock_guard<std::mutex> const lock{mutex_};
19  exit_requested_ = true;
20  }
21  cond_var_.notify_all();
22 }
23 
24 void DiagnosticManager::Main() noexcept {
25  // Initialize the module
26  Initialize();
27  // Run the module
28  Run();
29  // Entering infinite loop
30  while (!exit_requested_) {
31  std::unique_lock<std::mutex> lck(mutex_);
32  cond_var_.wait(lck, [this]() { return exit_requested_; });
33  // Thread exited
34  }
35  // Shutdown module
36  Shutdown();
37 }
38 
40  Result<void> result{};
41  {
42  std::lock_guard<std::mutex> lock{mutex_};
43  exit_requested_ = true;
44  }
45  cond_var_.notify_all();
46  return result;
47 }
48 } // namespace common
49 } // namespace client
50 } // namespace diag
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29
virtual void Initialize() noexcept=0
Function to initialize the DiagnosticManager.
virtual void Main() noexcept
Function to manage the whole lifecycle of DiagnosticManager.
virtual void Run() noexcept=0
Function to run DiagnosticManager.
virtual void Shutdown() noexcept=0
Function to shutdown the DiagnosticManager.
DiagnosticManager() noexcept
Constructs an instance of DiagnosticManager.
bool exit_requested_
Flag to terminate the main thread.
std::condition_variable cond_var_
Conditional variable to block the thread.
std::mutex mutex_
For locking critical section of code.
virtual ~DiagnosticManager() noexcept
Destructs an instance of DiagnosticManager.
virtual Result< void > SignalShutdown() noexcept
Function to initiate shutdown of DiagnosticManager.