Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
PeriodicSingleThreadExecutor.h
Go to the documentation of this file.
1#pragma once
2
3#include <Common/config.h>
4#include <Common/primitive_type.h>
5
6#include <atomic>
7#include <vector>
8#include <functional>
9#include <thread>
10#include <mutex>
11#include <condition_variable>
12
13namespace ph
14{
15
21{
22public:
23 using Work = std::function<void()>;
24
25public:
28 explicit PeriodicSingleThreadExecutor(uint64 periodMs);
29
33
39 void addWork(const Work& work);
40 void addWork(Work&& work);
42
47 void setPeriodMs(uint64 periodMs);
48
53 void pause();
54
59 void resume();
60
68 void requestTermination();
69
70private:
74 void asyncProcessWork();
75
79 bool isWorkerThread() const;
80
81
82#if PH_ENSURE_LOCKFREE_ALGORITHMS_ARE_LOCKLESS
83 static_assert(std::atomic<uint64>::is_always_lock_free);
84#endif
85
86 std::thread m_worker;
87 std::vector<Work> m_works;
88 std::mutex m_executorMutex;
89 std::condition_variable m_workerCv;
90 bool m_isWorking;
91 bool m_isTerminationRequested;
92 std::atomic<uint64> m_periodMs;
93};
94
95// In-header Implementations:
96
97inline void PeriodicSingleThreadExecutor::setPeriodMs(const uint64 periodMs)
98{
99 m_periodMs.store(periodMs, std::memory_order_relaxed);
100}
101
102inline bool PeriodicSingleThreadExecutor::isWorkerThread() const
103{
104 return std::this_thread::get_id() == m_worker.get_id();
105}
106
107}// end namespace ph
A single-thread executor that runs specified works periodically. The executor can be used concurrentl...
Definition PeriodicSingleThreadExecutor.h:21
void addWork(const Work &work)
Add a work to the executor. Work will not start until calling resume().
Definition PeriodicSingleThreadExecutor.cpp:38
void setPeriodMs(uint64 periodMs)
Sets execution period of the executor in milliseconds. The new period will take effect within finite ...
Definition PeriodicSingleThreadExecutor.h:97
PeriodicSingleThreadExecutor(uint64 periodMs)
Create an executor in the paused state.
Definition PeriodicSingleThreadExecutor.cpp:12
void requestTermination()
Stop the executor. Worker will stop processing any work as soon as possible. Any work that is already...
Definition PeriodicSingleThreadExecutor.cpp:134
std::function< void()> Work
Definition PeriodicSingleThreadExecutor.h:23
void pause()
Pause the execution. Should not be called on worker thread.
Definition PeriodicSingleThreadExecutor.cpp:110
void resume()
Resume the execution. Should not be called on worker thread.
Definition PeriodicSingleThreadExecutor.cpp:120
~PeriodicSingleThreadExecutor()
Terminate the execution. Wait for any ongoing work to finish.
Definition PeriodicSingleThreadExecutor.cpp:27
The root for all renderer implementations.
Definition EEngineProject.h:6