All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Andrzej K. Haczewski" <ahaczewski@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Sixt <j.sixt@viscovery.net>,
	"Andrzej K. Haczewski" <ahaczewski@gmail.com>
Subject: [PATCH] MSVC: Windows-native implementation for subset of Pthreads API
Date: Wed,  4 Nov 2009 16:55:00 +0100	[thread overview]
Message-ID: <1257350100-29281-1-git-send-email-ahaczewski@gmail.com> (raw)
In-Reply-To: <1257331059-26344-1-git-send-email-ahaczewski@gmail.com>

This patch implements native to Windows subset of pthreads API used by Git.
It allows to remove Pthreads for Win32 dependency for msysgit and cygwin.

The patch modifies Makefile only for MSVC (that's the environment I'm capable
of testing on), so it requires further corrections to compile with MinGW
or Cygwin.

Signed-off-by: Andrzej K. Haczewski <ahaczewski@gmail.com>
---
 Makefile               |    8 ++-
 builtin-pack-objects.c |   41 +++++++++++++-
 compat/mingw.c         |    2 +-
 compat/mingw.h         |    5 ++
 compat/win32/pthread.h |  136 ++++++++++++++++++++++++++++++++++++++++++++++++
 git-compat-util.h      |   10 ++++
 preload-index.c        |    4 +-
 7 files changed, 198 insertions(+), 8 deletions(-)
 create mode 100644 compat/win32/pthread.h

diff --git a/Makefile b/Makefile
index 521e8a5..14c371c 100644
--- a/Makefile
+++ b/Makefile
@@ -939,7 +939,8 @@ ifdef MSVC
 	OBJECT_CREATION_USES_RENAMES = UnfortunatelyNeedsTo
 	NO_REGEX = YesPlease
 	NO_CURL = YesPlease
-	NO_PTHREADS = YesPlease
+	THREADED_DELTA_SEARCH = YesPlease
+	NO_STATIC_PTHREADS_INIT = YesPlease
 	BLK_SHA1 = YesPlease
 
 	CC = compat/vcbuild/scripts/clink.pl
@@ -947,7 +948,7 @@ ifdef MSVC
 	CFLAGS =
 	BASIC_CFLAGS = -nologo -I. -I../zlib -Icompat/vcbuild -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE
 	COMPAT_OBJS = compat/msvc.o compat/fnmatch/fnmatch.o compat/winansi.o
-	COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DNOGDI -DHAVE_STRING_H -DHAVE_ALLOCA_H -Icompat -Icompat/fnmatch -Icompat/regex -Icompat/fnmatch -DSTRIP_EXTENSION=\".exe\"
+	COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DNOGDI -DHAVE_STRING_H -DHAVE_ALLOCA_H -Icompat -Icompat/fnmatch -Icompat/regex -Icompat/fnmatch -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
 	BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -SUBSYSTEM:CONSOLE -NODEFAULTLIB:MSVCRT.lib
 	EXTLIBS = advapi32.lib shell32.lib wininet.lib ws2_32.lib
 	lib =
@@ -1293,6 +1294,9 @@ ifdef THREADED_DELTA_SEARCH
 	BASIC_CFLAGS += -DTHREADED_DELTA_SEARCH
 	LIB_OBJS += thread-utils.o
 endif
+ifdef NO_STATIC_PTHREADS_INIT
+	COMPAT_CFLAGS += -DNO_STATIC_PTHREADS_INIT
+endif
 ifdef DIR_HAS_BSD_GROUP_SEMANTICS
 	COMPAT_CFLAGS += -DDIR_HAS_BSD_GROUP_SEMANTICS
 endif
diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
index 02f9246..f297d82 100644
--- a/builtin-pack-objects.c
+++ b/builtin-pack-objects.c
@@ -1592,7 +1592,34 @@ struct thread_params {
 
 static pthread_cond_t progress_cond = PTHREAD_COND_INITIALIZER;
 
-static void *threaded_find_deltas(void *arg)
+/*
+ * For platforms that do support static pthreads initialization
+ * make these noops.
+ */
+#ifndef NO_STATIC_PTHREADS_INIT
+# define init_threaded_delta_search()
+# define cleanup_threaded_delta_search()
+#else
+static void init_threaded_delta_search()
+{
+	pthread_mutex_init(&read_mutex);
+	pthread_mutex_init(&cache_mutex);
+	pthread_mutex_init(&progress_mutex);
+	pthread_cond_init(&progress_cond, NULL);
+}
+
+static void cleanup_threaded_delta_search()
+{
+	/* cleanup Windows threads thingies */
+	pthread_cond_destroy(&progress_cond);
+	pthread_mutex_destroy(&read_mutex);
+	pthread_mutex_destroy(&cache_mutex);
+	pthread_mutex_destroy(&progress_mutex);
+
+}
+#endif
+
+static THREAD_RET_TYPE threaded_find_deltas(void *arg)
 {
 	struct thread_params *me = arg;
 
@@ -1620,7 +1647,7 @@ static void *threaded_find_deltas(void *arg)
 		pthread_mutex_unlock(&me->mutex);
 	}
 	/* leave ->working 1 so that this doesn't get more work assigned */
-	return NULL;
+	return 0;
 }
 
 static void ll_find_deltas(struct object_entry **list, unsigned list_size,
@@ -2327,6 +2354,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
 #ifdef THREADED_DELTA_SEARCH
 	if (!delta_search_threads)	/* --threads=0 means autodetect */
 		delta_search_threads = online_cpus();
+
+	init_threaded_delta_search();
 #endif
 
 	prepare_packed_git();
@@ -2345,7 +2374,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
 	stop_progress(&progress_state);
 
 	if (non_empty && !nr_result)
-		return 0;
+		goto cleanup;
 	if (nr_result)
 		prepare_pack(window, depth);
 	write_pack_file();
@@ -2353,5 +2382,11 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
 		fprintf(stderr, "Total %"PRIu32" (delta %"PRIu32"),"
 			" reused %"PRIu32" (delta %"PRIu32")\n",
 			written, written_delta, reused, reused_delta);
+
+cleanup:
+#ifdef THREADED_DELTA_SEARCH
+	cleanup_threaded_delta_search();
+#endif
+
 	return 0;
 }
diff --git a/compat/mingw.c b/compat/mingw.c
index 6b5b5b2..f2e9f02 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -5,7 +5,7 @@
 
 #include <shellapi.h>
 
