From: Stefan Metzmacher Date: Wed, 25 Apr 2018 12:43:22 +0000 (+0200) Subject: pthreadpool: add pthreadpool_tevent_job_cancel() X-Git-Tag: ldb-1.5.0~321 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=791c05144ee9296024cc0fdebe4afeaaf67e26bc;p=thirdparty%2Fsamba.git pthreadpool: add pthreadpool_tevent_job_cancel() Signed-off-by: Stefan Metzmacher Reviewed-by: Ralph Boehme --- diff --git a/lib/pthreadpool/pthreadpool_tevent.c b/lib/pthreadpool/pthreadpool_tevent.c index 7c8015d2f59..bfd178c09a6 100644 --- a/lib/pthreadpool/pthreadpool_tevent.c +++ b/lib/pthreadpool/pthreadpool_tevent.c @@ -267,6 +267,7 @@ static void pthreadpool_tevent_job_fn(void *private_data); static void pthreadpool_tevent_job_done(struct tevent_context *ctx, struct tevent_immediate *im, void *private_data); +static bool pthreadpool_tevent_job_cancel(struct tevent_req *req); static int pthreadpool_tevent_job_destructor(struct pthreadpool_tevent_job *job) { @@ -441,6 +442,7 @@ struct tevent_req *pthreadpool_tevent_job_send( return tevent_req_post(req, ev); } + tevent_req_set_cancel_fn(req, pthreadpool_tevent_job_cancel); return req; } @@ -522,6 +524,44 @@ static void pthreadpool_tevent_job_done(struct tevent_context *ctx, tevent_req_done(state->req); } +static bool pthreadpool_tevent_job_cancel(struct tevent_req *req) +{ + struct pthreadpool_tevent_job_state *state = + tevent_req_data(req, + struct pthreadpool_tevent_job_state); + struct pthreadpool_tevent_job *job = state->job; + size_t num; + + if (job == NULL) { + return false; + } + + num = pthreadpool_cancel_job(job->pool->pool, 0, + pthreadpool_tevent_job_fn, + job); + if (num == 0) { + /* + * It was too late to cancel the request. + */ + return false; + } + + /* + * It was not too late to cancel the request. + * + * We can remove job->im, as it will never be used. + */ + TALLOC_FREE(job->im); + + /* + * pthreadpool_tevent_job_cleanup() + * will destroy the job. + */ + tevent_req_defer_callback(req, state->ev); + tevent_req_error(req, ECANCELED); + return true; +} + int pthreadpool_tevent_job_recv(struct tevent_req *req) { return tevent_req_simple_recv_unix(req);