xref: /freebsd/contrib/ntp/sntp/m4/openldap-thread-check.m4 (revision b626f5a73a48f44a31a200291b141e1da408a2ff)
1a25439b6SCy Schubertdnl OpenLDAP Autoconf thread check
2a25439b6SCy Schubertdnl
3a25439b6SCy Schubertdnl This work is part of OpenLDAP Software <http://www.openldap.org/>.
4a25439b6SCy Schubertdnl
5a25439b6SCy Schubertdnl Copyright 1998-2010 The OpenLDAP Foundation.
6a25439b6SCy Schubertdnl All rights reserved.
7a25439b6SCy Schubertdnl
8a25439b6SCy Schubertdnl Redistribution and use in source and binary forms, with or without
9a25439b6SCy Schubertdnl modification, are permitted only as authorized by the OpenLDAP
10a25439b6SCy Schubertdnl Public License.
11a25439b6SCy Schubertdnl
12a25439b6SCy Schubertdnl A copy of this license is available in the file LICENSE-OPENLDAP in
13a25439b6SCy Schubertdnl this directory of the distribution or, alternatively, at
14a25439b6SCy Schubertdnl <http://www.OpenLDAP.org/license.html>.
15a25439b6SCy Schubertdnl
16a25439b6SCy Schubertdnl --------------------------------------------------------------------
17a25439b6SCy Schubert
18*9034852cSGleb Smirnoffdnl This file is a fragment of OpenLDAP's build/openldap.m4 and some
19*9034852cSGleb Smirnoffdnl fragments of OpenLDAP's configure.ac .
20*9034852cSGleb Smirnoff
21a25439b6SCy Schubert#   OL_THREAD_CHECK([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
22a25439b6SCy Schubert
23a25439b6SCy SchubertAC_DEFUN([OL_THREAD_CHECK], [
24a25439b6SCy SchubertAC_REQUIRE([AC_CANONICAL_HOST])
25a25439b6SCy SchubertAC_LANG_SAVE
26a25439b6SCy SchubertAC_LANG([C])
27a25439b6SCy SchubertOL_ARG_WITH(threads,[  --with-threads	  with threads],
28a25439b6SCy Schubert	auto, [auto nt posix mach pth lwp yes no manual] )
29a25439b6SCy Schubert
30a25439b6SCy Schubertcase "$ol_with_threads$host" in
31a25439b6SCy Schubert auto*-*-solaris2.[[0-6]])
32a25439b6SCy Schubert    dnl signals sometimes delivered to wrong thread with Solaris 2.6
33a25439b6SCy Schubert    ol_with_threads=no
34a25439b6SCy Schubert    ;;
35a25439b6SCy Schubertesac
36a25439b6SCy Schubert
37a25439b6SCy Schubertdnl AIX Thread requires we use cc_r or xlc_r.
38a25439b6SCy Schubertdnl But only do this IF AIX and CC is not set
39a25439b6SCy Schubertdnl and threads are auto|yes|posix.
40a25439b6SCy Schubertdnl
41a25439b6SCy Schubertdnl If we find cc_r|xlc_r, force pthreads and assume
42a25439b6SCy Schubertdnl		pthread_create is in $LIBS (ie: don't bring in
43a25439b6SCy Schubertdnl		any additional thread libraries)
44a25439b6SCy Schubertdnl If we do not find cc_r|xlc_r, disable threads
45a25439b6SCy Schubert
46a25439b6SCy Schubertol_aix_threads=no
47a25439b6SCy Schubertcase "$host" in
48a25439b6SCy Schubert*-*-aix*) dnl all AIX is not a good idea.
49a25439b6SCy Schubert	if test -z "$CC" ; then
50a25439b6SCy Schubert		case "$ol_with_threads" in
51a25439b6SCy Schubert		auto | yes |  posix) ol_aix_threads=yes ;;
52a25439b6SCy Schubert		esac
53a25439b6SCy Schubert	fi
54a25439b6SCy Schubert;;
55a25439b6SCy Schubertesac
56a25439b6SCy Schubert
57a25439b6SCy Schubertif test $ol_aix_threads = yes ; then
58a25439b6SCy Schubert	if test -z "${CC}" ; then
59a25439b6SCy Schubert		AC_CHECK_PROGS(CC,cc_r xlc_r cc)
60a25439b6SCy Schubert
61a25439b6SCy Schubert		if test "$CC" = cc ; then
62a25439b6SCy Schubert			dnl no CC! don't allow --with-threads
63a25439b6SCy Schubert			if test $ol_with_threads != auto ; then
64a25439b6SCy Schubert				AC_MSG_ERROR([--with-threads requires cc_r (or other suitable compiler) on AIX])
65a25439b6SCy Schubert			else
66a25439b6SCy Schubert				AC_MSG_WARN([disabling threads, no cc_r on AIX])
67a25439b6SCy Schubert			fi
68a25439b6SCy Schubert			ol_with_threads=no
69a25439b6SCy Schubert  		fi
70a25439b6SCy Schubert	fi
71a25439b6SCy Schubert
72a25439b6SCy Schubert	case ${CC} in cc_r | xlc_r)
73a25439b6SCy Schubert		ol_with_threads=posix
74a25439b6SCy Schubert		ol_cv_pthread_create=yes
75a25439b6SCy Schubert		;;
76a25439b6SCy Schubert	esac
77a25439b6SCy Schubertfi
78a25439b6SCy Schubert
79a25439b6SCy Schubertdnl ----------------------------------------------------------------
80a25439b6SCy Schubertdnl Threads?
81a25439b6SCy Schubertol_link_threads=no
82a25439b6SCy Schubertdnl ol_with_yielding_select=${ol_with_yielding_select:-auto}
83a25439b6SCy SchubertOL_ARG_WITH(yielding_select,[  --with-yielding-select  with yielding select],
84a25439b6SCy Schubert	auto, [auto yes no manual] )
85a25439b6SCy Schubert
86a25439b6SCy Schubertcase $ol_with_threads in auto | yes | nt)
87a25439b6SCy Schubert
88a25439b6SCy Schubert	OL_NT_THREADS
89a25439b6SCy Schubert
90a25439b6SCy Schubert	if test "$ol_cv_nt_threads" = yes ; then
91a25439b6SCy Schubert		ol_link_threads=nt
92a25439b6SCy Schubert		ol_with_threads=found
93a25439b6SCy Schubert		ol_with_yielding_select=yes
94a25439b6SCy Schubert
95a25439b6SCy Schubert		AC_DEFINE([HAVE_NT_SERVICE_MANAGER], [1], [if you have NT Service Manager])
96a25439b6SCy Schubert		AC_DEFINE([HAVE_NT_EVENT_LOG], [1], [if you have NT Event Log])
97a25439b6SCy Schubert	fi
98a25439b6SCy Schubert
99a25439b6SCy Schubert	if test $ol_with_threads = nt ; then
100a25439b6SCy Schubert		AC_MSG_ERROR([could not locate NT Threads])
101a25439b6SCy Schubert	fi
102a25439b6SCy Schubert	;;
103a25439b6SCy Schubertesac
104a25439b6SCy Schubert
105a25439b6SCy Schubertcase $ol_with_threads in auto | yes | posix)
106a25439b6SCy Schubert
107a25439b6SCy Schubert	AC_CHECK_HEADERS(pthread.h)
108a25439b6SCy Schubert
109a25439b6SCy Schubert	if test $ac_cv_header_pthread_h = yes ; then
110a25439b6SCy Schubert		OL_POSIX_THREAD_VERSION
111a25439b6SCy Schubert
112a25439b6SCy Schubert		if test $ol_cv_pthread_version != 0 ; then
113a25439b6SCy Schubert			AC_DEFINE_UNQUOTED([HAVE_PTHREADS], [$ol_cv_pthread_version],
114a25439b6SCy Schubert				[define to pthreads API spec revision])
115a25439b6SCy Schubert		else
116a25439b6SCy Schubert			AC_MSG_ERROR([unknown pthread version])
117a25439b6SCy Schubert		fi
118a25439b6SCy Schubert
119a25439b6SCy Schubert		# consider threads found
120a25439b6SCy Schubert		ol_with_threads=found
121a25439b6SCy Schubert
122a25439b6SCy Schubert		OL_HEADER_LINUX_THREADS
123a25439b6SCy Schubert		OL_HEADER_GNU_PTH_PTHREAD_H
124a25439b6SCy Schubert
125a25439b6SCy Schubert		if test $ol_cv_header_gnu_pth_pthread_h = no ; then
126a25439b6SCy Schubert			AC_CHECK_HEADERS(sched.h)
127a25439b6SCy Schubert		fi
128a25439b6SCy Schubert
129a25439b6SCy Schubert		dnl Now the hard part, how to link?
130a25439b6SCy Schubert		dnl
131a25439b6SCy Schubert		dnl currently supported checks:
132a25439b6SCy Schubert		dnl
133a25439b6SCy Schubert		dnl Check for no flags
134a25439b6SCy Schubert		dnl 	pthread_create() in $LIBS
135a25439b6SCy Schubert		dnl
136a25439b6SCy Schubert		dnl Check special pthread (final) flags
137a25439b6SCy Schubert		dnl 	[skipped] pthread_create() with -mt (Solaris) [disabled]
138a25439b6SCy Schubert		dnl 	pthread_create() with -kthread (FreeBSD)
139a25439b6SCy Schubert		dnl 	pthread_create() with -pthread (FreeBSD/Digital Unix)
140a25439b6SCy Schubert		dnl 	pthread_create() with -pthreads (?)
141a25439b6SCy Schubert		dnl 	pthread_create() with -mthreads (AIX)
142a25439b6SCy Schubert		dnl 	pthread_create() with -thread (?)
143a25439b6SCy Schubert		dnl
144a25439b6SCy Schubert		dnl Check pthread (final) libraries
145a25439b6SCy Schubert		dnl 	pthread_mutex_unlock() in -lpthread -lmach -lexc -lc_r (OSF/1)
146a25439b6SCy Schubert		dnl 	pthread_mutex_lock() in -lpthread -lmach -lexc (OSF/1)
147a25439b6SCy Schubert		dnl 	[skipped] pthread_mutex_trylock() in -lpthread -lexc (OSF/1)
148a25439b6SCy Schubert		dnl 	pthread_join() -Wl,-woff,85 -lpthread (IRIX)
149a25439b6SCy Schubert		dnl 	pthread_create() in -lpthread (many)
150a25439b6SCy Schubert		dnl 	pthread_create() in -lc_r (FreeBSD)
151a25439b6SCy Schubert		dnl
152a25439b6SCy Schubert		dnl Check pthread (draft4) flags (depreciated)
153a25439b6SCy Schubert		dnl 	pthread_create() with -threads (OSF/1)
154a25439b6SCy Schubert		dnl
155a25439b6SCy Schubert		dnl Check pthread (draft4) libraries (depreciated)
156a25439b6SCy Schubert		dnl 	pthread_mutex_unlock() in -lpthreads -lmach -lexc -lc_r (OSF/1)
157a25439b6SCy Schubert		dnl 	pthread_mutex_lock() in -lpthreads -lmach -lexc (OSF/1)
158a25439b6SCy Schubert		dnl 	pthread_mutex_trylock() in -lpthreads -lexc (OSF/1)
159a25439b6SCy Schubert		dnl 	pthread_create() in -lpthreads (many)
160a25439b6SCy Schubert		dnl
161a25439b6SCy Schubert
162a25439b6SCy Schubert		dnl pthread_create in $LIBS
163a25439b6SCy Schubert		AC_CACHE_CHECK([for pthread_create in default libraries],
164a25439b6SCy Schubert			ol_cv_pthread_create,[
165a25439b6SCy Schubert			AC_RUN_IFELSE([OL_PTHREAD_TEST_PROGRAM],
166a25439b6SCy Schubert				[ol_cv_pthread_create=yes],
167a25439b6SCy Schubert				[ol_cv_pthread_create=no],
168a25439b6SCy Schubert				[AC_TRY_LINK(OL_PTHREAD_TEST_INCLUDES,OL_PTHREAD_TEST_FUNCTION,
169a25439b6SCy Schubert					[ol_cv_pthread_create=yes],
170a25439b6SCy Schubert					[ol_cv_pthread_create=no])])])
171a25439b6SCy Schubert
172a25439b6SCy Schubert		if test $ol_cv_pthread_create != no ; then
173a25439b6SCy Schubert			ol_link_threads=posix
174a25439b6SCy Schubert			ol_link_pthreads=""
175a25439b6SCy Schubert		fi
176a25439b6SCy Schubert
177a25439b6SCy Schubertdnl		OL_PTHREAD_TRY([-mt],		[ol_cv_pthread_mt])
178a25439b6SCy Schubert		OL_PTHREAD_TRY([-kthread],	[ol_cv_pthread_kthread])
179a25439b6SCy Schubert		OL_PTHREAD_TRY([-pthread],	[ol_cv_pthread_pthread])
180a25439b6SCy Schubert		OL_PTHREAD_TRY([-pthreads],	[ol_cv_pthread_pthreads])
181a25439b6SCy Schubert		OL_PTHREAD_TRY([-mthreads],	[ol_cv_pthread_mthreads])
182a25439b6SCy Schubert		OL_PTHREAD_TRY([-thread],	[ol_cv_pthread_thread])
183a25439b6SCy Schubert
184a25439b6SCy Schubert		OL_PTHREAD_TRY([-lpthread -lmach -lexc -lc_r],
185a25439b6SCy Schubert			[ol_cv_pthread_lpthread_lmach_lexc_lc_r])
186a25439b6SCy Schubert		OL_PTHREAD_TRY([-lpthread -lmach -lexc],
187a25439b6SCy Schubert			[ol_cv_pthread_lpthread_lmach_lexc])
188a25439b6SCy Schubertdnl		OL_PTHREAD_TRY([-lpthread -lexc],
189a25439b6SCy Schubertdnl			[ol_cv_pthread_lpthread_lexc])
190a25439b6SCy Schubert
191a25439b6SCy Schubert		OL_PTHREAD_TRY([-lpthread -Wl,-woff,85],
192a25439b6SCy Schubert			[ol_cv_pthread_lib_lpthread_woff])
193a25439b6SCy Schubert
194a25439b6SCy Schubert		OL_PTHREAD_TRY([-lpthread],	[ol_cv_pthread_lpthread])
195a25439b6SCy Schubert		OL_PTHREAD_TRY([-lc_r],		[ol_cv_pthread_lc_r])
196a25439b6SCy Schubert
197a25439b6SCy Schubert		OL_PTHREAD_TRY([-threads],	[ol_cv_pthread_threads])
198a25439b6SCy Schubert
199a25439b6SCy Schubert		OL_PTHREAD_TRY([-lpthreads -lmach -lexc -lc_r],
200a25439b6SCy Schubert			[ol_cv_pthread_lpthreads_lmach_lexc_lc_r])
201a25439b6SCy Schubert		OL_PTHREAD_TRY([-lpthreads -lmach -lexc],
202a25439b6SCy Schubert			[ol_cv_pthread_lpthreads_lmach_lexc])
203a25439b6SCy Schubert		OL_PTHREAD_TRY([-lpthreads -lexc],
204a25439b6SCy Schubert			[ol_cv_pthread_lpthreads_lexc])
205a25439b6SCy Schubert
206a25439b6SCy Schubert		OL_PTHREAD_TRY([-lpthreads],[ol_cv_pthread_lib_lpthreads])
207a25439b6SCy Schubert
208*9034852cSGleb SmirnoffAC_MSG_NOTICE([ol_link_threads: <$ol_link_threads> ol_link_pthreads <$ol_link_pthreads>])
209*9034852cSGleb Smirnoff
210a25439b6SCy Schubert		if test $ol_link_threads != no ; then
211a25439b6SCy Schubert			LTHREAD_LIBS="$LTHREAD_LIBS $ol_link_pthreads"
212a25439b6SCy Schubert
213a25439b6SCy Schubert			dnl save flags
214a25439b6SCy Schubert			save_CPPFLAGS="$CPPFLAGS"
215a25439b6SCy Schubert			save_LIBS="$LIBS"
216a25439b6SCy Schubert			LIBS="$LTHREAD_LIBS $LIBS"
217a25439b6SCy Schubert
218a25439b6SCy Schubert			dnl All POSIX Thread (final) implementations should have
219a25439b6SCy Schubert			dnl sched_yield instead of pthread yield.
220a25439b6SCy Schubert			dnl check for both, and thr_yield for Solaris
221a25439b6SCy Schubert			AC_CHECK_FUNCS(sched_yield pthread_yield thr_yield)
222a25439b6SCy Schubert
223a25439b6SCy Schubert			if test $ac_cv_func_sched_yield = no &&
224a25439b6SCy Schubert			   test $ac_cv_func_pthread_yield = no &&
225a25439b6SCy Schubert			   test $ac_cv_func_thr_yield = no ; then
226a25439b6SCy Schubert				dnl Digital UNIX has sched_yield() in -lrt
227a25439b6SCy Schubert				AC_CHECK_LIB(rt, sched_yield,
228a25439b6SCy Schubert					[LTHREAD_LIBS="$LTHREAD_LIBS -lrt"
229a25439b6SCy Schubert					AC_DEFINE([HAVE_SCHED_YIELD], [1],
230a25439b6SCy Schubert						[Define if you have the sched_yield function.])
231a25439b6SCy Schubert					ac_cv_func_sched_yield=yes],
232a25439b6SCy Schubert					[ac_cv_func_sched_yield=no])
233a25439b6SCy Schubert			fi
234a25439b6SCy Schubert			if test $ac_cv_func_sched_yield = no &&
235a25439b6SCy Schubert			   test $ac_cv_func_pthread_yield = no &&
236a25439b6SCy Schubert			   test "$ac_cv_func_thr_yield" = no ; then
237a25439b6SCy Schubert				AC_MSG_WARN([could not locate sched_yield() or pthread_yield()])
238a25439b6SCy Schubert			fi
239a25439b6SCy Schubert
240a25439b6SCy Schubert			dnl Check functions for compatibility
241a25439b6SCy Schubert			AC_CHECK_FUNCS(pthread_kill)
242a25439b6SCy Schubert
243a25439b6SCy Schubert			dnl Check for pthread_rwlock_destroy with <pthread.h>
244a25439b6SCy Schubert			dnl as pthread_rwlock_t may not be defined.
245a25439b6SCy Schubert			AC_CACHE_CHECK([for pthread_rwlock_destroy with <pthread.h>],
246a25439b6SCy Schubert				[ol_cv_func_pthread_rwlock_destroy], [
247a25439b6SCy Schubert				dnl save the flags
248a25439b6SCy Schubert				AC_LINK_IFELSE([AC_LANG_PROGRAM([[
249a25439b6SCy Schubert#include <pthread.h>
250a25439b6SCy Schubertpthread_rwlock_t rwlock;
251a25439b6SCy Schubert]], [[pthread_rwlock_destroy(&rwlock);]])],[ol_cv_func_pthread_rwlock_destroy=yes],[ol_cv_func_pthread_rwlock_destroy=no])
252a25439b6SCy Schubert			])
253a25439b6SCy Schubert			if test $ol_cv_func_pthread_rwlock_destroy = yes ; then
254a25439b6SCy Schubert				AC_DEFINE([HAVE_PTHREAD_RWLOCK_DESTROY], [1],
255a25439b6SCy Schubert					[define if you have pthread_rwlock_destroy function])
256a25439b6SCy Schubert			fi
257a25439b6SCy Schubert
258a25439b6SCy Schubert			dnl Check for pthread_detach with <pthread.h> inclusion
259a25439b6SCy Schubert			dnl as it's symbol may have been mangled.
260a25439b6SCy Schubert			AC_CACHE_CHECK([for pthread_detach with <pthread.h>],
261a25439b6SCy Schubert				[ol_cv_func_pthread_detach], [
262a25439b6SCy Schubert				dnl save the flags
263a25439b6SCy Schubert				AC_LINK_IFELSE([AC_LANG_PROGRAM([[
264a25439b6SCy Schubert#include <pthread.h>
265a25439b6SCy Schubert#ifndef NULL
266a25439b6SCy Schubert#define NULL (void*)0
267a25439b6SCy Schubert#endif
268a25439b6SCy Schubert]], [[pthread_detach(NULL);]])],[ol_cv_func_pthread_detach=yes],[ol_cv_func_pthread_detach=no])
269a25439b6SCy Schubert			])
270a25439b6SCy Schubert
271a25439b6SCy Schubert			if test $ol_cv_func_pthread_detach = no ; then
272a25439b6SCy Schubert				AC_MSG_ERROR([could not locate pthread_detach()])
273a25439b6SCy Schubert			fi
274a25439b6SCy Schubert
275a25439b6SCy Schubert			AC_DEFINE([HAVE_PTHREAD_DETACH], [1],
276a25439b6SCy Schubert				[define if you have pthread_detach function])
277a25439b6SCy Schubert
278a25439b6SCy Schubert			dnl Check for setconcurreny functions
279a25439b6SCy Schubert			AC_CHECK_FUNCS(	\
280a25439b6SCy Schubert				pthread_setconcurrency \
281a25439b6SCy Schubert				pthread_getconcurrency \
282a25439b6SCy Schubert				thr_setconcurrency \
283a25439b6SCy Schubert				thr_getconcurrency \
284a25439b6SCy Schubert			)
285a25439b6SCy Schubert
286a25439b6SCy Schubert			OL_SYS_LINUX_THREADS
287a25439b6SCy Schubert			OL_LINUX_THREADS
288a25439b6SCy Schubert
289a25439b6SCy Schubert			if test $ol_cv_linux_threads = error; then
290a25439b6SCy Schubert				AC_MSG_ERROR([LinuxThreads header/library mismatch]);
291a25439b6SCy Schubert			fi
292a25439b6SCy Schubert
293a25439b6SCy Schubert			AC_CACHE_CHECK([if pthread_create() works],
294a25439b6SCy Schubert				ol_cv_pthread_create_works,[
295a25439b6SCy Schubert			AC_RUN_IFELSE([OL_PTHREAD_TEST_PROGRAM],
296a25439b6SCy Schubert				[ol_cv_pthread_create_works=yes],
297a25439b6SCy Schubert				[ol_cv_pthread_create_works=no],
298a25439b6SCy Schubert				[dnl assume yes
299a25439b6SCy Schubert				ol_cv_pthread_create_works=yes])])
300a25439b6SCy Schubert
301a25439b6SCy Schubert			if test $ol_cv_pthread_create_works = no ; then
302a25439b6SCy Schubert				AC_MSG_ERROR([pthread_create is not usable, check environment settings])
303a25439b6SCy Schubert			fi
304a25439b6SCy Schubert
305a25439b6SCy Schubert			ol_replace_broken_yield=no
306a25439b6SCy Schubertdnl			case "$host" in
307a25439b6SCy Schubertdnl			*-*-linux*)
308a25439b6SCy Schubertdnl				AC_CHECK_FUNCS(nanosleep)
309a25439b6SCy Schubertdnl				ol_replace_broken_yield=yes
310a25439b6SCy Schubertdnl			;;
311a25439b6SCy Schubertdnl			esac
312a25439b6SCy Schubert
313a25439b6SCy Schubert			if test $ol_replace_broken_yield = yes ; then
314a25439b6SCy Schubert				AC_DEFINE([REPLACE_BROKEN_YIELD], [1],
315a25439b6SCy Schubert					[define if sched_yield yields the entire process])
316a25439b6SCy Schubert			fi
317a25439b6SCy Schubert
318a25439b6SCy Schubert			dnl Check if select causes an yield
319a25439b6SCy Schubert			if test x$ol_with_yielding_select = xauto ; then
320a25439b6SCy Schubert				AC_CACHE_CHECK([if select yields when using pthreads],
321a25439b6SCy Schubert					ol_cv_pthread_select_yields,[
322a25439b6SCy Schubert				AC_RUN_IFELSE([AC_LANG_SOURCE([[
323a25439b6SCy Schubert#include <sys/types.h>
324a25439b6SCy Schubert#include <sys/time.h>
325a25439b6SCy Schubert#include <unistd.h>
326a25439b6SCy Schubert#include <pthread.h>
327a25439b6SCy Schubert#ifndef NULL
328a25439b6SCy Schubert#define NULL (void*) 0
329a25439b6SCy Schubert#endif
330a25439b6SCy Schubert
331a25439b6SCy Schubertstatic int fildes[2];
332a25439b6SCy Schubert
333a25439b6SCy Schubertstatic void *task(p)
334a25439b6SCy Schubert	void *p;
335a25439b6SCy Schubert{
336a25439b6SCy Schubert	int i;
337a25439b6SCy Schubert	struct timeval tv;
338a25439b6SCy Schubert
339a25439b6SCy Schubert	fd_set rfds;
340a25439b6SCy Schubert
341a25439b6SCy Schubert	tv.tv_sec=10;
342a25439b6SCy Schubert	tv.tv_usec=0;
343a25439b6SCy Schubert
344a25439b6SCy Schubert	FD_ZERO(&rfds);
345a25439b6SCy Schubert	FD_SET(fildes[0], &rfds);
346a25439b6SCy Schubert
347a25439b6SCy Schubert	/* we're not interested in any fds */
348a25439b6SCy Schubert	i = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
349a25439b6SCy Schubert
350a25439b6SCy Schubert	if(i < 0) {
351a25439b6SCy Schubert		perror("select");
352a25439b6SCy Schubert		exit(10);
353a25439b6SCy Schubert	}
354a25439b6SCy Schubert
355a25439b6SCy Schubert	exit(0); /* if we exit here, the select blocked the whole process */
356a25439b6SCy Schubert}
357a25439b6SCy Schubert
358a25439b6SCy Schubertint main(argc, argv)
359a25439b6SCy Schubert	int argc;
360a25439b6SCy Schubert	char **argv;
361a25439b6SCy Schubert{
362a25439b6SCy Schubert	pthread_t t;
363a25439b6SCy Schubert
364a25439b6SCy Schubert	/* create a pipe to select */
365a25439b6SCy Schubert	if(pipe(&fildes[0])) {
366a25439b6SCy Schubert		perror("select");
367a25439b6SCy Schubert		exit(1);
368a25439b6SCy Schubert	}
369a25439b6SCy Schubert
370a25439b6SCy Schubert#ifdef HAVE_PTHREAD_SETCONCURRENCY
371a25439b6SCy Schubert	(void) pthread_setconcurrency(2);
372a25439b6SCy Schubert#else
373a25439b6SCy Schubert#ifdef HAVE_THR_SETCONCURRENCY
374a25439b6SCy Schubert	/* Set Solaris LWP concurrency to 2 */
375a25439b6SCy Schubert	thr_setconcurrency(2);
376a25439b6SCy Schubert#endif
377a25439b6SCy Schubert#endif
378a25439b6SCy Schubert
379a25439b6SCy Schubert#if HAVE_PTHREADS < 6
380a25439b6SCy Schubert	pthread_create(&t, pthread_attr_default, task, NULL);
381a25439b6SCy Schubert#else
382a25439b6SCy Schubert	pthread_create(&t, NULL, task, NULL);
383a25439b6SCy Schubert#endif
384a25439b6SCy Schubert
385a25439b6SCy Schubert	/* make sure task runs first */
386a25439b6SCy Schubert#ifdef HAVE_THR_YIELD
387a25439b6SCy Schubert	thr_yield();
388a25439b6SCy Schubert#elif defined( HAVE_SCHED_YIELD )
389a25439b6SCy Schubert	sched_yield();
390a25439b6SCy Schubert#elif defined( HAVE_PTHREAD_YIELD )
391a25439b6SCy Schubert	pthread_yield();
392a25439b6SCy Schubert#endif
393a25439b6SCy Schubert
394a25439b6SCy Schubert	exit(2);
395a25439b6SCy Schubert}]])],[ol_cv_pthread_select_yields=no],[ol_cv_pthread_select_yields=yes],[ol_cv_pthread_select_yields=cross])])
396a25439b6SCy Schubert
397a25439b6SCy Schubert				if test $ol_cv_pthread_select_yields = cross ; then
398a25439b6SCy Schubert					AC_MSG_ERROR([crossing compiling: use --with-yielding-select=yes|no|manual])
399a25439b6SCy Schubert				fi
400a25439b6SCy Schubert
401a25439b6SCy Schubert				if test $ol_cv_pthread_select_yields = yes ; then
402a25439b6SCy Schubert					ol_with_yielding_select=yes
403a25439b6SCy Schubert				fi
404a25439b6SCy Schubert			fi
405a25439b6SCy Schubert
406a25439b6SCy Schubert			dnl restore flags
407a25439b6SCy Schubert			CPPFLAGS="$save_CPPFLAGS"
408a25439b6SCy Schubert			LIBS="$save_LIBS"
409a25439b6SCy Schubert		else
410a25439b6SCy Schubert			AC_MSG_ERROR([could not locate usable POSIX Threads])
411a25439b6SCy Schubert		fi
412a25439b6SCy Schubert	fi
413a25439b6SCy Schubert
414a25439b6SCy Schubert	if test $ol_with_threads = posix ; then
415a25439b6SCy Schubert		AC_MSG_ERROR([could not locate POSIX Threads])
416a25439b6SCy Schubert	fi
417a25439b6SCy Schubert	;;
418a25439b6SCy Schubertesac
419a25439b6SCy Schubert
420a25439b6SCy Schubertcase $ol_with_threads in auto | yes | mach)
421a25439b6SCy Schubert
422a25439b6SCy Schubert	dnl check for Mach CThreads
423a25439b6SCy Schubert	AC_CHECK_HEADERS(mach/cthreads.h cthreads.h)
424a25439b6SCy Schubert	if test $ac_cv_header_mach_cthreads_h = yes ; then
425a25439b6SCy Schubert		ol_with_threads=found
426a25439b6SCy Schubert
427a25439b6SCy Schubert		dnl check for cthreads support in current $LIBS
428a25439b6SCy Schubert		AC_CHECK_FUNC(cthread_fork,[ol_link_threads=yes])
429a25439b6SCy Schubert
430a25439b6SCy Schubert		if test $ol_link_threads = no ; then
431a25439b6SCy Schubert			dnl try -all_load
432a25439b6SCy Schubert			dnl this test needs work
433a25439b6SCy Schubert			AC_CACHE_CHECK([for cthread_fork with -all_load],
434a25439b6SCy Schubert				[ol_cv_cthread_all_load], [
435a25439b6SCy Schubert				dnl save the flags
436a25439b6SCy Schubert				save_LIBS="$LIBS"
437a25439b6SCy Schubert				LIBS="-all_load $LIBS"
438a25439b6SCy Schubert				AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <mach/cthreads.h>]], [[
439a25439b6SCy Schubert					cthread_fork((void *)0, (void *)0);
440a25439b6SCy Schubert					]])],[ol_cv_cthread_all_load=yes],[ol_cv_cthread_all_load=no])
441a25439b6SCy Schubert				dnl restore the LIBS
442a25439b6SCy Schubert				LIBS="$save_LIBS"
443a25439b6SCy Schubert			])
444a25439b6SCy Schubert
445a25439b6SCy Schubert			if test $ol_cv_cthread_all_load = yes ; then
446a25439b6SCy Schubert				LTHREAD_LIBS="$LTHREAD_LIBS -all_load"
447a25439b6SCy Schubert				ol_link_threads=mach
448a25439b6SCy Schubert				ol_with_threads=found
449a25439b6SCy Schubert			fi
450a25439b6SCy Schubert		fi
451a25439b6SCy Schubert
452a25439b6SCy Schubert	elif test $ac_cv_header_cthreads_h = yes ; then
453a25439b6SCy Schubert		dnl Hurd variant of Mach Cthreads
454a25439b6SCy Schubert		dnl uses <cthreads.h> and -lthreads
455a25439b6SCy Schubert
456a25439b6SCy Schubert		ol_with_threads=found
457a25439b6SCy Schubert
458a25439b6SCy Schubert		dnl save the flags
459a25439b6SCy Schubert		save_LIBS="$LIBS"
460a25439b6SCy Schubert		LIBS="$LIBS -lthreads"
461a25439b6SCy Schubert		AC_CHECK_FUNC(cthread_fork,[ol_link_threads=yes])
462a25439b6SCy Schubert		LIBS="$save_LIBS"
463a25439b6SCy Schubert
464a25439b6SCy Schubert		if test $ol_link_threads = yes ; then
465a25439b6SCy Schubert			LTHREAD_LIBS="-lthreads"
466a25439b6SCy Schubert			ol_link_threads=mach
467a25439b6SCy Schubert			ol_with_threads=found
468a25439b6SCy Schubert		else
469a25439b6SCy Schubert			AC_MSG_ERROR([could not link with Mach CThreads])
470a25439b6SCy Schubert		fi
471a25439b6SCy Schubert
472a25439b6SCy Schubert	elif test $ol_with_threads = mach ; then
473a25439b6SCy Schubert		AC_MSG_ERROR([could not locate Mach CThreads])
474a25439b6SCy Schubert	fi
475a25439b6SCy Schubert
476a25439b6SCy Schubert	if test $ol_link_threads = mach ; then
477a25439b6SCy Schubert		AC_DEFINE([HAVE_MACH_CTHREADS], [1],
478a25439b6SCy Schubert			[define if you have Mach Cthreads])
479a25439b6SCy Schubert	elif test $ol_with_threads = found ; then
480a25439b6SCy Schubert		AC_MSG_ERROR([could not link with Mach CThreads])
481a25439b6SCy Schubert	fi
482a25439b6SCy Schubert	;;
483a25439b6SCy Schubertesac
484a25439b6SCy Schubert
485a25439b6SCy Schubertcase $ol_with_threads in auto | yes | pth)
486a25439b6SCy Schubert
487a25439b6SCy Schubert	AC_CHECK_HEADERS(pth.h)
488a25439b6SCy Schubert
489a25439b6SCy Schubert	if test $ac_cv_header_pth_h = yes ; then
490a25439b6SCy Schubert		AC_CHECK_LIB(pth, pth_version, [have_pth=yes], [have_pth=no])
491a25439b6SCy Schubert
492a25439b6SCy Schubert		if test $have_pth = yes ; then
493a25439b6SCy Schubert			AC_DEFINE([HAVE_GNU_PTH], [1], [if you have GNU Pth])
494a25439b6SCy Schubert			LTHREAD_LIBS="$LTHREAD_LIBS -lpth"
495a25439b6SCy Schubert			ol_link_threads=pth
496a25439b6SCy Schubert			ol_with_threads=found
497a25439b6SCy Schubert
498a25439b6SCy Schubert			if test x$ol_with_yielding_select = xauto ; then
499a25439b6SCy Schubert				ol_with_yielding_select=yes
500a25439b6SCy Schubert			fi
501a25439b6SCy Schubert		fi
502a25439b6SCy Schubert	fi
503a25439b6SCy Schubert	;;
504a25439b6SCy Schubertesac
505a25439b6SCy Schubert
506a25439b6SCy Schubertcase $ol_with_threads in auto | yes | lwp)
507a25439b6SCy Schubert
508a25439b6SCy Schubert	dnl check for SunOS5 LWP
509a25439b6SCy Schubert	AC_CHECK_HEADERS(thread.h synch.h)
510a25439b6SCy Schubert	if test $ac_cv_header_thread_h = yes &&
511a25439b6SCy Schubert	   test $ac_cv_header_synch_h = yes ; then
512a25439b6SCy Schubert		AC_CHECK_LIB(thread, thr_create, [have_thr=yes], [have_thr=no])
513a25439b6SCy Schubert
514a25439b6SCy Schubert		if test $have_thr = yes ; then
515a25439b6SCy Schubert			AC_DEFINE([HAVE_THR], [1],
516a25439b6SCy Schubert				[if you have Solaris LWP (thr) package])
517a25439b6SCy Schubert			LTHREAD_LIBS="$LTHREAD_LIBS -lthread"
518a25439b6SCy Schubert			ol_link_threads=thr
519a25439b6SCy Schubert
520a25439b6SCy Schubert			if test x$ol_with_yielding_select = xauto ; then
521a25439b6SCy Schubert				ol_with_yielding_select=yes
522a25439b6SCy Schubert			fi
523a25439b6SCy Schubert
524a25439b6SCy Schubert			dnl Check for setconcurrency functions
525a25439b6SCy Schubert			AC_CHECK_FUNCS(	\
526a25439b6SCy Schubert				thr_setconcurrency \
527a25439b6SCy Schubert				thr_getconcurrency \
528a25439b6SCy Schubert			)
529a25439b6SCy Schubert		fi
530a25439b6SCy Schubert	fi
531a25439b6SCy Schubert
532a25439b6SCy Schubert	dnl check for SunOS4 LWP
533a25439b6SCy Schubert	AC_CHECK_HEADERS(lwp/lwp.h)
534a25439b6SCy Schubert	if test $ac_cv_header_lwp_lwp_h = yes ; then
535a25439b6SCy Schubert		AC_CHECK_LIB(lwp, lwp_create, [have_lwp=yes], [have_lwp=no])
536a25439b6SCy Schubert
537a25439b6SCy Schubert		if test $have_lwp = yes ; then
538a25439b6SCy Schubert			AC_DEFINE([HAVE_LWP], [1],
539a25439b6SCy Schubert				[if you have SunOS LWP package])
540a25439b6SCy Schubert			LTHREAD_LIBS="$LTHREAD_LIBS -llwp"
541a25439b6SCy Schubert			ol_link_threads=lwp
542a25439b6SCy Schubert
543a25439b6SCy Schubert			if test x$ol_with_yielding_select = xauto ; then
544a25439b6SCy Schubert				ol_with_yielding_select=no
545a25439b6SCy Schubert			fi
546a25439b6SCy Schubert		fi
547a25439b6SCy Schubert	fi
548a25439b6SCy Schubert	;;
549a25439b6SCy Schubertesac
550a25439b6SCy Schubert
551a25439b6SCy Schubertif test $ol_with_yielding_select = yes ; then
552a25439b6SCy Schubert	AC_DEFINE([HAVE_YIELDING_SELECT], [1],
553a25439b6SCy Schubert		[define if select implicitly yields])
554a25439b6SCy Schubertfi
555a25439b6SCy Schubert
556a25439b6SCy Schubertif test $ol_with_threads = manual ; then
557a25439b6SCy Schubert	dnl User thinks he can manually configure threads.
558a25439b6SCy Schubert	ol_link_threads=yes
559a25439b6SCy Schubert
560a25439b6SCy Schubert	AC_MSG_WARN([thread defines and link options must be set manually])
561a25439b6SCy Schubert
562a25439b6SCy Schubert	AC_CHECK_HEADERS(pthread.h sched.h)
563a25439b6SCy Schubert	AC_CHECK_FUNCS(sched_yield pthread_yield)
564a25439b6SCy Schubert	OL_HEADER_LINUX_THREADS
565a25439b6SCy Schubert
566a25439b6SCy Schubert	AC_CHECK_HEADERS(mach/cthreads.h)
567a25439b6SCy Schubert	AC_CHECK_HEADERS(lwp/lwp.h)
568a25439b6SCy Schubert	AC_CHECK_HEADERS(thread.h synch.h)
569a25439b6SCy Schubertfi
570a25439b6SCy Schubert
571a25439b6SCy Schubertif test $ol_link_threads != no && test $ol_link_threads != nt ; then
572a25439b6SCy Schubert	dnl needed to get reentrant/threadsafe versions
573a25439b6SCy Schubert	dnl
574a25439b6SCy Schubert	AC_DEFINE([REENTRANT], [1], [enable thread safety])
575a25439b6SCy Schubert	AC_DEFINE([_REENTRANT], [1], [enable thread safety])
576a25439b6SCy Schubert	AC_DEFINE([THREAD_SAFE], [1], [enable thread safety])
577a25439b6SCy Schubert	AC_DEFINE([_THREAD_SAFE], [1], [enable thread safety])
578a25439b6SCy Schubert	AC_DEFINE([THREADSAFE], [1], [enable thread safety])
579a25439b6SCy Schubert	AC_DEFINE([_THREADSAFE], [1], [enable thread safety])
580a25439b6SCy Schubert	AC_DEFINE([_SGI_MP_SOURCE], [1], [enable thread safety])
581a25439b6SCy Schubert
582a25439b6SCy Schubert	dnl The errno declaration may dependent upon _REENTRANT.
583a25439b6SCy Schubert	dnl If it does, we must link with thread support.
584a25439b6SCy Schubert	AC_CACHE_CHECK([for thread specific errno],
585a25439b6SCy Schubert		[ol_cv_errno_thread_specific], [
586a25439b6SCy Schubert		AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <errno.h>]], [[errno = 0;]])],[ol_cv_errno_thread_specific=yes],[ol_cv_errno_thread_specific=no])
587a25439b6SCy Schubert	])
588a25439b6SCy Schubert
589a25439b6SCy Schubert	dnl The h_errno declaration may dependent upon _REENTRANT.
590a25439b6SCy Schubert	dnl If it does, we must link with thread support.
591a25439b6SCy Schubert	AC_CACHE_CHECK([for thread specific h_errno],
592a25439b6SCy Schubert		[ol_cv_h_errno_thread_specific], [
593a25439b6SCy Schubert		AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <netdb.h>]], [[h_errno = 0;]])],[ol_cv_h_errno_thread_specific=yes],[ol_cv_h_errno_thread_specific=no])
594a25439b6SCy Schubert	])
595a25439b6SCy Schubert
596a25439b6SCy Schubert	if test $ol_cv_errno_thread_specific != yes ||
597a25439b6SCy Schubert	   test $ol_cv_h_errno_thread_specific != yes ; then
598a25439b6SCy Schubert		LIBS="$LTHREAD_LIBS $LIBS"
599a25439b6SCy Schubert		LTHREAD_LIBS=""
600a25439b6SCy Schubert	fi
601a25439b6SCy Schubert
602a25439b6SCy Schubertdnl When in thread environment, use
603a25439b6SCy Schubertdnl		#if defined( HAVE_REENTRANT_FUNCTIONS ) || defined( HAVE_FUNC_R )
604a25439b6SCy Schubertdnl			func_r(...);
605a25439b6SCy Schubertdnl		#else
606a25439b6SCy Schubertdnl		#	if defined( HAVE_THREADS )
607a25439b6SCy Schubertdnl				/* lock */
608a25439b6SCy Schubertdnl		#	endif
609a25439b6SCy Schubertdnl				func(...);
610a25439b6SCy Schubertdnl		#	if defined( HAVE_THREADS )
611a25439b6SCy Schubertdnl				/* unlock */
612a25439b6SCy Schubertdnl		#	endif
613a25439b6SCy Schubertdnl		#endif
614a25439b6SCy Schubertdnl
615a25439b6SCy Schubertdnl HAVE_REENTRANT_FUNCTIONS is derived from:
616a25439b6SCy Schubertdnl		_POSIX_REENTRANT_FUNCTIONS
617a25439b6SCy Schubertdnl		_POSIX_THREAD_SAFE_FUNCTIONS
618a25439b6SCy Schubertdnl		_POSIX_THREADSAFE_FUNCTIONS
619a25439b6SCy Schubertdnl
620a25439b6SCy Schubertdnl		and is currently defined in <ldap_pvt_thread.h>
621a25439b6SCy Schubertdnl
622a25439b6SCy Schubertdnl HAVE_THREADS is defined by <ldap_pvt_thread.h> iff -UNO_THREADS
623a25439b6SCy Schubertdnl
624a25439b6SCy Schubertdnl libldap/*.c should only include <ldap_pvt_thread.h> iff
625a25439b6SCy Schubertdnl LDAP_R_COMPILE is defined.  ie:
626a25439b6SCy Schubertdnl		#ifdef LDAP_R_COMPILE
627a25439b6SCy Schubertdnl		#	include <ldap_pvt_thread.h>
628a25439b6SCy Schubertdnl		#endif
629a25439b6SCy Schubertdnl
630a25439b6SCy Schubertdnl LDAP_R_COMPILE is defined by libldap_r/Makefile.in
631a25439b6SCy Schubertdnl specifically for compiling the threadsafe version of
632a25439b6SCy Schubertdnl	the ldap library (-lldap_r).
633a25439b6SCy Schubertdnl
634a25439b6SCy Schubertdnl	dnl check for reentrant/threadsafe functions
635a25439b6SCy Schubertdnl	dnl
636a25439b6SCy Schubertdnl	dnl note: these should only be used when linking
637a25439b6SCy Schubertdnl	dnl		with $LTHREAD_LIBS
638a25439b6SCy Schubertdnl	dnl
639a25439b6SCy Schubertdnl	save_CPPFLAGS="$CPPFLAGS"
640a25439b6SCy Schubertdnl	save_LIBS="$LIBS"
641a25439b6SCy Schubertdnl	LIBS="$LTHREAD_LIBS $LIBS"
642a25439b6SCy Schubertdnl	AC_CHECK_FUNCS(	\
643a25439b6SCy Schubertdnl		gmtime_r \
644a25439b6SCy Schubertdnl		gethostbyaddr_r gethostbyname_r \
645a25439b6SCy Schubertdnl		feof_unlocked unlocked_feof \
646a25439b6SCy Schubertdnl		putc_unlocked unlocked_putc \
647a25439b6SCy Schubertdnl		flockfile ftrylockfile \
648a25439b6SCy Schubertdnl	)
649a25439b6SCy Schubertdnl	CPPFLAGS="$save_CPPFLAGS"
650a25439b6SCy Schubertdnl	LIBS="$save_LIBS"
651a25439b6SCy Schubertfi
652a25439b6SCy Schubert
653a25439b6SCy Schubertif test $ol_link_threads = no ; then
654a25439b6SCy Schubert	if test $ol_with_threads = yes ; then
655a25439b6SCy Schubert		AC_MSG_ERROR([no suitable thread support])
656a25439b6SCy Schubert	fi
657a25439b6SCy Schubert
658a25439b6SCy Schubert	if test $ol_with_threads = auto ; then
659a25439b6SCy Schubert		AC_MSG_WARN([no suitable thread support, disabling threads])
660a25439b6SCy Schubert		ol_with_threads=no
661a25439b6SCy Schubert	fi
662a25439b6SCy Schubert
663a25439b6SCy Schubert	AC_DEFINE([NO_THREADS], [1],
664a25439b6SCy Schubert		[define if you have (or want) no threads])
665a25439b6SCy Schubert	LTHREAD_LIBS=""
666a25439b6SCy Schubert	BUILD_THREAD=no
667a25439b6SCy Schubertelse
668a25439b6SCy Schubert	BUILD_THREAD=yes
669a25439b6SCy Schubertfi
670a25439b6SCy Schubert
671a25439b6SCy Schubertif test $ol_link_threads != no ; then
672a25439b6SCy Schubert	AC_DEFINE([LDAP_API_FEATURE_X_OPENLDAP_THREAD_SAFE], [1],
673a25439b6SCy Schubert		[define to 1 if library is thread safe])
674a25439b6SCy Schubertfi
675a25439b6SCy Schubert
676a25439b6SCy Schubert# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
677a25439b6SCy Schubertcase "$ol_with_threads" in
678a25439b6SCy Schubert no)
679a25439b6SCy Schubert    ol_pthread_ok=no
680a25439b6SCy Schubert    $2
681a25439b6SCy Schubert    ;;
682a25439b6SCy Schubert *)
683a25439b6SCy Schubert    ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
684a25439b6SCy Schubert    ;;
685a25439b6SCy Schubertesac
686a25439b6SCy Schubert
687a25439b6SCy SchubertAC_LANG_RESTORE
688*9034852cSGleb Smirnoff
689*9034852cSGleb SmirnoffAC_SUBST(BUILD_THREAD)
690*9034852cSGleb SmirnoffAC_SUBST(LTHREAD_LIBS)
691*9034852cSGleb Smirnoff
692a25439b6SCy Schubert])
693