delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2014/08/05/13:55:59

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:message-id:date:from:mime-version:to:subject
:references:in-reply-to:content-type; q=dns; s=default; b=letSYI
e7m7whncEuKBAMzfw8B+nEfM9GcrGquMiyxr+Oea2v1t8Ui/pWJf/dSG5vCM3hNX
3aMV5W3JKRdk7j7b1li2s5rxxckQ+bLAD8s6jNAipiRRGk61k5CbZKSHnhBquovC
lUy3h6h7FukBbToTQeJcaJJtqYRkpB0+pJ0l4=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:message-id:date:from:mime-version:to:subject
:references:in-reply-to:content-type; s=default; bh=CgTX6B/1aESF
EJb174ZO7ZXCSwE=; b=H0H1GYBZoK8Bi7LLdfm3uN8gansl2QY7ZlXqBTUcokwY
USQHu+b4KJyrwV1oH+ejMro1Rh+raRmpYSaXmstKWiqJ/WvdZINM6fy6X1BpmC5o
DnQKy8E98pXuF1uYrUKoiPRaBL10VPIBdXgWhYiLehlJDMvWx8Hy48oNKb+rOFg=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2
X-HELO: limerock03.mail.cornell.edu
X-CornellRouted: This message has been Routed already.
Message-ID: <53E11A93.9070800@cornell.edu>
Date: Tue, 05 Aug 2014 13:55:31 -0400
From: Ken Brown <kbrown AT cornell DOT edu>
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: (call-process ...) hangs in emacs
References: <53DB8D23 DOT 7060806 AT alice DOT it> <CAK9Gx1cjj-7cDP7CunD7Bxz35L+SU9+4Ro3HRot5cwjcArudOA AT mail DOT gmail DOT com> <20140801133225 DOT GD25860 AT calimero DOT vinschen DOT de> <53DEDBBA DOT 20102 AT cornell DOT edu> <20140804080034 DOT GA2578 AT calimero DOT vinschen DOT de> <53DF8BDC DOT 8090104 AT cornell DOT edu> <20140804134526 DOT GK2578 AT calimero DOT vinschen DOT de> <53E0CC2D DOT 4080305 AT cornell DOT edu> <20140805135830 DOT GA9994 AT calimero DOT vinschen DOT de>
In-Reply-To: <20140805135830.GA9994@calimero.vinschen.de>
X-IsSubscribed: yes

--------------020304080105040401080000
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

