Properly output TimeoutError
Fixed issue where receiver devices aren't detected (hopefully).
This commit is contained in:
@@ -38,6 +38,7 @@ task::task(const std::function<void()>& function,
|
||||
void task::run()
|
||||
{
|
||||
_status = Running;
|
||||
_status_cv.notify_all();
|
||||
_task_pkg();
|
||||
_status = Completed;
|
||||
}
|
||||
@@ -52,6 +53,18 @@ void task::wait()
|
||||
_task_pkg.get_future().wait();
|
||||
}
|
||||
|
||||
void task::waitStart()
|
||||
{
|
||||
std::mutex wait_start;
|
||||
std::unique_lock<std::mutex> lock(wait_start);
|
||||
_status_cv.wait(lock, [this](){ return _status != Waiting; });
|
||||
}
|
||||
|
||||
std::future_status task::waitFor(std::chrono::milliseconds ms)
|
||||
{
|
||||
return _task_pkg.get_future().wait_for(ms);
|
||||
}
|
||||
|
||||
void task::spawn(const std::function<void ()>& function,
|
||||
const std::function<void (std::exception &)>& exception_handler)
|
||||
{
|
||||
|
@@ -44,6 +44,8 @@ namespace logid
|
||||
|
||||
void run(); // Runs synchronously
|
||||
void wait();
|
||||
void waitStart();
|
||||
std::future_status waitFor(std::chrono::milliseconds ms);
|
||||
|
||||
/* This function spawns a new task into the least used worker queue
|
||||
* and forgets about it.
|
||||
@@ -53,13 +55,12 @@ namespace logid
|
||||
exception_handler={[](std::exception& e)
|
||||
{ExceptionHandler::Default(e);}});
|
||||
|
||||
static void autoQueue(std::shared_ptr<task>);
|
||||
|
||||
private:
|
||||
std::shared_ptr<std::function<void()>> _function;
|
||||
std::shared_ptr<std::function<void(std::exception&)>>
|
||||
_exception_handler;
|
||||
std::atomic<Status> _status;
|
||||
std::condition_variable _status_cv;
|
||||
std::packaged_task<void()> _task_pkg;
|
||||
};
|
||||
}
|
||||
|
@@ -15,6 +15,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#include <cassert>
|
||||
#include "workqueue.h"
|
||||
#include "log.h"
|
||||
|
||||
@@ -48,6 +49,7 @@ workqueue::~workqueue()
|
||||
|
||||
void workqueue::queue(std::shared_ptr<task> t)
|
||||
{
|
||||
assert(t != nullptr);
|
||||
_queue.push(t);
|
||||
_queue_cv.notify_all();
|
||||
}
|
||||
@@ -63,27 +65,6 @@ void workqueue::stop()
|
||||
std::unique_lock<std::mutex> lock(_run_lock);
|
||||
}
|
||||
|
||||
void workqueue::setThreadCount(std::size_t count)
|
||||
{
|
||||
while(_workers.size() < count)
|
||||
_workers.push_back(std::make_unique<worker_thread>(this,
|
||||
_workers.size()));
|
||||
|
||||
if(_workers.size() > count) {
|
||||
// Restart manager thread
|
||||
stop();
|
||||
while (_workers.size() > count)
|
||||
_workers.pop_back();
|
||||
_manager_thread = std::make_unique<thread>(
|
||||
[this](){ _run(); }
|
||||
, [this](std::exception& e){ _exception_handler(e); }
|
||||
);
|
||||
_manager_thread->run();
|
||||
}
|
||||
|
||||
_worker_count = count;
|
||||
}
|
||||
|
||||
std::size_t workqueue::threadCount() const
|
||||
{
|
||||
return _workers.size();
|
||||
@@ -98,7 +79,6 @@ void workqueue::_run()
|
||||
while(_continue_run) {
|
||||
_queue_cv.wait(lock, [this]{ return !(_queue.empty()); });
|
||||
while(!_queue.empty()) {
|
||||
|
||||
if(_workers.empty()) {
|
||||
if(_worker_count)
|
||||
logPrintf(DEBUG, "No workers were found, running task in"
|
||||
|
@@ -35,7 +35,6 @@ namespace logid
|
||||
|
||||
void stop();
|
||||
|
||||
void setThreadCount(std::size_t count);
|
||||
std::size_t threadCount() const;
|
||||
private:
|
||||
void _run();
|
||||
@@ -53,7 +52,7 @@ namespace logid
|
||||
std::size_t _worker_count;
|
||||
};
|
||||
|
||||
extern std::unique_ptr<workqueue> global_workqueue;
|
||||
extern std::shared_ptr<workqueue> global_workqueue;
|
||||
}
|
||||
|
||||
#endif //LOGID_WORKQUEUE_H
|
||||
|
Reference in New Issue
Block a user