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