git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Erik Faye-Lund <kusmabite@googlemail.com>
To: Dmitry Potapov <dpotapov@gmail.com>
Cc: Johannes Sixt <j6t@kdbg.org>, msysgit <msysgit@googlegroups.com>,
	git@vger.kernel.org,
	 "Andrzej K. Haczewski" <ahaczewski@gmail.com>
Subject: Re: [PATCH 1/5] MSVC: Windows-native implementation for  subset of Pthreads API
Date: Fri, 8 Jan 2010 11:58:56 +0100	[thread overview]
Message-ID: <40aa078e1001080258n67e0711sf4733a99d512bf1@mail.gmail.com> (raw)
In-Reply-To: <20100108033232.GA28263@dpotapov.dyndns.org>

On Fri, Jan 8, 2010 at 4:32 AM, Dmitry Potapov <dpotapov@gmail.com> wrote:
> 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.

>> +
>> +     /* 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.

>> +
>> +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

  reply	other threads:[~2010-01-08 10:59 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-07 21:54 [PATCH 0/5] Miscellaneous improvements on Windows Johannes Sixt
2010-01-07 21:54 ` [PATCH 1/5] MSVC: Windows-native implementation for subset of Pthreads API Johannes Sixt
2010-01-08  3:32   ` Dmitry Potapov
2010-01-08 10:58     ` Erik Faye-Lund [this message]
2010-01-08 20:40       ` Johannes Sixt
2010-01-08 21:37         ` Dmitry Potapov
2010-01-12 21:13       ` Johannes Sixt
2010-01-13 12:53         ` Dmitry Potapov
2010-01-13 18:40           ` Johannes Sixt
2010-01-14  5:12             ` Dmitry Potapov
2010-01-14 13:43               ` Peter Harris
2010-01-14 19:55               ` Johannes Sixt
2010-01-07 21:54 ` [PATCH 2/5] MinGW: enable pthreads Johannes Sixt
2010-01-07 21:54 ` [PATCH 3/5] Windows: boost startup by avoiding a static dependency on shell32.dll Johannes Sixt
2010-01-07 21:55 ` [PATCH 4/5] Windows: simplify the pipe(2) implementation Johannes Sixt
2010-01-07 21:55 ` [PATCH 5/5] Windows: avoid the "dup dance" when spawning a child process Johannes Sixt
2010-01-15 20:12 ` [PATCH v2 0/7] Miscellaneous improvements on Windows Johannes Sixt
2010-01-15 20:12   ` [PATCH v2 1/7] Windows: disable Python Johannes Sixt
2010-01-15 20:12   ` [PATCH v2 2/7] Windows: boost startup by avoiding a static dependency on shell32.dll Johannes Sixt
2010-01-15 20:12   ` [PATCH v2 3/7] Windows: simplify the pipe(2) implementation Johannes Sixt
2010-01-15 20:12   ` [PATCH v2 4/7] Windows: avoid the "dup dance" when spawning a child process Johannes Sixt
2010-01-15 20:12   ` [PATCH v2 5/7] MSVC: Fix an "incompatible pointer types" compiler warning Johannes Sixt
2010-01-15 20:12   ` [PATCH v2 6/7] MSVC: Windows-native implementation for subset of Pthreads API Johannes Sixt
2010-01-15 20:12   ` [PATCH v2 7/7] Do not use date.c:tm_to_time_t() from compat/mingw.c 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=40aa078e1001080258n67e0711sf4733a99d512bf1@mail.gmail.com \
    --to=kusmabite@googlemail.com \
    --cc=ahaczewski@gmail.com \
    --cc=dpotapov@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=j6t@kdbg.org \
    --cc=kusmabite@gmail.com \
    --cc=msysgit@googlegroups.com \
    /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).