git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Rose via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Sixt <j6t@kdbg.org>,
	Rose <83477269+AtariDreams@users.noreply.github.com>,
	Seija Kijin <doremylover123@gmail.com>
Subject: [PATCH v2] win32: fix thread usage for win32
Date: Mon, 23 Jan 2023 16:36:36 +0000	[thread overview]
Message-ID: <pull.1440.v2.git.git.1674491796648.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1440.git.git.1674334159116.gitgitgadget@gmail.com>

From: Seija Kijin <doremylover123@gmail.com>

Use _beginthreadex instead of CreateThread
since we use the Windows CRT.

Finally, check for NULL handles, not "INVALID_HANDLE,"
as _beginthreadex guarantees a valid handle in most cases

Signed-off-by: Seija Kijin <doremylover123@gmail.com>
---
    win32: fix thread usage for win32
    
    Use pthread_exit instead of async_exit.
    
    This means we do not have to deal with Windows's implementation
    requiring an unsigned exit coded despite the POSIX exit code requiring a
    signed exit code.
    
    Use _beginthreadex instead of CreateThread since we use the Windows CRT.
    
    Finally, check for NULL handles, not "INVALID_HANDLE," as _beginthreadex
    guarantees a valid handle in most cases
    
    Signed-off-by: Seija Kijin doremylover123@gmail.com

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v2
Pull-Request: https://github.com/git/git/pull/1440

Range-diff vs v1:

 1:  f5de6bfb759 ! 1:  4a2c3da9d4c win32: fix thread usage for win32
     @@ Metadata
       ## Commit message ##
          win32: fix thread usage for win32
      
     -    Use pthread_exit instead of async_exit.
     -
     -    This means we do not have
     -    to deal with Windows's implementation
     -    requiring an unsigned exit coded
     -    despite the POSIX exit code requiring
     -    a signed exit code.
     -
          Use _beginthreadex instead of CreateThread
          since we use the Windows CRT.
      
     @@ compat/winansi.c: void winansi_init(void)
       
       	/* schedule cleanup routine */
       	if (atexit(winansi_exit))
     -
     - ## run-command.c ##
     -@@ run-command.c: static void *run_thread(void *data)
     - 	return (void *)ret;
     - }
     - 
     -+int in_async(void)
     -+{
     -+	if (!main_thread_set)
     -+		return 0; /* no asyncs started yet */
     -+	return !pthread_equal(main_thread, pthread_self());
     -+}
     -+
     - static NORETURN void die_async(const char *err, va_list params)
     - {
     - 	report_fn die_message_fn = get_die_message_routine();
     -@@ run-command.c: static int async_die_is_recursing(void)
     - 	return ret != NULL;
     - }
     - 
     --int in_async(void)
     --{
     --	if (!main_thread_set)
     --		return 0; /* no asyncs started yet */
     --	return !pthread_equal(main_thread, pthread_self());
     --}
     --
     --static void NORETURN async_exit(int code)
     --{
     --	pthread_exit((void *)(intptr_t)code);
     --}
     --
     - #else
     - 
     - static struct {
     -@@ run-command.c: int in_async(void)
     - 	return process_is_async;
     - }
     - 
     --static void NORETURN async_exit(int code)
     --{
     --	exit(code);
     --}
     --
     - #endif
     - 
     - void check_pipe(int err)
     - {
     - 	if (err == EPIPE) {
     --		if (in_async())
     --			async_exit(141);
     -+		if (in_async()) {
     -+#ifdef NO_PTHREADS
     -+			exit(141);
     -+#else
     -+			pthread_exit((void *)141);
     -+#endif
     -+		}
     - 
     - 		signal(SIGPIPE, SIG_DFL);
     - 		raise(SIGPIPE);


 compat/mingw.c   | 2 +-
 compat/winansi.c | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/compat/mingw.c b/compat/mingw.c
index e433740381b..715f1c87e11 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -2291,7 +2291,7 @@ static int start_timer_thread(void)
 	timer_event = CreateEvent(NULL, FALSE, FALSE, NULL);
 	if (timer_event) {
 		timer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);
-		if (!timer_thread )
+		if (!timer_thread)
 			return errno = ENOMEM,
 				error("cannot start timer thread");
 	} else
diff --git a/compat/winansi.c b/compat/winansi.c
index 3abe8dd5a27..be65b27bd75 100644
--- a/compat/winansi.c
+++ b/compat/winansi.c
@@ -340,7 +340,7 @@ enum {
 	TEXT = 0, ESCAPE = 033, BRACKET = '['
 };
 
-static DWORD WINAPI console_thread(LPVOID unused)
+static unsigned int WINAPI console_thread(LPVOID unused)
 {
 	unsigned char buffer[BUFFER_SIZE];
 	DWORD bytes;
@@ -643,9 +643,9 @@ void winansi_init(void)
 		die_lasterr("CreateFile for named pipe failed");
 
 	/* start console spool thread on the pipe's read end */
-	hthread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);
-	if (hthread == INVALID_HANDLE_VALUE)
-		die_lasterr("CreateThread(console_thread) failed");
+	hthread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);
+	if (!hthread)
+		die_lasterr("_beginthreadex(console_thread) failed");
 
 	/* schedule cleanup routine */
 	if (atexit(winansi_exit))

base-commit: 56c8fb1e95377900ec9d53c07886022af0a5d3c2
-- 
gitgitgadget

  parent reply	other threads:[~2023-01-23 16:36 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-21 20:49 [PATCH] win32: fix thread usage for win32 Rose via GitGitGadget
2023-01-21 22:53 ` Johannes Sixt
2023-01-23 16:36 ` Rose via GitGitGadget [this message]
2023-01-23 16:46   ` [PATCH v3] " Rose via GitGitGadget
2023-01-23 16:48     ` [PATCH v4] " Rose via GitGitGadget
2023-01-23 17:43       ` Jeff Hostetler
2023-01-23 21:47         ` Johannes Sixt
2023-01-31 14:47       ` [PATCH v5] " Rose via GitGitGadget
2023-01-31 20:00         ` Johannes Sixt
2023-02-10 15:04         ` [PATCH v6] " Rose via GitGitGadget
2023-02-10 15:06           ` [PATCH v7] win32: prefer beginthreadex over CreateThread Rose via GitGitGadget

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=pull.1440.v2.git.git.1674491796648.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=83477269+AtariDreams@users.noreply.github.com \
    --cc=doremylover123@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=j6t@kdbg.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).