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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
//
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#include "core/nng_impl.h"
#include "nng/nng.h"
typedef struct nni_taskq_thr nni_taskq_thr;
struct nni_taskq_thr {
nni_taskq *tqt_tq;
nni_thr tqt_thread;
};
struct nni_taskq {
nni_list tq_tasks;
nni_mtx tq_mtx;
nni_cv tq_sched_cv;
nni_cv tq_wait_cv;
nni_taskq_thr *tq_threads;
int tq_nthreads;
bool tq_run;
};
static nni_taskq *nni_taskq_systq = NULL;
static void
nni_taskq_thread(void *self)
{
nni_taskq_thr *thr = self;
nni_taskq *tq = thr->tqt_tq;
nni_task *task;
nni_thr_set_name(NULL, "nng:task");
nni_mtx_lock(&tq->tq_mtx);
for (;;) {
if ((task = nni_list_first(&tq->tq_tasks)) != NULL) {
nni_list_remove(&tq->tq_tasks, task);
nni_mtx_unlock(&tq->tq_mtx);
task->task_cb(task->task_arg);
nni_mtx_lock(&task->task_mtx);
task->task_busy--;
if (task->task_busy == 0) {
nni_cv_wake(&task->task_cv);
}
nni_mtx_unlock(&task->task_mtx);
nni_mtx_lock(&tq->tq_mtx);
continue;
}
nni_cv_wake(&tq->tq_wait_cv);
if (!tq->tq_run) {
break;
}
nni_cv_wait(&tq->tq_sched_cv);
}
nni_mtx_unlock(&tq->tq_mtx);
}
int
nni_taskq_init(nni_taskq **tqp, int nthr)
{
nni_taskq *tq;
if ((tq = NNI_ALLOC_STRUCT(tq)) == NULL) {
return (NNG_ENOMEM);
}
if ((tq->tq_threads = NNI_ALLOC_STRUCTS(tq->tq_threads, nthr)) ==
NULL) {
NNI_FREE_STRUCT(tq);
return (NNG_ENOMEM);
}
tq->tq_nthreads = nthr;
NNI_LIST_INIT(&tq->tq_tasks, nni_task, task_node);
nni_mtx_init(&tq->tq_mtx);
nni_cv_init(&tq->tq_sched_cv, &tq->tq_mtx);
nni_cv_init(&tq->tq_wait_cv, &tq->tq_mtx);
for (int i = 0; i < nthr; i++) {
int rv;
tq->tq_threads[i].tqt_tq = tq;
rv = nni_thr_init(&tq->tq_threads[i].tqt_thread,
nni_taskq_thread, &tq->tq_threads[i]);
if (rv != 0) {
nni_taskq_fini(tq);
return (rv);
}
}
tq->tq_run = true;
for (int i = 0; i < tq->tq_nthreads; i++) {
nni_thr_run(&tq->tq_threads[i].tqt_thread);
}
*tqp = tq;
return (0);
}
void
nni_taskq_fini(nni_taskq *tq)
{
if (tq == NULL) {
return;
}
if (tq->tq_run) {
nni_mtx_lock(&tq->tq_mtx);
tq->tq_run = false;
nni_cv_wake(&tq->tq_sched_cv);
nni_mtx_unlock(&tq->tq_mtx);
}
for (int i = 0; i < tq->tq_nthreads; i++) {
nni_thr_fini(&tq->tq_threads[i].tqt_thread);
}
nni_cv_fini(&tq->tq_wait_cv);
nni_cv_fini(&tq->tq_sched_cv);
nni_mtx_fini(&tq->tq_mtx);
NNI_FREE_STRUCTS(tq->tq_threads, tq->tq_nthreads);
NNI_FREE_STRUCT(tq);
}
bool
nni_taskq_drain(nni_taskq *tq)
{
bool result = false;
nni_mtx_lock(&tq->tq_mtx);
while (!nni_list_empty(&tq->tq_tasks)) {
result = true;
nni_cv_wait(&tq->tq_wait_cv);
}
nni_mtx_unlock(&tq->tq_mtx);
return (result);
}
void
nni_task_exec(nni_task *task)
{
nni_mtx_lock(&task->task_mtx);
if (task->task_prep) {
task->task_prep = false;
} else {
task->task_busy++;
}
nni_mtx_unlock(&task->task_mtx);
if (task->task_cb != NULL) {
task->task_cb(task->task_arg);
}
nni_mtx_lock(&task->task_mtx);
task->task_busy--;
if (task->task_busy == 0) {
nni_cv_wake(&task->task_cv);
}
nni_mtx_unlock(&task->task_mtx);
}
void
nni_task_dispatch(nni_task *task)
{
nni_taskq *tq = task->task_tq;
// If there is no callback to perform, then do nothing!
// The user will be none the wiser.
if (task->task_cb == NULL) {
nni_task_exec(task);
return;
}
nni_mtx_lock(&task->task_mtx);
if (task->task_prep) {
task->task_prep = false;
} else {
task->task_busy++;
}
nni_mtx_unlock(&task->task_mtx);
nni_mtx_lock(&tq->tq_mtx);
nni_list_append(&tq->tq_tasks, task);
nni_cv_wake1(&tq->tq_sched_cv); // waking just one waiter is adequate
nni_mtx_unlock(&tq->tq_mtx);
}
void
nni_task_prep(nni_task *task)
{
nni_mtx_lock(&task->task_mtx);
task->task_busy++;
task->task_prep = true;
nni_mtx_unlock(&task->task_mtx);
}
void
nni_task_abort(nni_task *task)
{
// This is called when unscheduling the task.
nni_mtx_lock(&task->task_mtx);
if (task->task_prep) {
task->task_prep = false;
task->task_busy--;
if (task->task_busy == 0) {
nni_cv_wake(&task->task_cv);
}
}
nni_mtx_unlock(&task->task_mtx);
}
void
nni_task_wait(nni_task *task)
{
nni_mtx_lock(&task->task_mtx);
while (task->task_busy) {
nni_cv_wait(&task->task_cv);
}
nni_mtx_unlock(&task->task_mtx);
}
bool
nni_task_busy(nni_task *task)
{
bool busy;
nni_mtx_lock(&task->task_mtx);
busy = task->task_busy;
nni_mtx_unlock(&task->task_mtx);
return (busy);
}
void
nni_task_init(nni_task *task, nni_taskq *tq, nni_cb cb, void *arg)
{
NNI_LIST_NODE_INIT(&task->task_node);
nni_mtx_init(&task->task_mtx);
nni_cv_init(&task->task_cv, &task->task_mtx);
task->task_prep = false;
task->task_busy = 0;
task->task_cb = cb;
task->task_arg = arg;
task->task_tq = tq != NULL ? tq : nni_taskq_systq;
}
void
nni_task_fini(nni_task *task)
{
nni_mtx_lock(&task->task_mtx);
while (task->task_busy) {
nni_cv_wait(&task->task_cv);
}
nni_mtx_unlock(&task->task_mtx);
nni_cv_fini(&task->task_cv);
nni_mtx_fini(&task->task_mtx);
}
int
nni_taskq_sys_init(nng_init_params *params)
{
int16_t num_thr;
int16_t max_thr;
max_thr = params->max_task_threads;
num_thr = params->num_task_threads;
if ((max_thr > 0) && (num_thr > max_thr)) {
num_thr = max_thr;
}
if (num_thr < 2) {
num_thr = 2;
}
params->num_task_threads = num_thr;
return (nni_taskq_init(&nni_taskq_systq, (int) num_thr));
}
bool
nni_taskq_sys_drain(void)
{
return (nni_taskq_drain(nni_taskq_systq));
}
void
nni_taskq_sys_fini(void)
{
nni_taskq_fini(nni_taskq_systq);
nni_taskq_systq = NULL;
}
|