apr_threads.m4 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. dnl -------------------------------------------------------- -*- autoconf -*-
  2. dnl Licensed to the Apache Software Foundation (ASF) under one or more
  3. dnl contributor license agreements. See the NOTICE file distributed with
  4. dnl this work for additional information regarding copyright ownership.
  5. dnl The ASF licenses this file to You under the Apache License, Version 2.0
  6. dnl (the "License"); you may not use this file except in compliance with
  7. dnl the License. You may obtain a copy of the License at
  8. dnl
  9. dnl http://www.apache.org/licenses/LICENSE-2.0
  10. dnl
  11. dnl Unless required by applicable law or agreed to in writing, software
  12. dnl distributed under the License is distributed on an "AS IS" BASIS,
  13. dnl WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. dnl See the License for the specific language governing permissions and
  15. dnl limitations under the License.
  16. dnl -----------------------------------------------------------------
  17. dnl apr_threads.m4: APR's autoconf macros for testing thread support
  18. dnl
  19. dnl
  20. dnl APR_CHECK_PTHREADS_H([ ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]])
  21. dnl
  22. dnl gcc issues warnings when parsing AIX 4.3.3's pthread.h
  23. dnl which causes autoconf to incorrectly conclude that
  24. dnl pthreads is not available.
  25. dnl Turn off warnings if we're using gcc.
  26. dnl
  27. AC_DEFUN([APR_CHECK_PTHREADS_H], [
  28. if test "$GCC" = "yes"; then
  29. SAVE_FL="$CPPFLAGS"
  30. CPPFLAGS="$CPPFLAGS -w"
  31. AC_CHECK_HEADERS(pthread.h, [ $1 ] , [ $2 ] )
  32. CPPFLAGS="$SAVE_FL"
  33. else
  34. AC_CHECK_HEADERS(pthread.h, [ $1 ] , [ $2 ] )
  35. fi
  36. ])dnl
  37. dnl
  38. dnl APR_CHECK_PTHREAD_GETSPECIFIC_TWO_ARGS
  39. dnl
  40. AC_DEFUN([APR_CHECK_PTHREAD_GETSPECIFIC_TWO_ARGS], [
  41. AC_CACHE_CHECK(whether pthread_getspecific takes two arguments, ac_cv_pthread_getspecific_two_args,[
  42. AC_TRY_COMPILE([
  43. #include <pthread.h>
  44. ],[
  45. pthread_key_t key;
  46. void *tmp;
  47. pthread_getspecific(key,&tmp);
  48. ],[
  49. ac_cv_pthread_getspecific_two_args=yes
  50. ],[
  51. ac_cv_pthread_getspecific_two_args=no
  52. ])
  53. ])
  54. if test "$ac_cv_pthread_getspecific_two_args" = "yes"; then
  55. AC_DEFINE(PTHREAD_GETSPECIFIC_TAKES_TWO_ARGS, 1, [Define if pthread_getspecific() has two args])
  56. fi
  57. ])dnl
  58. dnl
  59. dnl APR_CHECK_PTHREAD_ATTR_GETDETACHSTATE_ONE_ARG
  60. dnl
  61. AC_DEFUN([APR_CHECK_PTHREAD_ATTR_GETDETACHSTATE_ONE_ARG], [
  62. AC_CACHE_CHECK(whether pthread_attr_getdetachstate takes one argument, ac_cv_pthread_attr_getdetachstate_one_arg,[
  63. AC_TRY_COMPILE([
  64. #include <pthread.h>
  65. ],[
  66. pthread_attr_t *attr;
  67. pthread_attr_getdetachstate(attr);
  68. ],[
  69. ac_cv_pthread_attr_getdetachstate_one_arg=yes
  70. ],[
  71. ac_cv_pthread_attr_getdetachstate_one_arg=no
  72. ])
  73. ])
  74. if test "$ac_cv_pthread_attr_getdetachstate_one_arg" = "yes"; then
  75. AC_DEFINE(PTHREAD_ATTR_GETDETACHSTATE_TAKES_ONE_ARG, 1, [Define if pthread_attr_getdetachstate() has one arg])
  76. fi
  77. ])dnl
  78. dnl
  79. dnl APR_PTHREADS_TRY_RUN(actions-if-success)
  80. dnl
  81. dnl Try running a program which uses pthreads, executing the
  82. dnl actions-if-success commands on success.
  83. dnl
  84. AC_DEFUN([APR_PTHREADS_TRY_RUN], [
  85. AC_TRY_RUN( [
  86. #include <pthread.h>
  87. #include <stddef.h>
  88. void *thread_routine(void *data) {
  89. return data;
  90. }
  91. int main(void) {
  92. pthread_t thd;
  93. pthread_mutexattr_t mattr;
  94. pthread_once_t once_init = PTHREAD_ONCE_INIT;
  95. int data = 1;
  96. pthread_mutexattr_init(&mattr);
  97. return pthread_create(&thd, NULL, thread_routine, &data);
  98. } ], [apr_p_t_r=yes], [apr_p_t_r=no], [apr_p_t_r=no])
  99. if test $apr_p_t_r = yes; then
  100. $1
  101. fi
  102. ])dnl
  103. dnl
  104. dnl APR_PTHREADS_CHECK()
  105. dnl
  106. dnl Try to find a way to enable POSIX threads. Sets the
  107. dnl pthreads_working variable to "yes" on success.
  108. dnl
  109. AC_DEFUN([APR_PTHREADS_CHECK],[
  110. AC_CACHE_CHECK([for CFLAGS needed for pthreads], [apr_cv_pthreads_cflags],
  111. [apr_ptc_cflags=$CFLAGS
  112. for flag in none -kthread -pthread -pthreads -mt -mthreads -Kthread -threads; do
  113. CFLAGS=$apr_ptc_cflags
  114. test "x$flag" != "xnone" && CFLAGS="$CFLAGS $flag"
  115. APR_PTHREADS_TRY_RUN([
  116. apr_cv_pthreads_cflags="$flag"
  117. break
  118. ])
  119. done
  120. CFLAGS=$apr_ptc_cflags
  121. ])
  122. if test -n "$apr_cv_pthreads_cflags"; then
  123. pthreads_working=yes
  124. if test "x$apr_cv_pthreads_cflags" != "xnone"; then
  125. APR_ADDTO(CFLAGS,[$apr_cv_pthreads_cflags])
  126. fi
  127. fi
  128. # The CFLAGS may or may not be sufficient to ensure that libapr
  129. # depends on the pthreads library: some versions of libtool
  130. # drop -pthread when passed on the link line; some versions of
  131. # gcc ignore -pthread when linking a shared object. So always
  132. # try and add the relevant library to LIBS too.
  133. AC_CACHE_CHECK([for LIBS needed for pthreads], [apr_cv_pthreads_lib], [
  134. apr_ptc_libs=$LIBS
  135. for lib in -lpthread -lpthreads -lc_r; do
  136. LIBS="$apr_ptc_libs $lib"
  137. APR_PTHREADS_TRY_RUN([
  138. apr_cv_pthreads_lib=$lib
  139. break
  140. ])
  141. done
  142. LIBS=$apr_ptc_libs
  143. ])
  144. if test -n "$apr_cv_pthreads_lib"; then
  145. pthreads_working=yes
  146. APR_ADDTO(LIBS,[$apr_cv_pthreads_lib])
  147. fi
  148. if test "$pthreads_working" = "yes"; then
  149. threads_result="POSIX Threads found"
  150. else
  151. threads_result="POSIX Threads not found"
  152. fi
  153. ])dnl
  154. dnl
  155. dnl APR_PTHREADS_CHECK_SAVE
  156. dnl APR_PTHREADS_CHECK_RESTORE
  157. dnl
  158. dnl Save the global environment variables that might be modified during
  159. dnl the checks for threading support so that they can restored if the
  160. dnl result is not what the caller wanted.
  161. dnl
  162. AC_DEFUN([APR_PTHREADS_CHECK_SAVE], [
  163. apr_pthsv_CFLAGS="$CFLAGS"
  164. apr_pthsv_LIBS="$LIBS"
  165. ])dnl
  166. AC_DEFUN([APR_PTHREADS_CHECK_RESTORE], [
  167. CFLAGS="$apr_pthsv_CFLAGS"
  168. LIBS="$apr_pthsv_LIBS"
  169. ])dnl
  170. dnl
  171. dnl APR_CHECK_SIGWAIT_ONE_ARG
  172. dnl
  173. AC_DEFUN([APR_CHECK_SIGWAIT_ONE_ARG],[
  174. AC_CACHE_CHECK(whether sigwait takes one argument,ac_cv_sigwait_one_arg,[
  175. AC_TRY_COMPILE([
  176. #if defined(__NETBSD__) || defined(DARWIN)
  177. /* When using the unproven-pthreads package, we need to pull in this
  178. * header to get a prototype for sigwait(). Else things will fail later
  179. * on. XXX Should probably be fixed in the unproven-pthreads package.
  180. * Darwin is declaring sigwait() in the wrong place as well.
  181. */
  182. #include <pthread.h>
  183. #endif
  184. #include <signal.h>
  185. ],[
  186. sigset_t set;
  187. sigwait(&set);
  188. ],[
  189. ac_cv_sigwait_one_arg=yes
  190. ],[
  191. ac_cv_sigwait_one_arg=no
  192. ])])
  193. if test "$ac_cv_sigwait_one_arg" = "yes"; then
  194. AC_DEFINE(SIGWAIT_TAKES_ONE_ARG,1,[ ])
  195. fi
  196. ])
  197. dnl Check for recursive mutex support (per SUSv3).
  198. AC_DEFUN([APR_CHECK_PTHREAD_RECURSIVE_MUTEX], [
  199. AC_CACHE_CHECK([for recursive mutex support], [apr_cv_mutex_recursive],
  200. [AC_TRY_RUN([
  201. #ifndef __sun
  202. #define _XOPEN_SOURCE 600
  203. #endif
  204. #include <sys/types.h>
  205. #include <pthread.h>
  206. #include <stdlib.h>
  207. int main(void) {
  208. pthread_mutexattr_t attr;
  209. pthread_mutex_t m;
  210. exit (pthread_mutexattr_init(&attr)
  211. || pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)
  212. || pthread_mutex_init(&m, &attr));
  213. }], [apr_cv_mutex_recursive=yes], [apr_cv_mutex_recursive=no],
  214. [apr_cv_mutex_recursive=no])])
  215. if test "$apr_cv_mutex_recursive" = "yes"; then
  216. AC_DEFINE([HAVE_PTHREAD_MUTEX_RECURSIVE], 1,
  217. [Define if recursive pthread mutexes are available])
  218. fi
  219. ])
  220. dnl Check for robust process-shared mutex support
  221. AC_DEFUN([APR_CHECK_PTHREAD_ROBUST_SHARED_MUTEX], [
  222. AC_CACHE_CHECK([for robust cross-process mutex support],
  223. [apr_cv_mutex_robust_shared],
  224. [AC_TRY_RUN([
  225. #include <sys/types.h>
  226. #include <pthread.h>
  227. #include <stdlib.h>
  228. int main(int argc, char **argv)
  229. {
  230. pthread_mutex_t mutex;
  231. pthread_mutexattr_t attr;
  232. if (pthread_mutexattr_init(&attr))
  233. exit(1);
  234. if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED))
  235. exit(2);
  236. if (pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP))
  237. exit(3);
  238. if (pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT))
  239. exit(4);
  240. if (pthread_mutex_init(&mutex, &attr))
  241. exit(5);
  242. if (pthread_mutexattr_destroy(&attr))
  243. exit(6);
  244. if (pthread_mutex_destroy(&mutex))
  245. exit(7);
  246. exit(0);
  247. }], [apr_cv_mutex_robust_shared=yes], [apr_cv_mutex_robust_shared=no])])
  248. if test "$apr_cv_mutex_robust_shared" = "yes"; then
  249. AC_DEFINE([HAVE_PTHREAD_MUTEX_ROBUST], 1,
  250. [Define if cross-process robust mutexes are available])
  251. fi
  252. ])