git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] MSVC: Windows-native implementation for subset of Pthreads API

From
Johannes Sixt <j.sixt@viscovery.net>
Date
Nov 5, 2009, 12:27 UTC
Message-ID
<4AF2C4B9.10402@viscovery.net>
In-Reply-To
<1257416325-5605-1-git-send-email-ahaczewski@gmail.com>
Andrzej K. Haczewski schrieb:
Show 31 quoted lines
> diff --git a/Makefile b/Makefile
> index bc039ac..30089a8 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -453,6 +453,7 @@ LIB_H += commit.h
>  LIB_H += compat/bswap.h
>  LIB_H += compat/cygwin.h
>  LIB_H += compat/mingw.h
> +LIB_H += compat/win32/pthread.h
>  LIB_H += csum-file.h
>  LIB_H += decorate.h
>  LIB_H += delta.h
> @@ -971,15 +972,15 @@ ifdef MSVC
>  	OBJECT_CREATION_USES_RENAMES = UnfortunatelyNeedsTo
>  	NO_REGEX = YesPlease
>  	NO_CURL = YesPlease
> -	NO_PTHREADS = YesPlease
> +	THREADED_DELTA_SEARCH = YesPlease
>  	BLK_SHA1 = YesPlease
>  
>  	CC = compat/vcbuild/scripts/clink.pl
>  	AR = compat/vcbuild/scripts/lib.pl
>  	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_OBJS = compat/msvc.o compat/fnmatch/fnmatch.o compat/winansi.o compat/win32/pthread.o
> +	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 =
What compiles compat/win32/pthread.c?
Please don't forget to add compat/win32/*.o to the clean target.
Show 5 quoted lines
> +int pthread_create(pthread_t *thread, const void *unused,
> +		   void *(*start_routine)(void*), void *arg)
> +{
> +	thread->arg = arg;
> +	thread->handle = CreateThread(NULL, 0, win32_start_routine, thread, 0, NULL);
Elsewhere we use _beginthreadex(). What's the difference?
Show 69 quoted lines
> +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)
> +		die("");
> +	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;
> +}

The pthread_cond_* functions are quite voluminous, but not performance critical. Could you please move them to pthread.c as well?

-- Hannes
Previous: Andrzej K. HaczewskiNext: Andrzej K. Haczewski
Message 40 of 62 in “Port of pthreads to Windows API threads”
  1. 0/1 Port of pthreads to Windows API threadsAndrzej K. Haczewski, Nov 3, 2009
  2. 1/1 MSVC: port pthread code to native Windows threadsAndrzej K. Haczewski, Nov 3, 2009
  3. Johannes SchindelinNov 3, 2009
  4. Joshua JensenNov 4, 2009
  5. Andrzej K. HaczewskiNov 4, 2009
  6. Johannes SixtNov 4, 2009
  7. Johannes SchindelinNov 4, 2009
  8. Andrzej K. HaczewskiNov 4, 2009
  9. Johannes SixtNov 4, 2009
  10. Michael WookeyNov 4, 2009
  11. Andrzej K. HaczewskiNov 4, 2009
  12. MSVC: port pthread code to native Windows threadsAndrzej K. Haczewski, Nov 4, 2009
  13. Erik Faye-LundNov 4, 2009
  14. Andrzej K. HaczewskiNov 4, 2009
  15. Paolo BonziniNov 4, 2009
  16. Paolo BonziniNov 4, 2009
  17. Johannes SixtNov 4, 2009
  18. Andrzej K. HaczewskiNov 4, 2009
  19. Johannes SixtNov 4, 2009
  20. Andrzej K. HaczewskiNov 4, 2009
  21. Daniel BarkalowNov 4, 2009
  22. Nicolas PitreNov 4, 2009
  23. Daniel BarkalowNov 4, 2009
  24. Nicolas PitreNov 5, 2009
  25. Dmitry PotapovNov 5, 2009
  26. Andrzej K. HaczewskiNov 4, 2009
  27. Erik Faye-LundNov 4, 2009
  28. Johannes SchindelinNov 4, 2009
  29. MSVC: Windows-native implementation for subset of Pthreads APIAndrzej K. Haczewski, Nov 4, 2009
  30. Nicolas PitreNov 4, 2009
  31. Andrzej K. HaczewskiNov 4, 2009
  32. pack-objects: move thread autodetection closer to relevant codeNicolas Pitre, Nov 4, 2009
  33. Junio C HamanoNov 6, 2009
  34. Erik Faye-LundNov 4, 2009
  35. Andrzej K. HaczewskiNov 4, 2009
  36. Nicolas PitreNov 5, 2009
  37. Andrzej K. HaczewskiNov 5, 2009
  38. Erik Faye-LundNov 5, 2009
  39. MSVC: Windows-native implementation for subset of Pthreads APIAndrzej K. Haczewski, Nov 5, 2009
  40. Johannes SixtNov 5, 2009
  41. Andrzej K. HaczewskiNov 5, 2009
  42. Nicolas PitreNov 5, 2009
  43. Andrzej K. HaczewskiNov 5, 2009
  44. Nicolas PitreNov 5, 2009
  45. Nicolas PitreNov 4, 2009
  46. MSVC: Windows-native implementation for subset of Pthreads APIAndrzej K. Haczewski, Nov 4, 2009
  47. MSVC: Windows-native implementation for subset of Pthreads APIAndrzej K. Haczewski, Nov 4, 2009
  48. Nicolas PitreNov 5, 2009
  49. Andrzej K. HaczewskiNov 5, 2009
  50. Nicolas PitreNov 5, 2009
  51. Nicolas PitreNov 5, 2009
  52. Andrzej K. HaczewskiNov 5, 2009
  53. Nicolas PitreNov 5, 2009
  54. Johannes SixtNov 5, 2009
  55. Junio C HamanoNov 4, 2009
  56. MSVC: Windows-native implementation for subset of Pthreads APIAndrzej K. Haczewski, Nov 5, 2009
  57. Johannes SixtNov 5, 2009
  58. Nicolas PitreNov 5, 2009
  59. Andrzej K. HaczewskiNov 5, 2009
  60. Nicolas PitreNov 5, 2009
  61. MSVC: Windows-native implementation for subset of Pthreads APIAndrzej K. Haczewski, Nov 6, 2009
  62. Johannes SixtNov 6, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.