EFL 1.7 svn doobies
[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    solaris*)
31       _efl_enable_posix_threads="yes"
32       _efl_threads_cflags="-mt"
33       _efl_threads_libs="-mt"
34       ;;
35    *)
36       _efl_enable_posix_threads="yes"
37       _efl_threads_cflags="-pthread"
38       _efl_threads_libs="-pthread"
39       ;;
40 esac
41
42 dnl check if the compiler supports POSIX threads
43
44
45 if test "x${_efl_enable_posix_threads}" = "xyes" ; then
46
47    SAVE_CFLAGS=${CFLAGS}
48    CFLAGS="${CFLAGS} ${_efl_threads_cflags}"
49    SAVE_LIBS=${LIBS}
50    LIBS="${LIBS} ${_efl_threads_libs}"
51    AC_LINK_IFELSE(
52       [AC_LANG_PROGRAM([[
53 #include <pthread.h>
54                        ]],
55                        [[
56 pthread_t id;
57 id = pthread_self();
58                        ]])],
59       [
60        _efl_have_posix_threads="yes"
61        AC_DEFINE([EFL_HAVE_POSIX_THREADS], [1], [Define to mention that POSIX threads are supported])
62        AC_DEFINE([EFL_HAVE_THREADS], [1], [Define to mention that POSIX or Win32 threads are supported])
63        EFL_PTHREAD_CFLAGS=${_efl_threads_cflags}
64        EFL_PTHREAD_LIBS=${_efl_threads_libs}
65       ],
66       [_efl_have_posix_threads="no"])
67    CFLAGS=${SAVE_CFLAGS}
68    LIBS=${SAVE_LIBS}
69
70 fi
71
72 AC_MSG_CHECKING([which threads API is used])
73 if test "x${_efl_have_posix_threads}" = "xyes" ; then
74    efl_have_threads="POSIX"
75 else
76    if test "x${_efl_have_win32_threads}" = "xyes" ; then
77       efl_have_threads="Windows"
78    else
79       efl_have_threads="no"
80    fi
81 fi
82 AC_MSG_RESULT([${efl_have_threads}])
83
84 AC_SUBST(EFL_PTHREAD_CFLAGS)
85 AC_SUBST(EFL_PTHREAD_LIBS)
86
87 dnl check if the compiler supports pthreads spinlock
88
89 efl_have_posix_threads_spinlock="no"
90
91 if test "x${_efl_have_posix_threads}" = "xyes" ; then
92
93    SAVE_CFLAGS=${CFLAGS}
94    CFLAGS="${CFLAGS} ${EFL_PTHREAD_CFLAGS}"
95    SAVE_LIBS=${LIBS}
96    LIBS="${LIBS} ${EFL_PTHREAD_LIBS}"
97    AC_LINK_IFELSE(
98       [AC_LANG_PROGRAM([[
99 #include <pthread.h>
100                        ]],
101                        [[
102 pthread_spinlock_t lock;
103 int res;
104 res = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
105                        ]])],
106       [efl_have_posix_threads_spinlock="yes"],
107       [efl_have_posix_threads_spinlock="no"])
108    CFLAGS=${SAVE_CFLAGS}
109    LIBS=${SAVE_LIBS}
110
111 fi
112
113 AC_MSG_CHECKING([whether to build POSIX threads spinlock code])
114 AC_MSG_RESULT([${efl_have_posix_threads_spinlock}])
115
116 if test "x${efl_have_posix_threads_spinlock}" = "xyes" ; then
117    AC_DEFINE([EFL_HAVE_POSIX_THREADS_SPINLOCK], [1], [Define to mention that POSIX threads spinlocks are supported])
118 fi
119
120 dnl Check ON-OFF threads
121
122 _efl_enable_on_off_threads="no"
123 AC_ARG_ENABLE([on-off-threads],
124    [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])],
125    [_efl_enable_on_off_threads="${enableval}"])
126
127 efl_have_on_off_threads="no"
128 if test "x${_efl_have_posix_threads}" = "xyes" && test "x${_efl_enable_on_off_threads}" = "xyes"; then
129    efl_have_on_off_threads="yes"
130    AC_DEFINE([EFL_ON_OFF_THREADS], [1], [make it possible to disable all locks])
131 fi
132 AC_MSG_CHECKING([whether to turn on/off threads lock on demand])
133 AC_MSG_RESULT([${efl_have_on_off_threads}])
134
135 dnl Check debug threads
136
137 _efl_enable_debug_threads="no"
138 AC_ARG_ENABLE([debug-threads],
139    [AC_HELP_STRING([--enable-debug-threads], [disable assert when you forgot to call eina_threads_init])],
140    [_efl_enable_debug_threads="${enableval}"])
141
142 efl_have_debug_threads="no"
143 if test "x${_efl_have_posix_threads}" = "xyes" && test "x${_efl_enable_debug_threads}" = "xyes"; then
144    efl_have_debug_threads="yes"
145    AC_DEFINE([EFL_DEBUG_THREADS], [1], [Assert when forgot to call eina_threads_init])
146 fi
147
148 AS_IF([test "x$_efl_have_posix_threads" = "xyes" || test "x$_efl_have_win32_threads" = "xyes"], [$1], [$2])
149 ])