1281348Scydnl OpenLDAP Autoconf thread check
2281348Scydnl
3281348Scydnl This work is part of OpenLDAP Software <http://www.openldap.org/>.
4281348Scydnl
5281348Scydnl Copyright 1998-2010 The OpenLDAP Foundation.
6281348Scydnl All rights reserved.
7281348Scydnl
8281348Scydnl Redistribution and use in source and binary forms, with or without
9281348Scydnl modification, are permitted only as authorized by the OpenLDAP
10281348Scydnl Public License.
11281348Scydnl
12281348Scydnl A copy of this license is available in the file LICENSE-OPENLDAP in
13281348Scydnl this directory of the distribution or, alternatively, at
14281348Scydnl <http://www.OpenLDAP.org/license.html>.
15281348Scydnl
16281348Scydnl --------------------------------------------------------------------
17281348Scy
18289764Sglebiusdnl This file is a fragment of OpenLDAP's build/openldap.m4 and some
19289764Sglebiusdnl fragments of OpenLDAP's configure.ac .
20289764Sglebius
21281348Scy#   OL_THREAD_CHECK([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
22281348Scy
23281348ScyAC_DEFUN([OL_THREAD_CHECK], [
24281348ScyAC_REQUIRE([AC_CANONICAL_HOST])
25281348ScyAC_LANG_SAVE
26281348ScyAC_LANG([C])
27281348ScyOL_ARG_WITH(threads,[  --with-threads	  with threads],
28281348Scy	auto, [auto nt posix mach pth lwp yes no manual] )
29281348Scy
30281348Scycase "$ol_with_threads$host" in
31281348Scy auto*-*-solaris2.[[0-6]])
32281348Scy    dnl signals sometimes delivered to wrong thread with Solaris 2.6
33281348Scy    ol_with_threads=no
34281348Scy    ;;
35281348Scyesac
36281348Scy
37281348Scydnl AIX Thread requires we use cc_r or xlc_r.
38281348Scydnl But only do this IF AIX and CC is not set
39281348Scydnl and threads are auto|yes|posix.
40281348Scydnl
41281348Scydnl If we find cc_r|xlc_r, force pthreads and assume
42281348Scydnl		pthread_create is in $LIBS (ie: don't bring in
43281348Scydnl		any additional thread libraries)
44281348Scydnl If we do not find cc_r|xlc_r, disable threads
45281348Scy
46281348Scyol_aix_threads=no
47281348Scycase "$host" in
48281348Scy*-*-aix*) dnl all AIX is not a good idea.
49281348Scy	if test -z "$CC" ; then
50281348Scy		case "$ol_with_threads" in
51281348Scy		auto | yes |  posix) ol_aix_threads=yes ;;
52281348Scy		esac
53281348Scy	fi
54281348Scy;;
55281348Scyesac
56281348Scy
57281348Scyif test $ol_aix_threads = yes ; then
58281348Scy	if test -z "${CC}" ; then
59281348Scy		AC_CHECK_PROGS(CC,cc_r xlc_r cc)
60281348Scy
61281348Scy		if test "$CC" = cc ; then
62281348Scy			dnl no CC! don't allow --with-threads
63281348Scy			if test $ol_with_threads != auto ; then
64281348Scy				AC_MSG_ERROR([--with-threads requires cc_r (or other suitable compiler) on AIX])
65281348Scy			else
66281348Scy				AC_MSG_WARN([disabling threads, no cc_r on AIX])
67281348Scy			fi
68281348Scy			ol_with_threads=no
69281348Scy  		fi
70281348Scy	fi
71281348Scy
72281348Scy	case ${CC} in cc_r | xlc_r)
73281348Scy		ol_with_threads=posix
74281348Scy		ol_cv_pthread_create=yes
75281348Scy		;;
76281348Scy	esac
77281348Scyfi
78281348Scy
79281348Scydnl ----------------------------------------------------------------
80281348Scydnl Threads?
81281348Scyol_link_threads=no
82281348Scydnl ol_with_yielding_select=${ol_with_yielding_select:-auto}
83281348ScyOL_ARG_WITH(yielding_select,[  --with-yielding-select  with yielding select],
84281348Scy	auto, [auto yes no manual] )
85281348Scy
86281348Scycase $ol_with_threads in auto | yes | nt)
87281348Scy
88281348Scy	OL_NT_THREADS
89281348Scy
90281348Scy	if test "$ol_cv_nt_threads" = yes ; then
91281348Scy		ol_link_threads=nt
92281348Scy		ol_with_threads=found
93281348Scy		ol_with_yielding_select=yes
94281348Scy
95281348Scy		AC_DEFINE([HAVE_NT_SERVICE_MANAGER], [1], [if you have NT Service Manager])
96281348Scy		AC_DEFINE([HAVE_NT_EVENT_LOG], [1], [if you have NT Event Log])
97281348Scy	fi
98281348Scy
99281348Scy	if test $ol_with_threads = nt ; then
100281348Scy		AC_MSG_ERROR([could not locate NT Threads])
101281348Scy	fi
102281348Scy	;;
103281348Scyesac
104281348Scy
105281348Scycase $ol_with_threads in auto | yes | posix)
106281348Scy
107281348Scy	AC_CHECK_HEADERS(pthread.h)
108281348Scy
109281348Scy	if test $ac_cv_header_pthread_h = yes ; then
110281348Scy		OL_POSIX_THREAD_VERSION
111281348Scy
112281348Scy		if test $ol_cv_pthread_version != 0 ; then
113281348Scy			AC_DEFINE_UNQUOTED([HAVE_PTHREADS], [$ol_cv_pthread_version],
114281348Scy				[define to pthreads API spec revision])
115281348Scy		else
116281348Scy			AC_MSG_ERROR([unknown pthread version])
117281348Scy		fi
118281348Scy
119281348Scy		# consider threads found
120281348Scy		ol_with_threads=found
121281348Scy
122281348Scy		OL_HEADER_LINUX_THREADS
123281348Scy		OL_HEADER_GNU_PTH_PTHREAD_H
124281348Scy
125281348Scy		if test $ol_cv_header_gnu_pth_pthread_h = no ; then
126281348Scy			AC_CHECK_HEADERS(sched.h)
127281348Scy		fi
128281348Scy
129281348Scy		dnl Now the hard part, how to link?
130281348Scy		dnl
131281348Scy		dnl currently supported checks:
132281348Scy		dnl
133281348Scy		dnl Check for no flags 
134281348Scy		dnl 	pthread_create() in $LIBS
135281348Scy		dnl
136281348Scy		dnl Check special pthread (final) flags
137281348Scy		dnl 	[skipped] pthread_create() with -mt (Solaris) [disabled]
138281348Scy		dnl 	pthread_create() with -kthread (FreeBSD)
139281348Scy		dnl 	pthread_create() with -pthread (FreeBSD/Digital Unix)
140281348Scy		dnl 	pthread_create() with -pthreads (?)
141281348Scy		dnl 	pthread_create() with -mthreads (AIX)
142281348Scy		dnl 	pthread_create() with -thread (?)
143281348Scy		dnl
144281348Scy		dnl Check pthread (final) libraries
145281348Scy		dnl 	pthread_mutex_unlock() in -lpthread -lmach -lexc -lc_r (OSF/1)
146281348Scy		dnl 	pthread_mutex_lock() in -lpthread -lmach -lexc (OSF/1)
147281348Scy		dnl 	[skipped] pthread_mutex_trylock() in -lpthread -lexc (OSF/1)
148281348Scy		dnl 	pthread_join() -Wl,-woff,85 -lpthread (IRIX)
149281348Scy		dnl 	pthread_create() in -lpthread (many)
150281348Scy		dnl 	pthread_create() in -lc_r (FreeBSD)
151281348Scy		dnl
152281348Scy		dnl Check pthread (draft4) flags (depreciated)
153281348Scy		dnl 	pthread_create() with -threads (OSF/1)
154281348Scy		dnl
155281348Scy		dnl Check pthread (draft4) libraries (depreciated)
156281348Scy		dnl 	pthread_mutex_unlock() in -lpthreads -lmach -lexc -lc_r (OSF/1)
157281348Scy		dnl 	pthread_mutex_lock() in -lpthreads -lmach -lexc (OSF/1)
158281348Scy		dnl 	pthread_mutex_trylock() in -lpthreads -lexc (OSF/1)
159281348Scy		dnl 	pthread_create() in -lpthreads (many)
160281348Scy		dnl
161281348Scy
162281348Scy		dnl pthread_create in $LIBS
163281348Scy		AC_CACHE_CHECK([for pthread_create in default libraries],
164281348Scy			ol_cv_pthread_create,[
165281348Scy			AC_RUN_IFELSE([OL_PTHREAD_TEST_PROGRAM],
166281348Scy				[ol_cv_pthread_create=yes],
167281348Scy				[ol_cv_pthread_create=no],
168281348Scy				[AC_TRY_LINK(OL_PTHREAD_TEST_INCLUDES,OL_PTHREAD_TEST_FUNCTION,
169281348Scy					[ol_cv_pthread_create=yes],
170281348Scy					[ol_cv_pthread_create=no])])])
171281348Scy
172281348Scy		if test $ol_cv_pthread_create != no ; then
173281348Scy			ol_link_threads=posix
174281348Scy			ol_link_pthreads=""
175281348Scy		fi
176281348Scy		
177281348Scydnl		OL_PTHREAD_TRY([-mt],		[ol_cv_pthread_mt])
178281348Scy		OL_PTHREAD_TRY([-kthread],	[ol_cv_pthread_kthread])
179281348Scy		OL_PTHREAD_TRY([-pthread],	[ol_cv_pthread_pthread])
180281348Scy		OL_PTHREAD_TRY([-pthreads],	[ol_cv_pthread_pthreads])
181281348Scy		OL_PTHREAD_TRY([-mthreads],	[ol_cv_pthread_mthreads])
182281348Scy		OL_PTHREAD_TRY([-thread],	[ol_cv_pthread_thread])
183281348Scy
184281348Scy		OL_PTHREAD_TRY([-lpthread -lmach -lexc -lc_r],
185281348Scy			[ol_cv_pthread_lpthread_lmach_lexc_lc_r])
186281348Scy		OL_PTHREAD_TRY([-lpthread -lmach -lexc],
187281348Scy			[ol_cv_pthread_lpthread_lmach_lexc])
188281348Scydnl		OL_PTHREAD_TRY([-lpthread -lexc],
189281348Scydnl			[ol_cv_pthread_lpthread_lexc])
190281348Scy
191281348Scy		OL_PTHREAD_TRY([-lpthread -Wl,-woff,85],
192281348Scy			[ol_cv_pthread_lib_lpthread_woff])
193281348Scy
194281348Scy		OL_PTHREAD_TRY([-lpthread],	[ol_cv_pthread_lpthread])
195281348Scy		OL_PTHREAD_TRY([-lc_r],		[ol_cv_pthread_lc_r])
196281348Scy
197281348Scy		OL_PTHREAD_TRY([-threads],	[ol_cv_pthread_threads])
198281348Scy
199281348Scy		OL_PTHREAD_TRY([-lpthreads -lmach -lexc -lc_r],
200281348Scy			[ol_cv_pthread_lpthreads_lmach_lexc_lc_r])
201281348Scy		OL_PTHREAD_TRY([-lpthreads -lmach -lexc],
202281348Scy			[ol_cv_pthread_lpthreads_lmach_lexc])
203281348Scy		OL_PTHREAD_TRY([-lpthreads -lexc],
204281348Scy			[ol_cv_pthread_lpthreads_lexc])
205281348Scy
206281348Scy		OL_PTHREAD_TRY([-lpthreads],[ol_cv_pthread_lib_lpthreads])
207281348Scy
208289764SglebiusAC_MSG_NOTICE([ol_link_threads: <$ol_link_threads> ol_link_pthreads <$ol_link_pthreads>])
209289764Sglebius
210281348Scy		if test $ol_link_threads != no ; then
211281348Scy			LTHREAD_LIBS="$LTHREAD_LIBS $ol_link_pthreads"
212281348Scy
213281348Scy			dnl save flags
214281348Scy			save_CPPFLAGS="$CPPFLAGS"
215281348Scy			save_LIBS="$LIBS"
216281348Scy			LIBS="$LTHREAD_LIBS $LIBS"
217281348Scy
218281348Scy			dnl All POSIX Thread (final) implementations should have
219281348Scy			dnl sched_yield instead of pthread yield.
220281348Scy			dnl check for both, and thr_yield for Solaris
221281348Scy			AC_CHECK_FUNCS(sched_yield pthread_yield thr_yield)
222281348Scy
223281348Scy			if test $ac_cv_func_sched_yield = no &&
224281348Scy			   test $ac_cv_func_pthread_yield = no &&
225281348Scy			   test $ac_cv_func_thr_yield = no ; then
226281348Scy				dnl Digital UNIX has sched_yield() in -lrt
227281348Scy				AC_CHECK_LIB(rt, sched_yield,
228281348Scy					[LTHREAD_LIBS="$LTHREAD_LIBS -lrt"
229281348Scy					AC_DEFINE([HAVE_SCHED_YIELD], [1],
230281348Scy						[Define if you have the sched_yield function.])
231281348Scy					ac_cv_func_sched_yield=yes],
232281348Scy					[ac_cv_func_sched_yield=no])
233281348Scy			fi
234281348Scy			if test $ac_cv_func_sched_yield = no &&
235281348Scy			   test $ac_cv_func_pthread_yield = no &&
236281348Scy			   test "$ac_cv_func_thr_yield" = no ; then
237281348Scy				AC_MSG_WARN([could not locate sched_yield() or pthread_yield()])
238281348Scy			fi
239281348Scy
240281348Scy			dnl Check functions for compatibility
241281348Scy			AC_CHECK_FUNCS(pthread_kill)
242281348Scy
243281348Scy			dnl Check for pthread_rwlock_destroy with <pthread.h>
244281348Scy			dnl as pthread_rwlock_t may not be defined.
245281348Scy			AC_CACHE_CHECK([for pthread_rwlock_destroy with <pthread.h>],
246281348Scy				[ol_cv_func_pthread_rwlock_destroy], [
247281348Scy				dnl save the flags
248281348Scy				AC_LINK_IFELSE([AC_LANG_PROGRAM([[
249281348Scy#include <pthread.h>
250281348Scypthread_rwlock_t rwlock;
251281348Scy]], [[pthread_rwlock_destroy(&rwlock);]])],[ol_cv_func_pthread_rwlock_destroy=yes],[ol_cv_func_pthread_rwlock_destroy=no])
252281348Scy			])
253281348Scy			if test $ol_cv_func_pthread_rwlock_destroy = yes ; then
254281348Scy				AC_DEFINE([HAVE_PTHREAD_RWLOCK_DESTROY], [1],
255281348Scy					[define if you have pthread_rwlock_destroy function])
256281348Scy			fi
257281348Scy
258281348Scy			dnl Check for pthread_detach with <pthread.h> inclusion
259281348Scy			dnl as it's symbol may have been mangled.
260281348Scy			AC_CACHE_CHECK([for pthread_detach with <pthread.h>],
261281348Scy				[ol_cv_func_pthread_detach], [
262281348Scy				dnl save the flags
263281348Scy				AC_LINK_IFELSE([AC_LANG_PROGRAM([[
264281348Scy#include <pthread.h>
265281348Scy#ifndef NULL
266281348Scy#define NULL (void*)0
267281348Scy#endif
268281348Scy]], [[pthread_detach(NULL);]])],[ol_cv_func_pthread_detach=yes],[ol_cv_func_pthread_detach=no])
269281348Scy			])
270281348Scy
271281348Scy			if test $ol_cv_func_pthread_detach = no ; then
272281348Scy				AC_MSG_ERROR([could not locate pthread_detach()])
273281348Scy			fi
274281348Scy
275281348Scy			AC_DEFINE([HAVE_PTHREAD_DETACH], [1],
276281348Scy				[define if you have pthread_detach function])
277281348Scy
278281348Scy			dnl Check for setconcurreny functions
279281348Scy			AC_CHECK_FUNCS(	\
280281348Scy				pthread_setconcurrency \
281281348Scy				pthread_getconcurrency \
282281348Scy				thr_setconcurrency \
283281348Scy				thr_getconcurrency \
284281348Scy			)
285281348Scy
286281348Scy			OL_SYS_LINUX_THREADS
287281348Scy			OL_LINUX_THREADS
288281348Scy
289281348Scy			if test $ol_cv_linux_threads = error; then
290281348Scy				AC_MSG_ERROR([LinuxThreads header/library mismatch]);
291281348Scy			fi
292281348Scy
293281348Scy			AC_CACHE_CHECK([if pthread_create() works],
294281348Scy				ol_cv_pthread_create_works,[
295281348Scy			AC_RUN_IFELSE([OL_PTHREAD_TEST_PROGRAM],
296281348Scy				[ol_cv_pthread_create_works=yes],
297281348Scy				[ol_cv_pthread_create_works=no],
298281348Scy				[dnl assume yes
299281348Scy				ol_cv_pthread_create_works=yes])])
300281348Scy
301281348Scy			if test $ol_cv_pthread_create_works = no ; then
302281348Scy				AC_MSG_ERROR([pthread_create is not usable, check environment settings])
303281348Scy			fi
304281348Scy
305281348Scy			ol_replace_broken_yield=no
306281348Scydnl			case "$host" in
307281348Scydnl			*-*-linux*) 
308281348Scydnl				AC_CHECK_FUNCS(nanosleep)
309281348Scydnl				ol_replace_broken_yield=yes
310281348Scydnl			;;
311281348Scydnl			esac
312281348Scy
313281348Scy			if test $ol_replace_broken_yield = yes ; then
314281348Scy				AC_DEFINE([REPLACE_BROKEN_YIELD], [1],
315281348Scy					[define if sched_yield yields the entire process])
316281348Scy			fi
317281348Scy
318281348Scy			dnl Check if select causes an yield
319281348Scy			if test x$ol_with_yielding_select = xauto ; then
320281348Scy				AC_CACHE_CHECK([if select yields when using pthreads],
321281348Scy					ol_cv_pthread_select_yields,[
322281348Scy				AC_RUN_IFELSE([AC_LANG_SOURCE([[
323281348Scy#include <sys/types.h>
324281348Scy#include <sys/time.h>
325281348Scy#include <unistd.h>
326281348Scy#include <pthread.h>
327281348Scy#ifndef NULL
328281348Scy#define NULL (void*) 0
329281348Scy#endif
330281348Scy
331281348Scystatic int fildes[2];
332281348Scy
333281348Scystatic void *task(p)
334281348Scy	void *p;
335281348Scy{
336281348Scy	int i;
337281348Scy	struct timeval tv;
338281348Scy
339281348Scy	fd_set rfds;
340281348Scy
341281348Scy	tv.tv_sec=10;
342281348Scy	tv.tv_usec=0;
343281348Scy
344281348Scy	FD_ZERO(&rfds);
345281348Scy	FD_SET(fildes[0], &rfds);
346281348Scy
347281348Scy	/* we're not interested in any fds */
348281348Scy	i = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
349281348Scy
350281348Scy	if(i < 0) {
351281348Scy		perror("select");
352281348Scy		exit(10);
353281348Scy	}
354281348Scy
355281348Scy	exit(0); /* if we exit here, the select blocked the whole process */
356281348Scy}
357281348Scy
358281348Scyint main(argc, argv)
359281348Scy	int argc;
360281348Scy	char **argv;
361281348Scy{
362281348Scy	pthread_t t;
363281348Scy
364281348Scy	/* create a pipe to select */
365281348Scy	if(pipe(&fildes[0])) {
366281348Scy		perror("select");
367281348Scy		exit(1);
368281348Scy	}
369281348Scy
370281348Scy#ifdef HAVE_PTHREAD_SETCONCURRENCY
371281348Scy	(void) pthread_setconcurrency(2);
372281348Scy#else
373281348Scy#ifdef HAVE_THR_SETCONCURRENCY
374281348Scy	/* Set Solaris LWP concurrency to 2 */
375281348Scy	thr_setconcurrency(2);
376281348Scy#endif
377281348Scy#endif
378281348Scy
379281348Scy#if HAVE_PTHREADS < 6
380281348Scy	pthread_create(&t, pthread_attr_default, task, NULL);
381281348Scy#else
382281348Scy	pthread_create(&t, NULL, task, NULL);
383281348Scy#endif
384281348Scy
385281348Scy	/* make sure task runs first */
386281348Scy#ifdef HAVE_THR_YIELD
387281348Scy	thr_yield();
388281348Scy#elif defined( HAVE_SCHED_YIELD )
389281348Scy	sched_yield();
390281348Scy#elif defined( HAVE_PTHREAD_YIELD )
391281348Scy	pthread_yield();
392281348Scy#endif
393281348Scy
394281348Scy	exit(2);
395281348Scy}]])],[ol_cv_pthread_select_yields=no],[ol_cv_pthread_select_yields=yes],[ol_cv_pthread_select_yields=cross])])
396281348Scy
397281348Scy				if test $ol_cv_pthread_select_yields = cross ; then
398281348Scy					AC_MSG_ERROR([crossing compiling: use --with-yielding-select=yes|no|manual])
399281348Scy				fi
400281348Scy
401281348Scy				if test $ol_cv_pthread_select_yields = yes ; then
402281348Scy					ol_with_yielding_select=yes
403281348Scy				fi
404281348Scy			fi
405281348Scy
406281348Scy			dnl restore flags
407281348Scy			CPPFLAGS="$save_CPPFLAGS"
408281348Scy			LIBS="$save_LIBS"
409281348Scy		else
410281348Scy			AC_MSG_ERROR([could not locate usable POSIX Threads])
411281348Scy		fi
412281348Scy	fi
413281348Scy
414281348Scy	if test $ol_with_threads = posix ; then
415281348Scy		AC_MSG_ERROR([could not locate POSIX Threads])
416281348Scy	fi
417281348Scy	;;
418281348Scyesac
419281348Scy
420281348Scycase $ol_with_threads in auto | yes | mach)
421281348Scy
422281348Scy	dnl check for Mach CThreads
423281348Scy	AC_CHECK_HEADERS(mach/cthreads.h cthreads.h)
424281348Scy	if test $ac_cv_header_mach_cthreads_h = yes ; then
425281348Scy		ol_with_threads=found
426281348Scy
427281348Scy		dnl check for cthreads support in current $LIBS
428281348Scy		AC_CHECK_FUNC(cthread_fork,[ol_link_threads=yes])
429281348Scy
430281348Scy		if test $ol_link_threads = no ; then
431281348Scy			dnl try -all_load
432281348Scy			dnl this test needs work
433281348Scy			AC_CACHE_CHECK([for cthread_fork with -all_load],
434281348Scy				[ol_cv_cthread_all_load], [
435281348Scy				dnl save the flags
436281348Scy				save_LIBS="$LIBS"
437281348Scy				LIBS="-all_load $LIBS"
438281348Scy				AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <mach/cthreads.h>]], [[
439281348Scy					cthread_fork((void *)0, (void *)0);
440281348Scy					]])],[ol_cv_cthread_all_load=yes],[ol_cv_cthread_all_load=no])
441281348Scy				dnl restore the LIBS
442281348Scy				LIBS="$save_LIBS"
443281348Scy			])
444281348Scy
445281348Scy			if test $ol_cv_cthread_all_load = yes ; then
446281348Scy				LTHREAD_LIBS="$LTHREAD_LIBS -all_load"
447281348Scy				ol_link_threads=mach
448281348Scy				ol_with_threads=found
449281348Scy			fi
450281348Scy		fi
451281348Scy
452281348Scy	elif test $ac_cv_header_cthreads_h = yes ; then
453281348Scy		dnl Hurd variant of Mach Cthreads
454281348Scy		dnl uses <cthreads.h> and -lthreads
455281348Scy
456281348Scy		ol_with_threads=found
457281348Scy 
458281348Scy		dnl save the flags
459281348Scy		save_LIBS="$LIBS"
460281348Scy		LIBS="$LIBS -lthreads"
461281348Scy		AC_CHECK_FUNC(cthread_fork,[ol_link_threads=yes])
462281348Scy		LIBS="$save_LIBS"
463281348Scy
464281348Scy		if test $ol_link_threads = yes ; then
465281348Scy			LTHREAD_LIBS="-lthreads"
466281348Scy			ol_link_threads=mach
467281348Scy			ol_with_threads=found
468281348Scy		else
469281348Scy			AC_MSG_ERROR([could not link with Mach CThreads])
470281348Scy		fi
471281348Scy
472281348Scy	elif test $ol_with_threads = mach ; then
473281348Scy		AC_MSG_ERROR([could not locate Mach CThreads])
474281348Scy	fi
475281348Scy
476281348Scy	if test $ol_link_threads = mach ; then
477281348Scy		AC_DEFINE([HAVE_MACH_CTHREADS], [1],
478281348Scy			[define if you have Mach Cthreads])
479281348Scy	elif test $ol_with_threads = found ; then
480281348Scy		AC_MSG_ERROR([could not link with Mach CThreads])
481281348Scy	fi
482281348Scy	;;
483281348Scyesac
484281348Scy
485281348Scycase $ol_with_threads in auto | yes | pth)
486281348Scy
487281348Scy	AC_CHECK_HEADERS(pth.h)
488281348Scy
489281348Scy	if test $ac_cv_header_pth_h = yes ; then
490281348Scy		AC_CHECK_LIB(pth, pth_version, [have_pth=yes], [have_pth=no])
491281348Scy
492281348Scy		if test $have_pth = yes ; then
493281348Scy			AC_DEFINE([HAVE_GNU_PTH], [1], [if you have GNU Pth])
494281348Scy			LTHREAD_LIBS="$LTHREAD_LIBS -lpth"
495281348Scy			ol_link_threads=pth
496281348Scy			ol_with_threads=found
497281348Scy
498281348Scy			if test x$ol_with_yielding_select = xauto ; then
499281348Scy				ol_with_yielding_select=yes
500281348Scy			fi
501281348Scy		fi
502281348Scy	fi
503281348Scy	;;
504281348Scyesac
505281348Scy
506281348Scycase $ol_with_threads in auto | yes | lwp)
507281348Scy
508281348Scy	dnl check for SunOS5 LWP
509281348Scy	AC_CHECK_HEADERS(thread.h synch.h)
510281348Scy	if test $ac_cv_header_thread_h = yes &&
511281348Scy	   test $ac_cv_header_synch_h = yes ; then
512281348Scy		AC_CHECK_LIB(thread, thr_create, [have_thr=yes], [have_thr=no])
513281348Scy
514281348Scy		if test $have_thr = yes ; then
515281348Scy			AC_DEFINE([HAVE_THR], [1],
516281348Scy				[if you have Solaris LWP (thr) package])
517281348Scy			LTHREAD_LIBS="$LTHREAD_LIBS -lthread"
518281348Scy			ol_link_threads=thr
519281348Scy
520281348Scy			if test x$ol_with_yielding_select = xauto ; then
521281348Scy				ol_with_yielding_select=yes
522281348Scy			fi
523281348Scy
524281348Scy			dnl Check for setconcurrency functions
525281348Scy			AC_CHECK_FUNCS(	\
526281348Scy				thr_setconcurrency \
527281348Scy				thr_getconcurrency \
528281348Scy			)
529281348Scy		fi
530281348Scy	fi
531281348Scy
532281348Scy	dnl check for SunOS4 LWP
533281348Scy	AC_CHECK_HEADERS(lwp/lwp.h)
534281348Scy	if test $ac_cv_header_lwp_lwp_h = yes ; then
535281348Scy		AC_CHECK_LIB(lwp, lwp_create, [have_lwp=yes], [have_lwp=no])
536281348Scy
537281348Scy		if test $have_lwp = yes ; then
538281348Scy			AC_DEFINE([HAVE_LWP], [1],
539281348Scy				[if you have SunOS LWP package])
540281348Scy			LTHREAD_LIBS="$LTHREAD_LIBS -llwp"
541281348Scy			ol_link_threads=lwp
542281348Scy
543281348Scy			if test x$ol_with_yielding_select = xauto ; then
544281348Scy				ol_with_yielding_select=no
545281348Scy			fi
546281348Scy		fi
547281348Scy	fi
548281348Scy	;;
549281348Scyesac
550281348Scy
551281348Scyif test $ol_with_yielding_select = yes ; then
552281348Scy	AC_DEFINE([HAVE_YIELDING_SELECT], [1],
553281348Scy		[define if select implicitly yields])
554281348Scyfi
555281348Scy
556281348Scyif test $ol_with_threads = manual ; then
557281348Scy	dnl User thinks he can manually configure threads.
558281348Scy	ol_link_threads=yes
559281348Scy
560281348Scy	AC_MSG_WARN([thread defines and link options must be set manually])
561281348Scy
562281348Scy	AC_CHECK_HEADERS(pthread.h sched.h)
563281348Scy	AC_CHECK_FUNCS(sched_yield pthread_yield)
564281348Scy	OL_HEADER_LINUX_THREADS
565281348Scy
566281348Scy	AC_CHECK_HEADERS(mach/cthreads.h)
567281348Scy	AC_CHECK_HEADERS(lwp/lwp.h)
568281348Scy	AC_CHECK_HEADERS(thread.h synch.h)
569281348Scyfi
570281348Scy
571281348Scyif test $ol_link_threads != no && test $ol_link_threads != nt ; then
572281348Scy	dnl needed to get reentrant/threadsafe versions
573281348Scy	dnl
574281348Scy	AC_DEFINE([REENTRANT], [1], [enable thread safety])
575281348Scy	AC_DEFINE([_REENTRANT], [1], [enable thread safety])
576281348Scy	AC_DEFINE([THREAD_SAFE], [1], [enable thread safety])
577281348Scy	AC_DEFINE([_THREAD_SAFE], [1], [enable thread safety])
578281348Scy	AC_DEFINE([THREADSAFE], [1], [enable thread safety])
579281348Scy	AC_DEFINE([_THREADSAFE], [1], [enable thread safety])
580281348Scy	AC_DEFINE([_SGI_MP_SOURCE], [1], [enable thread safety])
581281348Scy
582281348Scy	dnl The errno declaration may dependent upon _REENTRANT.
583281348Scy	dnl If it does, we must link with thread support.
584281348Scy	AC_CACHE_CHECK([for thread specific errno],
585281348Scy		[ol_cv_errno_thread_specific], [
586281348Scy		AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <errno.h>]], [[errno = 0;]])],[ol_cv_errno_thread_specific=yes],[ol_cv_errno_thread_specific=no])
587281348Scy	])
588281348Scy
589281348Scy	dnl The h_errno declaration may dependent upon _REENTRANT.
590281348Scy	dnl If it does, we must link with thread support.
591281348Scy	AC_CACHE_CHECK([for thread specific h_errno],
592281348Scy		[ol_cv_h_errno_thread_specific], [
593281348Scy		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])
594281348Scy	])
595281348Scy
596281348Scy	if test $ol_cv_errno_thread_specific != yes ||
597281348Scy	   test $ol_cv_h_errno_thread_specific != yes ; then
598281348Scy		LIBS="$LTHREAD_LIBS $LIBS"
599281348Scy		LTHREAD_LIBS=""
600281348Scy	fi
601281348Scy
602281348Scydnl When in thread environment, use 
603281348Scydnl		#if defined( HAVE_REENTRANT_FUNCTIONS ) || defined( HAVE_FUNC_R )
604281348Scydnl			func_r(...);
605281348Scydnl		#else
606281348Scydnl		#	if defined( HAVE_THREADS ) 
607281348Scydnl				/* lock */
608281348Scydnl		#	endif
609281348Scydnl				func(...);
610281348Scydnl		#	if defined( HAVE_THREADS ) 
611281348Scydnl				/* unlock */
612281348Scydnl		#	endif
613281348Scydnl		#endif
614281348Scydnl
615281348Scydnl HAVE_REENTRANT_FUNCTIONS is derived from:
616281348Scydnl		_POSIX_REENTRANT_FUNCTIONS
617281348Scydnl		_POSIX_THREAD_SAFE_FUNCTIONS
618281348Scydnl		_POSIX_THREADSAFE_FUNCTIONS
619281348Scydnl
620281348Scydnl		and is currently defined in <ldap_pvt_thread.h>
621281348Scydnl
622281348Scydnl HAVE_THREADS is defined by <ldap_pvt_thread.h> iff -UNO_THREADS
623281348Scydnl 
624281348Scydnl libldap/*.c should only include <ldap_pvt_thread.h> iff
625281348Scydnl LDAP_R_COMPILE is defined.  ie:
626281348Scydnl		#ifdef LDAP_R_COMPILE
627281348Scydnl		#	include <ldap_pvt_thread.h>
628281348Scydnl		#endif
629281348Scydnl
630281348Scydnl LDAP_R_COMPILE is defined by libldap_r/Makefile.in
631281348Scydnl specifically for compiling the threadsafe version of
632281348Scydnl	the ldap library (-lldap_r).
633281348Scydnl		
634281348Scydnl	dnl check for reentrant/threadsafe functions
635281348Scydnl	dnl
636281348Scydnl	dnl note: these should only be used when linking
637281348Scydnl	dnl		with $LTHREAD_LIBS
638281348Scydnl	dnl
639281348Scydnl	save_CPPFLAGS="$CPPFLAGS"
640281348Scydnl	save_LIBS="$LIBS"
641281348Scydnl	LIBS="$LTHREAD_LIBS $LIBS"
642281348Scydnl	AC_CHECK_FUNCS(	\
643281348Scydnl		gmtime_r \
644281348Scydnl		gethostbyaddr_r gethostbyname_r \
645281348Scydnl		feof_unlocked unlocked_feof \
646281348Scydnl		putc_unlocked unlocked_putc \
647281348Scydnl		flockfile ftrylockfile \
648281348Scydnl	)
649281348Scydnl	CPPFLAGS="$save_CPPFLAGS"
650281348Scydnl	LIBS="$save_LIBS"
651281348Scyfi  
652281348Scy
653281348Scyif test $ol_link_threads = no ; then
654281348Scy	if test $ol_with_threads = yes ; then
655281348Scy		AC_MSG_ERROR([no suitable thread support])
656281348Scy	fi
657281348Scy
658281348Scy	if test $ol_with_threads = auto ; then
659281348Scy		AC_MSG_WARN([no suitable thread support, disabling threads])
660281348Scy		ol_with_threads=no
661281348Scy	fi
662281348Scy
663281348Scy	AC_DEFINE([NO_THREADS], [1],
664281348Scy		[define if you have (or want) no threads])
665281348Scy	LTHREAD_LIBS=""
666281348Scy	BUILD_THREAD=no
667281348Scyelse
668281348Scy	BUILD_THREAD=yes
669281348Scyfi
670281348Scy
671281348Scyif test $ol_link_threads != no ; then
672281348Scy	AC_DEFINE([LDAP_API_FEATURE_X_OPENLDAP_THREAD_SAFE], [1],
673281348Scy		[define to 1 if library is thread safe])
674281348Scyfi
675281348Scy
676281348Scy# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
677281348Scycase "$ol_with_threads" in
678281348Scy no)
679281348Scy    ol_pthread_ok=no
680281348Scy    $2
681281348Scy    ;;
682281348Scy *)
683281348Scy    ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
684281348Scy    ;;
685281348Scyesac
686281348Scy
687281348ScyAC_LANG_RESTORE
688289764Sglebius
689289764SglebiusAC_SUBST(BUILD_THREAD)
690289764SglebiusAC_SUBST(LTHREAD_LIBS)
691289764Sglebius
692281348Scy])
693