summaryrefslogtreecommitdiffstats
path: root/source3/lib/fncall.c
blob: 6e6b7c92504f7c1553b73a6a67891f4a65cc471b (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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * Unix SMB/CIFS implementation.
 * Async fn calls
 * Copyright (C) Volker Lendecke 2009
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "../lib/util/tevent_unix.h"

#if WITH_PTHREADPOOL

#include "lib/pthreadpool/pthreadpool.h"

struct fncall_state {
	struct fncall_context *ctx;
	int job_id;
	bool done;

	void *private_parent;
	void *job_private;
};

struct fncall_context {
	struct pthreadpool *pool;
	int next_job_id;
	int sig_fd;
	struct tevent_req **pending;

	struct fncall_state **orphaned;
	int num_orphaned;

	struct fd_event *fde;
};

static void fncall_handler(struct tevent_context *ev, struct tevent_fd *fde,
			   uint16_t flags, void *private_data);

static int fncall_context_destructor(struct fncall_context *ctx)
{
	while (talloc_array_length(ctx->pending) != 0) {
		/* No TALLOC_FREE here */
		talloc_free(ctx->pending[0]);
	}

	while (ctx->num_orphaned != 0) {
		/*
		 * We've got jobs in the queue for which the tevent_req has
		 * been finished already. Wait for all of them to finish.
		 */
		fncall_handler(NULL, NULL, TEVENT_FD_READ, ctx);
	}

	pthreadpool_destroy(ctx->pool);
	ctx->pool = NULL;

	return 0;
}

struct fncall_context *fncall_context_init(TALLOC_CTX *mem_ctx,
					   int max_threads)
{
	struct fncall_context *ctx;
	int ret;

	ctx = talloc_zero(mem_ctx, struct fncall_context);
	if (ctx == NULL) {
		return NULL;
	}

	ret = pthreadpool_init(max_threads, &ctx->pool);
	if (ret != 0) {
		TALLOC_FREE(ctx);
		return NULL;
	}
	talloc_set_destructor(ctx, fncall_context_destructor);

	ctx->sig_fd = pthreadpool_signal_fd(ctx->pool);
	if (ctx->sig_fd == -1) {
		TALLOC_FREE(ctx);
		return NULL;
	}

	return ctx;
}

static int fncall_next_job_id(struct fncall_context *ctx)
{
	int num_pending = talloc_array_length(ctx->pending);
	int result;

	while (true) {
		int i;

		result = ctx->next_job_id++;
		if (result == 0) {
			continue;
		}

		for (i=0; i<num_pending; i++) {
			struct fncall_state *state = tevent_req_data(
				ctx->pending[i], struct fncall_state);

			if (result == state->job_id) {
				break;
			}
		}
		if (i == num_pending) {
			return result;
		}
	}
}

static void fncall_unset_pending(struct tevent_req *req);
static int fncall_destructor(struct tevent_req *req);

static bool fncall_set_pending(struct tevent_req *req,
			       struct fncall_context *ctx,
			       struct tevent_context *ev)
{
	struct tevent_req **pending;
	int num_pending, orphaned_array_length;

	num_pending = talloc_array_length(ctx->pending);

	pending = talloc_realloc(ctx, ctx->pending, struct tevent_req *,
				 num_pending+1);
	if (pending == NULL) {
		return false;
	}
	pending[num_pending] = req;
	num_pending += 1;
	ctx->pending = pending;
	talloc_set_destructor(req, fncall_destructor);

	/*
	 * Make sure that the orphaned array of fncall_state structs has
	 * enough space. A job can change from pending to orphaned in
	 * fncall_destructor, and to fail in a talloc destructor should be
	 * avoided if possible.
	 */

	orphaned_array_length = talloc_array_length(ctx->orphaned);
	if (num_pending > orphaned_array_length) {
		struct fncall_state **orphaned;

		orphaned = talloc_realloc(ctx, ctx->orphaned,
					  struct fncall_state *,
					  orphaned_array_length + 1);
		if (orphaned == NULL) {
			fncall_unset_pending(req);
			return false;
		}
		ctx->orphaned = orphaned;
	}

	if (ctx->fde != NULL) {
		return true;
	}

	ctx->fde = tevent_add_fd(ev, ctx->pending, ctx->sig_fd, TEVENT_FD_READ,
				 fncall_handler, ctx);
	if (ctx->fde == NULL) {
		fncall_unset_pending(req);
		return false;
	}
	return true;
}

static void fncall_unset_pending(struct tevent_req *req)
{
	struct fncall_state *state = tevent_req_data(req, struct fncall_state);
	struct fncall_context *ctx = state->ctx;
	int num_pending = talloc_array_length(ctx->pending);
	int i;

	if (num_pending == 1) {
		TALLOC_FREE(ctx->fde);
		TALLOC_FREE(ctx->pending);
		return;
	}

	for (i=0; i<num_pending; i++) {
		if (req == ctx->pending[i]) {
			break;
		}
	}
	if (i == num_pending) {
		return;
	}
	if (num_pending > 1) {
		ctx->pending[i] = ctx->pending[num_pending-1];
	}
	ctx->pending = talloc_realloc(NULL, ctx->pending, struct tevent_req *,
				      num_pending - 1);
}

static int fncall_destructor(struct tevent_req *req)
{
	struct fncall_state *state = tevent_req_data(
		req, struct fncall_state);
	struct fncall_context *ctx = state->ctx;

	fncall_unset_pending(req);

	if (state->done) {
		return 0;
	}

	/*
	 * Keep around the state of the deleted request until the request has
	 * finished in the helper thread. fncall_handler will destroy it.
	 */
	ctx->orphaned[ctx->num_orphaned] = talloc_move(ctx->orphaned, &state);
	ctx->num_orphaned += 1;

	return 0;
}

struct tevent_req *fncall_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
			       struct fncall_context *ctx,
			       void (*fn)(void *private_data),
			       void *private_data)
{
	struct tevent_req *req;
	struct fncall_state *state;
	int ret;

	req = tevent_req_create(mem_ctx, &state, struct fncall_state);
	if (req == NULL) {
		return NULL;
	}
	state->ctx = ctx;
	state->job_id = fncall_next_job_id(state->ctx);
	state->done = false;

	/*
	 * We need to keep the private data we handed out to the thread around
	 * as long as the job is not finished. This is a bit of an abstraction
	 * violation, because the "req->state1->subreq->state2" (we're
	 * "subreq", "req" is the request our caller creates) is broken to
	 * "ctx->state2->state1", but we are right now in the destructor for
	 * "subreq2", so what can we do. We need to keep state1 around,
	 * otherwise the helper thread will have no place to put its results.
	 */

	state->private_parent = talloc_parent(private_data);
	state->job_private = talloc_move(state, &private_data);

	ret = pthreadpool_add_job(state->ctx->pool, state->job_id, fn,
				  state->job_private);
	if (ret == -1) {
		tevent_req_error(req, errno);
		return tevent_req_post(req, ev);
	}
	if (!fncall_set_pending(req, state->ctx, ev)) {
		tevent_req_oom(req);
		return tevent_req_post(req, ev);
	}
	return req;
}

