2 * Unix SMB/CIFS implementation.
3 * threadpool implementation based on pthreads
4 * Copyright (C) Volker Lendecke 2009,2011
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "pthreadpool_tevent.h"
22 #include "pthreadpool.h"
23 #include "lib/util/tevent_unix.h"
24 #include "lib/util/dlinklist.h"
26 struct pthreadpool_tevent_job_state;
29 * We need one pthreadpool_tevent_glue object per unique combintaion of tevent
30 * contexts and pthreadpool_tevent objects. Maintain a list of used tevent
31 * contexts in a pthreadpool_tevent.
33 struct pthreadpool_tevent_glue {
34 struct pthreadpool_tevent_glue *prev, *next;
35 struct pthreadpool_tevent *pool; /* back-pointer to owning object. */
36 /* Tuple we are keeping track of in this list. */
37 struct tevent_context *ev;
38 struct tevent_threaded_context *tctx;
39 /* Pointer to link object owned by *ev. */
40 struct pthreadpool_tevent_glue_ev_link *ev_link;
44 * The pthreadpool_tevent_glue_ev_link and its destructor ensure we remove the
45 * tevent context from our list of active event contexts if the event context
47 * This structure is talloc()'ed from the struct tevent_context *, and is a
48 * back-pointer allowing the related struct pthreadpool_tevent_glue object
49 * to be removed from the struct pthreadpool_tevent glue list if the owning
50 * tevent_context is talloc_free()'ed.
52 struct pthreadpool_tevent_glue_ev_link {
53 struct pthreadpool_tevent_glue *glue;
56 struct pthreadpool_tevent {
57 struct pthreadpool *pool;
58 struct pthreadpool_tevent_glue *glue_list;
60 struct pthreadpool_tevent_job *jobs;
63 struct pthreadpool_tevent_job_state {
64 struct tevent_context *ev;
65 struct tevent_req *req;
66 struct pthreadpool_tevent_job *job;
69 struct pthreadpool_tevent_job {
70 struct pthreadpool_tevent_job *prev, *next;
72 struct pthreadpool_tevent *pool;
73 struct pthreadpool_tevent_job_state *state;
74 struct tevent_immediate *im;
76 void (*fn)(void *private_data);
80 static int pthreadpool_tevent_destructor(struct pthreadpool_tevent *pool);
82 static void pthreadpool_tevent_job_orphan(struct pthreadpool_tevent_job *job);
84 static int pthreadpool_tevent_job_signal(int jobid,
85 void (*job_fn)(void *private_data),
86 void *job_private_data,
89 int pthreadpool_tevent_init(TALLOC_CTX *mem_ctx, unsigned max_threads,
90 struct pthreadpool_tevent **presult)
92 struct pthreadpool_tevent *pool;
95 pool = talloc_zero(mem_ctx, struct pthreadpool_tevent);
100 ret = pthreadpool_init(max_threads, &pool->pool,
101 pthreadpool_tevent_job_signal, pool);
107 talloc_set_destructor(pool, pthreadpool_tevent_destructor);
113 size_t pthreadpool_tevent_max_threads(struct pthreadpool_tevent *pool)
115 if (pool->pool == NULL) {
119 return pthreadpool_max_threads(pool->pool);
122 size_t pthreadpool_tevent_queued_jobs(struct pthreadpool_tevent *pool)
124 if (pool->pool == NULL) {
128 return pthreadpool_queued_jobs(pool->pool);
131 static int pthreadpool_tevent_destructor(struct pthreadpool_tevent *pool)
133 struct pthreadpool_tevent_job *job = NULL;
134 struct pthreadpool_tevent_job *njob = NULL;
135 struct pthreadpool_tevent_glue *glue = NULL;
138 ret = pthreadpool_destroy(pool->pool);
144 for (job = pool->jobs; job != NULL; job = njob) {
147 /* The job this removes it from the list */
148 pthreadpool_tevent_job_orphan(job);
152 * Delete all the registered
153 * tevent_context/tevent_threaded_context
156 for (glue = pool->glue_list; glue != NULL; glue = pool->glue_list) {
157 /* The glue destructor removes it from the list */
160 pool->glue_list = NULL;
165 static int pthreadpool_tevent_glue_destructor(
166 struct pthreadpool_tevent_glue *glue)
168 if (glue->pool->glue_list != NULL) {
169 DLIST_REMOVE(glue->pool->glue_list, glue);
172 /* Ensure the ev_link destructor knows we're gone */
173 glue->ev_link->glue = NULL;
175 TALLOC_FREE(glue->ev_link);
176 TALLOC_FREE(glue->tctx);
182 * Destructor called either explicitly from
183 * pthreadpool_tevent_glue_destructor(), or indirectly
184 * when owning tevent_context is destroyed.
186 * When called from pthreadpool_tevent_glue_destructor()
187 * ev_link->glue is already NULL, so this does nothing.
189 * When called from talloc_free() of the owning
190 * tevent_context we must ensure we also remove the
191 * linked glue object from the list inside
192 * struct pthreadpool_tevent.
194 static int pthreadpool_tevent_glue_link_destructor(
195 struct pthreadpool_tevent_glue_ev_link *ev_link)
197 TALLOC_FREE(ev_link->glue);
201 static int pthreadpool_tevent_register_ev(struct pthreadpool_tevent *pool,
202 struct tevent_context *ev)
204 struct pthreadpool_tevent_glue *glue = NULL;
205 struct pthreadpool_tevent_glue_ev_link *ev_link = NULL;
208 * See if this tevent_context was already registered by
209 * searching the glue object list. If so we have nothing
210 * to do here - we already have a tevent_context/tevent_threaded_context
213 for (glue = pool->glue_list; glue != NULL; glue = glue->next) {
214 if (glue->ev == ev) {
220 * Event context not yet registered - create a new glue
221 * object containing a tevent_context/tevent_threaded_context
222 * pair and put it on the list to remember this registration.
223 * We also need a link object to ensure the event context
224 * can't go away without us knowing about it.
226 glue = talloc_zero(pool, struct pthreadpool_tevent_glue);
230 *glue = (struct pthreadpool_tevent_glue) {
234 talloc_set_destructor(glue, pthreadpool_tevent_glue_destructor);
237 * Now allocate the link object to the event context. Note this
238 * is allocated OFF THE EVENT CONTEXT ITSELF, so if the event
239 * context is freed we are able to cleanup the glue object
240 * in the link object destructor.
243 ev_link = talloc_zero(ev, struct pthreadpool_tevent_glue_ev_link);
244 if (ev_link == NULL) {
248 ev_link->glue = glue;
249 talloc_set_destructor(ev_link, pthreadpool_tevent_glue_link_destructor);
251 glue->ev_link = ev_link;
254 glue->tctx = tevent_threaded_context_create(pool, ev);
255 if (glue->tctx == NULL) {
256 TALLOC_FREE(ev_link);
262 DLIST_ADD(pool->glue_list, glue);
266 static void pthreadpool_tevent_job_fn(void *private_data);
267 static void pthreadpool_tevent_job_done(struct tevent_context *ctx,
268 struct tevent_immediate *im,
271 static int pthreadpool_tevent_job_destructor(struct pthreadpool_tevent_job *job)
274 * We should never be called with state->state != NULL.
275 * Only pthreadpool_tevent_job_orphan() will call TALLOC_FREE(job)
276 * after detaching from the request state and pool list.
278 if (job->state != NULL) {
283 * If the job is not finished (job->im still there)
284 * and it's still attached to the pool,
285 * we try to cancel it (before it was starts)
287 if (job->im != NULL && job->pool != NULL) {
290 num = pthreadpool_cancel_job(job->pool->pool, 0,
291 pthreadpool_tevent_job_fn,
295 * It was not too late to cancel the request.
297 * We can remove job->im, as it will never be used.
299 TALLOC_FREE(job->im);
304 * pthreadpool_tevent_job_orphan() already removed
305 * it from pool->jobs. And we don't need try
306 * pthreadpool_cancel_job() again.
310 if (job->im != NULL) {
312 * state->im still there means, we need to wait for the
313 * immediate event to be triggered or just leak the memory.
321 static void pthreadpool_tevent_job_orphan(struct pthreadpool_tevent_job *job)
324 * We're the only function that sets
327 if (job->state == NULL) {
332 * We need to reparent to a long term context.
333 * And detach from the request state.
334 * Maybe the destructor will keep the memory
335 * and leak it for now.
337 (void)talloc_reparent(job->state, NULL, job);
338 job->state->job = NULL;
342 * job->pool will only be set to NULL
343 * in the first destructur run.
345 if (job->pool == NULL) {
350 * Dettach it from the pool.
352 * The job might still be running,
353 * so we keep job->pool.
354 * The destructor will set it to NULL
355 * after trying pthreadpool_cancel_job()
357 DLIST_REMOVE(job->pool->jobs, job);
362 static void pthreadpool_tevent_job_cleanup(struct tevent_req *req,
363 enum tevent_req_state req_state)
365 struct pthreadpool_tevent_job_state *state =
367 struct pthreadpool_tevent_job_state);
369 if (state->job == NULL) {
371 * The job request is not scheduled in the pool
378 * We need to reparent to a long term context.
379 * Maybe the destructor will keep the memory
380 * and leak it for now.
382 pthreadpool_tevent_job_orphan(state->job);
383 state->job = NULL; /* not needed but looks better */
387 struct tevent_req *pthreadpool_tevent_job_send(
388 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
389 struct pthreadpool_tevent *pool,
390 void (*fn)(void *private_data), void *private_data)
392 struct tevent_req *req = NULL;
393 struct pthreadpool_tevent_job_state *state = NULL;
394 struct pthreadpool_tevent_job *job = NULL;
397 req = tevent_req_create(mem_ctx, &state,
398 struct pthreadpool_tevent_job_state);
405 tevent_req_set_cleanup_fn(req, pthreadpool_tevent_job_cleanup);
408 tevent_req_error(req, EINVAL);
409 return tevent_req_post(req, ev);
411 if (pool->pool == NULL) {
412 tevent_req_error(req, EINVAL);
413 return tevent_req_post(req, ev);
416 ret = pthreadpool_tevent_register_ev(pool, ev);
417 if (tevent_req_error(req, ret)) {
418 return tevent_req_post(req, ev);
421 job = talloc_zero(state, struct pthreadpool_tevent_job);
422 if (tevent_req_nomem(job, req)) {
423 return tevent_req_post(req, ev);
427 job->private_data = private_data;
428 job->im = tevent_create_immediate(state->job);
429 if (tevent_req_nomem(job->im, req)) {
430 return tevent_req_post(req, ev);
432 talloc_set_destructor(job, pthreadpool_tevent_job_destructor);
433 DLIST_ADD_END(job->pool->jobs, job);
437 ret = pthreadpool_add_job(job->pool->pool, 0,
438 pthreadpool_tevent_job_fn,
440 if (tevent_req_error(req, ret)) {
441 return tevent_req_post(req, ev);
447 static void pthreadpool_tevent_job_fn(void *private_data)
449 struct pthreadpool_tevent_job *job =
450 talloc_get_type_abort(private_data,
451 struct pthreadpool_tevent_job);
453 job->fn(job->private_data);
456 static int pthreadpool_tevent_job_signal(int jobid,
457 void (*job_fn)(void *private_data),
458 void *job_private_data,
461 struct pthreadpool_tevent_job *job =
462 talloc_get_type_abort(job_private_data,
463 struct pthreadpool_tevent_job);
464 struct pthreadpool_tevent_job_state *state = job->state;
465 struct tevent_threaded_context *tctx = NULL;
466 struct pthreadpool_tevent_glue *g = NULL;
469 /* Request already gone */
474 for (g = job->pool->glue_list; g != NULL; g = g->next) {
475 if (g->ev == state->ev) {
487 /* with HAVE_PTHREAD */
488 tevent_threaded_schedule_immediate(tctx, job->im,
489 pthreadpool_tevent_job_done,
492 /* without HAVE_PTHREAD */
493 tevent_schedule_immediate(job->im, state->ev,
494 pthreadpool_tevent_job_done,
501 static void pthreadpool_tevent_job_done(struct tevent_context *ctx,
502 struct tevent_immediate *im,
505 struct pthreadpool_tevent_job *job =
506 talloc_get_type_abort(private_data,
507 struct pthreadpool_tevent_job);
508 struct pthreadpool_tevent_job_state *state = job->state;
510 TALLOC_FREE(job->im);
513 /* Request already gone */
519 * pthreadpool_tevent_job_cleanup()
520 * will destroy the job.
522 tevent_req_done(state->req);
525 int pthreadpool_tevent_job_recv(struct tevent_req *req)
527 return tevent_req_simple_recv_unix(req);