Bug 14617 – PTHREAD_MUTEX_INITIALIZER does not work on OSX

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Mac OS X
Creation time
2015-05-22T19:06:00Z
Last change time
2017-07-19T17:41:46Z
Keywords
pull
Assigned to
nobody
Creator
andrei

Comments

Comment #0 by andrei — 2015-05-22T19:06:24Z
Initializing a mutex with PTHREAD_MUTEX_INITIALIZER puts it in an all-zero state: pthread_mutex_t(0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) This state is unsuitable for calls such as pthread_mutex_lock etc. The state after calling the dynamic initialization routine pthread_mutex_init is: pthread_mutex_t(1297437784, [0, 0, 0, 0, 96, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) How do we define this state using the available pthread/OSX constants?
Comment #1 by code — 2015-05-23T09:17:17Z
According to the headers, the initializer should be {0x32AAABA7}. http://www.opensource.apple.com/source/Libc/Libc-167/pthreads.subproj/pthread.h
Comment #2 by schveiguy — 2015-05-23T17:27:21Z
According to testing: Stevens-MacBook-Pro:testd steves$ cat pthreadm.cpp #include <pthread.h> #include <stdio.h> int main(int argc, char *argv[]) { pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER; unsigned char *b = (unsigned char *)&x; unsigned char *e = (unsigned char *)((&x) + 1); while(b != e) printf("%02x ", (int)*b++); printf("\n"); } Stevens-MacBook-Pro:testd steves$ ./pthreadm a7 ab aa 32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 So that jives with Martin's research. I'll see about a PR.
Comment #3 by schveiguy — 2015-05-23T17:45:38Z
A neat advantage over C is that we can define the .init value so we don't need such an initializer. In fact, core.posix.pthread defines PTHREAD_MUTEX_INITIALIZER as pthread_mutex_t.init. However, there is an issue, because core/sys/posix/sys/types.d is not included in the build. So if I define a new init, it won't be found (tried it). I also tried updating the PTHREAD_MUTEX_INITIALIZER in pthread.d, and that also is not found. So we need some Makefile updates in order to fix this. And I'm not sure we want to do that. Thoughts?
Comment #4 by andrei — 2015-05-23T17:55:57Z
Thanks for all this, folks! Steve, can't you just insert the proper defaults where the struct is defined? I.e. we have in src/core/sys/posix/sys/types.d the following definition at line 594: struct pthread_mutex_t { c_long __sig; byte[__PTHREAD_MUTEX_SIZE__] __opaque; } Changing it to the following makes at least my tests pass: struct pthread_mutex_t { c_long __sig = 0x32AAABA7; byte[__PTHREAD_MUTEX_SIZE__] __opaque; }
Comment #5 by schveiguy — 2015-05-24T10:27:09Z
Andrei, that's exactly what I did. I just realized, I messed up and left that initializer in the types.d file, when I tried doing it in the pthread.d file. Fixing that, it does work. So we have a potential fix (not the preferred) of fixing only PTHREAD_MUTEXT_INITIALIZER. I'll push the one with setting the init value, and you can see whether it's easy to fix the Makefile. Maybe I did something wrong with my testing...
Comment #6 by schveiguy — 2015-05-24T10:30:31Z
Comment #7 by schveiguy — 2015-05-25T01:53:30Z
Yep, still fails in the autotester. What do we want to do? a) just fix pthread.d so PTHREAD_MUTEX_INITIALIZER is proper, but pthread_mutex_t.init is not b) fix the Makefile so it properly includes types.d in the (unittest?) build. Whatever we do, we should spend the time to figure out all the initializers on all systems, so code will be portable.
Comment #8 by andrei — 2015-05-25T16:37:16Z
I now understand. Let's leave types.d be code-free for now (in fact it could be renamed to types.di) and only fix PTHREAD_MUTEX_INITIALIZER at least for now.
Comment #9 by github-bugzilla — 2015-05-25T18:57:08Z
Commits pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/ab78a534dc017efecbe86f79f5d4559b85ef8619 fix issue 14617 - Define correct values for PTHREAD_MUTEX_INITIALIZER and PTHREAD_ONCE_INIT. https://github.com/D-Programming-Language/druntime/commit/19f3f0db648b6361248e2c53e00fcb3794c2521b Merge pull request #1285 from schveiguy/pthreadmutexosx Fix issue 14617 - PTHREAD_MUTEX_INITIALIZER does not work on OSX
Comment #10 by github-bugzilla — 2015-06-17T21:03:04Z
Commits pushed to stable at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/ab78a534dc017efecbe86f79f5d4559b85ef8619 fix issue 14617 - Define correct values for PTHREAD_MUTEX_INITIALIZER and PTHREAD_ONCE_INIT. https://github.com/D-Programming-Language/druntime/commit/19f3f0db648b6361248e2c53e00fcb3794c2521b Merge pull request #1285 from schveiguy/pthreadmutexosx
Comment #11 by github-bugzilla — 2017-07-19T17:41:46Z
Commits pushed to dmd-cxx at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/ab78a534dc017efecbe86f79f5d4559b85ef8619 fix issue 14617 - Define correct values for PTHREAD_MUTEX_INITIALIZER and PTHREAD_ONCE_INIT. https://github.com/dlang/druntime/commit/19f3f0db648b6361248e2c53e00fcb3794c2521b Merge pull request #1285 from schveiguy/pthreadmutexosx