diff --git a/src/mbgl/util/work_queue.cpp b/src/mbgl/util/work_queue.cpp new file mode 100644 index 000000000..7e1406bba --- /dev/null +++ b/src/mbgl/util/work_queue.cpp @@ -0,0 +1,37 @@ +#include + +#include + +namespace mbgl { +namespace util { + +WorkQueue::WorkQueue() : runLoop(RunLoop::Get()) { +} + +WorkQueue::~WorkQueue() { + assert(runLoop == RunLoop::Get()); + + // Cancel all pending WorkRequests. + while (!queue.empty()) { + queue.pop(); + } +} + +void WorkQueue::push(std::function&& fn) { + std::lock_guard lock(queueMutex); + + auto workRequest = runLoop->invokeCancellable(std::bind(&WorkQueue::pop, this, std::move(fn))); + queue.push(std::move(workRequest)); +} + +void WorkQueue::pop(const std::function& fn) { + assert(runLoop == RunLoop::Get()); + + fn(); + + std::lock_guard lock(queueMutex); + queue.pop(); +} + +} +} diff --git a/src/mbgl/util/work_queue.hpp b/src/mbgl/util/work_queue.hpp new file mode 100644 index 000000000..dcec5668b --- /dev/null +++ b/src/mbgl/util/work_queue.hpp @@ -0,0 +1,36 @@ +#ifndef MBGL_UTIL_WORK_QUEUE +#define MBGL_UTIL_WORK_QUEUE + +#include +#include + +#include +#include +#include +#include + +namespace mbgl { +namespace util { + +class RunLoop; + +class WorkQueue : private util::noncopyable { +public: + WorkQueue(); + virtual ~WorkQueue(); + + void push(std::function&&); + +private: + void pop(const std::function&); + + std::queue> queue; + std::mutex queueMutex; + + RunLoop* runLoop; +}; + +} +} + +#endif