-static int err_win_to_posix(DWORD winerr)
+int err_win_to_posix(DWORD winerr)
 {
 	int error = ENOSYS;
 	switch(winerr) {
diff --git a/compat/mingw.h b/compat/mingw.h
index 6907345..7e25fb5 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -294,3 +294,8 @@ struct mingw_dirent
 #define readdir(x) mingw_readdir(x)
 struct dirent *mingw_readdir(DIR *dir);
 #endif // !NO_MINGW_REPLACE_READDIR
+
+/*
+ * Used by Pthread API implementation for Windows
+ */
+extern int err_win_to_posix(DWORD winerr);
diff --git a/compat/win32/pthread.h b/compat/win32/pthread.h
new file mode 100644
index 0000000..a71b90f
--- /dev/null
+++ b/compat/win32/pthread.h
@@ -0,0 +1,136 @@
+/*
+ * Header used to adapt pthread-based POSIX code to Windows API threads.
+ *
+ * Copyright (C) 2009 Andrzej K. Haczewski <ahaczewski@gmail.com>
+ */
+
+#ifndef PTHREAD_H
+#define PTHREAD_H
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+
+#include <windows.h>
+
+/*
+ * Implement simple condition variable for Windows threads, based on ACE
+ * implementation.
+ *
+ * See original implementation: http://bit.ly/1vkDjo
+ * ACE homepage: http://www.cse.wustl.edu/~schmidt/ACE.html
+ * See also: http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
+ */
+typedef struct {
+	LONG waiters;
+	CRITICAL_SECTION waiters_lock;
+	HANDLE sema;
+} pthread_cond_t;
+
+#define PTHREAD_COND_INITIALIZER { 0, { 0 }, NULL }
+
+static inline int pthread_cond_init(pthread_cond_t *cond, const void *unused)
+{
+	cond->waiters = 0;
+
+	InitializeCriticalSection(&cond->waiters_lock);
+
+	cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
+	if (!cond->sema)
+		return 0; /* POSIX do not allow pthread_cond_init to fail */
+	return 0;
+}
+
+static inline int pthread_cond_destroy(pthread_cond_t *cond)
+{
+	CloseHandle(cond->sema);
+	cond->sema = NULL;
+
+	DeleteCriticalSection(&cond->waiters_lock);
+
+	return 0;
+}
+
+static inline int pthread_cond_wait(pthread_cond_t *cond, CRITICAL_SECTION *mutex)
+{
+	/* serialize access to waiters count */
+	EnterCriticalSection(&cond->waiters_lock);
+	++cond->waiters;
+	LeaveCriticalSection(&cond->waiters_lock);
+
+	/*
+	 * Unlock external mutex and wait for signal.
+	 * NOTE: we've held mutex locked long enough to increment
+	 * waiters count above, so there's no problem with
+	 * leaving mutex unlocked before we wait on semaphore.
+	 */
+	LeaveCriticalSection(mutex);
+
+	/* let's wait */
+	WaitForSingleObject(cond->sema, INFINITE))
+
+	/* we're done waiting, so make sure we decrease waiters count */
+	EnterCriticalSection(&cond->waiters_lock);
+	--cond->waiters;
+	LeaveCriticalSection(&cond->waiters_lock);
+
+	/* lock external mutex again */
+	EnterCriticalSection(mutex);
+
+	return 0;
+}
+
+static inline int pthread_cond_signal(pthread_cond_t *cond)
+{
+	int have_waiters;
+
+	/* serialize access to waiters count */
+	EnterCriticalSection(&cond->waiters_lock);
+	have_waiters = cond->waiters > 0;
+	LeaveCriticalSection(&cond->waiters_lock);
+
+	/*
+	 * Signal only when there are waiters
+	 */
+	if (have_waiters)
+		return ReleaseSemaphore(cond->sema, 1, NULL) ?
+			0 : err_win_to_posix(GetLastError();
+	else
+		return 0;
+}
+
+#define pthread_t HANDLE
+#define pthread_mutex_t CRITICAL_SECTION
+
+#define PTHREAD_MUTEX_INITIALIZER { 0 }
+
+#define pthread_mutex_init(a,b) InitializeCriticalSection((a))
+#define pthread_mutex_destroy(a) DeleteCriticalSection((a))
+#define pthread_mutex_lock EnterCriticalSection
+#define pthread_mutex_unlock LeaveCriticalSection
+
+static inline int pthread_create(pthread_t *thread, const void *unused,
+		DWORD (__stdcall *start_routine)(LPVOID), void *arg)
+{
+	*thread = CreateThread(NULL, 0, start_routine, arg, 0, NULL);
+
+	if (!*thread)
+		return err_win_to_posix(GetLastError());
+	else
+		return 0;
+}
+
+static inline int pthread_join(pthread_t thread, void **unused)
+{
+	DWORD result = WaitForSingleObject(t, INFINITE);
+	switch (result) {
+		case WAIT_OBJECT_0:
+			return 0;
+		case WAIT_ABANDONED:
+			return EINVAL;
+		default:
+			return err_win_to_posix(GetLastError());
+	}
+}
+
+#endif /* PTHREAD_H */
diff --git a/git-compat-util.h b/git-compat-util.h
index ef60803..4311117 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -464,4 +464,14 @@ void git_qsort(void *base, size_t nmemb, size_t size,
  */
 int unlink_or_warn(const char *path);
 
+/*
+ * Define type of thread function return type to distinguish
+ * Windows and POSIX.
+ */
+#ifndef _WIN32
+# define THREAD_RET_TYPE void *
+#else
+# define THREAD_RET_TYPE DWORD __stdcall
+#endif
+
 #endif
diff --git a/preload-index.c b/preload-index.c
index 9289933..41b11a3 100644
--- a/preload-index.c
+++ b/preload-index.c
@@ -28,7 +28,7 @@ struct thread_data {
 	int offset, nr;
 };
 
-static void *preload_thread(void *_data)
+static THREAD_RET_TYPE preload_thread(void* _data)
 {
 	int nr;
 	struct thread_data *p = _data;
@@ -59,7 +59,7 @@ static void *preload_thread(void *_data)
 			continue;
 		ce_mark_uptodate(ce);
 	} while (--nr > 0);
-	return NULL;
+	return 0;
 }
 
 static void preload_index(struct index_state *index, const char **pathspec)
-- 
1.6.5.2

  parent reply	other threads:[~2009-11-04 15:55 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-03 21:30 [PATCH 0/1] Port of pthreads to Windows API threads Andrzej K. Haczewski
2009-11-03 21:30 ` [PATCH 1/1] MSVC: port pthread code to native Windows threads Andrzej K. Haczewski
2009-11-03 23:38   ` Johannes Schindelin
2009-11-04  2:34     ` Joshua Jensen
2009-11-04  7:44       ` Andrzej K. Haczewski
2009-11-04  8:24         ` Johannes Sixt
2009-11-04 11:02       ` Johannes Schindelin
2009-11-04  8:17     ` Andrzej K. Haczewski
2009-11-04  8:15   ` Johannes Sixt
2009-11-04  8:48     ` Michael Wookey
2009-11-04 10:53     ` Andrzej K. Haczewski
2009-11-04 10:37 ` [PATCH] " Andrzej K. Haczewski
2009-11-04 10:50   ` Erik Faye-Lund
2009-11-04 10:56     ` Andrzej K. Haczewski
2009-11-04 11:14     ` Paolo Bonzini
2009-11-04 11:23   ` Paolo Bonzini
2009-11-04 12:39   ` Johannes Sixt
2009-11-04 13:47     ` Andrzej K. Haczewski
2009-11-04 14:34       ` Johannes Sixt
2009-11-04 14:50         ` Andrzej K. Haczewski
2009-11-04 20:43           ` Daniel Barkalow
2009-11-04 21:17             ` Nicolas Pitre
2009-11-04 22:22               ` Daniel Barkalow
2009-11-05  0:27                 ` Nicolas Pitre
2009-11-05 13:48       ` Dmitry Potapov
2009-11-04 14:14     ` Andrzej K. Haczewski
2009-11-04 14:19       ` Erik Faye-Lund
2009-11-04 14:04   ` Johannes Schindelin
2009-11-04 15:55   ` Andrzej K. Haczewski [this message]
2009-11-04 18:10     ` [PATCH] MSVC: Windows-native implementation for subset of Pthreads API Nicolas Pitre
2009-11-04 21:16       ` Andrzej K. Haczewski
2009-11-04 21:32         ` [PATCH] pack-objects: move thread autodetection closer to relevant code Nicolas Pitre
2009-11-06  7:20           ` Junio C Hamano
2009-11-04 21:41         ` [PATCH] MSVC: Windows-native implementation for subset of Pthreads API Erik Faye-Lund
2009-11-04 22:50           ` Andrzej K. Haczewski
2009-11-05  2:47             ` Nicolas Pitre
2009-11-05  9:00               ` Andrzej K. Haczewski
2009-11-05  9:41                 ` Erik Faye-Lund
2009-11-05 10:18                 ` Andrzej K. Haczewski
2009-11-05 12:27                   ` Johannes Sixt
2009-11-05 12:53                     ` Andrzej K. Haczewski
2009-11-05 19:25                 ` Nicolas Pitre
2009-11-05 20:38                   ` Andrzej K. Haczewski
2009-11-05 22:15                     ` Nicolas Pitre
2009-11-04 21:52         ` Nicolas Pitre
2009-11-04 23:47         ` Andrzej K. Haczewski
2009-11-04 23:57           ` Andrzej K. Haczewski
2009-11-05  0:22             ` Nicolas Pitre
2009-11-05  8:51               ` Andrzej K. Haczewski
2009-11-05 19:22                 ` Nicolas Pitre
2009-11-05  2:10             ` Nicolas Pitre
2009-11-05  8:45               ` Andrzej K. Haczewski
2009-11-05 19:17                 ` Nicolas Pitre
2009-11-05  7:33             ` Johannes Sixt
2009-11-04 23:58       ` Junio C Hamano
2009-11-05 16:45 ` Andrzej K. Haczewski
2009-11-05 17:31   ` Johannes Sixt
2009-11-05 19:39   ` Nicolas Pitre
2009-11-05 20:09     ` Andrzej K. Haczewski
2009-11-05 20:36       ` Nicolas Pitre
2009-11-06  8:10 ` Andrzej K. Haczewski
2009-11-06  8:25   ` Johannes Sixt

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=1257350100-29281-1-git-send-email-ahaczewski@gmail.com \
    --to=ahaczewski@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=j.sixt@viscovery.net \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.