chiark / gitweb /
octave4.4
[nlopt.git] / m4 / ax_c_threadlocal.m4
1 dnl @synopsis AX_C_THREADLOCAL([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
2 dnl @summary determine C keyword for threadlocal storage
3 dnl
4 dnl This macro tries to discover a C keyword to declare variables
5 dnl as having thread-local storage.  Most commonly, this is either
6 dnl __thread [gcc] or __declspec(thread) [Windows].
7 dnl
8 dnl On success, it #defines the THREADLOCAL preprocessor symbol to
9 dnl the appropriate keyword.  You would then use it in C code as, e.g.:
10 dnl     THREADLOCAL int myvariable;
11 dnl
12 dnl ACTION-IF-FOUND is a list of shell commands to run if an thread-local
13 dnl keyword is found, and ACTION-IF-NOT-FOUND is a list of commands
14 dnl to run it if one is not found.  If ACTION-IF-FOUND is not specified,
15 dnl the default action does nothing.
16 dnl
17 dnl @version 2010-05-28
18 dnl @license GPLWithACException
19 dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
20 AC_DEFUN([AX_C_THREADLOCAL],
21 [AC_ARG_WITH(threadlocal,
22   [AC_HELP_STRING([--without-threadlocal], [no thread-local storage keyword])],
23   with_ax_c_threadlocal=$withval, with_ax_c_threadlocal=yes)
24  AC_CACHE_CHECK([for C thread-local keyword], ax_cv_c_threadlocal,
25 [if test "x$with_ax_c_threadlocal" = xno; then
26    ax_cv_c_threadlocal=disabled
27  else
28    ax_cv_c_threadlocal=unsupported
29    AC_LANG_SAVE
30    AC_LANG_C
31    for ax_kw in __thread "__declspec(thread)"; do
32      AC_TRY_COMPILE([], [static $ax_kw int x = 0;], 
33                         [ax_cv_c_threadlocal=$ax_kw; break])
34    done
35    AC_LANG_RESTORE
36  fi
37 ])
38  ax_kw="$ax_cv_c_threadlocal"
39  if test "x$ax_kw" = xunsupported; then ax_kw=""; fi
40  if test "x$ax_kw" = xdisabled; then ax_kw=""; fi
41  AC_DEFINE_UNQUOTED(THREADLOCAL, $ax_kw, [Define to C thread-local keyword, or to nothing if this is not supported in your compiler.])
42  if test "$ax_cv_c_threadlocal" = unsupported; then
43    m4_default([$2],:)
44  else
45    m4_default([$1],:)
46  fi
47 ])