Commit f2f32ee7 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

win32: make thread killed flag atomic

Setting the flag in APC had the benefit of not needing atomicity, but
it meant the flag only got set at the next opportunity to run APC's.

Especially vlc_testcancel() is not an alertable function, so it would
typically be slower. If the thread did not go to alertable sleep, then
vlc_testcancel() would not work at all.

Since vlc_cancel() and vlc_testcancel() do not imply any memory
barriers, the loads and stores can be relaxed. That removes most if not
all of the overhead of the atomic operations.
parent 9503a2c7
......@@ -53,11 +53,7 @@ struct vlc_thread
bool killable;
bool killed;
atomic_bool killed;
vlc_cleanup_t *cleaners;
void *(*entry) (void *);
......@@ -478,11 +474,7 @@ static int vlc_clone_attr (vlc_thread_t *p_handle, bool detached,
th->entry = entry;
th->data = data;
th->killable = false; /* not until vlc_entry() ! */
th->killed = false;
atomic_init(&th->killed, false);
th->cleaners = NULL;
/* When using the MSVCRT C library you have to use the _beginthreadex
......@@ -565,19 +557,15 @@ int vlc_set_priority (vlc_thread_t th, int priority)
/* APC procedure for thread cancellation */
static void CALLBACK vlc_cancel_self (ULONG_PTR self)
struct vlc_thread *th = (void *)self;
if (likely(th != NULL))
th->killed = true;
(void) self;
void vlc_cancel (vlc_thread_t th)
atomic_store_explicit(&th->killed, true, memory_order_relaxed);
QueueUserAPC (vlc_cancel_self, th->id, (uintptr_t)th);
atomic_store (&th->killed, true);
......@@ -611,13 +599,8 @@ void vlc_testcancel (void)
return; /* Main thread - cannot be cancelled anyway */
if (!th->killable)
if (likely(!th->killed))
if (!atomic_load(&th->killed))
if (!atomic_load_explicit(&th->killed, memory_order_relaxed))
th->killable = true; /* Do not re-enter cancellation cleanup */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment