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_state *jobs;
63 struct pthreadpool_tevent_job_state {
64 struct pthreadpool_tevent_job_state *prev, *next;
65 struct pthreadpool_tevent *pool;
66 struct tevent_context *ev;
67 struct tevent_immediate *im;
68 struct tevent_req *req;
70 void (*fn)(void *private_data);
74 static int pthreadpool_tevent_destructor(struct pthreadpool_tevent *pool);
76 static int pthreadpool_tevent_job_signal(int jobid,
77 void (*job_fn)(void *private_data),
78 void *job_private_data,
81 int pthreadpool_tevent_init(TALLOC_CTX *mem_ctx, unsigned max_threads,
82 struct pthreadpool_tevent **presult)
84 struct pthreadpool_tevent *pool;
87 pool = talloc_zero(mem_ctx, struct pthreadpool_tevent);
92 ret = pthreadpool_init(max_threads, &pool->pool,
93 pthreadpool_tevent_job_signal, pool);
99 talloc_set_destructor(pool, pthreadpool_tevent_destructor);
105 size_t pthreadpool_tevent_max_threads(struct pthreadpool_tevent *pool)
107 if (pool->pool == NULL) {
111 return pthreadpool_max_threads(pool->pool);
114 size_t pthreadpool_tevent_queued_jobs(struct pthreadpool_tevent *pool)
116 if (pool->pool == NULL) {
120 return pthreadpool_queued_jobs(pool->pool);
123 static int pthreadpool_tevent_destructor(struct pthreadpool_tevent *pool)
125 struct pthreadpool_tevent_job_state *state, *next;
126 struct pthreadpool_tevent_glue *glue = NULL;
129 ret = pthreadpool_destroy(pool->pool);
135 for (state = pool->jobs; state != NULL; state = next) {
137 DLIST_REMOVE(pool->jobs, state);
142 * Delete all the registered
143 * tevent_context/tevent_threaded_context
146 for (glue = pool->glue_list; glue != NULL; glue = pool->glue_list) {
147 /* The glue destructor removes it from the list */
150 pool->glue_list = NULL;
155 static int pthreadpool_tevent_glue_destructor(
156 struct pthreadpool_tevent_glue *glue)
158 if (glue->pool->glue_list != NULL) {
159 DLIST_REMOVE(glue->pool->glue_list, glue);
162 /* Ensure the ev_link destructor knows we're gone */
163 glue->ev_link->glue = NULL;
165 TALLOC_FREE(glue->ev_link);
166 TALLOC_FREE(glue->tctx);
172 * Destructor called either explicitly from
173 * pthreadpool_tevent_glue_destructor(), or indirectly
174 * when owning tevent_context is destroyed.
176 * When called from pthreadpool_tevent_glue_destructor()
177 * ev_link->glue is already NULL, so this does nothing.
179 * When called from talloc_free() of the owning
180 * tevent_context we must ensure we also remove the
181 * linked glue object from the list inside
182 * struct pthreadpool_tevent.
184 static int pthreadpool_tevent_glue_link_destructor(
185 struct pthreadpool_tevent_glue_ev_link *ev_link)
187 TALLOC_FREE(ev_link->glue);
191 static int pthreadpool_tevent_register_ev(struct pthreadpool_tevent *pool,
192 struct tevent_context *ev)
194 struct pthreadpool_tevent_glue *glue = NULL;
195 struct pthreadpool_tevent_glue_ev_link *ev_link = NULL;
198 * See if this tevent_context was already registered by
199 * searching the glue object list. If so we have nothing
200 * to do here - we already have a tevent_context/tevent_threaded_context
203 for (glue = pool->glue_list; glue != NULL; glue = glue->next) {
204 if (glue->ev == ev) {
210 * Event context not yet registered - create a new glue
211 * object containing a tevent_context/tevent_threaded_context
212 * pair and put it on the list to remember this registration.
213 * We also need a link object to ensure the event context
214 * can't go away without us knowing about it.
216 glue = talloc_zero(pool, struct pthreadpool_tevent_glue);
220 *glue = (struct pthreadpool_tevent_glue) {
224 talloc_set_destructor(glue, pthreadpool_tevent_glue_destructor);
227 * Now allocate the link object to the event context. Note this
228 * is allocated OFF THE EVENT CONTEXT ITSELF, so if the event
229 * context is freed we are able to cleanup the glue object
230 * in the link object destructor.
233 ev_link = talloc_zero(ev, struct pthreadpool_tevent_glue_ev_link);
234 if (ev_link == NULL) {
238 ev_link->glue = glue;
239 talloc_set_destructor(ev_link, pthreadpool_tevent_glue_link_destructor);
241 glue->ev_link = ev_link;
244 glue->tctx = tevent_threaded_context_create(pool, ev);
245 if (glue->tctx == NULL) {
246 TALLOC_FREE(ev_link);
252 DLIST_ADD(pool->glue_list, glue);
256 static void pthreadpool_tevent_job_fn(void *private_data);
257 static void pthreadpool_tevent_job_done(struct tevent_context *ctx,
258 struct tevent_immediate *im,
261 static int pthreadpool_tevent_job_state_destructor(
262 struct pthreadpool_tevent_job_state *state)
264 if (state->pool == NULL) {
269 * We should never be called with state->req == NULL,
270 * state->pool must be cleared before the 2nd talloc_free().
272 if (state->req == NULL) {
277 * We need to reparent to a long term context.
279 (void)talloc_reparent(state->req, NULL, state);
284 struct tevent_req *pthreadpool_tevent_job_send(
285 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
286 struct pthreadpool_tevent *pool,
287 void (*fn)(void *private_data), void *private_data)
289 struct tevent_req *req;
290 struct pthreadpool_tevent_job_state *state;
293 req = tevent_req_create(mem_ctx, &state,
294 struct pthreadpool_tevent_job_state);
302 state->private_data = private_data;
304 state->im = tevent_create_immediate(state);
305 if (tevent_req_nomem(state->im, req)) {
306 return tevent_req_post(req, ev);
309 ret = pthreadpool_tevent_register_ev(pool, ev);
310 if (tevent_req_error(req, ret)) {
311 return tevent_req_post(req, ev);
314 ret = pthreadpool_add_job(pool->pool, 0,
315 pthreadpool_tevent_job_fn,
317 if (tevent_req_error(req, ret)) {
318 return tevent_req_post(req, ev);
322 * Once the job is scheduled, we need to protect
325 talloc_set_destructor(state, pthreadpool_tevent_job_state_destructor);
327 DLIST_ADD_END(pool->jobs, state);
332 static void pthreadpool_tevent_job_fn(void *private_data)
334 struct pthreadpool_tevent_job_state *state = talloc_get_type_abort(
335 private_data, struct pthreadpool_tevent_job_state);
336 state->fn(state->private_data);
339 static int pthreadpool_tevent_job_signal(int jobid,
340 void (*job_fn)(void *private_data),
341 void *job_private_data,
344 struct pthreadpool_tevent_job_state *state = talloc_get_type_abort(
345 job_private_data, struct pthreadpool_tevent_job_state);
346 struct tevent_threaded_context *tctx = NULL;
347 struct pthreadpool_tevent_glue *g = NULL;
349 if (state->pool == NULL) {
350 /* The pthreadpool_tevent is already gone */
355 for (g = state->pool->glue_list; g != NULL; g = g->next) {
356 if (g->ev == state->ev) {
368 /* with HAVE_PTHREAD */
369 tevent_threaded_schedule_immediate(tctx, state->im,
370 pthreadpool_tevent_job_done,
373 /* without HAVE_PTHREAD */
374 tevent_schedule_immediate(state->im, state->ev,
375 pthreadpool_tevent_job_done,
382 static void pthreadpool_tevent_job_done(struct tevent_context *ctx,
383 struct tevent_immediate *im,
386 struct pthreadpool_tevent_job_state *state = talloc_get_type_abort(
387 private_data, struct pthreadpool_tevent_job_state);
389 if (state->pool != NULL) {
390 DLIST_REMOVE(state->pool->jobs, state);
394 if (state->req == NULL) {
396 * There was a talloc_free() state->req
397 * while the job was pending,
398 * which mean we're reparented on a longterm
401 * We just cleanup here...
407 tevent_req_done(state->req);
410 int pthreadpool_tevent_job_recv(struct tevent_req *req)
412 return tevent_req_simple_recv_unix(req);