diff --git a/hcore/componentThread.cpp b/hcore/componentThread.cpp index e5fb5b3..5e56ee9 100644 --- a/hcore/componentThread.cpp +++ b/hcore/componentThread.cpp @@ -40,7 +40,6 @@ void ComponentThread::signalThread(std::thread::id id) void ComponentThread::main(ComponentThread& self) { - std::cout << __func__ << ": Entered." << std::endl; // We sleep on spawn until the main thread tells us to continue. { std::unique_lock lock(self.startupSync.mutex); diff --git a/main.cpp b/main.cpp index 0df0852..9807879 100644 --- a/main.cpp +++ b/main.cpp @@ -21,25 +21,25 @@ int main(int argc, char **argv, char **envp) { try { std::cout << __func__ << ": Entering main()" << std::endl; + boost::asio::io_service mrntLoop; + boost::asio::io_service::work work(mrntLoop); - // Validate thread IDs + // Validate thread IDs hk::ComponentThread::validateThreadIds(); - int ret = hk::initializeHarikoff(argc, argv, envp); - if (ret != 0) { - return ret; - } + // Post initializeHarikoff to mrntLoop + mrntLoop.post([&]() + { + int ret = hk::initializeHarikoff(argc, argv, envp); + if (ret != 0) + { + std::cerr << "Initialization failed with code: " + << ret << std::endl; + std::exit(ret); + } + }); - // Signal all threads - for (const auto& [id, componentThread] - : hk::ComponentThread::componentThreads) { - hk::ComponentThread::signalThread(id); - } - - // Infinite loop calling yield - while (true) { - std::this_thread::yield(); - } + mrntLoop.run(); } catch (const std::exception& e) { @@ -47,7 +47,8 @@ int main(int argc, char **argv, char **envp) << std::endl; return EXIT_FAILURE; } - catch (...) { + catch (...) + { std::cerr << __func__ << ": Unknown exception occurred" << std::endl; return EXIT_FAILURE; } @@ -70,6 +71,7 @@ static int initializeHarikoff(int argc, char **argv, char **envp) try { options.parseArguments(argc, argv, envp); + std::cout << options.stringifyOptions() << std::endl; } catch (const std::invalid_argument& e) { @@ -78,18 +80,22 @@ static int initializeHarikoff(int argc, char **argv, char **envp) return EXIT_FAILURE; } - if (options.printUsage) { + if (options.printUsage) + { std::cout << options.getUsage() << std::endl; return EXIT_SUCCESS; } - std::cout << options.stringifyOptions() << std::endl; DeviceManager::getInstance().collateAllDeviceSpecs(); DeviceManager::getInstance().parseAllDeviceSpecs(); std::cout << DeviceManager::stringifyDeviceSpecs() << std::endl; sense_api::SenseApiManager::getInstance().loadAllSenseApiLibsFromOptions(); /* Start the threads */ + for (const auto& [id, componentThread] + : hk::ComponentThread::componentThreads) { + hk::ComponentThread::signalThread(id); + } std::cout << __func__ << ": Exiting" << std::endl; return 0;