blob: d8ddd48ae5d2712f08b44d5346277e72133830e6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#pragma once
#include "base/assert.hpp"
#include "base/thread.hpp"
#include "base/thread_checker.hpp"
#include <condition_variable>
#include <mutex>
#include <queue>
#include <utility>
namespace base
{
// This class represents a simple worker thread with a queue of tasks.
//
// *NOTE* This class is not thread-safe.
class WorkerThread
{
public:
enum class Exit
{
ExecPending,
SkipPending
};
using Task = std::function<void()>;
WorkerThread();
~WorkerThread();
template <typename T>
void Push(T && t)
{
ASSERT(m_checker.CalledOnOriginalThread(), ());
CHECK(!m_shutdown, ());
std::lock_guard<std::mutex> lk(m_mu);
m_queue.emplace(std::forward<T>(t));
m_cv.notify_one();
}
void Shutdown(Exit e);
private:
void ProcessTasks();
threads::SimpleThread m_thread;
std::mutex m_mu;
std::condition_variable m_cv;
bool m_shutdown = false;
Exit m_exit = Exit::SkipPending;
std::queue<Task> m_queue;
ThreadChecker m_checker;
};
} // namespace base
|