Aurore
A Switch-like interface for your app, on every screen
Loading...
Searching...
No Matches
thread.hpp
Go to the documentation of this file.
1/*
2 Copyright 2021 XITRIX
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
19#include <unistd.h>
20
21#include <atomic>
22#include <chrono>
23#include <condition_variable>
24#include <cstdint>
25#include <functional>
26#include <mutex>
27#include <set>
28#include <thread>
29#include <vector>
30
31namespace aurore
32{
33
35{
36#ifdef PS4
37 uint64_t startPoint;
38#else
39 // getCPUTimeUsec(): the test harness' simulated clock when it runs
40 int64_t startPoint;
41#endif
43 size_t index;
44 std::function<void()> func;
45};
46
47typedef std::vector<DelayOperation>::iterator DelayOperationIterator;
48
58extern void sync(std::function<void()> func);
59
66extern void async(std::function<void()> func);
67
68extern size_t delay(long milliseconds, std::function<void()> func);
69
70extern void cancelDelay(size_t iter);
71
72// The free functions above are the API for apps; Threading is what the
73// application runs them with (start, stop, performSyncTasks each frame)
75{
76 public:
84 static void sync(std::function<void()> func);
85
90 static void async(std::function<void()> func);
91
97 static void asyncDecode(std::function<void()> func);
98
99 static size_t delay(long milliseconds, std::function<void()> func);
100
101 static void cancelDelay(size_t iter);
102
103 static void start();
104
105 static void stop();
106
107 static void performSyncTasks();
108
115 static void setSyncTaskBudget(uint64_t microseconds);
116
122 static bool isIdle();
123
124 private:
125 inline static std::mutex m_sync_mutex;
126 inline static std::vector<std::function<void()>> m_sync_functions;
127 inline static uint64_t m_sync_budget = 0;
128
129 inline static std::mutex m_async_mutex;
130 inline static std::vector<std::function<void()>> m_async_tasks;
131 inline static std::condition_variable m_async_cv;
132 inline static int m_async_running = 0; // under m_async_mutex
133
134 inline static std::mutex m_delay_mutex;
135 inline static std::vector<DelayOperation> m_delay_tasks;
136 inline static std::set<size_t> m_delay_cancel_set;
137 inline static std::set<size_t> m_delay_pending; // queued, not run nor cancelled
138 inline static size_t m_delay_index = 0;
139
140 inline static std::atomic<bool> task_loop_active { true };
141 inline static bool task_loop_running = false; // started and not stopped
142
143 static void* task_loop(void* a);
144 static void std_task_loop();
145 static void* decode_loop(void* a);
146
147 static void start_task_loop();
148};
149
150} // namespace aurore
Definition thread.hpp:75
static void cancelDelay(size_t iter)
static size_t delay(long milliseconds, std::function< void()> func)
static void async(std::function< void()> func)
static void start()
static bool isIdle()
static void performSyncTasks()
static void asyncDecode(std::function< void()> func)
static void sync(std::function< void()> func)
static void stop()
static void setSyncTaskBudget(uint64_t microseconds)
Definition actions.hpp:25
void async(std::function< void()> func)
void cancelDelay(size_t iter)
void sync(std::function< void()> func)
size_t delay(long milliseconds, std::function< void()> func)
std::vector< DelayOperation >::iterator DelayOperationIterator
Definition thread.hpp:47
Definition thread.hpp:35
std::function< void()> func
Definition thread.hpp:44
long delayMilliseconds
Definition thread.hpp:42
int64_t startPoint
Definition thread.hpp:40
size_t index
Definition thread.hpp:43