-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.cpp
More file actions
133 lines (100 loc) · 2.93 KB
/
Copy pathThread.cpp
File metadata and controls
133 lines (100 loc) · 2.93 KB
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
// Copyright (c) 2013 Hyperceptive, LLC
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
#include "Thread.h"
#include <assert.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
Thread::Thread() : _thread(0), _status(Created)
{
_threadDown = false; //The thread has not crashed.
_stopThread = false; //The thread has not been signaled to stop.
}
Thread::~Thread()
{
if (_status != Running) return;
_status = Finished;
int returnVal = pthread_join(_thread, NULL);
if (returnVal != 0)
{
fprintf(stderr, "Error: %d %s\n", returnVal, strerror(returnVal));
}
}
//------------------------------------------------------------------------------
// Create a thread and attach code to it.
void Thread::start(int priority)
{
assert(_status != Running);
int status = pthread_create(&_thread, NULL, &executeThread, this);
// Allow realtime threads with high priority.
if (priority > 0)
{
struct sched_param p;
p.sched_priority = priority;
pthread_setschedparam(_thread, SCHED_FIFO, &p);
}
if (status == 0)
_status = Running;
else
_status = Invalid;
return;
}
//------------------------------------------------------------------------------
// Static method to call the overriden run() method for 'this' object.
void *Thread::executeThread(void *i_thread)
{
reinterpret_cast<Thread*>(i_thread)->run();
reinterpret_cast<Thread*>(i_thread)->_status = Finished;
return NULL;
}
//------------------------------------------------------------------------------
// Override in the derived class.
void Thread::run()
{
fprintf(stderr, "Error: Thread:run() should be overriden in derived class.");
}
//------------------------------------------------------------------------------
// Tell the thread to suspend.
void Thread::suspend()
{
pthread_mutex_lock(&_suspendMutex);
_status = Suspended;
pthread_mutex_unlock(&_suspendMutex);
}
//------------------------------------------------------------------------------
// Check if this thread should suspend.
void Thread::checkSuspend()
{
if(_status == Suspended)
{
pthread_mutex_lock(&_suspendMutex);
while (_status == Suspended)
{
pthread_cond_wait(&_resumeCondition, &_suspendMutex);
}
pthread_mutex_unlock(&_suspendMutex);
}
}
//------------------------------------------------------------------------------
// Resume a suspended the thread.
void Thread::resume()
{
pthread_mutex_lock(&_suspendMutex);
_status = Running;
pthread_cond_signal(&_resumeCondition);
pthread_mutex_unlock(&_suspendMutex);
}
//------------------------------------------------------------------------------
// Signal a thread to stop.
void Thread::stop()
{
_stopThread = true;
}
//------------------------------------------------------------------------------
// Terminate the thread.
void Thread::terminate(unsigned long i_return)
{
_status = Finished;
pthread_exit(&i_return);
}