-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreadmonitor.cpp
More file actions
136 lines (115 loc) · 3.95 KB
/
threadmonitor.cpp
File metadata and controls
136 lines (115 loc) · 3.95 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
132
133
134
135
136
#include "threadmonitor.h"
namespace Spy{
ThreadMonitor::ThreadMonitor(QVector<QString> *listener_paths,
QMutex *listener_paths_mutex,
uint32_t* p_rate,
QMutex *p_rate_mutex,
QObject *parent) :
QThread(parent),
listener_paths(listener_paths),
listener_paths_mutex(listener_paths_mutex),
kill(false),
p_rate(p_rate),
p_rate_mutex(p_rate_mutex)
{
}
ThreadMonitor::~ThreadMonitor()
{
qDebug() << "Killing ThreadMonitor.";
kill_mutex.lock();
kill = true;
kill_mutex.unlock();
wait();
qDeleteAll(worker_pool);
worker_pool.empty();
}
void ThreadMonitor::run()
{
while(1){
kill_mutex.unlock();
if (kill){
kill_mutex.unlock();
break;
}
kill_mutex.unlock();
listener_paths_mutex->lock();
QVectorIterator<QString> listener_path_itr(*listener_paths);
// Spawn a new thread for each path that is not already there.
while (listener_path_itr.hasNext()){
QString listener_path = listener_path_itr.next();
if (get_worker_by_work(listener_path) == 0){
//propagate_notif("Added " + listener_path + " to listener.", N_ADDED_REPOSITORY);
SubversionWorker* worker = new SubversionWorker(listener_path, p_rate, p_rate_mutex);
worker_pool.append(worker);
worker->start();
connect(worker, SIGNAL(give_feedback(QString,SpyNotifications)), this, SLOT(propagate_notif(QString,SpyNotifications)));
}
}
// Stop workers that no longer is in list.
QVectorIterator<SubversionWorker*> worker_itr(worker_pool);
int32_t i = 0;
while (worker_itr.hasNext()){
SubversionWorker* worker_in_pool = worker_itr.next();
if (!is_in_pool(worker_in_pool->get_working_path())){
if (worker_pool.size() > i){ // Check if in range.
listener_paths_mutex->unlock();
worker_pool.remove(i);
delete worker_in_pool;
listener_paths_mutex->lock();
} else {
break;
}
}
i++;
}
listener_paths_mutex->unlock();
sleep(THREAD_MONITOR_ITERATION);
}
}
void ThreadMonitor::propagate_notif(QString message, SpyNotifications type)
{
emit send_notif(message, type);
}
SubversionWorker* ThreadMonitor::get_worker_by_work(QString listenerPath)
{
QVectorIterator<SubversionWorker*> it(worker_pool);
while (it.hasNext())
{
SubversionWorker* worker = it.next();
if (worker->get_working_path() == listenerPath)
{
return worker;
}
}
return 0;
}
inline bool ThreadMonitor::is_in_pool(QString listenerPath)
{
bool exists = false;
QVectorIterator<QString> path(*listener_paths);
while (path.hasNext()){
if (path.next() == listenerPath)
exists = true;
}
return exists;
}
QVariantMap ThreadMonitor::get_threads_state()
{
QVariantMap threads_state;
QVectorIterator<SubversionWorker*> workerIt(worker_pool);
while (workerIt.hasNext()){
QVariantMap thread_state;
SubversionWorker* workerInPool = workerIt.next();
thread_state["state"] = workerInPool->get_state();
thread_state["id"] = workerInPool->get_thread_id();
threads_state[workerInPool->get_working_path()] = thread_state;
}
return threads_state;
}
QVector<SubversionLog> *ThreadMonitor::get_worker_logs(QString path, QMutex **vector_mutex)
{
SubversionWorker *worker = get_worker_by_work(path);
QVector<SubversionLog> *svn_logs = worker->get_logs(vector_mutex);
return svn_logs;
}
} // namespace Spy