- fixes & improvements

This commit is contained in:
dmytro.bogovych 2018-12-13 14:09:12 +02:00
parent 22acd46e30
commit 98a87714fa
3 changed files with 23 additions and 10 deletions

View File

@ -214,15 +214,20 @@ std::string OsProcess::execCommand(const std::string& cmd)
#include "helper/HL_String.h"
std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdline,
std::function<void(const std::string& line)> callback,
std::function<void(const std::string& line)> line_callback,
std::function<void(const std::string& reason)> finished_callback,
bool& finish_flag)
{
std::shared_ptr<std::thread> t = std::make_shared<std::thread>([cmdline, callback, &finish_flag]()
std::shared_ptr<std::thread> t = std::make_shared<std::thread>([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<std::thread> 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<std::thread> 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<std::thread> 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<std::thread> OsProcess::asyncExecCommand(const std::string& cmdl
pclose(pipe);
finish_flag = true;
if (finished_callback)
finished_callback(std::string());
});
return t;

View File

@ -11,7 +11,8 @@ class OsProcess
public:
static std::string execCommand(const std::string& cmdline);
static std::shared_ptr<std::thread> asyncExecCommand(const std::string& cmdline,
std::function<void(const std::string& line)> callback,
std::function<void(const std::string& line)> line_callback,
std::function<void(const std::string& reason)> finished_callback,
bool& finish_flag);
};

View File

@ -7,6 +7,7 @@
#include <assert.h>
#include <atomic>
#include <memory.h>
#include <iostream>
#ifdef TARGET_OSX
# include <libkern/OSAtomic.h>
@ -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<std::chrono::milliseconds>(end.second - std::chrono::steady_clock::now()).count();
m_checkWork.waitFor(milliseconds);
int milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>
(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();