summaryrefslogtreecommitdiff
path: root/Source/Core/Common/WorkQueueThread.h
blob: c3315ac26fa587c39a1a17c6bc39d088454d8998 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <functional>
#include <queue>
#include <string>
#include <thread>

#include "Common/Event.h"
#include "Common/Flag.h"
#include "Common/Thread.h"

// A thread that executes the given function for every item placed into its queue.

namespace Common
{
template <typename T>
class WorkQueueThread
{
public:
  WorkQueueThread(std::string name) : m_thread_name(name){};
  WorkQueueThread(std::function<void(T)> function, std::string name) : m_thread_name(name)
  {
    Reset(std::move(function));
  }
  ~WorkQueueThread() { Shutdown(); }
  void Reset(std::function<void(T)> function)
  {
    Shutdown();
    std::lock_guard lg(m_lock);
    m_cancelled = false;
    m_function = std::move(function);
    m_thread = std::thread(&WorkQueueThread::ThreadLoop, this);
  }

  template <typename... Args>
  void EmplaceItem(Args&&... args)
  {
    std::lock_guard lg(m_lock);
    if (m_shutdown)
    {
      return;
    }
    m_items.emplace(std::forward<Args>(args)...);
    m_idle = false;
    m_worker_cond_var.notify_one();
  }

  void Push(T&& item)
  {
    std::lock_guard lg(m_lock);
    if (m_cancelled)
    {
      return;
    }
    m_items.push(item);
    m_idle = false;
    m_worker_cond_var.notify_one();
  }

  void Push(const T& item)
  {
      std::lock_guard lg(m_lock);
      if (m_cancelled)
      {
        return;
      }
      m_items.push(item);
      m_idle = false;
      m_worker_cond_var.notify_one();
  }

  void Clear()
  {
    std::lock_guard lg(m_lock);
    m_items = std::queue<T>();
    m_worker_cond_var.notify_one();
  }

  void Cancel()
  {
    if (!m_thread.joinable())
    {
      return;
    }

    {
      std::unique_lock lg(m_lock);
      m_items = std::queue<T>();
      m_cancelled = true;
      m_shutdown = true;
      m_worker_cond_var.notify_one();
    }
    m_thread.join();
  }

  void Shutdown()
  {
    if (!m_thread.joinable())
    {
      return;
    }

    {
      std::unique_lock lg(m_lock);
      m_shutdown = true;
      m_worker_cond_var.notify_one();
    }
    m_thread.join();
  }

  // Doesn't return until the most recent function invocation has finished.
  void ClearAndFlush()
  {
    if (!m_thread.joinable())
    {
      return;
    }

    std::unique_lock lg(m_lock);
    m_items = std::queue<T>();
    m_wait_cond_var.wait(lg, [&] {
        return m_idle;
    });
  }

  // Doesn't return until the most recent function invocation has finished.
  void Flush()
  {
    if (!m_thread.joinable())
    {
      return;
    }

    std::unique_lock lg(m_lock);
    m_wait_cond_var.wait(lg, [&] {
        return m_idle;
    });
  }

private:
  void ThreadLoop()
  {
    Common::SetCurrentThreadName(m_thread_name.c_str());

    while (true)
    {
      std::unique_lock lg(m_lock);
      if (m_items.empty())
      {
        m_idle = true;
        m_wait_cond_var.notify_all();
        m_worker_cond_var.wait(lg, [&] {
            return m_shutdown || !m_items.empty();
        });
        if (m_shutdown)
        {
          break;
        }
        continue;
      }
      T item{std::move(m_items.front())};
      m_items.pop();
      lg.unlock();

      m_function(std::move(item));
    }
  }

  std::function<void(T)> m_function;
  std::string m_thread_name;
  std::thread m_thread;
  std::mutex m_lock;
  std::queue<T> m_items;
  std::condition_variable m_wait_cond_var;
  std::condition_variable m_worker_cond_var;
  bool m_idle = true;
  bool m_shutdown = false;
  bool m_cancelled = false;
};

}  // namespace Common