static void fncall_handler(struct tevent_context *ev, struct tevent_fd *fde,
			   uint16_t flags, void *private_data)
{
	struct fncall_context *ctx = talloc_get_type_abort(
		private_data, struct fncall_context);
	int i, num_pending;
	int job_id;

	job_id = pthreadpool_finished_job(ctx->pool);
	if (job_id <= 0) {
		return;
	}

	num_pending = talloc_array_length(ctx->pending);

	for (i=0; i<num_pending; i++) {
		struct fncall_state *state = tevent_req_data(
			ctx->pending[i], struct fncall_state);

		if (job_id == state->job_id) {
			state->done = true;
			talloc_move(state->private_parent,
				    &state->job_private);
			tevent_req_done(ctx->pending[i]);
			return;
		}
	}

	for (i=0; i<ctx->num_orphaned; i++) {
		if (job_id == ctx->orphaned[i]->job_id) {
			break;
		}
	}
	if (i == ctx->num_orphaned) {
		return;
	}

	TALLOC_FREE(ctx->orphaned[i]);

	if (i < ctx->num_orphaned-1) {
		ctx->orphaned[i] = ctx->orphaned[ctx->num_orphaned-1];
	}
	ctx->num_orphaned -= 1;
}

int fncall_recv(struct tevent_req *req, int *perr)
{
	if (tevent_req_is_unix_error(req, perr)) {
		return -1;
	}
	return 0;
}

#else  /* WITH_PTHREADPOOL */

struct fncall_context {
	uint8_t dummy;
};

struct fncall_context *fncall_context_init(TALLOC_CTX *mem_ctx,
					   int max_threads)
{
	return talloc(mem_ctx, struct fncall_context);
}

struct fncall_state {
	uint8_t dummy;
};

struct tevent_req *fncall_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
			       struct fncall_context *ctx,
			       void (*fn)(void *private_data),
			       void *private_data)
{
	struct tevent_req *req;
	struct fncall_state *state;

	req = tevent_req_create(mem_ctx, &state, struct fncall_state);
	if (req == NULL) {
		return NULL;
	}
	fn(private_data);
	tevent_req_post(req, ev);
	return req;
}

int fncall_recv(struct tevent_req *req, int *perr)
{
	return 0;
}

#endif