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
|
#include "Async.h"
namespace {
class AsyncStopper : public Balau::AsyncOperation {
public:
virtual bool needsSynchronousCallback() { return false; }
virtual void done() { delete this; }
};
};
void Balau::AsyncManager::setIdleReadyCallback(void (*callback)(void *), void * param) {
while (!m_ready);
TLS * tls = getTLS();
tls->idleReadyCallback = callback;
tls->idleReadyParam = param;
}
void Balau::AsyncManager::queueOp(AsyncOperation * op) {
if (m_stopperPushed) {
Printer::elog(E_ASYNC, "AsyncManager's queue has been stopped; running operation %p on this thread instead.", op);
op->run();
op->finalize();
return;
}
while (!m_ready);
TLS * tls = getTLS();
Printer::elog(E_ASYNC, "Queuing operation at %p", op);
if (op->needsSynchronousCallback()) {
Printer::elog(E_ASYNC, "Operation at %p needs synchronous callback, copying values; idleQueue = %p; idleReadyCallback = %p; idleReadyParam = %p", op, &tls->idleQueue, tls->idleReadyCallback, tls->idleReadyParam);
op->m_idleQueue = &tls->idleQueue;
op->m_idleReadyCallback = tls->idleReadyCallback;
op->m_idleReadyParam = tls->idleReadyParam;
}
if (op->needsMainQueue()) {
m_queue.push(op);
} else if (op->needsFinishWorker()) {
m_finished.push(op);
} else {
op->run();
op->finalize();
}
}
void Balau::AsyncManager::checkIdle() {
if (m_numFinishersIdle > m_maxIdle)
killOneFinisher();
if (m_numFinishersIdle < m_minIdle)
startOneFinisher();
joinStoppedFinishers();
}
void Balau::AsyncManager::killOneFinisher() {
Printer::elog(E_ASYNC, "Too many workers idle (%i / %i), killing one.", m_numFinishersIdle, m_maxIdle);
m_finished.push(new AsyncStopper());
}
void Balau::AsyncManager::startOneFinisher() {
AsyncFinishWorker * worker = new AsyncFinishWorker(this, &m_finished);
Printer::elog(E_ASYNC, "Not enough workers idle (%i / %i), starting one at %p.", m_numFinishersIdle, m_minIdle, worker);
m_workers.push_back(worker);
m_numFinishers++;
worker->threadStart();
}
void Balau::AsyncManager::joinStoppedFinishers() {
for (auto i = m_workers.begin(); i != m_workers.end(); i++) {
AsyncFinishWorker * worker = *i;
if (!worker->stopped())
continue;
Printer::elog(E_ASYNC, "Joining stopped worker at %p", worker);
m_numFinishers--;
m_workers.erase(i);
worker->join();
delete worker;
break;
}
}
void * Balau::AsyncManager::proc() {
Printer::elog(E_ASYNC, "AsyncManager thread starting up");
m_tlsManager.init();
m_ready = true;
while (!m_stopping) {
checkIdle();
AsyncOperation * op = m_queue.pop();
Printer::elog(E_ASYNC, "AsyncManager got an operation at %p", op);
if (dynamic_cast<AsyncStopper *>(op)) {
Printer::elog(E_ASYNC, "AsyncManager got a stopper operation");
m_stopping = true;
}
Printer::elog(E_ASYNC, "AsyncManager running operation at %p", op);
op->run();
if (op->needsFinishWorker()) {
Printer::elog(E_ASYNC, "AsyncManager pushing operation at %p in the finisher's queue", op);
m_finished.push(op);
} else {
Printer::elog(E_ASYNC, "AsyncManager finalizing operation at %p", op);
op->finalize();
}
}
stopAllWorkers();
Printer::elog(E_ASYNC, "Async thread waits for all idle queues to empty");
while (m_numTLSes--) {
TLS * tls = m_TLSes.pop();
while (!tls->idleQueue.isEmpty());
}
Printer::elog(E_ASYNC, "Async thread stopping");
return NULL;
}
void * Balau::AsyncFinishWorker::proc() {
Printer::elog(E_ASYNC, "AsyncFinishWorker thread starting up");
AsyncOperation * op;
while (!m_stopping) {
m_async->incIdle();
op = m_queue->pop();
m_async->decIdle();
Printer::elog(E_ASYNC, "AsyncFinishWorker got operation at %p", op);
if (dynamic_cast<AsyncStopper *>(op)) {
Printer::elog(E_ASYNC, "AsyncFinishWorker got a stopper operation");
m_stopping = true;
}
if (!op->needsMainQueue())
op->run();
op->finalize();
}
m_stopped = true;
Printer::elog(E_ASYNC, "AsyncFinishWorker thread stopping");
return NULL;
}
void Balau::AsyncOperation::finalize() {
Printer::elog(E_ASYNC, "AsyncOperation::finalize() is finishing operation %p", this);
finish();
if (needsSynchronousCallback()) {
Printer::elog(E_ASYNC, "AsyncOperation::finalize() is pushing operation %p to its idle queue", this);
bool wasEmpty = m_idleQueue->isEmpty();
m_idleQueue->push(this);
Printer::elog(E_ASYNC, "AsyncOperation::finalize() has pushed operation %p to its idle queue; wasEmpty = %s; callback = %p", this, wasEmpty ? "true" : "false", m_idleReadyCallback);
if (wasEmpty && m_idleReadyCallback) {
Printer::elog(E_ASYNC, "AsyncOperation::finalize() is calling ready callback to wake up main loop");
m_idleReadyCallback(m_idleReadyParam);
}
} else {
Printer::elog(E_ASYNC, "AsyncOperation::finalize() is wrapping up operation %p", this);
done();
}
}
void Balau::AsyncManager::idle() {
Printer::elog(E_ASYNC, "AsyncManager::idle() is running");
while (!m_ready);
AsyncOperation * op;
TLS * tls = getTLS();
while ((op = tls->idleQueue.pop())) {
Printer::elog(E_ASYNC, "AsyncManager::idle() is wrapping up operation %p", op);
op->done();
}
}
void Balau::AsyncManager::threadExit() {
Printer::elog(E_ASYNC, "AsyncManager thread is being asked to stop; creating stopper");
if (!m_stopperPushed.exchange(true))
m_queue.push(new AsyncStopper());
}
void Balau::AsyncManager::stopAllWorkers() {
Printer::elog(E_ASYNC, "AsyncManager thread is stopping and joining %i workers", m_numFinishers);
for (int i = 0; i < m_numFinishers; i++)
m_finished.push(new AsyncStopper());
for (auto worker : m_workers)
worker->join();
}
|