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.