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