forked from vfonic/RgbMatrixLuas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.cpp
More file actions
93 lines (69 loc) · 2.03 KB
/
Thread.cpp
File metadata and controls
93 lines (69 loc) · 2.03 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
// Copyright (c) 2013 Matt Hill
// 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.");
}
//------------------------------------------------------------------------------
// 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);
}