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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "thread.hpp"
#include "libvideostitch/utils/semaphore.hpp"
#ifdef _MSC_VER
#include <ws2tcpip.h> //must be included before Windows.h
#include <iphlpapi.h>
#include <windows.h>
#else
#include <sys/param.h>
#if (!defined __ANDROID__)
#include <sys/sysctl.h>
#include <ifaddrs.h>
#endif /* (!defined __ANDROID__) */
#include <sys/socket.h>
#include <unistd.h>
#endif
#include <cassert>
#include <cstring>
#include <fstream>
#include <iterator>
#include <memory>
#include <sstream>
namespace VideoStitch {
Thread::Thread() : thread(NULL) {}
Thread::~Thread() { assert(!thread); }
void Thread::start() {
assert(!thread);
thread = new std::thread(Thread::runPrivate, this);
}
void Thread::join() {
thread->join();
delete thread;
thread = NULL;
}
void Thread::runPrivate(Thread* thread) { thread->run(); }
ThreadPool::Task::Task() {}
ThreadPool::Task::~Task() {}
namespace {
class ThreadPoolWorker : public Thread {
public:
ThreadPoolWorker() : busy(1), work(0), task(NULL), dead(false) {}
~ThreadPoolWorker() {
// check that task has been deleted
assert(task == NULL);
}
bool runTaskIfNotBusy(ThreadPool::Task* taskToRun) {
if (busy.wait_for(0)) {
assert(!task);
assert(taskToRun);
task = taskToRun;
work.notify();
return true;
} else {
return false;
}
}
void run() {
for (;;) {
work.wait();
if (dead) {
return;
}
assert(task);
task->run();
delete task;
task = NULL;
busy.notify();
}
}
void kill() {
busy.wait();
dead = true;
work.notify();
join();
}
void wait() {
busy.wait();
busy.notify();
}
private:
Semaphore busy;
Semaphore work;
ThreadPool::Task* task;
bool dead;
};
} // namespace
ThreadPool::ThreadPool(int numThreads) {
for (int i = 0; i < numThreads; ++i) {
workers.push_back(new ThreadPoolWorker());
assert(workers.back()); // TODO: should fail
workers.back()->start();
}
}
ThreadPool::~ThreadPool() {
for (size_t i = 0; i < workers.size(); ++i) {
static_cast<ThreadPoolWorker*>(workers[i])->kill();
delete workers[i];
}
}
bool ThreadPool::tryRun(Task* task) {
for (size_t i = 0; i < workers.size(); ++i) {
if (static_cast<ThreadPoolWorker*>(workers[i])->runTaskIfNotBusy(task)) {
return true;
}
}
return false;
}
void ThreadPool::waitAll() {
for (size_t i = 0; i < workers.size(); ++i) {
static_cast<ThreadPoolWorker*>(workers[i])->wait();
}
}
int getNumCores() {
#ifdef WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#elif MACOS
int nm[2];
size_t len = 4;
uint32_t count;
nm[0] = CTL_HW;
nm[1] = HW_AVAILCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
if (count < 1) {
nm[1] = HW_NCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
if (count < 1) {
count = 1;
}
}
return count;
#else
return (int)sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
} // namespace VideoStitch