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

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

From
Erik Faye-Lund <kusmabite@googlemail.com>
Date
Jan 8, 2010, 10:58 UTC
Message-ID
<40aa078e1001080258n67e0711sf4733a99d512bf1@mail.gmail.com>
In-Reply-To
<20100108033232.GA28263@dpotapov.dyndns.org>
On Fri, Jan 8, 2010 at 4:32 AM, Dmitry Potapov <dpotapov@gmail.com> wrote:
Show 29 quoted lines
> On Thu, Jan 07, 2010 at 10:54:57PM +0100, Johannes Sixt wrote:
>> +
>> +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);
>
> InterlockedIncrement(&cond->waiters);
>
>> +
>> +     /*
>> +      * 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 - ignore return value */
>> +     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);
>
> InterlockedDecrement(&cond->waiters);
Nice.
Show 19 quoted lines
>> +
>> +     /* lock external mutex again */
>> +     EnterCriticalSection(mutex);
>> +
>> +     return 0;
>> +}
>> +
>> +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);
>
> AFAIK, Win32 API assumes that reading LONG is always atomic, so
> the critical section is not really necesary here, but you need
> to declare 'waiters' as 'volatile':

"Simple reads and writes to properly-aligned 32-bit variables are atomic operations." http://msdn.microsoft.com/en-us/library/ms684122(VS.85).aspx

In other words: Yes, you are right.
Show 8 quoted lines
>> +
>> +int pthread_cond_init(pthread_cond_t *cond, const void *unused)
>> +{
>> +     cond->waiters = 0;
>> +
>> +     InitializeCriticalSection(&cond->waiters_lock);
>
> Is waiters_lock really necessary?
(Yeah, I moved this one to the end)
No, I think you've proven that it isn't.
-- 
Erik "kusma" Faye-Lund
Previous: Dmitry PotapovNext: Johannes Sixt
Message 4 of 24 in “Miscellaneous improvements on Windows”
  1. 0/5 Miscellaneous improvements on WindowsJohannes Sixt, Jan 7, 2010
  2. 1/5 MSVC: Windows-native implementation for subset of Pthreads APIJohannes Sixt, Jan 7, 2010
  3. Dmitry PotapovJan 8, 2010
  4. Erik Faye-LundJan 8, 2010
  5. Johannes SixtJan 8, 2010
  6. Dmitry PotapovJan 8, 2010
  7. Johannes SixtJan 12, 2010
  8. Dmitry PotapovJan 13, 2010
  9. Johannes SixtJan 13, 2010
  10. Dmitry PotapovJan 14, 2010
  11. Peter HarrisJan 14, 2010
  12. Johannes SixtJan 14, 2010
  13. 2/5 MinGW: enable pthreadsJohannes Sixt, Jan 7, 2010
  14. 3/5 Windows: boost startup by avoiding a static dependency on shell32.dllJohannes Sixt, Jan 7, 2010
  15. 4/5 Windows: simplify the pipe(2) implementationJohannes Sixt, Jan 7, 2010
  16. 5/5 Windows: avoid the "dup dance" when spawning a child processJohannes Sixt, Jan 7, 2010
  17. 0/7 Miscellaneous improvements on WindowsJohannes Sixt, Jan 15, 2010
  18. 1/7 Windows: disable PythonJohannes Sixt, Jan 15, 2010
  19. 2/7 Windows: boost startup by avoiding a static dependency on shell32.dllJohannes Sixt, Jan 15, 2010
  20. 3/7 Windows: simplify the pipe(2) implementationJohannes Sixt, Jan 15, 2010
  21. 4/7 Windows: avoid the "dup dance" when spawning a child processJohannes Sixt, Jan 15, 2010
  22. 5/7 MSVC: Fix an "incompatible pointer types" compiler warningJohannes Sixt, Jan 15, 2010
  23. 6/7 MSVC: Windows-native implementation for subset of Pthreads APIJohannes Sixt, Jan 15, 2010
  24. 7/7 Do not use date.c:tm_to_time_t() from compat/mingw.cJohannes Sixt, Jan 15, 2010

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.