git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "AZero13 via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,  AZero13 <gfunni234@gmail.com>
Subject: Re: [PATCH v2] win32: remove handling for impossible cases in win32_pthread_join
Date: Tue, 18 Nov 2025 09:02:36 -0800	[thread overview]
Message-ID: <xmqq4iqrff4j.fsf@gitster.g> (raw)
In-Reply-To: <pull.2102.v2.git.git.1763480720264.gitgitgadget@gmail.com> (AZero's message of "Tue, 18 Nov 2025 15:45:20 +0000")

"AZero13 via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Greg Funni <gfunni234@gmail.com>

Looking much better, but there still are some puzzlement left.

> WAIT_FAILED is the only real possible error here.
>
> WAIT_TIMEOUT would not be returned as the INFINITE
> is given to the call.

OK.

> WAIT_ABANDONED would be returned if the handle
> pointed to a mutex object that was not released
> by the thread that owned the mutex object before
> the owning thread terminated.

... and we know the handle we are passing to WaitForSingleObject()
is a Thread, not a Mutex, so this error condition is irrelevant?

OK.

> WAIT_IO_COMPLETION would not be returned because
> we pass FALSE so the wait is not alertable.

FALSE where?  Ah, this can be done with WaitForSingleObjectEx, but
WaitForSingleObject() is what we call here, so WAIT_IO_COMPLETION
won't be returned with or without FALSE.  Is there a reason why you
want to change the code to call Ex variant (the manual page tells us
to use it _if_ we want to enter an alertable wait state, and I am
assuming that we are not interested in doing so)?

In any case, among the four possible return values from
WaitForSingleObject(), we know WAIT_ABANDONED and WAIT_TIMEOUT will
not be relevant for this code path.

Because WAIT_OBJECT_0 is the cryptic synonym for "Success!" for this
call, WAIT_FAILED is indeed the only possible error here, just like
you said at the beginning.

I think it is easier to understand for mere-mortal readers like me,
who are not familiar with Win32 API, if we explained this change
more like:

  Subject: [PATCH] win32: simplify win32_pthread_join() error handling

  Among the four possible result WaitForSingleObject() can return,
  WAIT_TIMEOUT and WAIT_ABANDONED are not relevant in this code
  path, because we do not ask for the call to time-out, and we do
  not pass a mutex object to the call (we are passing a thread
  object).

  Simplify the code to

  - return an error without closing the handle if the call failed
    (i.e., returns WAIT_FAILED that is not zero);

  - otherwise, WAIT_OBJECT_0 (which is 0) is returned to signal a
    success.  Do exactly what the original code did in this case.

What do you think?

> Signed-off-by: Greg Funni <gfunni234@gmail.com>
>  compat/win32/pthread.c | 20 +++++++-------------
>  1 file changed, 7 insertions(+), 13 deletions(-)


> diff --git a/compat/win32/pthread.c b/compat/win32/pthread.c
> index 58980a529c..54c43b4146 100644
> --- a/compat/win32/pthread.c
> +++ b/compat/win32/pthread.c
> @@ -37,20 +37,14 @@ int pthread_create(pthread_t *thread, const void *attr UNUSED,
>  
>  int win32_pthread_join(pthread_t *thread, void **value_ptr)
>  {
> -	DWORD result = WaitForSingleObject(thread->handle, INFINITE);
> -	switch (result) {
> -	case WAIT_OBJECT_0:
> -		if (value_ptr)
> -			*value_ptr = thread->arg;
> -		CloseHandle(thread->handle);
> -		return 0;
> -	case WAIT_ABANDONED:
> -		CloseHandle(thread->handle);
> -		return EINVAL;
> -	default:

And the above is what the patch simplifies away, which is great.

> -		/* the wait failed, so do not detach */

I think this comment is worth keeping (I am assuming "do not detach"
refers to the fact that CloseHandle(thread->handle) is not called in
the error case).

> +	if (WaitForSingleObjectEx(thread->handle, INFINITE, FALSE) == WAIT_FAILED)
>  		return err_win_to_posix(GetLastError());

The change is based on out belief that WAIT_FAILED is the only
possible error from this call.  Even if our belief turns out to be
wrong, we would want to take the error code path, wouldn't we?  IOW,
I think the above should be more like

	if (WaitForSingleObject(thread->handle, INFINITE))
		/* the wait failed; do not detach */
		return err_win_to_posix(GetLastError());

i.e., if we get an error, report the error to the caller, regardless
of what kind of an error it is, even though we expect it to be a
WAIT_FAILED.  And the case this if() condition does not catch is a
successful wait (i.e., WAIT_OBJECT_0), which is handled ...

> +	if (value_ptr)
> +		*value_ptr = thread->arg;
> +
> +	CloseHandle(thread->handle);
> +	return 0;

... exactly as before.

>  }
>  
>  pthread_t pthread_self(void)
>
> base-commit: 9a2fb147f2c61d0cab52c883e7e26f5b7948e3ed

Looking good.  Thanks.

      parent reply	other threads:[~2025-11-18 17:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-18  1:00 [PATCH] win32: remove handling for impossible cases in win32_pthread_join AZero13 via GitGitGadget
2025-11-18  2:11 ` Junio C Hamano
2025-11-18 15:45 ` [PATCH v2] " AZero13 via GitGitGadget
2025-11-18 15:47   ` [PATCH v3] " AZero13 via GitGitGadget
2025-11-18 17:02   ` Junio C Hamano [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq4iqrff4j.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=gfunni234@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).