Etterna
0.74.4
Loading...
Searching...
No Matches
src
RageUtil
Utils
RageUtil_WorkerThread.h
1
/* RageWorkerThread - a worker thread for operations that are allowed to time
2
* out. */
3
4
#ifndef RAGE_UTIL_WORKER_THREAD_H
5
#define RAGE_UTIL_WORKER_THREAD_H
6
7
#include "RageUtil/Misc/RageThreads.h"
8
#include "RageUtil/Misc/RageTimer.h"
9
10
class
RageWorkerThread
11
{
12
public
:
13
RageWorkerThread
(
const
std::string& sName);
14
virtual
~RageWorkerThread
();
15
16
/* Call SetTimeout(10) to start a timeout period of 10 seconds. This is not
17
* a per-request timeout; you have 10 seconds to do your work, at which
18
* point all requests time out until SetTimeout is called again. */
19
void
SetTimeout(
float
fSeconds);
20
bool
TimeoutEnabled()
const
{
return
m_Timeout > 0.F; }
21
22
/* Return true if the last operation has timed out and has not yet
23
* recovered. */
24
bool
IsTimedOut()
const
{
return
m_bTimedOut; }
25
26
/* Pause until the next heartbeat completes. Returns false if timed out.
27
* This triggers no actions, so no cleanup is run and IsTimedOut() is not
28
* affected. */
29
bool
WaitForOneHeartbeat();
30
31
protected
:
32
/* Call this in the derived class to start and stop the thread. */
33
void
StartThread();
34
void
StopThread();
35
36
/* Run the given request. Return true if the operation completed, false on
37
* timeout. Always call IsTimedOut() first; if true is returned, the thread
38
* is currently timed out and DoRequest() must not be called. */
39
bool
DoRequest(
int
iRequest);
40
41
/* Overload this in the derived class to handle requests. */
42
virtual
void
HandleRequest(
int
iRequest) = 0;
43
44
/* If DoRequest times out, this will be called in the thread after
45
* completion. Clean up. No new requests will be allowed until this
46
* completes. */
47
virtual
void
RequestTimedOut() {}
48
49
/* Enable a heartbeat. DoHeartbeat will be called every fSeconds while
50
* idle. DoHeartbeat may safely time out; if DoRequest tries to start a
51
* request in the main thread, it'll simply time out. */
52
void
SetHeartbeat(
float
fSeconds)
53
{
54
m_fHeartbeat = fSeconds;
55
m_NextHeartbeat = 0.F;
56
}
57
virtual
void
DoHeartbeat() {}
58
59
private
:
60
static
int
StartWorkerMain(
void
* pThis)
61
{
62
(
reinterpret_cast<
RageWorkerThread
*
>
(pThis))->WorkerMain();
63
return
0;
64
}
65
void
WorkerMain();
66
67
enum
68
{
69
REQ_SHUTDOWN = -1,
70
REQ_NONE = -2
71
};
72
RageThread
m_WorkerThread;
73
RageEvent
m_WorkerEvent;
74
std::string m_sName;
75
int
m_iRequest;
76
bool
m_bRequestFinished;
77
bool
m_bTimedOut;
78
float
m_Timeout;
79
80
float
m_fHeartbeat;
81
float
m_NextHeartbeat;
82
RageEvent
m_HeartbeatEvent;
83
};
84
85
#endif
RageEvent
Definition
RageThreads.h:309
RageThread
Thread, mutex, semaphore, and event classes.
Definition
RageThreads.h:155
RageWorkerThread
Definition
RageUtil_WorkerThread.h:11
Generated by
1.9.8