diff --git a/src/engine/helper/HL_Process.cpp b/src/engine/helper/HL_Process.cpp index 675d6ade..1193c67e 100644 --- a/src/engine/helper/HL_Process.cpp +++ b/src/engine/helper/HL_Process.cpp @@ -214,15 +214,20 @@ std::string OsProcess::execCommand(const std::string& cmd) #include "helper/HL_String.h" std::shared_ptr OsProcess::asyncExecCommand(const std::string& cmdline, - std::function callback, + std::function line_callback, + std::function finished_callback, bool& finish_flag) { - std::shared_ptr t = std::make_shared([cmdline, callback, &finish_flag]() + std::shared_ptr t = std::make_shared([cmdline, line_callback, finished_callback, &finish_flag]() { std::string cp = cmdline; FILE* pipe = popen(cp.c_str(), "r"); if (!pipe) - throw std::runtime_error("Failed to run."); + { + if (finished_callback) + finished_callback("Failed to open pipe"); + return; + } char buffer[1024]; std::string lines; @@ -247,7 +252,7 @@ std::shared_ptr OsProcess::asyncExecCommand(const std::string& cmdl int r; do { - r = read(fno, buffer, sizeof(buffer)-1); + r = (int)read(fno, buffer, sizeof(buffer) - 1); if (r > 0) { buffer[r] = 0; @@ -256,7 +261,7 @@ std::shared_ptr OsProcess::asyncExecCommand(const std::string& cmdl } while (r == sizeof(buffer) - 1); - if (lines.find("\n") != std::string::npos && callback) + if (lines.find("\n") != std::string::npos && line_callback) { std::string::size_type p = 0; while (p < lines.size()) @@ -264,7 +269,8 @@ std::shared_ptr OsProcess::asyncExecCommand(const std::string& cmdl std::string::size_type d = lines.find("\n", p); if (d != std::string::npos) { - callback(StringHelper::trim(lines.substr(p, d-p))); + if (line_callback) + line_callback(StringHelper::trim(lines.substr(p, d-p))); p = d + 1; } } @@ -277,6 +283,8 @@ std::shared_ptr OsProcess::asyncExecCommand(const std::string& cmdl pclose(pipe); finish_flag = true; + if (finished_callback) + finished_callback(std::string()); }); return t; diff --git a/src/engine/helper/HL_Process.h b/src/engine/helper/HL_Process.h index dc14b5eb..fcd7307f 100644 --- a/src/engine/helper/HL_Process.h +++ b/src/engine/helper/HL_Process.h @@ -11,7 +11,8 @@ class OsProcess public: static std::string execCommand(const std::string& cmdline); static std::shared_ptr asyncExecCommand(const std::string& cmdline, - std::function callback, + std::function line_callback, + std::function finished_callback, bool& finish_flag); }; diff --git a/src/engine/helper/HL_Sync.cpp b/src/engine/helper/HL_Sync.cpp index 6514a526..522e0421 100644 --- a/src/engine/helper/HL_Sync.cpp +++ b/src/engine/helper/HL_Sync.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #ifdef TARGET_OSX # include @@ -160,6 +161,7 @@ bool Semaphore::waitFor(int milliseconds) { if (!m_cv.wait_for(lock, std::chrono::milliseconds(milliseconds), [this]() { return m_count > 0; })) return false; + m_count--; return true; } @@ -224,7 +226,7 @@ size_t TimerQueue::cancel(uint64_t id) { newItem.handler = std::move(item.handler); item.handler = nullptr; m_items.push(std::move(newItem)); - + std::cout << "Cancelled timer. " << std::endl; lk.unlock(); // Something changed, so wake up timer thread m_checkWork.notify(); @@ -263,8 +265,10 @@ void TimerQueue::run() { // Timers found, so wait until it expires (or something else // changes) - int milliseconds = std::chrono::duration_cast(end.second - std::chrono::steady_clock::now()).count(); - m_checkWork.waitFor(milliseconds); + int milliseconds = std::chrono::duration_cast + (end.second - std::chrono::steady_clock::now()).count(); + //std::cout << "Waiting m_checkWork for " << milliseconds * 1000 << "ms." << std::endl; + m_checkWork.waitFor(milliseconds * 1000); } else { // No timers exist, so wait forever until something changes m_checkWork.wait();