{"thread":{"id":"59134","subject":"[PATCH] win32: fix thread usage for win32","startedAt":"2023-01-21T20:49:25Z","lastAt":"2023-02-10T15:07:19Z","messageCount":11,"participants":["Rose via GitGitGadget","Johannes Sixt","Jeff Hostetler"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"470847","messageId":"pull.1440.git.git.1674334159116.gitgitgadget@gmail.com","threadId":"59134","inReplyTo":null,"subject":"[PATCH] win32: fix thread usage for win32","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-01-21T20:49:18Z","receivedAt":"2023-01-21T20:49:25Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nUse pthread_exit instead of async_exit.\n\nThis means we do not have\nto deal with Windows's implementation\nrequiring an unsigned exit coded\ndespite the POSIX exit code requiring\na signed exit code.\n\nUse _beginthreadex instead of CreateThread\nsince we use the Windows CRT.\n\nFinally, check for NULL handles, not \"INVALID_HANDLE,\"\nas _beginthreadex guarantees a valid handle in most cases\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    win32: fix thread usage for win32\n    \n    Use pthread_exit instead of async_exit.\n    \n    This means we do not have to deal with Windows's implementation\n    requiring an unsigned exit coded despite the POSIX exit code requiring a\n    signed exit code.\n    \n    Use _beginthreadex instead of CreateThread since we use the Windows CRT.\n    \n    Finally, check for NULL handles, not \"INVALID_HANDLE,\" as _beginthreadex\n    guarantees a valid handle in most cases\n    \n    Signed-off-by: Seija Kijin doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v1\nPull-Request: https://github.com/git/git/pull/1440\n\n compat/mingw.c   |  2 +-\n compat/winansi.c |  8 ++++----\n run-command.c    | 33 ++++++++++++++-------------------\n 3 files changed, 19 insertions(+), 24 deletions(-)\n\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex af397e68a1d..c41d821b382 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -2295,7 +2295,7 @@ static int start_timer_thread(void)\n \ttimer_event = CreateEvent(NULL, FALSE, FALSE, NULL);\n \tif (timer_event) {\n \t\ttimer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);\n-\t\tif (!timer_thread )\n+\t\tif (!timer_thread)\n \t\t\treturn errno = ENOMEM,\n \t\t\t\terror(\"cannot start timer thread\");\n \t} else\ndiff --git a/compat/winansi.c b/compat/winansi.c\nindex 3abe8dd5a27..be65b27bd75 100644\n--- a/compat/winansi.c\n+++ b/compat/winansi.c\n@@ -340,7 +340,7 @@ enum {\n \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n };\n \n-static DWORD WINAPI console_thread(LPVOID unused)\n+static unsigned int WINAPI console_thread(LPVOID unused)\n {\n \tunsigned char buffer[BUFFER_SIZE];\n \tDWORD bytes;\n@@ -643,9 +643,9 @@ void winansi_init(void)\n \t\tdie_lasterr(\"CreateFile for named pipe failed\");\n \n \t/* start console spool thread on the pipe's read end */\n-\ththread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n-\tif (hthread == INVALID_HANDLE_VALUE)\n-\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n+\ththread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);\n+\tif (!hthread)\n+\t\tdie_lasterr(\"_beginthreadex(console_thread) failed\");\n \n \t/* schedule cleanup routine */\n \tif (atexit(winansi_exit))\ndiff --git a/run-command.c b/run-command.c\nindex 50cc011654e..93fd0d22d4f 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -1030,6 +1030,13 @@ static void *run_thread(void *data)\n \treturn (void *)ret;\n }\n \n+int in_async(void)\n+{\n+\tif (!main_thread_set)\n+\t\treturn 0; /* no asyncs started yet */\n+\treturn !pthread_equal(main_thread, pthread_self());\n+}\n+\n static NORETURN void die_async(const char *err, va_list params)\n {\n \treport_fn die_message_fn = get_die_message_routine();\n@@ -1055,18 +1062,6 @@ static int async_die_is_recursing(void)\n \treturn ret != NULL;\n }\n \n-int in_async(void)\n-{\n-\tif (!main_thread_set)\n-\t\treturn 0; /* no asyncs started yet */\n-\treturn !pthread_equal(main_thread, pthread_self());\n-}\n-\n-static void NORETURN async_exit(int code)\n-{\n-\tpthread_exit((void *)(intptr_t)code);\n-}\n-\n #else\n \n static struct {\n@@ -1112,18 +1107,18 @@ int in_async(void)\n \treturn process_is_async;\n }\n \n-static void NORETURN async_exit(int code)\n-{\n-\texit(code);\n-}\n-\n #endif\n \n void check_pipe(int err)\n {\n \tif (err == EPIPE) {\n-\t\tif (in_async())\n-\t\t\tasync_exit(141);\n+\t\tif (in_async()) {\n+#ifdef NO_PTHREADS\n+\t\t\texit(141);\n+#else\n+\t\t\tpthread_exit((void *)141);\n+#endif\n+\t\t}\n \n \t\tsignal(SIGPIPE, SIG_DFL);\n \t\traise(SIGPIPE);\n\nbase-commit: 904d404274fef6695c78a6b055edd184b72e2f9b\n-- \ngitgitgadget\n"},{"id":"470848","messageId":"b516a0a0-6802-2a06-597b-0837a89be9da@kdbg.org","threadId":"59134","inReplyTo":"pull.1440.git.git.1674334159116.gitgitgadget@gmail.com","subject":"Re: [PATCH] win32: fix thread usage for win32","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2023-01-21T22:53:25Z","receivedAt":"2023-01-21T22:53:53Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 21.01.23 um 21:49 schrieb Rose via GitGitGadget:\n> From: Seija Kijin <doremylover123@gmail.com>\n> \n> Use pthread_exit instead of async_exit.\n> \n> This means we do not have\n> to deal with Windows's implementation\n> requiring an unsigned exit coded\n> despite the POSIX exit code requiring\n> a signed exit code.\n> \n> Use _beginthreadex instead of CreateThread\n> since we use the Windows CRT.\n> \n> Finally, check for NULL handles, not \"INVALID_HANDLE,\"\n> as _beginthreadex guarantees a valid handle in most cases\n\nThis explains *what* the patch does, but not *why*. You replace\nCreateThread() in winansi.c, but what has this to do with async_exit and\nwhy must it be changed?\n\nPlease take the time to explain this story more thoroughly.\n\n> \n> Signed-off-by: Seija Kijin <doremylover123@gmail.com>\n> ---\n>     win32: fix thread usage for win32\n>     \n>     Use pthread_exit instead of async_exit.\n>     \n>     This means we do not have to deal with Windows's implementation\n>     requiring an unsigned exit coded despite the POSIX exit code requiring a\n>     signed exit code.\n>     \n>     Use _beginthreadex instead of CreateThread since we use the Windows CRT.\n>     \n>     Finally, check for NULL handles, not \"INVALID_HANDLE,\" as _beginthreadex\n>     guarantees a valid handle in most cases\n>     \n>     Signed-off-by: Seija Kijin doremylover123@gmail.com\n> \n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v1\n> Pull-Request: https://github.com/git/git/pull/1440\n> \n>  compat/mingw.c   |  2 +-\n>  compat/winansi.c |  8 ++++----\n>  run-command.c    | 33 ++++++++++++++-------------------\n>  3 files changed, 19 insertions(+), 24 deletions(-)\n> \n> diff --git a/compat/mingw.c b/compat/mingw.c\n> index af397e68a1d..c41d821b382 100644\n> --- a/compat/mingw.c\n> +++ b/compat/mingw.c\n> @@ -2295,7 +2295,7 @@ static int start_timer_thread(void)\n>  \ttimer_event = CreateEvent(NULL, FALSE, FALSE, NULL);\n>  \tif (timer_event) {\n>  \t\ttimer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);\n> -\t\tif (!timer_thread )\n> +\t\tif (!timer_thread)\n>  \t\t\treturn errno = ENOMEM,\n>  \t\t\t\terror(\"cannot start timer thread\");\n>  \t} else\n> diff --git a/compat/winansi.c b/compat/winansi.c\n> index 3abe8dd5a27..be65b27bd75 100644\n> --- a/compat/winansi.c\n> +++ b/compat/winansi.c\n> @@ -340,7 +340,7 @@ enum {\n>  \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n>  };\n>  \n> -static DWORD WINAPI console_thread(LPVOID unused)\n> +static unsigned int WINAPI console_thread(LPVOID unused)\n>  {\n>  \tunsigned char buffer[BUFFER_SIZE];\n>  \tDWORD bytes;\n> @@ -643,9 +643,9 @@ void winansi_init(void)\n>  \t\tdie_lasterr(\"CreateFile for named pipe failed\");\n>  \n>  \t/* start console spool thread on the pipe's read end */\n> -\ththread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n> -\tif (hthread == INVALID_HANDLE_VALUE)\n> -\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n> +\ththread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);\n> +\tif (!hthread)\n> +\t\tdie_lasterr(\"_beginthreadex(console_thread) failed\");\n>  \n>  \t/* schedule cleanup routine */\n>  \tif (atexit(winansi_exit))\n> diff --git a/run-command.c b/run-command.c\n> index 50cc011654e..93fd0d22d4f 100644\n> --- a/run-command.c\n> +++ b/run-command.c\n> @@ -1030,6 +1030,13 @@ static void *run_thread(void *data)\n>  \treturn (void *)ret;\n>  }\n>  \n> +int in_async(void)\n> +{\n> +\tif (!main_thread_set)\n> +\t\treturn 0; /* no asyncs started yet */\n> +\treturn !pthread_equal(main_thread, pthread_self());\n> +}\n> +\n>  static NORETURN void die_async(const char *err, va_list params)\n>  {\n>  \treport_fn die_message_fn = get_die_message_routine();\n> @@ -1055,18 +1062,6 @@ static int async_die_is_recursing(void)\n>  \treturn ret != NULL;\n>  }\n>  \n> -int in_async(void)\n> -{\n> -\tif (!main_thread_set)\n> -\t\treturn 0; /* no asyncs started yet */\n> -\treturn !pthread_equal(main_thread, pthread_self());\n> -}\n> -\n> -static void NORETURN async_exit(int code)\n> -{\n> -\tpthread_exit((void *)(intptr_t)code);\n> -}\n> -\n>  #else\n>  \n>  static struct {\n> @@ -1112,18 +1107,18 @@ int in_async(void)\n>  \treturn process_is_async;\n>  }\n>  \n> -static void NORETURN async_exit(int code)\n> -{\n> -\texit(code);\n> -}\n> -\n>  #endif\n>  \n>  void check_pipe(int err)\n>  {\n>  \tif (err == EPIPE) {\n> -\t\tif (in_async())\n> -\t\t\tasync_exit(141);\n> +\t\tif (in_async()) {\n> +#ifdef NO_PTHREADS\n> +\t\t\texit(141);\n> +#else\n> +\t\t\tpthread_exit((void *)141);\n> +#endif\n> +\t\t}\n>  \n>  \t\tsignal(SIGPIPE, SIG_DFL);\n>  \t\traise(SIGPIPE);\n> \n> base-commit: 904d404274fef6695c78a6b055edd184b72e2f9b\n\n"},{"id":"470929","messageId":"pull.1440.v2.git.git.1674491796648.gitgitgadget@gmail.com","threadId":"59134","inReplyTo":"pull.1440.git.git.1674334159116.gitgitgadget@gmail.com","subject":"[PATCH v2] win32: fix thread usage for win32","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-01-23T16:36:36Z","receivedAt":"2023-01-23T16:36:46Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nUse _beginthreadex instead of CreateThread\nsince we use the Windows CRT.\n\nFinally, check for NULL handles, not \"INVALID_HANDLE,\"\nas _beginthreadex guarantees a valid handle in most cases\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    win32: fix thread usage for win32\n    \n    Use pthread_exit instead of async_exit.\n    \n    This means we do not have to deal with Windows's implementation\n    requiring an unsigned exit coded despite the POSIX exit code requiring a\n    signed exit code.\n    \n    Use _beginthreadex instead of CreateThread since we use the Windows CRT.\n    \n    Finally, check for NULL handles, not \"INVALID_HANDLE,\" as _beginthreadex\n    guarantees a valid handle in most cases\n    \n    Signed-off-by: Seija Kijin doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v2\nPull-Request: https://github.com/git/git/pull/1440\n\nRange-diff vs v1:\n\n 1:  f5de6bfb759 ! 1:  4a2c3da9d4c win32: fix thread usage for win32\n     @@ Metadata\n       ## Commit message ##\n          win32: fix thread usage for win32\n      \n     -    Use pthread_exit instead of async_exit.\n     -\n     -    This means we do not have\n     -    to deal with Windows's implementation\n     -    requiring an unsigned exit coded\n     -    despite the POSIX exit code requiring\n     -    a signed exit code.\n     -\n          Use _beginthreadex instead of CreateThread\n          since we use the Windows CRT.\n      \n     @@ compat/winansi.c: void winansi_init(void)\n       \n       \t/* schedule cleanup routine */\n       \tif (atexit(winansi_exit))\n     -\n     - ## run-command.c ##\n     -@@ run-command.c: static void *run_thread(void *data)\n     - \treturn (void *)ret;\n     - }\n     - \n     -+int in_async(void)\n     -+{\n     -+\tif (!main_thread_set)\n     -+\t\treturn 0; /* no asyncs started yet */\n     -+\treturn !pthread_equal(main_thread, pthread_self());\n     -+}\n     -+\n     - static NORETURN void die_async(const char *err, va_list params)\n     - {\n     - \treport_fn die_message_fn = get_die_message_routine();\n     -@@ run-command.c: static int async_die_is_recursing(void)\n     - \treturn ret != NULL;\n     - }\n     - \n     --int in_async(void)\n     --{\n     --\tif (!main_thread_set)\n     --\t\treturn 0; /* no asyncs started yet */\n     --\treturn !pthread_equal(main_thread, pthread_self());\n     --}\n     --\n     --static void NORETURN async_exit(int code)\n     --{\n     --\tpthread_exit((void *)(intptr_t)code);\n     --}\n     --\n     - #else\n     - \n     - static struct {\n     -@@ run-command.c: int in_async(void)\n     - \treturn process_is_async;\n     - }\n     - \n     --static void NORETURN async_exit(int code)\n     --{\n     --\texit(code);\n     --}\n     --\n     - #endif\n     - \n     - void check_pipe(int err)\n     - {\n     - \tif (err == EPIPE) {\n     --\t\tif (in_async())\n     --\t\t\tasync_exit(141);\n     -+\t\tif (in_async()) {\n     -+#ifdef NO_PTHREADS\n     -+\t\t\texit(141);\n     -+#else\n     -+\t\t\tpthread_exit((void *)141);\n     -+#endif\n     -+\t\t}\n     - \n     - \t\tsignal(SIGPIPE, SIG_DFL);\n     - \t\traise(SIGPIPE);\n\n\n compat/mingw.c   | 2 +-\n compat/winansi.c | 8 ++++----\n 2 files changed, 5 insertions(+), 5 deletions(-)\n\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex e433740381b..715f1c87e11 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -2291,7 +2291,7 @@ static int start_timer_thread(void)\n \ttimer_event = CreateEvent(NULL, FALSE, FALSE, NULL);\n \tif (timer_event) {\n \t\ttimer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);\n-\t\tif (!timer_thread )\n+\t\tif (!timer_thread)\n \t\t\treturn errno = ENOMEM,\n \t\t\t\terror(\"cannot start timer thread\");\n \t} else\ndiff --git a/compat/winansi.c b/compat/winansi.c\nindex 3abe8dd5a27..be65b27bd75 100644\n--- a/compat/winansi.c\n+++ b/compat/winansi.c\n@@ -340,7 +340,7 @@ enum {\n \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n };\n \n-static DWORD WINAPI console_thread(LPVOID unused)\n+static unsigned int WINAPI console_thread(LPVOID unused)\n {\n \tunsigned char buffer[BUFFER_SIZE];\n \tDWORD bytes;\n@@ -643,9 +643,9 @@ void winansi_init(void)\n \t\tdie_lasterr(\"CreateFile for named pipe failed\");\n \n \t/* start console spool thread on the pipe's read end */\n-\ththread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n-\tif (hthread == INVALID_HANDLE_VALUE)\n-\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n+\ththread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);\n+\tif (!hthread)\n+\t\tdie_lasterr(\"_beginthreadex(console_thread) failed\");\n \n \t/* schedule cleanup routine */\n \tif (atexit(winansi_exit))\n\nbase-commit: 56c8fb1e95377900ec9d53c07886022af0a5d3c2\n-- \ngitgitgadget\n"},{"id":"470930","messageId":"pull.1440.v3.git.git.1674492373925.gitgitgadget@gmail.com","threadId":"59134","inReplyTo":"pull.1440.v2.git.git.1674491796648.gitgitgadget@gmail.com","subject":"[PATCH v3] win32: fix thread usage for win32","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-01-23T16:46:13Z","receivedAt":"2023-01-23T16:46:27Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nUse _beginthreadex instead of CreateThread\nsince we use the Windows CRT,\nas Microsoft recommends _beginthreadex\nover CreateThread for these situations.\n\nFinally, check for NULL handles, not \"INVALID_HANDLE,\"\nas _beginthreadex guarantees a valid handle in most cases\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    win32: fix thread usage for win32\n    \n    Use pthread_exit instead of async_exit.\n    \n    This means we do not have to deal with Windows's implementation\n    requiring an unsigned exit coded despite the POSIX exit code requiring a\n    signed exit code.\n    \n    Use _beginthreadex instead of CreateThread since we use the Windows CRT.\n    \n    Finally, check for NULL handles, not \"INVALID_HANDLE,\" as _beginthreadex\n    guarantees a valid handle in most cases\n    \n    Signed-off-by: Seija Kijin doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v3\nPull-Request: https://github.com/git/git/pull/1440\n\nRange-diff vs v2:\n\n 1:  4a2c3da9d4c ! 1:  68baafba2bd win32: fix thread usage for win32\n     @@ Commit message\n          win32: fix thread usage for win32\n      \n          Use _beginthreadex instead of CreateThread\n     -    since we use the Windows CRT.\n     +    since we use the Windows CRT,\n     +    as Microsoft recommends _beginthreadex\n     +    over CreateThread for these situations.\n      \n          Finally, check for NULL handles, not \"INVALID_HANDLE,\"\n          as _beginthreadex guarantees a valid handle in most cases\n\n\n compat/mingw.c   | 2 +-\n compat/winansi.c | 8 ++++----\n 2 files changed, 5 insertions(+), 5 deletions(-)\n\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex e433740381b..715f1c87e11 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -2291,7 +2291,7 @@ static int start_timer_thread(void)\n \ttimer_event = CreateEvent(NULL, FALSE, FALSE, NULL);\n \tif (timer_event) {\n \t\ttimer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);\n-\t\tif (!timer_thread )\n+\t\tif (!timer_thread)\n \t\t\treturn errno = ENOMEM,\n \t\t\t\terror(\"cannot start timer thread\");\n \t} else\ndiff --git a/compat/winansi.c b/compat/winansi.c\nindex 3abe8dd5a27..be65b27bd75 100644\n--- a/compat/winansi.c\n+++ b/compat/winansi.c\n@@ -340,7 +340,7 @@ enum {\n \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n };\n \n-static DWORD WINAPI console_thread(LPVOID unused)\n+static unsigned int WINAPI console_thread(LPVOID unused)\n {\n \tunsigned char buffer[BUFFER_SIZE];\n \tDWORD bytes;\n@@ -643,9 +643,9 @@ void winansi_init(void)\n \t\tdie_lasterr(\"CreateFile for named pipe failed\");\n \n \t/* start console spool thread on the pipe's read end */\n-\ththread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n-\tif (hthread == INVALID_HANDLE_VALUE)\n-\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n+\ththread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);\n+\tif (!hthread)\n+\t\tdie_lasterr(\"_beginthreadex(console_thread) failed\");\n \n \t/* schedule cleanup routine */\n \tif (atexit(winansi_exit))\n\nbase-commit: 56c8fb1e95377900ec9d53c07886022af0a5d3c2\n-- \ngitgitgadget\n"},{"id":"470931","messageId":"pull.1440.v4.git.git.1674492499537.gitgitgadget@gmail.com","threadId":"59134","inReplyTo":"pull.1440.v3.git.git.1674492373925.gitgitgadget@gmail.com","subject":"[PATCH v4] win32: fix thread usage for win32","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-01-23T16:48:19Z","receivedAt":"2023-01-23T16:48:27Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nUse _beginthreadex instead of CreateThread\nsince we use the Windows CRT,\nas Microsoft recommends _beginthreadex\nover CreateThread for these situations.\n\nFinally, check for NULL handles, not \"INVALID_HANDLE,\"\nas _beginthreadex guarantees a valid handle in most cases\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    win32: fix thread usage for win32\n    \n    Use pthread_exit instead of async_exit.\n    \n    This means we do not have to deal with Windows's implementation\n    requiring an unsigned exit coded despite the POSIX exit code requiring a\n    signed exit code.\n    \n    Use _beginthreadex instead of CreateThread since we use the Windows CRT.\n    \n    Finally, check for NULL handles, not \"INVALID_HANDLE,\" as _beginthreadex\n    guarantees a valid handle in most cases\n    \n    Signed-off-by: Seija Kijin doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v4\nPull-Request: https://github.com/git/git/pull/1440\n\nRange-diff vs v3:\n\n 1:  68baafba2bd ! 1:  2e2d5ce7745 win32: fix thread usage for win32\n     @@ Commit message\n      \n          Signed-off-by: Seija Kijin <doremylover123@gmail.com>\n      \n     - ## compat/mingw.c ##\n     -@@ compat/mingw.c: static int start_timer_thread(void)\n     - \ttimer_event = CreateEvent(NULL, FALSE, FALSE, NULL);\n     - \tif (timer_event) {\n     - \t\ttimer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);\n     --\t\tif (!timer_thread )\n     -+\t\tif (!timer_thread)\n     - \t\t\treturn errno = ENOMEM,\n     - \t\t\t\terror(\"cannot start timer thread\");\n     - \t} else\n     -\n       ## compat/winansi.c ##\n      @@ compat/winansi.c: enum {\n       \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n\n\n compat/winansi.c | 8 ++++----\n 1 file changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/compat/winansi.c b/compat/winansi.c\nindex 3abe8dd5a27..be65b27bd75 100644\n--- a/compat/winansi.c\n+++ b/compat/winansi.c\n@@ -340,7 +340,7 @@ enum {\n \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n };\n \n-static DWORD WINAPI console_thread(LPVOID unused)\n+static unsigned int WINAPI console_thread(LPVOID unused)\n {\n \tunsigned char buffer[BUFFER_SIZE];\n \tDWORD bytes;\n@@ -643,9 +643,9 @@ void winansi_init(void)\n \t\tdie_lasterr(\"CreateFile for named pipe failed\");\n \n \t/* start console spool thread on the pipe's read end */\n-\ththread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n-\tif (hthread == INVALID_HANDLE_VALUE)\n-\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n+\ththread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);\n+\tif (!hthread)\n+\t\tdie_lasterr(\"_beginthreadex(console_thread) failed\");\n \n \t/* schedule cleanup routine */\n \tif (atexit(winansi_exit))\n\nbase-commit: 56c8fb1e95377900ec9d53c07886022af0a5d3c2\n-- \ngitgitgadget\n"},{"id":"470941","messageId":"9e75f76b-081c-c763-0fae-edd6d97fbc88@jeffhostetler.com","threadId":"59134","inReplyTo":"pull.1440.v4.git.git.1674492499537.gitgitgadget@gmail.com","subject":"Re: [PATCH v4] win32: fix thread usage for win32","fromName":"Jeff Hostetler","fromEmail":"git@jeffhostetler.com","sentAt":"2023-01-23T17:43:53Z","receivedAt":"2023-01-23T17:44:08Z","isPatch":true,"sender":{"key":"git@jeffhostetler.com","avatar":null},"body":"\n\nOn 1/23/23 11:48 AM, Rose via GitGitGadget wrote:\n> From: Seija Kijin <doremylover123@gmail.com>\n> \n> Use _beginthreadex instead of CreateThread\n> since we use the Windows CRT,\n> as Microsoft recommends _beginthreadex\n> over CreateThread for these situations.\n> \n> Finally, check for NULL handles, not \"INVALID_HANDLE,\"\n> as _beginthreadex guarantees a valid handle in most cases\n> \n> Signed-off-by: Seija Kijin <doremylover123@gmail.com>\n> ---\n>      win32: fix thread usage for win32\n>      \n>      Use pthread_exit instead of async_exit.\n>      \n>      This means we do not have to deal with Windows's implementation\n>      requiring an unsigned exit coded despite the POSIX exit code requiring a\n>      signed exit code.\n>      \n>      Use _beginthreadex instead of CreateThread since we use the Windows CRT.\n>      \n>      Finally, check for NULL handles, not \"INVALID_HANDLE,\" as _beginthreadex\n>      guarantees a valid handle in most cases\n>      \n>      Signed-off-by: Seija Kijin doremylover123@gmail.com\n> \n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v4\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v4\n> Pull-Request: https://github.com/git/git/pull/1440\n> \n> Range-diff vs v3:\n> \n>   1:  68baafba2bd ! 1:  2e2d5ce7745 win32: fix thread usage for win32\n>       @@ Commit message\n>        \n>            Signed-off-by: Seija Kijin <doremylover123@gmail.com>\n>        \n>       - ## compat/mingw.c ##\n>       -@@ compat/mingw.c: static int start_timer_thread(void)\n>       - \ttimer_event = CreateEvent(NULL, FALSE, FALSE, NULL);\n>       - \tif (timer_event) {\n>       - \t\ttimer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);\n>       --\t\tif (!timer_thread )\n>       -+\t\tif (!timer_thread)\n>       - \t\t\treturn errno = ENOMEM,\n>       - \t\t\t\terror(\"cannot start timer thread\");\n>       - \t} else\n>       -\n>         ## compat/winansi.c ##\n>        @@ compat/winansi.c: enum {\n>         \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n> \n> \n>   compat/winansi.c | 8 ++++----\n>   1 file changed, 4 insertions(+), 4 deletions(-)\n> \n> diff --git a/compat/winansi.c b/compat/winansi.c\n> index 3abe8dd5a27..be65b27bd75 100644\n> --- a/compat/winansi.c\n> +++ b/compat/winansi.c\n> @@ -340,7 +340,7 @@ enum {\n>   \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n>   };\n>   \n> -static DWORD WINAPI console_thread(LPVOID unused)\n> +static unsigned int WINAPI console_thread(LPVOID unused)\n>   {\n>   \tunsigned char buffer[BUFFER_SIZE];\n>   \tDWORD bytes;\n> @@ -643,9 +643,9 @@ void winansi_init(void)\n>   \t\tdie_lasterr(\"CreateFile for named pipe failed\");\n>   \n>   \t/* start console spool thread on the pipe's read end */\n> -\ththread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n> -\tif (hthread == INVALID_HANDLE_VALUE)\n> -\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n> +\ththread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);\n> +\tif (!hthread)\n> +\t\tdie_lasterr(\"_beginthreadex(console_thread) failed\");\n>   \n>   \t/* schedule cleanup routine */\n>   \tif (atexit(winansi_exit))\n> \n> base-commit: 56c8fb1e95377900ec9d53c07886022af0a5d3c2\n\nThis change may or may not be harmless, but it scares me\nbecause it is possibly a very subtle change and is being\nmade for an unknown reason -- is there a problem being\nfixed here?  Or is this just churn for the sake of churn\nto avoid an awkward cast of the return code?\n\nWhat does _beginthreadex() specifically do that we need\nit to do for us?\n\n_beginthreadex() does some CRT init and then calls CreateThread(),\nso what are we missing by calling CreateThread() directly?\n\nThe code in question is 11+ years old and it hasn't been a\nproblem (right?), so I have to wonder what value do we get\nfrom this change.\n\nThe containing function here is setting up a special console\nthread and named pipe to access the console, so I doubt that\nany of the tests in the test suite actually would actually\nexercise this change (since the tests aren't interactive).\n\nThe low-level Windows startup code is very tricky and sensitive\n(and we need to test with both GCC's CRT and MSVC's CRT).\nAs I said earlier, the change may or may not be harmless, but\nI question the need for it.\n\nJeff\n\n\n"},{"id":"470951","messageId":"3dfc2925-0fc3-7c94-f579-331c1b9196b2@kdbg.org","threadId":"59134","inReplyTo":"9e75f76b-081c-c763-0fae-edd6d97fbc88@jeffhostetler.com","subject":"Re: [PATCH v4] win32: fix thread usage for win32","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2023-01-23T21:47:16Z","receivedAt":"2023-01-23T21:47:24Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 23.01.23 um 18:43 schrieb Jeff Hostetler:\n> \n> \n> On 1/23/23 11:48 AM, Rose via GitGitGadget wrote:\n>> From: Seija Kijin <doremylover123@gmail.com>\n>>\n>> Use _beginthreadex instead of CreateThread\n>> since we use the Windows CRT,\n>> as Microsoft recommends _beginthreadex\n>> over CreateThread for these situations.\n>>\n>> Finally, check for NULL handles, not \"INVALID_HANDLE,\"\n>> as _beginthreadex guarantees a valid handle in most cases\n>>\n>> Signed-off-by: Seija Kijin <doremylover123@gmail.com>\n>> ---\n>>      win32: fix thread usage for win32\n>>           Use pthread_exit instead of async_exit.\n>>           This means we do not have to deal with Windows's implementation\n>>      requiring an unsigned exit coded despite the POSIX exit code\n>> requiring a\n>>      signed exit code.\n>>           Use _beginthreadex instead of CreateThread since we use the\n>> Windows CRT.\n>>           Finally, check for NULL handles, not \"INVALID_HANDLE,\" as\n>> _beginthreadex\n>>      guarantees a valid handle in most cases\n>>           Signed-off-by: Seija Kijin doremylover123@gmail.com\n>>\n>> Published-As:\n>> https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v4\n>> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git\n>> pr-git-1440/AtariDreams/CreateThread-v4\n>> Pull-Request: https://github.com/git/git/pull/1440\n>>\n>> Range-diff vs v3:\n>>\n>>   1:  68baafba2bd ! 1:  2e2d5ce7745 win32: fix thread usage for win32\n>>       @@ Commit message\n>>                   Signed-off-by: Seija Kijin <doremylover123@gmail.com>\n>>              - ## compat/mingw.c ##\n>>       -@@ compat/mingw.c: static int start_timer_thread(void)\n>>       -     timer_event = CreateEvent(NULL, FALSE, FALSE, NULL);\n>>       -     if (timer_event) {\n>>       -         timer_thread = (HANDLE) _beginthreadex(NULL, 0,\n>> ticktack, NULL, 0, NULL);\n>>       --        if (!timer_thread )\n>>       -+        if (!timer_thread)\n>>       -             return errno = ENOMEM,\n>>       -                 error(\"cannot start timer thread\");\n>>       -     } else\n>>       -\n>>         ## compat/winansi.c ##\n>>        @@ compat/winansi.c: enum {\n>>             TEXT = 0, ESCAPE = 033, BRACKET = '['\n>>\n>>\n>>   compat/winansi.c | 8 ++++----\n>>   1 file changed, 4 insertions(+), 4 deletions(-)\n>>\n>> diff --git a/compat/winansi.c b/compat/winansi.c\n>> index 3abe8dd5a27..be65b27bd75 100644\n>> --- a/compat/winansi.c\n>> +++ b/compat/winansi.c\n>> @@ -340,7 +340,7 @@ enum {\n>>       TEXT = 0, ESCAPE = 033, BRACKET = '['\n>>   };\n>>   -static DWORD WINAPI console_thread(LPVOID unused)\n>> +static unsigned int WINAPI console_thread(LPVOID unused)\n>>   {\n>>       unsigned char buffer[BUFFER_SIZE];\n>>       DWORD bytes;\n>> @@ -643,9 +643,9 @@ void winansi_init(void)\n>>           die_lasterr(\"CreateFile for named pipe failed\");\n>>         /* start console spool thread on the pipe's read end */\n>> -    hthread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n>> -    if (hthread == INVALID_HANDLE_VALUE)\n>> -        die_lasterr(\"CreateThread(console_thread) failed\");\n>> +    hthread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL,\n>> 0, NULL);\n>> +    if (!hthread)\n>> +        die_lasterr(\"_beginthreadex(console_thread) failed\");\n>>         /* schedule cleanup routine */\n>>       if (atexit(winansi_exit))\n>>\n>> base-commit: 56c8fb1e95377900ec9d53c07886022af0a5d3c2\n> \n> This change may or may not be harmless, but it scares me\n> because it is possibly a very subtle change and is being\n> made for an unknown reason -- is there a problem being\n> fixed here?  Or is this just churn for the sake of churn\n> to avoid an awkward cast of the return code?\n> \n> What does _beginthreadex() specifically do that we need\n> it to do for us?\n> \n> _beginthreadex() does some CRT init and then calls CreateThread(),\n> so what are we missing by calling CreateThread() directly?\n\nI also question the value of this change. As long as the thread does not\ncall into any CRT functions, we do not need the services of\n_beginthreadex(). AFAICS, it only uses WinAPI functions and some\nuncritical C functions like memmove and memset. Am I missing something?\n\n> \n> The code in question is 11+ years old and it hasn't been a\n> problem (right?), so I have to wonder what value do we get\n> from this change.\n> \n> The containing function here is setting up a special console\n> thread and named pipe to access the console, so I doubt that\n> any of the tests in the test suite actually would actually\n> exercise this change (since the tests aren't interactive).\n> \n> The low-level Windows startup code is very tricky and sensitive\n> (and we need to test with both GCC's CRT and MSVC's CRT).\n> As I said earlier, the change may or may not be harmless, but\n> I question the need for it.\n> \n> Jeff\n> \n> \n\n"},{"id":"471183","messageId":"pull.1440.v5.git.git.1675176442381.gitgitgadget@gmail.com","threadId":"59134","inReplyTo":"pull.1440.v4.git.git.1674492499537.gitgitgadget@gmail.com","subject":"[PATCH v5] win32: fix thread usage for win32","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-01-31T14:47:22Z","receivedAt":"2023-01-31T14:47:30Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nUse _beginthreadex instead of CreateThread\nsince we use the Windows CRT,\nas Microsoft recommends _beginthreadex\nover CreateThread for these situations.\n\nFinally, check for NULL handles, not \"INVALID_HANDLE,\"\nas _beginthreadex guarantees a valid handle in most cases\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    win32: fix thread usage for win32\n    \n    Use pthread_exit instead of async_exit.\n    \n    This means we do not have to deal with Windows's implementation\n    requiring an unsigned exit coded despite the POSIX exit code requiring a\n    signed exit code.\n    \n    Use _beginthreadex instead of CreateThread since we use the Windows CRT.\n    \n    Finally, check for NULL handles, not \"INVALID_HANDLE,\" as _beginthreadex\n    guarantees a valid handle in most cases\n    \n    Signed-off-by: Seija Kijin doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v5\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v5\nPull-Request: https://github.com/git/git/pull/1440\n\nRange-diff vs v4:\n\n 1:  2e2d5ce7745 = 1:  6ab79d9275d win32: fix thread usage for win32\n\n\n compat/winansi.c | 8 ++++----\n 1 file changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/compat/winansi.c b/compat/winansi.c\nindex 3abe8dd5a27..be65b27bd75 100644\n--- a/compat/winansi.c\n+++ b/compat/winansi.c\n@@ -340,7 +340,7 @@ enum {\n \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n };\n \n-static DWORD WINAPI console_thread(LPVOID unused)\n+static unsigned int WINAPI console_thread(LPVOID unused)\n {\n \tunsigned char buffer[BUFFER_SIZE];\n \tDWORD bytes;\n@@ -643,9 +643,9 @@ void winansi_init(void)\n \t\tdie_lasterr(\"CreateFile for named pipe failed\");\n \n \t/* start console spool thread on the pipe's read end */\n-\ththread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n-\tif (hthread == INVALID_HANDLE_VALUE)\n-\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n+\ththread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);\n+\tif (!hthread)\n+\t\tdie_lasterr(\"_beginthreadex(console_thread) failed\");\n \n \t/* schedule cleanup routine */\n \tif (atexit(winansi_exit))\n\nbase-commit: 2fc9e9ca3c7505bc60069f11e7ef09b1aeeee473\n-- \ngitgitgadget\n"},{"id":"471200","messageId":"d2b0717c-8352-5b1a-0ca9-d68935321f9d@kdbg.org","threadId":"59134","inReplyTo":"pull.1440.v5.git.git.1675176442381.gitgitgadget@gmail.com","subject":"Re: [PATCH v5] win32: fix thread usage for win32","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2023-01-31T20:00:12Z","receivedAt":"2023-01-31T20:00:18Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 31.01.23 um 15:47 schrieb Rose via GitGitGadget:\n> Range-diff vs v4:\n> \n>  1:  2e2d5ce7745 = 1:  6ab79d9275d win32: fix thread usage for win32\n\nIf you do not agree with review comments, it would be more polite to\nexpress your thinking than to just resend a patch unchanged and silently.\n\n-- Hannes\n\n"},{"id":"471920","messageId":"pull.1440.v6.git.git.1676041473607.gitgitgadget@gmail.com","threadId":"59134","inReplyTo":"pull.1440.v5.git.git.1675176442381.gitgitgadget@gmail.com","subject":"[PATCH v6] win32: fix thread usage for win32","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-02-10T15:04:33Z","receivedAt":"2023-02-10T15:05:42Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nUse _beginthreadex instead of CreateThread\nsince we use the Windows CRT,\nas Microsoft recommends _beginthreadex\nover CreateThread for these situations.\n\nFinally, check for NULL handles, not \"INVALID_HANDLE,\"\nas _beginthreadex guarantees a valid handle in most cases\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    win32: fix thread usage for win32\n    \n    Use pthread_exit instead of async_exit.\n    \n    This means we do not have to deal with Windows's implementation\n    requiring an unsigned exit coded despite the POSIX exit code requiring a\n    signed exit code.\n    \n    Use _beginthreadex instead of CreateThread since we use the Windows CRT.\n    \n    Finally, check for NULL handles, not \"INVALID_HANDLE,\" as _beginthreadex\n    guarantees a valid handle in most cases\n    \n    Signed-off-by: Seija Kijin doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v6\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v6\nPull-Request: https://github.com/git/git/pull/1440\n\nRange-diff vs v5:\n\n 1:  6ab79d9275d ! 1:  67cef4bd8c9 win32: fix thread usage for win32\n     @@ compat/winansi.c: void winansi_init(void)\n       \n       \t/* start console spool thread on the pipe's read end */\n      -\ththread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n     --\tif (hthread == INVALID_HANDLE_VALUE)\n     --\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n      +\ththread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);\n     -+\tif (!hthread)\n     + \tif (!hthread)\n     +-\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n      +\t\tdie_lasterr(\"_beginthreadex(console_thread) failed\");\n       \n       \t/* schedule cleanup routine */\n\n\n compat/winansi.c | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/compat/winansi.c b/compat/winansi.c\nindex f83610f684d..be65b27bd75 100644\n--- a/compat/winansi.c\n+++ b/compat/winansi.c\n@@ -340,7 +340,7 @@ enum {\n \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n };\n \n-static DWORD WINAPI console_thread(LPVOID unused)\n+static unsigned int WINAPI console_thread(LPVOID unused)\n {\n \tunsigned char buffer[BUFFER_SIZE];\n \tDWORD bytes;\n@@ -643,9 +643,9 @@ void winansi_init(void)\n \t\tdie_lasterr(\"CreateFile for named pipe failed\");\n \n \t/* start console spool thread on the pipe's read end */\n-\ththread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n+\ththread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);\n \tif (!hthread)\n-\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n+\t\tdie_lasterr(\"_beginthreadex(console_thread) failed\");\n \n \t/* schedule cleanup routine */\n \tif (atexit(winansi_exit))\n\nbase-commit: 23c56f7bd5f1667f8b793d796bf30e39545920f6\n-- \ngitgitgadget\n"},{"id":"471921","messageId":"pull.1440.v7.git.git.1676041618809.gitgitgadget@gmail.com","threadId":"59134","inReplyTo":"pull.1440.v6.git.git.1676041473607.gitgitgadget@gmail.com","subject":"[PATCH v7] win32: prefer beginthreadex over CreateThread","fromName":"Rose via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2023-02-10T15:06:58Z","receivedAt":"2023-02-10T15:07:19Z","isPatch":true,"sender":{"key":"ckelsch@jgrcpa.com","avatar":null},"body":"From: Seija Kijin <doremylover123@gmail.com>\n\nUse _beginthreadex instead of CreateThread\nsince we use the Windows CRT,\nas Microsoft recommends _beginthreadex\nover CreateThread for these situations.\n\nFinally, check for NULL handles, not \"INVALID_HANDLE,\"\nas _beginthreadex guarantees a valid handle in most cases\n\nSigned-off-by: Seija Kijin <doremylover123@gmail.com>\n---\n    win32: prefer beginthreadex over CreateThread\n    \n    Use pthread_exit instead of async_exit.\n    \n    This means we do not have to deal with Windows's implementation\n    requiring an unsigned exit coded despite the POSIX exit code requiring a\n    signed exit code.\n    \n    Use _beginthreadex instead of CreateThread since we use the Windows CRT.\n    \n    Finally, check for NULL handles, not \"INVALID_HANDLE,\" as _beginthreadex\n    guarantees a valid handle in most cases\n    \n    Signed-off-by: Seija Kijin doremylover123@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1440%2FAtariDreams%2FCreateThread-v7\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1440/AtariDreams/CreateThread-v7\nPull-Request: https://github.com/git/git/pull/1440\n\nRange-diff vs v6:\n\n 1:  67cef4bd8c9 ! 1:  edee5e78c67 win32: fix thread usage for win32\n     @@ Metadata\n      Author: Seija Kijin <doremylover123@gmail.com>\n      \n       ## Commit message ##\n     -    win32: fix thread usage for win32\n     +    win32: prefer beginthreadex over CreateThread\n      \n          Use _beginthreadex instead of CreateThread\n          since we use the Windows CRT,\n\n\n compat/winansi.c | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/compat/winansi.c b/compat/winansi.c\nindex f83610f684d..be65b27bd75 100644\n--- a/compat/winansi.c\n+++ b/compat/winansi.c\n@@ -340,7 +340,7 @@ enum {\n \tTEXT = 0, ESCAPE = 033, BRACKET = '['\n };\n \n-static DWORD WINAPI console_thread(LPVOID unused)\n+static unsigned int WINAPI console_thread(LPVOID unused)\n {\n \tunsigned char buffer[BUFFER_SIZE];\n \tDWORD bytes;\n@@ -643,9 +643,9 @@ void winansi_init(void)\n \t\tdie_lasterr(\"CreateFile for named pipe failed\");\n \n \t/* start console spool thread on the pipe's read end */\n-\ththread = CreateThread(NULL, 0, console_thread, NULL, 0, NULL);\n+\ththread = (HANDLE)_beginthreadex(NULL, 0, console_thread, NULL, 0, NULL);\n \tif (!hthread)\n-\t\tdie_lasterr(\"CreateThread(console_thread) failed\");\n+\t\tdie_lasterr(\"_beginthreadex(console_thread) failed\");\n \n \t/* schedule cleanup routine */\n \tif (atexit(winansi_exit))\n\nbase-commit: 23c56f7bd5f1667f8b793d796bf30e39545920f6\n-- \ngitgitgadget\n"}]}