From: Stefan Beller <sbeller@google.com>
To: Johannes.Schindelin@gmx.de, j6t@kdbg.org
Cc: git@vger.kernel.org, simon@ruderich.org, peff@peff.net,
Stefan Beller <sbeller@google.com>
Subject: [PATCH] compat: Allow static initializer for pthreads on Windows
Date: Wed, 26 Oct 2016 14:57:32 -0700 [thread overview]
Message-ID: <20161026215732.16411-1-sbeller@google.com> (raw)
In Windows it is not possible to have a static initialized mutex as of
now, but that seems to be painful for the upcoming refactoring of the
attribute subsystem, as we have no good place to put the initialization
of the attr global lock.
The trick is to get a named mutex as CreateMutex[1] will return the
existing named mutex if it exists in a thread safe way, or return
a newly created mutex with that name.
Inside the critical section of the single named mutex, we need to double
check if the mutex was already initialized because the outer check is
not sufficient.
(e.g. 2 threads enter the first condition `(!a)` at the same time, but
only one of them will acquire the named mutex first and proceeds to
initialize the given mutex a. The second thread shall not re-initialize
the given mutex `a`, which is why we have the inner condition on `(!a)`.
Due to the use of memory barriers inside the critical section the mutex
`a` gets updated to other threads, such that any further invocation
will skip the initialization check code altogether on the first condition.
[1] https://msdn.microsoft.com/en-us/library/windows/desktop/ms682411(v=vs.85).aspx
Signed-off-by: Stefan Beller <sbeller@google.com>
---
Flying blind here, i.e. not compiled, not tested. For a system I do not
have deep knowledge of. The only help was the online documentation.
compat/win32/pthread.h | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/compat/win32/pthread.h b/compat/win32/pthread.h
index 1c16408..a900513 100644
--- a/compat/win32/pthread.h
+++ b/compat/win32/pthread.h
@@ -21,9 +21,25 @@
static inline int return_0(int i) {
return 0;
}
+
+#define PTHREAD_MUTEX_INITIALIZER NULL
#define pthread_mutex_init(a,b) return_0((InitializeCriticalSection((a)), 0))
#define pthread_mutex_destroy(a) DeleteCriticalSection((a))
-#define pthread_mutex_lock EnterCriticalSection
+#define pthread_mutex_lock(a) \
+{ \
+ if (!a) { \
+ HANDLE p = CreateMutex(NULL, FALSE, "Git-Global-Windows-Mutex"); \
+ EnterCriticalSection(p); \
+ MemoryBarrier(); \
+ if (!a)
+ pthread_mutex_init(a); \
+ MemoryBarrier(); \
+ ReleaseMutex(p); \
+ } \
+ EnterCriticalSection(a); \
+}
+
+
#define pthread_mutex_unlock LeaveCriticalSection
typedef int pthread_mutexattr_t;
--
2.10.1.508.g6572022
next reply other threads:[~2016-10-26 21:57 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-26 21:57 Stefan Beller [this message]
2016-10-26 22:14 ` [PATCH] compat: Allow static initializer for pthreads on Windows Stefan Beller
2016-10-27 5:49 ` Johannes Sixt
2016-10-27 6:02 ` Junio C Hamano
2016-10-27 6:10 ` Johannes Sixt
2016-10-27 6:21 ` Junio C Hamano
2016-10-27 6:29 ` Johannes Sixt
2016-10-27 6:33 ` Junio C Hamano
2016-10-27 17:01 ` Stefan Beller
2016-10-27 17:11 ` Jeff King
2016-10-27 18:22 ` Johannes Sixt
2016-10-27 18:49 ` Junio C Hamano
2016-10-27 19:08 ` Stefan Beller
2016-10-27 20:05 ` Johannes Sixt
2016-10-27 21:49 ` Jacob Keller
2016-10-27 21:59 ` Stefan Beller
2016-10-28 5:55 ` Johannes Sixt
2016-10-28 6:11 ` Jacob Keller
2016-10-28 13:01 ` Philip Oakley
2016-10-28 18:49 ` Jacob Keller
2016-10-28 11:58 ` Johannes Schindelin
2016-10-28 18:48 ` Jacob Keller
2016-10-28 20:26 ` Johannes Sixt
2016-10-28 20:29 ` Junio C Hamano
2016-10-28 20:36 ` Stefan Beller
2016-10-28 20:38 ` Junio C Hamano
2016-10-28 20:49 ` Johannes Sixt
2016-10-28 20:52 ` Junio C Hamano
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=20161026215732.16411-1-sbeller@google.com \
--to=sbeller@google.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=j6t@kdbg.org \
--cc=peff@peff.net \
--cc=simon@ruderich.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.
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).