@@ -602,7 +602,5 @@ set(MBGL_CORE_FILES
|
||||
src/mbgl/util/utf.hpp
|
||||
src/mbgl/util/version.cpp
|
||||
src/mbgl/util/version.hpp
|
||||
src/mbgl/util/work_queue.cpp
|
||||
src/mbgl/util/work_queue.hpp
|
||||
src/mbgl/util/work_request.cpp
|
||||
)
|
||||
|
||||
@@ -138,5 +138,4 @@ set(MBGL_TEST_FILES
|
||||
test/util/timer.test.cpp
|
||||
test/util/token.test.cpp
|
||||
test/util/url.test.cpp
|
||||
test/util/work_queue.test.cpp
|
||||
)
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
#include <mbgl/util/optional.hpp>
|
||||
#include <mbgl/util/font_stack.hpp>
|
||||
#include <mbgl/util/work_queue.hpp>
|
||||
#include <mbgl/util/image.hpp>
|
||||
#include <mbgl/gl/texture.hpp>
|
||||
#include <mbgl/gl/object.hpp>
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
#include <mbgl/util/work_queue.hpp>
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
WorkQueue::WorkQueue() : runLoop(RunLoop::Get()) {
|
||||
}
|
||||
|
||||
WorkQueue::~WorkQueue() {
|
||||
assert(runLoop == RunLoop::Get());
|
||||
|
||||
// Cancel all pending AsyncRequests.
|
||||
while (!queue.empty()) {
|
||||
queue.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void WorkQueue::push(std::function<void()>&& fn) {
|
||||
std::lock_guard<std::mutex> 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<void()>& fn) {
|
||||
assert(runLoop == RunLoop::Get());
|
||||
|
||||
fn();
|
||||
|
||||
std::lock_guard<std::mutex> lock(queueMutex);
|
||||
queue.pop();
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace mbgl
|
||||
@@ -1,40 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
#include <mbgl/util/async_request.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
class RunLoop;
|
||||
|
||||
// The WorkQueue will manage a queue of closures
|
||||
// and it will make sure they get executed on the
|
||||
// thread that created the WorkQueue. All pending
|
||||
// works are canceled when the queue gets destructed.
|
||||
class WorkQueue : private util::noncopyable {
|
||||
public:
|
||||
WorkQueue();
|
||||
~WorkQueue();
|
||||
|
||||
// Push a closure to the queue. It is advised to
|
||||
// only push tasks calling functions on the object
|
||||
// that owns the queue to avoid use after free errors.
|
||||
void push(std::function<void()>&&);
|
||||
|
||||
private:
|
||||
void pop(const std::function<void()>&);
|
||||
|
||||
std::queue<std::unique_ptr<AsyncRequest>> queue;
|
||||
std::mutex queueMutex;
|
||||
|
||||
RunLoop* runLoop;
|
||||
};
|
||||
|
||||
} // namespace util
|
||||
} // namespace mbgl
|
||||
@@ -1,59 +0,0 @@
|
||||
#include <mbgl/test/util.hpp>
|
||||
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
#include <mbgl/util/work_queue.hpp>
|
||||
|
||||
#include <thread>
|
||||
|
||||
using namespace mbgl::util;
|
||||
|
||||
class TestThread {
|
||||
public:
|
||||
TestThread(WorkQueue* queue_) : queue(queue_) {}
|
||||
|
||||
void send(std::function<void()>&& fn) {
|
||||
queue->push(std::move(fn));
|
||||
}
|
||||
|
||||
private:
|
||||
WorkQueue* queue;
|
||||
};
|
||||
|
||||
TEST(WorkQueue, push) {
|
||||
RunLoop loop;
|
||||
|
||||
WorkQueue queue;
|
||||
Thread<TestThread> thread({"Test"}, &queue);
|
||||
|
||||
uint8_t count = 0;
|
||||
|
||||
auto endTest = [&]() {
|
||||
if (++count == 4) {
|
||||
loop.stop();
|
||||
}
|
||||
};
|
||||
|
||||
thread.invoke(&TestThread::send, endTest);
|
||||
thread.invoke(&TestThread::send, endTest);
|
||||
thread.invoke(&TestThread::send, endTest);
|
||||
thread.invoke(&TestThread::send, endTest);
|
||||
|
||||
loop.run();
|
||||
}
|
||||
|
||||
TEST(WorkQueue, cancel) {
|
||||
RunLoop loop;
|
||||
|
||||
WorkQueue queue;
|
||||
|
||||
auto work = [&]() {
|
||||
FAIL() << "Should never be called";
|
||||
};
|
||||
|
||||
queue.push(work);
|
||||
queue.push(work);
|
||||
queue.push(work);
|
||||
queue.push(work);
|
||||
queue.push(work);
|
||||
}
|
||||
Reference in New Issue
Block a user