Skip to content

Fix undefined PTHREAD_MUTEX_RECURSIVE compilation error#423

Closed
nathanweeks wants to merge 1 commit intosamtools:developfrom
nathanweeks:issue/pthread_mutex_recursive
Closed

Fix undefined PTHREAD_MUTEX_RECURSIVE compilation error#423
nathanweeks wants to merge 1 commit intosamtools:developfrom
nathanweeks:issue/pthread_mutex_recursive

Conversation

@nathanweeks
Copy link
Copy Markdown
Contributor

A portable (POSIX) way to address issue #420 , which I have also observed in the Cray Linux Environment (based on SuSE 11.3) is to define _XOPEN_SOURCE 700 at the top of the affected file.

@jmarshall
Copy link
Copy Markdown
Member

Seems legit, although I'm not sure we're using anything XSI so might prefer to define _POSIX_C_SOURCE as 200809L (see §2.2.1) — if that works, modulo other bugs on these platforms.

If these platforms use glibc, I suppose the problem is that they predate this fix.

We should perhaps define this for all source files via config.h, or for all source files when it's needed via configure, rather than just for one file. Or is worrying about ODR overly pedantic when it comes to these POSIX mode defines?

@nathanweeks
Copy link
Copy Markdown
Contributor Author

This system does have a pthread.h that predates that fix, and just defining _POSIX_C_SOURCE as 200809L doesn't work. Addding -D_POSIX_C_SOURCE=200809L to the compile command also uncovered a few XSI-isms (in addition to implicitly-declared functions that have been omitted here):

kfunc.c:74:16: error: 'M_SQRT2' undeclared (first use in this function)
  z = fabs(x) * M_SQRT2;
                ^~~~~~~
...
htslib/ksort.h:264:14: warning: implicit declaration of function 'drand48' [-Wimplicit-function-declaration]
    j = (int)(drand48() * i);         \
  ...
errmod.c:88:34: error: 'M_LN10' undeclared (first use in this function)
                 beta[k] = -10. / M_LN10 * logl(sum1 / sum);
                                  ^~~~~~
...
errmod.c:97:45: error: 'M_LN2' undeclared (first use in this function)
             em->lhet[n<<8|k] = lC[n<<8|k] - M_LN2 * n;

As a matter of principle, I agree with enforcing some level of standard conformance globally, as it should enhance portability to lesser-used platforms---e.g., has anybody tried compiling htslib on Solaris recently?---and increase the likelihood of reproducibility (10 years from now will someone still be able to compile this version of htslib on, e.g., RHEL 9.x?). OTOH, I don't want to distract developers from issues currently affecting more users.

@jmarshall
Copy link
Copy Markdown
Member

jmarshall commented Oct 7, 2016

Thanks for the reminder about all those maths constants etc being XSI.

Re Solaris, in fact it compiles and passes the test suite just fine with some small build tweaks
(Oracle Solaris 11.3, Developer Studio 12.5; build with e.g. gmake CC=cc LIBS='-lsocket -lnsl'; remove -pthread from the final link commands, as it seems it doesn't like that when linking).

@jmarshall
Copy link
Copy Markdown
Member

Thanks Nathan; merged as afd9b56.

@jmarshall jmarshall closed this Dec 16, 2016
@jkbonfield
Copy link
Copy Markdown
Contributor

I'm curious whether XOPEN_SOURCE 700 was required, or whether something lower than 500 or 600 worked? I'm just experimenting at the moment with trying to work out what our actual minimum requirements are for compilation environments.

This thread seems to imply 500 is necessary:

idris-lang/Idris-dev#1059 (comment)

@jkbonfield
Copy link
Copy Markdown
Contributor

Regarding -D_POSIX_C_SOURCE=200809L, we're using drand48 instead of rand (random is also xopen or bsd) and usleep (albeit an admittance of failure when it is in use!) so it's not a simple case of ifndef then define for the M_SQRT2 etc defines.

I think I'd prefer to simply state we have a dependency on c99 and _XOPEN_SOURCE=600 or higher, glossing over the slight bit of c11 (anonymous unions) for now until we observe whether it causes issues anywhere.

We'll be testing changes more aggressively on various platforms at some stage, but for now I've done a few code tidyups over here:

https://github.com/jkbonfield/htslib/tree/C99

@nathanweeks nathanweeks deleted the issue/pthread_mutex_recursive branch May 27, 2024 14:02
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.

3 participants