[PATCH] Cygwin: pthread: Fix a race issue introduced by the commit 2c5433e5da82
Bruno Haible
bruno@clisp.org
Wed May 29 11:19:59 GMT 2024
Takashi Yano wrote in cygwin-patches:
> To avoid race issues, pthread::once() uses pthread_mutex. This caused
> the handle leak which was fixed by the commit 2c5433e5da82. However,
> this fix introduced another race issue, i.e., the mutex may be used
> after it is destroyed. With this patch, do not use pthread_mutex in
> pthread::once() to avoid both issues. Instead, InterlockedExchage()
> is used.
This patch is bogus as well, because it allows one thread to return
from a pthread_once call while the other thread is currently
executing the init_routine and not yet done with it.
> + if (!InterlockedExchange (&once_control->state, 1))
> + init_routine ();
> return 0;
> }
There is no code after the init_routine () call here. This means
that other threads are not notified when the init_routine () call
is complete. Therefore this implementation *cannot* be correct.
See: Assume thread1 and thread2 call pthread_once on the same
once_control.
thread1 thread2
------- -------
enters pthread_once enters pthread_once
sets state to 1
sees that state == 1
returns from pthread_once
executes code that assumes
init_routine has completed
starts executing
init_routine
finished executing
init_routine
returns from pthread_once
Bruno
More information about the Cygwin
mailing list