Eina: pthread detection fixes
[profile/ivi/eina.git] / m4 / common / efl_threads.m4
1 dnl Copyright (C) 2010 Vincent Torri <vtorri at univ-evry dot fr>
2 dnl rwlock code added by Mike Blumenkrantz <mike at zentific dot com>
3 dnl This code is public domain and can be freely used or copied.
4
5 dnl Macro that check if POSIX or Win32 threads library is available or not.
6
7 dnl Usage: EFL_CHECK_THREADS(ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND])
8 dnl Call AC_SUBST(EFL_PTHREAD_CFLAGS)
9 dnl Call AC_SUBST(EFL_PTHREAD_LIBS)
10 dnl Defines EFL_HAVE_POSIX_THREADS or EFL_HAVE_WIN32_THREADS, and EFL_HAVE_THREADS
11
12 AC_DEFUN([EFL_CHECK_THREADS],
13 [
14
15 dnl Generic thread detection
16
17 EFL_PTHREAD_CFLAGS=""
18 EFL_PTHREAD_LIBS=""
19
20 _efl_enable_posix_threads="no"
21 _efl_have_posix_threads="no"
22 _efl_have_win32_threads="no"
23
24 case "$host_os" in
25    mingw*)
26       _efl_have_win32_threads="yes"
27       AC_DEFINE([EFL_HAVE_WIN32_THREADS], [1], [Define to mention that Win32 threads are supported])
28       AC_DEFINE([EFL_HAVE_THREADS], [1], [Define to mention that POSIX or Win32 threads are supported])
29       ;;
30    *)
31       _efl_enable_posix_threads="yes"
32       _efl_threads_cflags="-D_REENTRANT"
33       _efl_threads_libs="-lpthread"
34       ;;
35 esac
36
37 dnl check if the compiler supports POSIX threads
38
39
40 if test "x${_efl_enable_posix_threads}" = "xyes" ; then
41
42    SAVE_CFLAGS=${CFLAGS}
43    CFLAGS="${CFLAGS} ${_efl_threads_cflags}"
44    SAVE_LIBS=${LIBS}
45    LIBS="${LIBS} ${_efl_threads_libs}"
46    AC_LINK_IFELSE(
47       [AC_LANG_PROGRAM([[
48 #include <pthread.h>
49                        ]],
50                        [[
51 pthread_t id;
52 id = pthread_self();
53                        ]])],
54       [
55        _efl_have_posix_threads="yes"
56        AC_DEFINE([EFL_HAVE_POSIX_THREADS], [1], [Define to mention that POSIX threads are supported])
57        AC_DEFINE([EFL_HAVE_THREADS], [1], [Define to mention that POSIX or Win32 threads are supported])
58        EFL_PTHREAD_CFLAGS=${_efl_threads_cflags}
59        EFL_PTHREAD_LIBS=${_efl_threads_libs}
60       ],
61       [_efl_have_posix_threads="no"])
62    CFLAGS=${SAVE_CFLAGS}
63    LIBS=${SAVE_LIBS}
64
65 fi
66
67 AC_MSG_CHECKING([which threads API is used])
68 if test "x${_efl_have_posix_threads}" = "xyes" ; then
69    efl_have_threads="POSIX"
70 else
71    if test "x${_efl_have_win32_threads}" = "xyes" ; then
72       efl_have_threads="Windows"
73    else
74       efl_have_threads="no"
75    fi
76 fi
77 AC_MSG_RESULT([${efl_have_threads}])
78
79 AC_SUBST(EFL_PTHREAD_CFLAGS)
80 AC_SUBST(EFL_PTHREAD_LIBS)
81
82 dnl check if the compiler supports pthreads spinlock
83
84 efl_have_posix_threads_spinlock="no"
85
86 if test "x${_efl_have_posix_threads}" = "xyes" ; then
87
88    SAVE_CFLAGS=${CFLAGS}
89    CFLAGS="${CFLAGS} ${EFL_PTHREAD_CFLAGS}"
90    SAVE_LIBS=${LIBS}
91    LIBS="${LIBS} ${EFL_PTHREAD_LIBS}"
92    AC_LINK_IFELSE(
93       [AC_LANG_PROGRAM([[
94 #include <pthread.h>
95                        ]],
96                        [[
97 pthread_spinlock_t lock;
98 int res;
99 res = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
100                        ]])],
101       [efl_have_posix_threads_spinlock="yes"],
102       [efl_have_posix_threads_spinlock="no"])
103    CFLAGS=${SAVE_CFLAGS}
104    LIBS=${SAVE_LIBS}
105
106 fi
107
108 AC_MSG_CHECKING([whether to build POSIX threads spinlock code])
109 AC_MSG_RESULT([${efl_have_posix_threads_spinlock}])
110
111 if test "x${efl_have_posix_threads_spinlock}" = "xyes" ; then
112    AC_DEFINE([EFL_HAVE_POSIX_THREADS_SPINLOCK], [1], [Define to mention that POSIX threads spinlocks are supported])
113 fi
114
115 dnl Check ON-OFF threads
116
117 _efl_enable_on_off_threads="no"
118 AC_ARG_ENABLE([on-off-threads],
119    [AC_HELP_STRING([--enable-on-off-threads], [only turn this on if you know what you are doing, and do not complain if the world freeze])],
120    [_efl_enable_on_off_threads="${enableval}"])
121
122 efl_have_on_off_threads="no"
123 if test "x${_efl_have_posix_threads}" = "xyes" && test "x${_efl_enable_on_off_threads}" = "xyes"; then
124    efl_have_on_off_threads="yes"
125    AC_DEFINE([EFL_ON_OFF_THREADS], [1], [make it possible to disable all locks])
126 fi
127 AC_MSG_CHECKING([whether to turn on/off threads lock on demand])
128 AC_MSG_RESULT([${efl_have_on_off_threads}])
129
130 dnl Check debug threads
131
132 _efl_enable_debug_threads="no"
133 AC_ARG_ENABLE([debug-threads],
134    [AC_HELP_STRING([--enable-debug-threads], [disable assert when you forgot to call eina_threads_init])],
135    [_efl_enable_debug_threads="${enableval}"])
136
137 efl_have_debug_threads="no"
138 if test "x${_efl_have_posix_threads}" = "xyes" && test "x${_efl_enable_debug_threads}" = "xyes"; then
139    efl_have_debug_threads="yes"
140    AC_DEFINE([EFL_DEBUG_THREADS], [1], [Assert when forgot to call eina_threads_init])
141 fi
142
143 AS_IF([test "x$_efl_have_posix_threads" = "xyes" || test "x$_efl_have_win32_threads" = "xyes"], [$1], [$2])
144 ])