From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <johannes.schindelin@gmx.de>,
Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH 3/4] pthread: provide `pthread_once()` shims for Windows and for NO_PTHREADS
Date: Mon, 28 Sep 2026 15:51:30 +0000 [thread overview]
Message-ID: <2f3077b60a7777b8d4c21551f9a8d02034a49cee.1790610691.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2240.git.1790610691.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>
I am about to introduce logic that needs to perform some initialization
once, and once only, even if called concurrently.
This is a perfect job for `pthread_once()`, but Git's source code
currently lacks a Win32 shim. So let's add one!
Also provide a trivial shim for `NO_PTHREAD` builds.
Assisted-by: GPT-6 Sol
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
compat/win32/pthread.c | 16 ++++++++++++++++
compat/win32/pthread.h | 5 +++++
thread-utils.h | 16 ++++++++++++++++
3 files changed, 37 insertions(+)
diff --git a/compat/win32/pthread.c b/compat/win32/pthread.c
index 398caa9602..5af95edd3b 100644
--- a/compat/win32/pthread.c
+++ b/compat/win32/pthread.c
@@ -60,6 +60,22 @@ pthread_t pthread_self(void)
return t;
}
+static BOOL CALLBACK win32_pthread_once(PINIT_ONCE once UNUSED,
+ PVOID parameter,
+ PVOID *context UNUSED)
+{
+ (*(void (**)(void))parameter)();
+ return TRUE;
+}
+
+int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
+{
+ if (!InitOnceExecuteOnce(once_control, win32_pthread_once,
+ &init_routine, NULL))
+ return err_win_to_posix(GetLastError());
+ return 0;
+}
+
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
if (SleepConditionVariableCS(cond, mutex, INFINITE) == 0)
diff --git a/compat/win32/pthread.h b/compat/win32/pthread.h
index d80df8d12a..79a6bd9680 100644
--- a/compat/win32/pthread.h
+++ b/compat/win32/pthread.h
@@ -26,6 +26,11 @@ static inline int return_0(int i UNUSED) {
#define pthread_mutex_lock EnterCriticalSection
#define pthread_mutex_unlock LeaveCriticalSection
+typedef INIT_ONCE pthread_once_t;
+#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
+
+int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
+
typedef int pthread_mutexattr_t;
#define pthread_mutexattr_init(a) (*(a) = 0)
#define pthread_mutexattr_destroy(a) do {} while (0)
diff --git a/thread-utils.h b/thread-utils.h
index 4961487ed9..98b574eff4 100644
--- a/thread-utils.h
+++ b/thread-utils.h
@@ -19,6 +19,22 @@
#define pthread_mutex_t int
#define pthread_cond_t int
#define pthread_key_t int
+#define pthread_once_t int
+#undef PTHREAD_ONCE_INIT
+#define PTHREAD_ONCE_INIT 0
+
+static inline int dummy_pthread_once(pthread_once_t *once_control,
+ void (*init_routine)(void))
+{
+ if (!*once_control) {
+ init_routine();
+ *once_control = 1;
+ }
+ return 0;
+}
+
+#define pthread_once(once_control, init_routine) \
+ dummy_pthread_once((once_control), (init_routine))
#define pthread_mutex_init(mutex, attr) dummy_pthread_init(mutex)
#define pthread_mutex_lock(mutex)
--
gitgitgadget
next prev parent reply other threads:[~2026-09-28 15:51 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-28 15:51 [PATCH 0/4] Add a compile-time option to use the new, very fast sha1dc Rust crate Johannes Schindelin via GitGitGadget
2026-09-28 15:51 ` [PATCH 1/4] libgitcore: add `sha1dc` as an optional feature Johannes Schindelin via GitGitGadget
2026-10-03 21:01 ` brian m. carlson
2026-09-28 15:51 ` [PATCH 2/4] sha1dc: allow selecting the C backend without rebuilding Johannes Schindelin via GitGitGadget
2026-09-28 15:51 ` Johannes Schindelin via GitGitGadget [this message]
2026-09-28 15:51 ` [PATCH 4/4] sha1dc: make `sha1dc_init()` thread-safe Johannes Schindelin via GitGitGadget
2026-09-28 16:20 ` [PATCH 0/4] Add a compile-time option to use the new, very fast sha1dc Rust crate Johannes Schindelin
2026-09-29 7:30 ` Junio C Hamano
2026-10-03 10:23 ` Johannes Schindelin
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=2f3077b60a7777b8d4c21551f9a8d02034a49cee.1790610691.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=git@vger.kernel.org \
--cc=johannes.schindelin@gmx.de \
/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