Re: [PATCH 3/4] run-command: allow aborting async code prematurely
- From
Erik Faye-Lund <kusmabite@gmail.com>
- Date
- Apr 1, 2011, 09:36 UTC
- Message-ID
- <AANLkTinAQiuYjs+pxBVKM2-aQNSDd_-CC_fMahasTB=V@mail.gmail.com>
- In-Reply-To
- <20110331184421.GC16906@sigill.intra.peff.net>
On Thu, Mar 31, 2011 at 8:44 PM, Jeff King <peff@peff.net> wrote:
Show 27 quoted lines
> We provide only two abstract promitives for async code:
> start and finish. Where "finish" means to wait until the
> async code tells us it is done. However, it may also be
> useful for us to to tell the async code to abort right away,
> because whatever it was doing is no longer useful.
>
> For a separate process, we just kill() it. For Windows, we
> need to do whatever the equivalent to pthread_cancel is.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
> run-command.c | 10 ++++++++++
> run-command.h | 1 +
> 2 files changed, 11 insertions(+), 0 deletions(-)
>
> diff --git a/run-command.c b/run-command.c
> index 258c880..f179d2a 100644
> --- a/run-command.c
> +++ b/run-command.c
> @@ -439,6 +439,16 @@ int finish_async(struct async *async)
> return ret;
> }
>
> +void abort_async(struct async *async)
> +{
> +#ifndef WIN32
> + kill(async->pid, 15);This doesn't compile unless NO_PTHREADS is set, no?
Show 6 quoted lines
> +#else > + /* no clue */ > +#endif > + finish_async(async); > +} > +
This should probably be
void abort_async(struct async *async)
{
#ifdef NO_PTHREADS
kill(async->pid, 15);
#else
pthread_cancel(async->tid)
#endif
finish_async(async);
}... and then us Windows-guys must implement something like pthread_cancel().
Or maybe not. Can pthread reliably cancel a thread while making sure that thread isn't holding a mutex (like some thread-safe malloc implementations do)? If not, I'm not entirely sure we can even reach this goal.