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

Re: [msysGit] Re: [PATCH/RFC] mingw: implement PTHREAD_MUTEX_INITIALIZER

From
Erik Faye-Lund <kusmabite@gmail.com>
Date
Oct 26, 2011, 13:08 UTC
Message-ID
<CABPQNSbuKuZ1yY1+ex3Y=ZY=7cjLwvo3Chbwi7ApoEjUOAErbA@mail.gmail.com>
In-Reply-To
<16520370.401.1319598319120.JavaMail.geo-discussion-forums@prms22>
On Wed, Oct 26, 2011 at 5:05 AM, Atsushi Nakagawa <atnak@chejz.com> wrote:
Show 21 quoted lines
> On Oct 25, 11:55 pm, Erik Faye-Lund <kusmab...@gmail.com> wrote:
>> [...]
>> +int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t
>> *attr)
>> +{
>> +       InitializeCriticalSection(&mutex->cs);
>> +       mutex->autoinit = 0;
>> +       return 0;
>> +}
>> +
>> +int pthread_mutex_lock(pthread_mutex_t *mutex)
>> +{
>> +       if (mutex->autoinit) {
>> +               if (InterlockedCompareExchange(&mutex->autoinit, -1, 1) !=
>> -1) {
> I'm making the assumption that mutex->autoinit starts off as 1 before things
> get multi-threaded..
> I've only looked at what's in the patch so I could be missing vital
> context..  Anyways, is there a reason why you made this
> "InterlockedCompareExchange(..., -1, 1) != -1" and not
> "InterlockedCompareExchange(..., -1, 1) == 1"?
No, not really.
> It looks to me the former adds a race condition after "if (mutex->autoinit)
> {".  e.g. A second thread could reinitialize mutex->cs after the first
> thread has already entered EnterCriticalSection(...).
You are indeed correct, thanks for spotting :)
Previous: Atsushi Nakagawa
Message 13 of 13 in “mingw: implement PTHREAD_MUTEX_INITIALIZER”
  1. mingw: implement PTHREAD_MUTEX_INITIALIZERErik Faye-Lund, Oct 25, 2011
  2. Johannes SixtOct 25, 2011
  3. Erik Faye-LundOct 25, 2011
  4. Johannes SixtOct 25, 2011
  5. Erik Faye-LundOct 25, 2011
  6. Johannes SixtOct 25, 2011
  7. Kyle MoffettOct 26, 2011
  8. Erik Faye-LundOct 26, 2011
  9. Atsushi NakagawaOct 27, 2011
  10. Kyle MoffettOct 27, 2011
  11. Atsushi NakagawaOct 28, 2011
  12. Atsushi NakagawaOct 26, 2011
  13. Erik Faye-LundOct 26, 2011

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.