-
Notifications
You must be signed in to change notification settings - Fork 0
/
THCManager.cpp
181 lines (144 loc) · 4.72 KB
/
THCManager.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "THCManager.h"
#include "THCCommand.h"
#include <iostream>
using namespace ThreadCommand;
THCManager::THCManager() {
pthread_mutex_init(&mutex, NULL);
}
THCManager::~THCManager() {
}
void* THCManager::RunLoop(void* _argument) {
ThreadCategory* _category = (ThreadCategory*)_argument;
pthread_mutex_lock(&_category->mutex_size);
while(!_category->list_command.empty()) {
pthread_mutex_unlock(&_category->mutex_size);
void* _res = NULL;
if(_category->UpdatePrev)
_res = _category->UpdatePrev(_category->reference);
pthread_mutex_lock(&_category->mutex_size);
std::list<THCCommand*>::iterator _it = _category->list_command.begin();
while (_it != _category->list_command.end()) {
THCCommand* _command = *_it;
_it++;
pthread_mutex_unlock(&_category->mutex_size);
if(_command) {
if(_command->Command(_res)) {
pthread_mutex_lock(&_category->mutex);
pthread_mutex_lock(&_category->mutex_size);
_category->list_command.remove(_command);
pthread_mutex_unlock(&_category->mutex_size);
delete _command;
pthread_mutex_unlock(&_category->mutex);
}
}
pthread_mutex_lock(&_category->mutex_size);
}
pthread_mutex_unlock(&_category->mutex_size);
if(_res)
free(_res);
if(_category->UpdateNext)
_category->UpdateNext(_category->reference);
pthread_mutex_lock(&_category->mutex_size);
}
pthread_mutex_unlock(&_category->mutex_size);
_category->t_id = NULL;
return NULL;
}
THCManager* __thc_manager = NULL;
THCManager* THCManager::Share() {
if(__thc_manager == NULL)
__thc_manager = new THCManager();
return __thc_manager;
}
void THCManager::Update() {
pthread_mutex_lock(&mutex);
std::list<ThreadCategory>::iterator it = list_thread.begin();
while(it != list_thread.end()) {
pthread_mutex_unlock(&mutex);
if((*it).t_id == NULL) {
pthread_mutex_lock(&(*it).mutex_size);
if((*it).list_command.size() > 0) {
sched_param current_schedparam;
int current_policy;
pthread_getschedparam(pthread_self(), ¤t_policy, ¤t_schedparam);
pthread_attr_t attr;
sched_param schedparam;
schedparam.sched_priority = current_schedparam.sched_priority * (*it).priotiry;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
#ifndef ANDROID_NDK
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
#endif
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_attr_setschedparam(&attr, &schedparam);
pthread_create(&(*it).t_id, &attr, THCManager::RunLoop, &(*it));
pthread_attr_destroy(&attr);
}
pthread_mutex_unlock(&(*it).mutex_size);
}
pthread_mutex_lock(&(*it).mutex);
pthread_mutex_lock(&(*it).mutex_size);
std::list<THCCommand*>::iterator ic = (*it).list_command.begin();
while (ic != (*it).list_command.end()) {
pthread_mutex_unlock(&(*it).mutex_size);
(*ic)->Update();
pthread_mutex_lock(&(*it).mutex_size);
ic++;
}
pthread_mutex_unlock(&(*it).mutex_size);
pthread_mutex_unlock(&(*it).mutex);
pthread_mutex_lock(&mutex);
it++;
}
pthread_mutex_unlock(&mutex);
}
bool THCManager::AddCommand(std::string _category, THCCommand* _command) {
pthread_mutex_lock(&mutex);
std::list<ThreadCategory>::iterator it = list_thread.begin();
while(it != list_thread.end()) {
pthread_mutex_unlock(&mutex);
if((*it).category == _category) {
pthread_mutex_lock(&(*it).mutex_size);
(*it).list_command.push_back(_command);
pthread_mutex_unlock(&(*it).mutex_size);
return true;
}
pthread_mutex_lock(&mutex);
it++;
}
pthread_mutex_unlock(&mutex);
return false;
}
bool THCManager::SetCategory(std::string _category, float _priotiry,
void* reference,
void* (*UpdatePrev)(void*),
void* (*UpdateNext)(void*)) {
std::list<ThreadCategory>::iterator it = list_thread.begin();
while(it != list_thread.end()) {
if((*it).category == _category) {
(*it).priotiry = _priotiry;
(*it).reference = reference;
(*it).UpdatePrev = UpdatePrev;
(*it).UpdateNext = UpdateNext;
return false;
}
it++;
}
list_thread.push_back((ThreadCategory){_category, _priotiry, std::list<THCCommand*>(), reference, UpdatePrev, UpdateNext, NULL, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER});
return true;
}
THCManager::ThreadCategory THCManager::GetCategoryList(std::string _category) {
std::list<ThreadCategory>::iterator it = list_thread.begin();
while(it != list_thread.end()) {
if((*it).category == _category) {
return *it;
}
it++;
}
ThreadCategory retcategory;
memset(&retcategory, 0x0, sizeof(ThreadCategory));
retcategory.category = std::string();
retcategory.priotiry = 0.0;
retcategory.list_command = std::list<THCCommand*>();
return retcategory;
}