Skip to content

Fix C11 threads.h compatibility - #64

Merged
wch merged 10 commits into
masterfrom
fix-c-thread
Aug 10, 2018
Merged

wch merged 10 commits into
masterfrom
fix-c-thread

Conversation

@wch

@wch wch commented Aug 9, 2018 •

Copy link
Copy Markdown
Member

This fixes #45 and fixes #63.

Still needs some work and documentation, but hopefully it will install on platforms with glibc 2.28 (Arch) and musl (Alpine).

@znmeb

znmeb commented Aug 9, 2018

Copy link
Copy Markdown

Let me know when this is ready to test - Twitter @znmeb works ;-)

@loqs

loqs commented Aug 9, 2018

Copy link
Copy Markdown

Tested with C11 threads supported required one change to ensure the environment was populated for the script then built successfully.

diff --git a/src/Makevars b/src/Makevars
index 78f82a3..dcacf0e 100644
--- a/src/Makevars
+++ b/src/Makevars
@@ -7,7 +7,7 @@ PKG_LIBS = -pthread
 # Does this platform support C11's <threads.h>? Note that not all C11
 # compilers and pthread libraries support <threads.h>, and that compilers that
 # use older standards (like C99) sometimes do support <threads.h>.
-THREADS_H_SUPPORT:=$(shell ./threads_h_support.sh)
+THREADS_H_SUPPORT:=$(shell CC="$(CC)" CPPFLAGS="$(CPPFLAGS)" PKG_CFLAGS="$(PKG_CFLAGS)" CFLAGS="$(CFLAGS)" PKG_LIBS="$(PKG_LIBS)" ./threads_h_support.sh)
 
 ifeq ($(THREADS_H_SUPPORT), 1)
 PKG_CPPFLAGS += -DTHREADS_H_SUPPORT

// jcheng 2017-11-03: _XOPEN_SOURCE 600 is necessary to prevent Solaris headers
// from complaining about the combination of C99 and _XOPEN_SOURCE <= 500. The
// error message starts with:
// "Compiler or options invalid for pre-UNIX 03 X/Open applications"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK that we're losing this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to keep this and maybe a few other things -- I haven't yet reviewed the changes we made to tinycthread that we may want to preserve in the new version.

Comment thread src/c11threads.h Outdated

/* If there's C11 <threads.h> support, use it; otherwise use tinycthread.h,
* which has a compatible API. */
#ifdef THREADS_H_SUPPORT

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather see this require THREADS_H_SUPPORT to be defined one way or another, so we know that the build environment made a conscious choice rather than not knowing this issue exists. (For example, Makevars.win doesn't deal with this yet, but it would happily compile anyway...)

@wch

wch commented Aug 10, 2018

Copy link
Copy Markdown
Member Author

@loqs Thanks! Although it happens to work, I think it's still not quite right. When I run it, the output is:

* installing *source* package 'later' ...
** libs
+ echo '#include <threads.h>
int main() {
    mtx_t mutex;
    mtx_init(&mutex, mtx_plain);
    return 0;
}'
+ cc -pthread -x c - -o /dev/null
+ '[' 0 -eq 0 ']'
+ echo 1
....

The problem is that $(CC) (and others) are resolving too early. It should be gcc, not cc, and there are a bunch of other compiler flags that should be there as well.

The next thing I tried is to change := to =. When I did this, the script ran twice: once at the beginning where it looks just like what I showed above, and once at the end, where it looks like this:

...
+ echo '#include <threads.h>
int main() {
    mtx_t mutex;
    mtx_init(&mutex, mtx_plain);
    return 0;
}'
+ gcc -D_FORTIFY_SOURCE=2 -DTHREADS_H_SUPPORT -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt -g -O0 -Wall -pthread -x c - -o /dev/null
+ '[' 0 -eq 0 ']'
+ echo 1
echo THREADS_H_SUPPORT is 1
THREADS_H_SUPPORT is 1
...

This time, it gets the correct CC, PKG_CFLAGS, and all that. However, it's running too late -- this happens after all the .o objects have already been built. The first run is triggered by the ifeq(). Ideally the ifeq() would happen after CC and other variables have been assigned, but that doesn't seem to be the case.

I think it is possible to hack the Makevars so that things will happen in the right order, but everything I'm finding looks pretty messy. It might be better to use a top-level configure script, similar to the one here: https://github.com/jeroen/openssl/blob/master/configure

@deluciaatgfz

Copy link
Copy Markdown

The fix-c-thread branch as it is now works for me on Arch! Thanks.

@wch
wch force-pushed the fix-c-thread branch 2 times, most recently from 9952c2b to dda9192 Compare August 10, 2018 17:20
@jcheng5 jcheng5 changed the title Fix C11 threads.h compatibility [wip] Fix C11 threads.h compatibility Aug 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to compile on linux, "Mutex creation failed" Alpine: Package fails to load with 'Mutex creation failed' error

5 participants