Return success, instead of ESRCH, from pthread_cancel(3) applied to the

exited but not yet joined thread.

Before, if the thread exited but was not yet joined, we returned
ESRCH.

According to IEEE Std 1003.1™-2017 recommendation in the
description of pthread_cancel(3):
  If an implementation detects use of a thread ID after the end of its
  lifetime, it is recommended that the function should fail and report
  an [ESRCH] error.
So it seems desirable to not return ESRCH until the lifetime of the
thread ID ends.  According to the section 2.9.2 Thread IDs,
  The lifetime of a thread ID ends after the thread terminates if it
  was created with the detachstate attribute set to
  PTHREAD_CREATE_DETACHED or if pthread_detach() or pthread_join()
  has been called for that thread.
In other words, lifetime for thread ID of exited but not yet joined thread
did not ended yet.

Prompted by:	cperciva
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
This commit is contained in:
Konstantin Belousov 2020-02-13 23:22:12 +00:00
parent 06ef60525f
commit 751fae1eff
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=357894

View File

@ -71,7 +71,7 @@ _thr_cancel(pthread_t pthread)
* _thr_find_thread and THR_THREAD_UNLOCK will enter and leave critical
* region automatically.
*/
if ((ret = _thr_find_thread(curthread, pthread, 0)) == 0) {
if ((ret = _thr_find_thread(curthread, pthread, 1)) == 0) {
if (!pthread->cancel_pending) {
pthread->cancel_pending = 1;
if (pthread->state != PS_DEAD)