- fixes & improvements
This commit is contained in:
parent
22acd46e30
commit
98a87714fa
|
|
@ -214,15 +214,20 @@ std::string OsProcess::execCommand(const std::string& cmd)
|
||||||
#include "helper/HL_String.h"
|
#include "helper/HL_String.h"
|
||||||
|
|
||||||
std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdline,
|
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)
|
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;
|
std::string cp = cmdline;
|
||||||
FILE* pipe = popen(cp.c_str(), "r");
|
FILE* pipe = popen(cp.c_str(), "r");
|
||||||
if (!pipe)
|
if (!pipe)
|
||||||
throw std::runtime_error("Failed to run.");
|
{
|
||||||
|
if (finished_callback)
|
||||||
|
finished_callback("Failed to open pipe");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
std::string lines;
|
std::string lines;
|
||||||
|
|
@ -247,7 +252,7 @@ std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdl
|
||||||
int r;
|
int r;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
r = read(fno, buffer, sizeof(buffer)-1);
|
r = (int)read(fno, buffer, sizeof(buffer) - 1);
|
||||||
if (r > 0)
|
if (r > 0)
|
||||||
{
|
{
|
||||||
buffer[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);
|
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;
|
std::string::size_type p = 0;
|
||||||
while (p < lines.size())
|
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);
|
std::string::size_type d = lines.find("\n", p);
|
||||||
if (d != std::string::npos)
|
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;
|
p = d + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -277,6 +283,8 @@ std::shared_ptr<std::thread> OsProcess::asyncExecCommand(const std::string& cmdl
|
||||||
pclose(pipe);
|
pclose(pipe);
|
||||||
|
|
||||||
finish_flag = true;
|
finish_flag = true;
|
||||||
|
if (finished_callback)
|
||||||
|
finished_callback(std::string());
|
||||||
});
|
});
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,8 @@ class OsProcess
|
||||||
public:
|
public:
|
||||||
static std::string execCommand(const std::string& cmdline);
|
static std::string execCommand(const std::string& cmdline);
|
||||||
static std::shared_ptr<std::thread> asyncExecCommand(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);
|
bool& finish_flag);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#ifdef TARGET_OSX
|
#ifdef TARGET_OSX
|
||||||
# include <libkern/OSAtomic.h>
|
# 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; }))
|
if (!m_cv.wait_for(lock, std::chrono::milliseconds(milliseconds), [this]() { return m_count > 0; }))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_count--;
|
m_count--;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -224,7 +226,7 @@ size_t TimerQueue::cancel(uint64_t id) {
|
||||||
newItem.handler = std::move(item.handler);
|
newItem.handler = std::move(item.handler);
|
||||||
item.handler = nullptr;
|
item.handler = nullptr;
|
||||||
m_items.push(std::move(newItem));
|
m_items.push(std::move(newItem));
|
||||||
|
std::cout << "Cancelled timer. " << std::endl;
|
||||||
lk.unlock();
|
lk.unlock();
|
||||||
// Something changed, so wake up timer thread
|
// Something changed, so wake up timer thread
|
||||||
m_checkWork.notify();
|
m_checkWork.notify();
|
||||||
|
|
@ -263,8 +265,10 @@ void TimerQueue::run()
|
||||||
{
|
{
|
||||||
// Timers found, so wait until it expires (or something else
|
// Timers found, so wait until it expires (or something else
|
||||||
// changes)
|
// changes)
|
||||||
int milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end.second - std::chrono::steady_clock::now()).count();
|
int milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>
|
||||||
m_checkWork.waitFor(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 {
|
} else {
|
||||||
// No timers exist, so wait forever until something changes
|
// No timers exist, so wait forever until something changes
|
||||||
m_checkWork.wait();
|
m_checkWork.wait();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue