aboutsummaryrefslogtreecommitdiff
path: root/src/core/taskq.c
blob: e45819b5d23cd497c5bdb456efe3a3cca4d53f89 (plain)
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
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
//
// 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"

typedef struct nni_taskq_thr nni_taskq_thr;
struct nni_taskq_thr {
	nni_taskq *    tqt_tq;
	nni_thr        tqt_thread;
	nni_taskq_ent *tqt_running;
	int            tqt_wait;
};
struct nni_taskq {
	nni_list       tq_ents;
	nni_mtx        tq_mtx;
	nni_cv         tq_cv;
	nni_taskq_thr *tq_threads;
	int            tq_nthreads;
	int            tq_close;
};

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_taskq_ent *ent;

	nni_mtx_lock(&tq->tq_mtx);
	for (;;) {
		if ((ent = nni_list_first(&tq->tq_ents)) != NULL) {
			nni_list_remove(&tq->tq_ents, ent);
			ent->tqe_tq      = NULL;
			thr->tqt_running = ent;
			nni_mtx_unlock(&tq->tq_mtx);
			ent->tqe_cb(ent->tqe_arg);
			nni_mtx_lock(&tq->tq_mtx);
			thr->tqt_running = NULL;
			if (thr->tqt_wait) {
				thr->tqt_wait = 0;
				nni_cv_wake(&tq->tq_cv);
			}
			continue;
		}

		if (tq->tq_close) {
			break;
		}

		nni_cv_wait(&tq->tq_cv);
	}
	nni_mtx_unlock(&tq->tq_mtx);
}

int
nni_taskq_init(nni_taskq **tqp, int nthr)
{
	int        rv;
	nni_taskq *tq;
	int        i;

	if ((tq = NNI_ALLOC_STRUCT(tq)) == NULL) {
		return (NNG_ENOMEM);
	}
	if ((rv = nni_mtx_init(&tq->tq_mtx)) != 0) {
		NNI_FREE_STRUCT(tq);
		return (rv);
	}
	if ((rv = nni_cv_init(&tq->tq_cv, &tq->tq_mtx)) != 0) {
		nni_mtx_fini(&tq->tq_mtx);
		NNI_FREE_STRUCT(tq);
		return (rv);
	}
	tq->tq_close = 0;
	NNI_LIST_INIT(&tq->tq_ents, nni_taskq_ent, tqe_node);

	tq->tq_threads = nni_alloc(sizeof(nni_taskq_thr) * nthr);
	if (tq->tq_threads == NULL) {
		nni_cv_fini(&tq->tq_cv);
		nni_mtx_fini(&tq->tq_mtx);
		NNI_FREE_STRUCT(tq);
		return (NNG_ENOMEM);
	}
	tq->tq_nthreads = nthr;
	for (i = 0; i < nthr; i++) {
		tq->tq_threads[i].tqt_tq      = tq;
		tq->tq_threads[i].tqt_running = NULL;
		rv = nni_thr_init(&tq->tq_threads[i].tqt_thread,
		    nni_taskq_thread, &tq->tq_threads[i]);
		if (rv != 0) {
			goto fail;
		}
	}
	tq->tq_nthreads = nthr;
	for (i = 0; i < tq->tq_nthreads; i++) {
		nni_thr_run(&tq->tq_threads[i].tqt_thread);
	}
	*tqp = tq;
	return (0);

fail:

	nni_taskq_fini(tq);
	return (rv);
}

void
nni_taskq_fini(nni_taskq *tq)
{
	int i;

	nni_mtx_lock(&tq->tq_mtx);
	tq->tq_close = 1;
	nni_cv_wake(&tq->tq_cv);
	nni_mtx_unlock(&tq->tq_mtx);
	for (i = 0; i < tq->tq_nthreads; i++) {
		nni_thr_fini(&tq->tq_threads[i].tqt_thread);
	}
	nni_free(tq->tq_threads, tq->tq_nthreads * sizeof(nni_taskq_thr));
	nni_cv_fini(&tq->tq_cv);
	nni_mtx_fini(&tq->tq_mtx);
	NNI_FREE_STRUCT(tq);
}

int
nni_taskq_dispatch(nni_taskq *tq, nni_taskq_ent *ent)
{
	if (tq == NULL) {
		tq = nni_taskq_systq;
	}

	nni_mtx_lock(&tq->tq_mtx);
	if (tq->tq_close) {
		nni_mtx_unlock(&tq->tq_mtx);
		return (NNG_ECLOSED);
	}
	// It might already be scheduled... if so don't redo it.
	if (ent->tqe_tq == NULL) {
		ent->tqe_tq = tq;
		nni_list_append(&tq->tq_ents, ent);
	}
	nni_cv_wake(&tq->tq_cv);
	nni_mtx_unlock(&tq->tq_mtx);
	return (0);
}

int
nni_taskq_cancel(nni_taskq *tq, nni_taskq_ent *ent)
{
	int i;
	int running;

	if (tq == NULL) {
		tq = nni_taskq_systq;
	}

	nni_mtx_lock(&tq->tq_mtx);
	running = 1;
	do {
		running = 0;
		for (i = 0; i < tq->tq_nthreads; i++) {
			if (tq->tq_threads[i].tqt_running == ent) {
				tq->tq_threads[i].tqt_wait = 1;
				nni_cv_wait(&tq->tq_cv);
				running = 1;
				break;
			}
		}
	} while (running != 0);

	if (ent->tqe_tq != tq) {
		nni_mtx_unlock(&tq->tq_mtx);
		return (NNG_ENOENT);
	}
	if (nni_list_active(&tq->tq_ents, ent)) {
		nni_list_remove(&tq->tq_ents, ent);
	}
	nni_mtx_unlock(&tq->tq_mtx);
	return (0);
}

void
nni_taskq_ent_init(nni_taskq_ent *ent, nni_cb cb, void *arg)
{
	NNI_LIST_NODE_INIT(&ent->tqe_node);
	ent->tqe_cb  = cb;
	ent->tqe_arg = arg;
	ent->tqe_tq  = NULL;
}

int
nni_taskq_sys_init(void)
{
	int rv;

	// XXX: Make the "16" = NCPUs * 2
	rv = nni_taskq_init(&nni_taskq_systq, 16);
	return (rv);
}

void
nni_taskq_sys_fini(void)
{
	nni_taskq_fini(nni_taskq_systq);
}