-
Notifications
You must be signed in to change notification settings - Fork 289
/
Thread.h
153 lines (126 loc) · 4.34 KB
/
Thread.h
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
/*
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __MULTITHREAD_H_
#define __MULTITHREAD_H_
#include <pthread.h>
/**
* Function pointer typedef representing a thread's main entry point.
* A user-defined parameter is passed through such that the user can
* pass data or other value to their thread so that it can self-identify.
* @ingroup threads
*/
typedef void* (*ThreadEntryFunction)( void* user_param );
/**
* Thread class for launching an asynchronous operating-system dependent thread.
* To make your own thread, provide a function pointer of the thread's entry point,
* or inherit from this class and implement your own Run() function.
* @ingroup threads
*/
class Thread
{
public:
/**
* Default constructor
*/
Thread();
/**
* Destructor. Automatically stops the thread.
*/
virtual ~Thread();
/**
* User-implemented thread main() function.
*/
virtual void Run();
/**
* Start the thread. This will asynchronously call the Run() function.
* @result False if an error occurred and the thread could not be started.
*/
bool Start();
/**
* Start the thread, utilizing an entry function pointer provided by the user.
* @result False if an error occurred and the thread could not be started.
*/
bool Start( ThreadEntryFunction entry, void* user_param=NULL );
/**
* Signal for the thread to stop running.
* If wait is true, Stop() will block until the thread has exited.
*/
void Stop( bool wait=false );
/**
* Prime the system for realtime use. Mostly this is locking a large group of pages into memory.
*/
static void InitRealtime();
/**
* Get the maximum priority level available
*/
static int GetMaxPriority();
/**
* Get the minimum priority level avaiable
*/
static int GetMinPriority();
/**
* Get the priority level of the thread.
* @param thread The thread, or if NULL, the currently running thread.
*/
static int GetPriority( pthread_t* thread=NULL );
/**
* Set the priority level of the thread.
* @param thread The thread, or if NULL, the currently running thread.
*/
static int SetPriority( int priority, pthread_t* thread=NULL );
/**
* Get this thread's priority level
*/
int GetPriorityLevel();
/**
* Set this thread's priority level
*/
bool SetPriorityLevel( int priority );
/**
* Whatever thread you are calling from, yield the processor for the specified number of milliseconds.
* Accuracy may vary wildly the lower you go, and depending on the platform.
*/
static void Yield( unsigned int ms );
/**
* Get thread identififer
*/
inline pthread_t* GetThreadID() { return &mThreadID; }
/**
* Lock this thread to a CPU core.
*/
bool LockAffinity( unsigned int cpu );
/**
* Lock the specified thread's affinity to a CPU core.
* @param cpu The CPU core to lock the thread to.
* @param thread The thread, or if NULL, the currently running thread.
*/
static bool SetAffinity( unsigned int cpu, pthread_t* thread=NULL );
/**
* Look up which CPU core the thread is running on.
*/
static int GetCPU();
protected:
static void* DefaultEntry( void* param );
pthread_t mThreadID;
bool mThreadStarted;
};
#endif