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 static int pthreadpool_tevent_destructor(struct pthreadpool_tevent
*pool
)
107 struct pthreadpool_tevent_job_state
*state
, *next
;
108 struct pthreadpool_tevent_glue
*glue
= NULL
;
111 ret
= pthreadpool_destroy(pool
->pool
);
117 for (state
= pool
->jobs
; state
!= NULL
; state
= next
) {
119 DLIST_REMOVE(pool
->jobs
, state
);
124 * Delete all the registered
125 * tevent_context/tevent_threaded_context
128 for (glue
= pool
->glue_list
; glue
!= NULL
; glue
= pool
->glue_list
) {
129 /* The glue destructor removes it from the list */
132 pool
->glue_list
= NULL
;
137 static int pthreadpool_tevent_glue_destructor(
138 struct pthreadpool_tevent_glue
*glue
)
140 if (glue
->pool
->glue_list
!= NULL
) {
141 DLIST_REMOVE(glue
->pool
->glue_list
, glue
);
144 /* Ensure the ev_link destructor knows we're gone */
145 glue
->ev_link
->glue
= NULL
;
147 TALLOC_FREE(glue
->ev_link
);
148 TALLOC_FREE(glue
->tctx
);
154 * Destructor called either explicitly from
155 * pthreadpool_tevent_glue_destructor(), or indirectly
156 * when owning tevent_context is destroyed.
158 * When called from pthreadpool_tevent_glue_destructor()
159 * ev_link->glue is already NULL, so this does nothing.
161 * When called from talloc_free() of the owning
162 * tevent_context we must ensure we also remove the
163 * linked glue object from the list inside
164 * struct pthreadpool_tevent.
166 static int pthreadpool_tevent_glue_link_destructor(
167 struct pthreadpool_tevent_glue_ev_link
*ev_link
)
169 TALLOC_FREE(ev_link
->glue
);
173 static int pthreadpool_tevent_register_ev(struct pthreadpool_tevent
*pool
,
174 struct tevent_context
*ev
)
176 struct pthreadpool_tevent_glue
*glue
= NULL
;
177 struct pthreadpool_tevent_glue_ev_link
*ev_link
= NULL
;
180 * See if this tevent_context was already registered by
181 * searching the glue object list. If so we have nothing
182 * to do here - we already have a tevent_context/tevent_threaded_context
185 for (glue
= pool
->glue_list
; glue
!= NULL
; glue
= glue
->next
) {
186 if (glue
->ev
== ev
) {
192 * Event context not yet registered - create a new glue
193 * object containing a tevent_context/tevent_threaded_context
194 * pair and put it on the list to remember this registration.
195 * We also need a link object to ensure the event context
196 * can't go away without us knowing about it.
198 glue
= talloc_zero(pool
, struct pthreadpool_tevent_glue
);
202 *glue
= (struct pthreadpool_tevent_glue
) {
206 talloc_set_destructor(glue
, pthreadpool_tevent_glue_destructor
);
209 * Now allocate the link object to the event context. Note this
210 * is allocated OFF THE EVENT CONTEXT ITSELF, so if the event
211 * context is freed we are able to cleanup the glue object
212 * in the link object destructor.
215 ev_link
= talloc_zero(ev
, struct pthreadpool_tevent_glue_ev_link
);
216 if (ev_link
== NULL
) {
220 ev_link
->glue
= glue
;
221 talloc_set_destructor(ev_link
, pthreadpool_tevent_glue_link_destructor
);
223 glue
->ev_link
= ev_link
;
226 glue
->tctx
= tevent_threaded_context_create(pool
, ev
);
227 if (glue
->tctx
== NULL
) {
228 TALLOC_FREE(ev_link
);
234 DLIST_ADD(pool
->glue_list
, glue
);
238 static void pthreadpool_tevent_job_fn(void *private_data
);
239 static void pthreadpool_tevent_job_done(struct tevent_context
*ctx
,
240 struct tevent_immediate
*im
,
243 static int pthreadpool_tevent_job_state_destructor(
244 struct pthreadpool_tevent_job_state
*state
)
246 if (state
->pool
== NULL
) {
251 * We should never be called with state->req == NULL,
252 * state->pool must be cleared before the 2nd talloc_free().
254 if (state
->req
== NULL
) {
259 * We need to reparent to a long term context.
261 (void)talloc_reparent(state
->req
, NULL
, state
);
266 struct tevent_req
*pthreadpool_tevent_job_send(
267 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
268 struct pthreadpool_tevent
*pool
,
269 void (*fn
)(void *private_data
), void *private_data
)
271 struct tevent_req
*req
;
272 struct pthreadpool_tevent_job_state
*state
;
275 req
= tevent_req_create(mem_ctx
, &state
,
276 struct pthreadpool_tevent_job_state
);
284 state
->private_data
= private_data
;
286 state
->im
= tevent_create_immediate(state
);
287 if (tevent_req_nomem(state
->im
, req
)) {
288 return tevent_req_post(req
, ev
);
291 ret
= pthreadpool_tevent_register_ev(pool
, ev
);
293 tevent_req_error(req
, errno
);
294 return tevent_req_post(req
, ev
);
297 ret
= pthreadpool_add_job(pool
->pool
, 0,
298 pthreadpool_tevent_job_fn
,
300 if (tevent_req_error(req
, ret
)) {
301 return tevent_req_post(req
, ev
);
305 * Once the job is scheduled, we need to protect
308 talloc_set_destructor(state
, pthreadpool_tevent_job_state_destructor
);
310 DLIST_ADD_END(pool
->jobs
, state
);
315 static void pthreadpool_tevent_job_fn(void *private_data
)
317 struct pthreadpool_tevent_job_state
*state
= talloc_get_type_abort(
318 private_data
, struct pthreadpool_tevent_job_state
);
319 state
->fn(state
->private_data
);
322 static int pthreadpool_tevent_job_signal(int jobid
,
323 void (*job_fn
)(void *private_data
),
324 void *job_private_data
,
327 struct pthreadpool_tevent_job_state
*state
= talloc_get_type_abort(
328 job_private_data
, struct pthreadpool_tevent_job_state
);
329 struct tevent_threaded_context
*tctx
= NULL
;
330 struct pthreadpool_tevent_glue
*g
= NULL
;
332 if (state
->pool
== NULL
) {
333 /* The pthreadpool_tevent is already gone */
338 for (g
= state
->pool
->glue_list
; g
!= NULL
; g
= g
->next
) {
339 if (g
->ev
== state
->ev
) {
351 /* with HAVE_PTHREAD */
352 tevent_threaded_schedule_immediate(tctx
, state
->im
,
353 pthreadpool_tevent_job_done
,
356 /* without HAVE_PTHREAD */
357 tevent_schedule_immediate(state
->im
, state
->ev
,
358 pthreadpool_tevent_job_done
,
365 static void pthreadpool_tevent_job_done(struct tevent_context
*ctx
,
366 struct tevent_immediate
*im
,
369 struct pthreadpool_tevent_job_state
*state
= talloc_get_type_abort(
370 private_data
, struct pthreadpool_tevent_job_state
);
372 if (state
->pool
!= NULL
) {
373 DLIST_REMOVE(state
->pool
->jobs
, state
);
377 if (state
->req
== NULL
) {
379 * There was a talloc_free() state->req
380 * while the job was pending,
381 * which mean we're reparented on a longterm
384 * We just cleanup here...
390 tevent_req_done(state
->req
);
393 int pthreadpool_tevent_job_recv(struct tevent_req
*req
)
395 return tevent_req_simple_recv_unix(req
);