On 8/5/2014 9:58 AM, Corinna Vinschen wrote:
> On Aug  5 08:21, Ken Brown wrote:
>> On 8/4/2014 9:45 AM, Corinna Vinschen wrote:
>>> ...this, and the fact that fork/exec (vfork == fork on Cygwin) still
>>> works nicely in other scenarios points to some problem with the usage of
>>> pthread_mutexes in the application may be the culprit.
>>>
>>> For instance, is it possible that emacs expects the pthread_mutexes
>>> in malloc to be ERRORCHECK mutexes?  What if you explicitely set
>>> them to ERRORCHECK at creation time?
>>
>> That doesn't seem to be the issue, but I think I did find the problem, and
>> it looks like there might be both an emacs bug and a Cygwin bug. Here's the
>> relevant code from emacs's gmalloc.c:
>>
>> pthread_mutex_t _malloc_mutex = PTHREAD_MUTEX_INITIALIZER;
>> pthread_mutex_t _aligned_blocks_mutex = PTHREAD_MUTEX_INITIALIZER;
>>
>> [...]
>>
>>    /* Some pthread implementations call malloc for statically
>>       initialized mutexes when they are used first.  To avoid such a
>>       situation, we initialize mutexes here while their use is
>>       disabled in malloc etc.  */
>>    pthread_mutex_init (&_malloc_mutex, NULL);
>>    pthread_mutex_init (&_aligned_blocks_mutex, NULL);
>>
>>
>> The pthread_mutexes are initialized twice, resulting in undefined behavior
>> according to Posix.  That's the emacs bug.
>
> That's not the problem.  It's not necessary to call pthread_mutex_init
> on statically initialized mutexes, but it doesn't hurt either.  Only
> when calling pthread_mutex_init twice on the same object it goes
> downhill, especially when the first incarnation of the mutex was already
> locked.
>
>> But simply removing the static
>> initialization doesn't fix the problem.  On the other hand, the following
>> patch does seem to fix it, at least in preliminary testing:
>>
>> === modified file 'src/gmalloc.c'
>> --- src/gmalloc.c       2014-03-04 19:02:49 +0000
>> +++ src/gmalloc.c       2014-08-05 01:35:38 +0000
>> @@ -490,8 +490,8 @@
>>   }
>>
>>   #ifdef USE_PTHREAD
>> -pthread_mutex_t _malloc_mutex = PTHREAD_MUTEX_INITIALIZER;
>> -pthread_mutex_t _aligned_blocks_mutex = PTHREAD_MUTEX_INITIALIZER;
>> +pthread_mutex_t _malloc_mutex;
>> +pthread_mutex_t _aligned_blocks_mutex;
>>   int _malloc_thread_enabled_p;
>>
>>   static void
>> @@ -526,8 +526,11 @@
>>        initialized mutexes when they are used first.  To avoid such a
>>        situation, we initialize mutexes here while their use is
>>        disabled in malloc etc.  */
>> -  pthread_mutex_init (&_malloc_mutex, NULL);
>> -  pthread_mutex_init (&_aligned_blocks_mutex, NULL);
>> +  pthread_mutexattr_t attr1, attr2;
>> +  pthread_mutexattr_settype (&attr1, PTHREAD_MUTEX_NORMAL);
>> +  pthread_mutexattr_settype (&attr2, PTHREAD_MUTEX_NORMAL);
>> +  pthread_mutex_init (&_malloc_mutex, &attr1);
>> +  pthread_mutex_init (&_aligned_blocks_mutex, &attr2);
>>     pthread_atfork (malloc_atfork_handler_prepare,
>>                    malloc_atfork_handler_parent,
>>                    malloc_atfork_handler_child);
>>
>>
>> The first hunk avoids the double initialization, but I don't understand why
>> the second hunk does anything.  Since PTHREAD_MUTEX_NORMAL is now the
>> default, shouldn't calling pthread_mutex_init with NULL second argument be
>> equivalent to my calls to pthread_mutexattr_settype?  Does this indicate a
>> Cygwin bug, or am I misunderstanding something?
>
> AFAICS you're missing something.  Your pthread_mutexattr_t attr1, attr2
> are not initialized.  They contain some random values, thus they are not
> good objects.  The calls to pthread_mutexattr_settype as well as the
> calls to pthread_mutex_init will fail with EINVAL, but you won't see it
> due to missing error handling, and you end up without mutexes at all.
> If you call pthread_mutexattr_init before calling
> pthread_mutexattr_settype the situation shoul;d be the same as before.

Thanks for catching my mistake.  Your earlier suggestion about 
explicitly setting the pthread_mutexes to be ERRORCHECK mutexes seems to 
fix the problem (as long as I remember to call pthread_mutexattr_init). 
  The revised patch is attached.  I went back to using both the static 
and dynamic initializations as in the original code, since you said 
that's harmless.

Angelo and Katsumi, could you test it and see if it solves the problems 
you reported?  If so, I'll issue new emacs releases.

Ken

--------------020304080105040401080000
Content-Type: text/plain; charset=windows-1252;
 name="pthread_mutex.patch"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="pthread_mutex.patch"

PT09IG1vZGlmaWVkIGZpbGUgJ3NyYy9nbWFsbG9jLmMnCi0tLSBzcmMvZ21h
bGxvYy5jCTIwMTQtMDMtMDQgMTk6MDI6NDkgKzAwMDAKKysrIHNyYy9nbWFs
bG9jLmMJMjAxNC0wOC0wNSAxNzozMDoxOCArMDAwMApAQCAtNDkwLDggKzQ5
MCw4IEBACiB9CiAKICNpZmRlZiBVU0VfUFRIUkVBRAotcHRocmVhZF9tdXRl
eF90IF9tYWxsb2NfbXV0ZXggPSBQVEhSRUFEX01VVEVYX0lOSVRJQUxJWkVS
OwotcHRocmVhZF9tdXRleF90IF9hbGlnbmVkX2Jsb2Nrc19tdX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F0dHIxKTsKKyAgcHRocmVh
ZF9tdXRleF9pbml0ICgmX2FsaWduZWRfYmxvY2tzX211dGV4LCAmYXR0cjIp
OwogICBwdGhyZWFkX2F0Zm9yayAobWFsbG9jX2F0Zm9ya19oYW5kbGVyX3By
ZXBhcmUsCiAJCSAgbWFsbG9jX2F0Zm9ya19oYW5kbGVyX3BhcmVudCwKIAkJ
ICBtYWxsb2NfYXRmb3JrX2hhbmRsZXJfY2hpbGQpOwoK


--------------020304080105040401080000
Content-Type: text/plain; charset=us-ascii

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
--------------020304080105040401080000--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019