libuv/include/uv/threadpool.h
Sam Schweigel 6d0459bccf unix: Add the ability to cancel thread pool work with a signal
This commit adds a new uv__work_kind, UV__WORK_FAST_IO_CANCELLABLE,
which allows us to uv_cancel work in the thread pool that has already
started executing.  It does this by signaling the thread with
pthread_kill, using a signal configurable with the UV_LOOP_CANCEL_SIGNAL
option.  Since we need to install a no-op signal handler, this signal
must be owned by libuv.  This could be relaxed, provided we document
that the signal must not have SA_RESTART set, and running the signal
handler on libuv worker threads must be harmless.

The uv__work struct gets two new fields on Unix: the thread that is
currently performing the work, and an atomic state field.  If the work
is not cancellable, it has state UV__WORK_BUSY the entire time.

If the work is cancellable, it is inserted into the work queue with
state UV__WORK_CANCELLABLE.  When we want to cancel the work, we first
check if the work is still in the queue (holding the queue lock), and if
it is not, we atomically check for UV__WORK_CANCELLABLE and swap in
UV__WORK_CANCEL_PENDING.  If that check succeeded, we signal the thread
with pthread_kill (the signal handler does nothing; we need only kick
the thread out of an interruptable syscall with EINTR).

In the worker thread, we check for UV__WORK_CANCEL_PENDING before and
after the syscall, setting it to UV__WORK_CANCELLED if we avoided
running the syscall, or if we got EINTR as the return value.  Since
there is a race between checking the flag and entering the syscall, we
prevent deadlocks by spinning on pthread_kill in uv__work_cancel, with
exponential backoff.  We wait a maximum of 1024 ms before returning
UV_EBUSY.
2026-01-02 17:18:05 -08:00

50 lines
1.7 KiB
C

/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/*
* This file is private to libuv. It provides common functionality to both
* Windows and Unix backends.
*/
#ifndef UV_THREADPOOL_H_
#define UV_THREADPOOL_H_
enum {
UV__WORK_BUSY = 0,
UV__WORK_CANCELLABLE,
UV__WORK_CANCEL_PENDING,
UV__WORK_CANCELLED,
UV__WORK_DONE
};
struct uv__work {
void (*work)(struct uv__work *w);
void (*done)(struct uv__work *w, int status);
struct uv_loop_s* loop;
struct uv__queue wq;
#ifndef _WIN32
pthread_t thread;
_Atomic char state;
#endif
};
#endif /* UV_THREADPOOL_H_ */