1# $Id: mutex.m4,v 12.27 2007/08/20 18:45:39 bostic Exp $
2
3# POSIX pthreads tests: inter-process safe and intra-process only.
4AC_DEFUN(AM_PTHREADS_SHARED, [
5AC_TRY_RUN([
6#include <pthread.h>
7main() {
8	pthread_cond_t cond;
9	pthread_mutex_t mutex;
10	pthread_condattr_t condattr;
11	pthread_mutexattr_t mutexattr;
12	exit (
13	pthread_condattr_init(&condattr) ||
14	pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED) ||
15	pthread_mutexattr_init(&mutexattr) ||
16	pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED) ||
17	pthread_cond_init(&cond, &condattr) ||
18	pthread_mutex_init(&mutex, &mutexattr) ||
19	pthread_mutex_lock(&mutex) ||
20	pthread_mutex_unlock(&mutex) ||
21	pthread_mutex_destroy(&mutex) ||
22	pthread_cond_destroy(&cond) ||
23	pthread_condattr_destroy(&condattr) ||
24	pthread_mutexattr_destroy(&mutexattr));
25}], [db_cv_mutex="$1"],,
26AC_TRY_LINK([
27#include <pthread.h>],[
28	pthread_cond_t cond;
29	pthread_mutex_t mutex;
30	pthread_condattr_t condattr;
31	pthread_mutexattr_t mutexattr;
32	exit (
33	pthread_condattr_init(&condattr) ||
34	pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED) ||
35	pthread_mutexattr_init(&mutexattr) ||
36	pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED) ||
37	pthread_cond_init(&cond, &condattr) ||
38	pthread_mutex_init(&mutex, &mutexattr) ||
39	pthread_mutex_lock(&mutex) ||
40	pthread_mutex_unlock(&mutex) ||
41	pthread_mutex_destroy(&mutex) ||
42	pthread_cond_destroy(&cond) ||
43	pthread_condattr_destroy(&condattr) ||
44	pthread_mutexattr_destroy(&mutexattr));
45], [db_cv_mutex="$1"]))])
46AC_DEFUN(AM_PTHREADS_PRIVATE, [
47AC_TRY_RUN([
48#include <pthread.h>
49main() {
50	pthread_cond_t cond;
51	pthread_mutex_t mutex;
52	pthread_condattr_t condattr;
53	pthread_mutexattr_t mutexattr;
54	exit (
55	pthread_condattr_init(&condattr) ||
56	pthread_mutexattr_init(&mutexattr) ||
57	pthread_cond_init(&cond, &condattr) ||
58	pthread_mutex_init(&mutex, &mutexattr) ||
59	pthread_mutex_lock(&mutex) ||
60	pthread_mutex_unlock(&mutex) ||
61	pthread_mutex_destroy(&mutex) ||
62	pthread_cond_destroy(&cond) ||
63	pthread_condattr_destroy(&condattr) ||
64	pthread_mutexattr_destroy(&mutexattr));
65}], [db_cv_mutex="$1"],,
66AC_TRY_LINK([
67#include <pthread.h>],[
68	pthread_cond_t cond;
69	pthread_mutex_t mutex;
70	pthread_condattr_t condattr;
71	pthread_mutexattr_t mutexattr;
72	exit (
73	pthread_condattr_init(&condattr) ||
74	pthread_mutexattr_init(&mutexattr) ||
75	pthread_cond_init(&cond, &condattr) ||
76	pthread_mutex_init(&mutex, &mutexattr) ||
77	pthread_mutex_lock(&mutex) ||
78	pthread_mutex_unlock(&mutex) ||
79	pthread_mutex_destroy(&mutex) ||
80	pthread_cond_destroy(&cond) ||
81	pthread_condattr_destroy(&condattr) ||
82	pthread_mutexattr_destroy(&mutexattr));
83], [db_cv_mutex="$1"]))])
84
85# Figure out mutexes for this compiler/architecture.
86#
87# There are 3 mutex groups in BDB: pthreads-style, test-and-set, or a hybrid
88# combination of the two.   We first test for the pthreads-style mutex, and
89# then for a test-and-set mutex.
90AC_DEFUN(AM_DEFINE_MUTEXES, [
91
92# Mutexes we don't test for, but want the #defines to exist for other ports.
93AH_TEMPLATE(HAVE_MUTEX_VMS, [Define to 1 to use VMS mutexes.])
94AH_TEMPLATE(HAVE_MUTEX_VXWORKS, [Define to 1 to use VxWorks mutexes.])
95
96AC_CACHE_CHECK([for mutexes], db_cv_mutex, [
97
98orig_libs=$LIBS
99
100db_cv_mutex=no
101
102# Mutexes can be disabled.
103if test "$db_cv_build_mutexsupport" = no; then
104	db_cv_mutex=disabled;
105fi
106
107# User-specified Win32 mutexes (MinGW build)
108if test "$db_cv_mingw" = yes; then
109	db_cv_mutex=win32/gcc
110fi
111
112if test "$db_cv_mutex" = no; then
113	# User-specified POSIX or UI mutexes.
114	#
115	# There are two different reasons to specify mutexes: First, the
116	# application is already using one type of mutex and doesn't want
117	# to mix-and-match (for example, on Solaris, which has POSIX, UI
118	# and LWP mutexes).  Second, the application's POSIX pthreads
119	# mutexes don't support inter-process locking, but the application
120	# wants to use them anyway (for example, some Linux and *BSD systems).
121	#
122	# Test for LWP threads before testing for UI/POSIX threads, we prefer
123	# them on Solaris, for two reasons: a bug in SunOS 5.7 causes
124	# applications to get pwrite, not pwrite64, if they load the C library
125	# before the appropriate threads library, e.g., tclsh using dlopen to
126	# load the DB library.  Second, LWP mutexes are faster than POSIX
127	# pthread mutexes by some amount.
128	#
129	# Otherwise, test for POSIX threads before UI threads.  There are Linux
130	# systems that support a UI compatibility mode, and applications are
131	# more likely to be written for POSIX threads than UI threads.
132
133	if test "$db_cv_posixmutexes" = yes; then
134		db_cv_mutex=posix_only;
135	fi
136	if test "$db_cv_uimutexes" = yes; then
137		db_cv_mutex=ui_only;
138	fi
139
140	# LWP threads: _lwp_XXX
141	if test "$db_cv_mutex" = no; then
142	AC_TRY_LINK([
143	#include <synch.h>],[
144		static lwp_mutex_t mi = SHAREDMUTEX;
145		static lwp_cond_t ci = SHAREDCV;
146		lwp_mutex_t mutex = mi;
147		lwp_cond_t cond = ci;
148		exit (
149		_lwp_mutex_lock(&mutex) ||
150		_lwp_mutex_unlock(&mutex));
151	], [db_cv_mutex=Solaris/lwp])
152	fi
153
154	# POSIX.1 pthreads: pthread_XXX
155	#
156	# If the user specified we use POSIX pthreads mutexes, and we fail to
157	# find the full interface, try and configure for just intra-process
158	# support.
159	if test "$db_cv_mutex" = no -o "$db_cv_mutex" = posix_only; then
160		LIBS="$LIBS -lpthread"
161		AM_PTHREADS_SHARED(POSIX/pthreads/library)
162		LIBS="$orig_libs"
163	fi
164	if test "$db_cv_mutex" = no -o "$db_cv_mutex" = posix_only; then
165		AM_PTHREADS_SHARED(POSIX/pthreads)
166	fi
167	if test "$db_cv_mutex" = posix_only; then
168		AM_PTHREADS_PRIVATE(POSIX/pthreads/private)
169	fi
170	if test "$db_cv_mutex" = posix_only; then
171		LIBS="$LIBS -lpthread"
172		AM_PTHREADS_PRIVATE(POSIX/pthreads/library/private)
173		LIBS="$orig_libs"
174	fi
175	if test "$db_cv_mutex" = posix_only; then
176		AC_MSG_ERROR([unable to find POSIX 1003.1 mutex interfaces])
177	fi
178
179	# UI threads: thr_XXX
180	if test "$db_cv_mutex" = no -o "$db_cv_mutex" = ui_only; then
181	LIBS="$LIBS -lthread"
182	AC_TRY_LINK([
183	#include <thread.h>
184	#include <synch.h>],[
185		mutex_t mutex;
186		cond_t cond;
187		int type = USYNC_PROCESS;
188		exit (
189		mutex_init(&mutex, type, NULL) ||
190		cond_init(&cond, type, NULL) ||
191		mutex_lock(&mutex) ||
192		mutex_unlock(&mutex));
193	], [db_cv_mutex=UI/threads/library])
194	LIBS="$orig_libs"
195	fi
196	if test "$db_cv_mutex" = no -o "$db_cv_mutex" = ui_only; then
197	AC_TRY_LINK([
198	#include <thread.h>
199	#include <synch.h>],[
200		mutex_t mutex;
201		cond_t cond;
202		int type = USYNC_PROCESS;
203		exit (
204		mutex_init(&mutex, type, NULL) ||
205		cond_init(&cond, type, NULL) ||
206		mutex_lock(&mutex) ||
207		mutex_unlock(&mutex));
208	], [db_cv_mutex=UI/threads])
209	fi
210	if test "$db_cv_mutex" = ui_only; then
211		AC_MSG_ERROR([unable to find UI mutex interfaces])
212	fi
213
214	# We're done testing for pthreads-style mutexes.  Next, check for
215	# test-and-set mutexes.  Check first for hybrid implementations,
216	# because we check for them even if we've already found a
217	# pthreads-style mutex and they're the most common architectures
218	# anyway.
219	#
220	# x86/gcc: FreeBSD, NetBSD, BSD/OS, Linux
221	AC_TRY_COMPILE(,[
222	#if (defined(i386) || defined(__i386__)) && defined(__GNUC__)
223		exit(0);
224	#else
225		FAIL TO COMPILE/LINK
226	#endif
227	], [db_cv_mutex="$db_cv_mutex/x86/gcc-assembly"])
228
229	# x86_64/gcc: FreeBSD, NetBSD, BSD/OS, Linux
230	AC_TRY_COMPILE(,[
231	#if (defined(x86_64) || defined(__x86_64__)) && defined(__GNUC__)
232		exit(0);
233	#else
234		FAIL TO COMPILE/LINK
235	#endif
236	], [db_cv_mutex="$db_cv_mutex/x86_64/gcc-assembly"])
237
238	# Solaris is one of the systems where we can configure hybrid mutexes.
239	# However, we require the membar_enter function for that, and only newer
240	# Solaris releases have it.  Check to see if we can configure hybrids.
241	AC_TRY_LINK([
242	#include <sys/atomic.h>
243	#include <sys/machlock.h>],[
244		typedef lock_t tsl_t;
245		lock_t x;
246		_lock_try(&x);
247		_lock_clear(&x);
248		membar_enter();
249	], [db_cv_mutex="$db_cv_mutex/Solaris/_lock_try/membar"])
250
251	# Sparc/gcc: SunOS, Solaris
252	AC_TRY_COMPILE(,[
253	#if defined(__sparc__) && defined(__GNUC__)
254		exit(0);
255	#else
256		FAIL TO COMPILE/LINK
257	#endif
258	], [db_cv_mutex="$db_cv_mutex/Sparc/gcc-assembly"])
259
260	# We're done testing for any hybrid mutex implementations.  If we did
261	# not find a pthreads-style mutex, but did find a test-and-set mutex,
262	# we set db_cv_mutex to "no/XXX" -- clean that up.
263	db_cv_mutex=`echo $db_cv_mutex | sed 's/^no\///'`
264fi
265
266# If we still don't have a mutex implementation yet, continue testing for a
267# test-and-set mutex implementation.
268
269# _lock_try/_lock_clear: Solaris
270# On Solaris systems without other mutex interfaces, DB uses the undocumented
271# _lock_try _lock_clear function calls instead of either the sema_trywait(3T)
272# or sema_wait(3T) function calls.  This is because of problems in those
273# interfaces in some releases of the Solaris C library.
274if test "$db_cv_mutex" = no; then
275AC_TRY_LINK([
276#include <sys/atomic.h>
277#include <sys/machlock.h>],[
278	typedef lock_t tsl_t;
279	lock_t x;
280	_lock_try(&x);
281	_lock_clear(&x);
282], [db_cv_mutex=Solaris/_lock_try])
283fi
284
285# msemaphore: HPPA only
286# Try HPPA before general msem test, it needs special alignment.
287if test "$db_cv_mutex" = no; then
288AC_TRY_LINK([
289#include <sys/mman.h>],[
290#if defined(__hppa)
291	typedef msemaphore tsl_t;
292	msemaphore x;
293	msem_init(&x, 0);
294	msem_lock(&x, 0);
295	msem_unlock(&x, 0);
296	exit(0);
297#else
298	FAIL TO COMPILE/LINK
299#endif
300], [db_cv_mutex=HP/msem_init])
301fi
302
303# msemaphore: AIX, OSF/1
304if test "$db_cv_mutex" = no; then
305AC_TRY_LINK([
306#include <sys/types.h>
307#include <sys/mman.h>],[
308	typedef msemaphore tsl_t;
309	msemaphore x;
310	msem_init(&x, 0);
311	msem_lock(&x, 0);
312	msem_unlock(&x, 0);
313	exit(0);
314], [db_cv_mutex=UNIX/msem_init])
315fi
316
317# ReliantUNIX
318if test "$db_cv_mutex" = no; then
319LIBS="$LIBS -lmproc"
320AC_TRY_LINK([
321#include <ulocks.h>],[
322	typedef spinlock_t tsl_t;
323	spinlock_t x;
324	initspin(&x, 1);
325	cspinlock(&x);
326	spinunlock(&x);
327], [db_cv_mutex=ReliantUNIX/initspin])
328LIBS="$orig_libs"
329fi
330
331# SCO: UnixWare has threads in libthread, but OpenServer doesn't.
332if test "$db_cv_mutex" = no; then
333AC_TRY_COMPILE(,[
334#if defined(__USLC__)
335	exit(0);
336#else
337	FAIL TO COMPILE/LINK
338#endif
339], [db_cv_mutex=SCO/x86/cc-assembly])
340fi
341
342# abilock_t: SGI
343if test "$db_cv_mutex" = no; then
344AC_TRY_LINK([
345#include <abi_mutex.h>],[
346	typedef abilock_t tsl_t;
347	abilock_t x;
348	init_lock(&x);
349	acquire_lock(&x);
350	release_lock(&x);
351], [db_cv_mutex=SGI/init_lock])
352fi
353
354# sema_t: Solaris
355# The sema_XXX calls do not work on Solaris 5.5.  I see no reason to ever
356# turn this test on, unless we find some other platform that uses the old
357# POSIX.1 interfaces.
358if test "$db_cv_mutex" = DOESNT_WORK; then
359AC_TRY_LINK([
360#include <synch.h>],[
361	typedef sema_t tsl_t;
362	sema_t x;
363	sema_init(&x, 1, USYNC_PROCESS, NULL);
364	sema_wait(&x);
365	sema_post(&x);
366], [db_cv_mutex=UNIX/sema_init])
367fi
368
369# _check_lock/_clear_lock: AIX
370if test "$db_cv_mutex" = no; then
371AC_TRY_LINK([
372#include <sys/atomic_op.h>],[
373	int x;
374	_check_lock(&x,0,1);
375	_clear_lock(&x,0);
376], [db_cv_mutex=AIX/_check_lock])
377fi
378
379# _spin_lock_try/_spin_unlock: Apple/Darwin
380if test "$db_cv_mutex" = no; then
381AC_TRY_LINK(,[
382	int x;
383	_spin_lock_try(&x);
384	_spin_unlock(&x);
385], [db_cv_mutex=Darwin/_spin_lock_try])
386fi
387
388# Tru64/cc
389if test "$db_cv_mutex" = no; then
390AC_TRY_COMPILE(,[
391#if defined(__alpha) && defined(__DECC)
392	exit(0);
393#else
394	FAIL TO COMPILE/LINK
395#endif
396], [db_cv_mutex=Tru64/cc-assembly])
397fi
398
399# Alpha/gcc
400if test "$db_cv_mutex" = no; then
401AC_TRY_COMPILE(,[
402#if defined(__alpha) && defined(__GNUC__)
403	exit(0);
404#else
405	FAIL TO COMPILE/LINK
406#endif
407], [db_cv_mutex=ALPHA/gcc-assembly])
408fi
409
410# ARM/gcc: Linux
411if test "$db_cv_mutex" = no; then
412AC_TRY_COMPILE(,[
413#if defined(__arm__) && defined(__GNUC__)
414	exit(0);
415#else
416	FAIL TO COMPILE/LINK
417#endif
418], [db_cv_mutex=ARM/gcc-assembly])
419fi
420
421# MIPS/gcc: Linux
422if test "$db_cv_mutex" = no; then
423AC_TRY_COMPILE(,[
424#if (defined(__mips) || defined(__mips__)) && defined(__GNUC__)
425	exit(0);
426#else
427	FAIL TO COMPILE/LINK
428#endif
429], [db_cv_mutex=MIPS/gcc-assembly])
430fi
431
432# PaRisc/gcc: HP/UX
433if test "$db_cv_mutex" = no; then
434AC_TRY_COMPILE(,[
435#if (defined(__hppa) || defined(__hppa__)) && defined(__GNUC__)
436	exit(0);
437#else
438	FAIL TO COMPILE/LINK
439#endif
440], [db_cv_mutex=HPPA/gcc-assembly])
441fi
442
443# PPC/gcc:
444if test "$db_cv_mutex" = no; then
445AC_TRY_COMPILE(,[
446#if (defined(__powerpc__) || defined(__ppc__)) && defined(__GNUC__)
447	exit(0);
448#else
449	FAIL TO COMPILE/LINK
450#endif
451], [db_cv_mutex=PPC/gcc-assembly])
452fi
453
454# 68K/gcc: SunOS
455if test "$db_cv_mutex" = no; then
456AC_TRY_COMPILE(,[
457#if (defined(mc68020) || defined(sun3)) && defined(__GNUC__)
458	exit(0);
459#else
460	FAIL TO COMPILE/LINK
461#endif
462], [db_cv_mutex=68K/gcc-assembly])
463fi
464
465# S390/cc: IBM OS/390 Unix
466if test "$db_cv_mutex" = no; then
467AC_TRY_COMPILE(,[
468#if defined(__MVS__) && defined(__IBMC__)
469	exit(0);
470#else
471	FAIL TO COMPILE/LINK
472#endif
473], [db_cv_mutex=S390/cc-assembly])
474fi
475
476# S390/gcc: Linux
477if test "$db_cv_mutex" = no; then
478AC_TRY_COMPILE(,[
479#if defined(__s390__) && defined(__GNUC__)
480	exit(0);
481#else
482	FAIL TO COMPILE/LINK
483#endif
484], [db_cv_mutex=S390/gcc-assembly])
485fi
486
487# ia64/gcc: Linux
488if test "$db_cv_mutex" = no; then
489AC_TRY_COMPILE(,[
490#if defined(__ia64) && defined(__GNUC__)
491	exit(0);
492#else
493	FAIL TO COMPILE/LINK
494#endif
495], [db_cv_mutex=ia64/gcc-assembly])
496fi
497
498# uts/cc: UTS
499if test "$db_cv_mutex" = no; then
500AC_TRY_COMPILE(,[
501#if defined(_UTS)
502	exit(0);
503#else
504	FAIL TO COMPILE/LINK
505#endif
506], [db_cv_mutex=UTS/cc-assembly])
507fi
508
509# UNIX fcntl system call mutexes.
510if test "$db_cv_mutex" = no; then
511	db_cv_mutex=UNIX/fcntl
512AC_TRY_LINK([
513#include <fcntl.h>],[
514	struct flock l;
515	l.l_whence = SEEK_SET;
516	l.l_start = 10;
517	l.l_len = 1;
518	l.l_type = F_WRLCK;
519	fcntl(0, F_SETLK, &l);
520], [db_cv_mutex=UNIX/fcntl])
521fi
522])
523
524# Configure a pthreads-style mutex implementation.
525hybrid=pthread
526case "$db_cv_mutex" in
527POSIX/pthreads*)	ADDITIONAL_OBJS="mut_pthread${o} $ADDITIONAL_OBJS"
528			AC_DEFINE(HAVE_MUTEX_PTHREADS)
529			AH_TEMPLATE(HAVE_MUTEX_PTHREADS,
530			    [Define to 1 to use POSIX 1003.1 pthread_XXX mutexes.]);;
531POSIX/pthreads/private*)ADDITIONAL_OBJS="mut_pthread${o} $ADDITIONAL_OBJS"
532			AC_DEFINE(HAVE_MUTEX_PTHREADS)
533			AC_DEFINE(HAVE_MUTEX_THREAD_ONLY)
534			AH_TEMPLATE(HAVE_MUTEX_THREAD_ONLY,
535			    [Define to 1 to configure mutexes intra-process only.]);;
536POSIX/pthreads/library*)ADDITIONAL_OBJS="mut_pthread${o} $ADDITIONAL_OBJS"
537			AC_DEFINE(HAVE_MUTEX_PTHREADS);;
538POSIX/pthreads/library/private*)
539			ADDITIONAL_OBJS="mut_pthread${o} $ADDITIONAL_OBJS"
540			AC_DEFINE(HAVE_MUTEX_PTHREADS)
541			AC_DEFINE(HAVE_MUTEX_THREAD_ONLY);;
542Solaris/lwp*)		ADDITIONAL_OBJS="mut_pthread${o} $ADDITIONAL_OBJS"
543			AC_DEFINE(HAVE_MUTEX_SOLARIS_LWP)
544			AH_TEMPLATE(HAVE_MUTEX_SOLARIS_LWP,
545			    [Define to 1 to use the Solaris lwp threads mutexes.]);;
546UI/threads*)		ADDITIONAL_OBJS="mut_pthread${o} $ADDITIONAL_OBJS"
547			AC_DEFINE(HAVE_MUTEX_UI_THREADS)
548			AH_TEMPLATE(HAVE_MUTEX_UI_THREADS,
549			    [Define to 1 to use the UNIX International mutexes.]);;
550UI/threads/library*)	ADDITIONAL_OBJS="mut_pthread${o} $ADDITIONAL_OBJS"
551			AC_DEFINE(HAVE_MUTEX_UI_THREADS);;
552*)			hybrid=no;;
553esac
554
555# Configure a test-and-set mutex implementation.
556case "$db_cv_mutex" in
55768K/gcc-assembly)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
558			AC_DEFINE(HAVE_MUTEX_68K_GCC_ASSEMBLY)
559			AH_TEMPLATE(HAVE_MUTEX_68K_GCC_ASSEMBLY,
560			    [Define to 1 to use the GCC compiler and 68K assembly language mutexes.]);;
561AIX/_check_lock)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
562			AC_DEFINE(HAVE_MUTEX_AIX_CHECK_LOCK)
563			AH_TEMPLATE(HAVE_MUTEX_AIX_CHECK_LOCK,
564			    [Define to 1 to use the AIX _check_lock mutexes.]);;
565Darwin/_spin_lock_try)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
566			AC_DEFINE(HAVE_MUTEX_DARWIN_SPIN_LOCK_TRY)
567			AH_TEMPLATE(HAVE_MUTEX_DARWIN_SPIN_LOCK_TRY,
568			    [Define to 1 to use the Apple/Darwin _spin_lock_try mutexes.]);;
569ALPHA/gcc-assembly)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
570			AC_DEFINE(HAVE_MUTEX_ALPHA_GCC_ASSEMBLY)
571			AH_TEMPLATE(HAVE_MUTEX_ALPHA_GCC_ASSEMBLY,
572			    [Define to 1 to use the GCC compiler and Alpha assembly language mutexes.]);;
573ARM/gcc-assembly)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
574			AC_DEFINE(HAVE_MUTEX_ARM_GCC_ASSEMBLY)
575			AH_TEMPLATE(HAVE_MUTEX_ARM_GCC_ASSEMBLY,
576			    [Define to 1 to use the GCC compiler and ARM assembly language mutexes.]);;
577HP/msem_init)		ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
578			AC_DEFINE(HAVE_MUTEX_HPPA_MSEM_INIT)
579			AH_TEMPLATE(HAVE_MUTEX_HPPA_MSEM_INIT,
580			    [Define to 1 to use the msem_XXX mutexes on HP-UX.]);;
581HPPA/gcc-assembly)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
582			AC_DEFINE(HAVE_MUTEX_HPPA_GCC_ASSEMBLY)
583			AH_TEMPLATE(HAVE_MUTEX_HPPA_GCC_ASSEMBLY,
584			    [Define to 1 to use the GCC compiler and PaRisc assembly language mutexes.]);;
585ia64/gcc-assembly)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
586			AC_DEFINE(HAVE_MUTEX_IA64_GCC_ASSEMBLY)
587			AH_TEMPLATE(HAVE_MUTEX_IA64_GCC_ASSEMBLY,
588			    [Define to 1 to use the GCC compiler and IA64 assembly language mutexes.]);;
589MIPS/gcc-assembly)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
590			AC_DEFINE(HAVE_MUTEX_MIPS_GCC_ASSEMBLY)
591			AH_TEMPLATE(HAVE_MUTEX_MIPS_GCC_ASSEMBLY,
592			    [Define to 1 to use the GCC compiler and MIPS assembly language mutexes.]);;
593PPC/gcc-assembly)
594			ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
595			AC_DEFINE(HAVE_MUTEX_PPC_GCC_ASSEMBLY)
596			AH_TEMPLATE(HAVE_MUTEX_PPC_GCC_ASSEMBLY,
597			    [Define to 1 to use the GCC compiler and PowerPC assembly language mutexes.]);;
598ReliantUNIX/initspin)	LIBSO_LIBS="$LIBSO_LIBS -lmproc"
599			ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
600			AC_DEFINE(HAVE_MUTEX_RELIANTUNIX_INITSPIN)
601			AH_TEMPLATE(HAVE_MUTEX_RELIANTUNIX_INITSPIN,
602			    [Define to 1 to use Reliant UNIX initspin mutexes.]);;
603S390/cc-assembly)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
604			AC_DEFINE(HAVE_MUTEX_S390_CC_ASSEMBLY)
605			AH_TEMPLATE(HAVE_MUTEX_S390_CC_ASSEMBLY,
606			    [Define to 1 to use the IBM C compiler and S/390 assembly language mutexes.]);;
607S390/gcc-assembly)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
608			AC_DEFINE(HAVE_MUTEX_S390_GCC_ASSEMBLY)
609			AH_TEMPLATE(HAVE_MUTEX_S390_GCC_ASSEMBLY,
610			    [Define to 1 to use the GCC compiler and S/390 assembly language mutexes.]);;
611SCO/x86/cc-assembly)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
612			AC_DEFINE(HAVE_MUTEX_SCO_X86_CC_ASSEMBLY)
613			AH_TEMPLATE(HAVE_MUTEX_SCO_X86_CC_ASSEMBLY,
614			    [Define to 1 to use the SCO compiler and x86 assembly language mutexes.]);;
615SGI/init_lock)		ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
616			AC_DEFINE(HAVE_MUTEX_SGI_INIT_LOCK)
617			AH_TEMPLATE(HAVE_MUTEX_SGI_INIT_LOCK,
618			    [Define to 1 to use the SGI XXX_lock mutexes.]);;
619Solaris/_lock_try)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
620			AC_DEFINE(HAVE_MUTEX_SOLARIS_LOCK_TRY)
621			AH_TEMPLATE(HAVE_MUTEX_SOLARIS_LOCK_TRY,
622			    [Define to 1 to use the Solaris _lock_XXX mutexes.]);;
623*Solaris/_lock_try/membar)
624			hybrid="$hybrid/tas"
625			ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
626			AC_DEFINE(HAVE_MUTEX_SOLARIS_LOCK_TRY)
627			AH_TEMPLATE(HAVE_MUTEX_SOLARIS_LOCK_TRY,
628			    [Define to 1 to use the Solaris _lock_XXX mutexes.]);;
629*Sparc/gcc-assembly)	hybrid="$hybrid/tas"
630			ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
631			AC_DEFINE(HAVE_MUTEX_SPARC_GCC_ASSEMBLY)
632			AH_TEMPLATE(HAVE_MUTEX_SPARC_GCC_ASSEMBLY,
633			    [Define to 1 to use the GCC compiler and Sparc assembly language mutexes.]);;
634Tru64/cc-assembly)	ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
635			AC_DEFINE(HAVE_MUTEX_TRU64_CC_ASSEMBLY)
636			AH_TEMPLATE(HAVE_MUTEX_TRU64_CC_ASSEMBLY,
637			    [Define to 1 to use the CC compiler and Tru64 assembly language mutexes.]);;
638UNIX/msem_init)		ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
639			AC_DEFINE(HAVE_MUTEX_MSEM_INIT)
640			AH_TEMPLATE(HAVE_MUTEX_MSEM_INIT,
641			    [Define to 1 to use the msem_XXX mutexes on systems other than HP-UX.]);;
642UNIX/sema_init)		ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
643			AC_DEFINE(HAVE_MUTEX_SEMA_INIT)
644			AH_TEMPLATE(HAVE_MUTEX_SEMA_INIT,
645			    [Define to 1 to use the obsolete POSIX 1003.1 sema_XXX mutexes.]);;
646UTS/cc-assembly)	ADDITIONAL_OBJS="uts4.cc${o} $ADDITIONAL_OBJS"
647			AC_DEFINE(HAVE_MUTEX_UTS_CC_ASSEMBLY)
648			AH_TEMPLATE(HAVE_MUTEX_UTS_CC_ASSEMBLY,
649			    [Define to 1 to use the UTS compiler and assembly language mutexes.]);;
650*x86/gcc-assembly)	hybrid="$hybrid/tas"
651			ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
652			AC_DEFINE(HAVE_MUTEX_X86_GCC_ASSEMBLY)
653			AH_TEMPLATE(HAVE_MUTEX_X86_GCC_ASSEMBLY,
654			    [Define to 1 to use the GCC compiler and 32-bit x86 assembly language mutexes.]);;
655*x86_64/gcc-assembly)	hybrid="$hybrid/tas"
656			ADDITIONAL_OBJS="mut_tas${o} $ADDITIONAL_OBJS"
657			AC_DEFINE(HAVE_MUTEX_X86_64_GCC_ASSEMBLY)
658			AH_TEMPLATE(HAVE_MUTEX_X86_64_GCC_ASSEMBLY,
659			[Define to 1 to use the GCC compiler and 64-bit x86 assembly language mutexes.]);;
660esac
661
662# Configure the remaining special cases.
663case "$db_cv_mutex" in
664UNIX/fcntl)		AC_MSG_WARN(
665			    [NO FAST MUTEXES FOUND FOR THIS COMPILER/ARCHITECTURE.])
666			ADDITIONAL_OBJS="mut_fcntl${o} $ADDITIONAL_OBJS"
667			AC_DEFINE(HAVE_MUTEX_FCNTL)
668			AH_TEMPLATE(HAVE_MUTEX_FCNTL,
669			    [Define to 1 to use the UNIX fcntl system call mutexes.]);;
670win32)			ADDITIONAL_OBJS="mut_win32${o} $ADDITIONAL_OBJS"
671			AC_DEFINE(HAVE_MUTEX_WIN32)
672			AH_TEMPLATE(HAVE_MUTEX_WIN32, [Define to 1 to use the MSVC compiler and Windows mutexes.]);;
673win32/gcc)		ADDITIONAL_OBJS="mut_win32${o} $ADDITIONAL_OBJS"
674			AC_DEFINE(HAVE_MUTEX_WIN32_GCC)
675			AH_TEMPLATE(HAVE_MUTEX_WIN32_GCC, [Define to 1 to use the GCC compiler and Windows mutexes.]);;
676esac
677
678# Mutexes may not have been found, or may have been disabled.
679case "$db_cv_mutex" in
680disabled)
681	;;
682*)
683	# Test to see if mutexes have been found by checking the list of
684	# additional objects for a mutex implementation.
685	case "$ADDITIONAL_OBJS" in
686	*mut_fcntl*|*mut_pthread*|*mut_tas*|*mut_win32*)
687		AC_DEFINE(HAVE_MUTEX_SUPPORT)
688		AH_TEMPLATE(HAVE_MUTEX_SUPPORT,
689	    [Define to 1 if the Berkeley DB library should support mutexes.]);;
690	*)
691		AC_MSG_ERROR([Unable to find a mutex implementation]);;
692	esac
693esac
694
695# We may have found both a pthreads-style mutex implementation as well as a
696# test-and-set, in which case configure for the hybrid.
697if test "$hybrid" = pthread/tas; then
698	AC_DEFINE(HAVE_MUTEX_HYBRID)
699	AH_TEMPLATE(HAVE_MUTEX_HYBRID,
700	    [Define to 1 to use test-and-set mutexes with blocking mutexes.])
701fi
702
703# The mutex selection may require specific declarations -- we fill in most of
704# them above, but here are the common ones.
705#
706# The mutex selection may tell us what kind of thread package we're using,
707# which we use to figure out the thread type.
708#
709# If we're configured for the POSIX pthread API, then force the thread ID type
710# and include function, regardless of the mutex selection.  Ditto for the
711# (default) Solaris lwp mutexes, because they don't have a way to return the
712# thread ID.
713#
714# Try and link with a threads library if possible.  The problem is the Solaris
715# C library has UI/POSIX interface stubs, but they're broken, configuring them
716# for inter-process mutexes doesn't return an error, but it doesn't work either.
717# For that reason always add -lpthread if we're using pthread calls or mutexes
718# and there's a pthread library.
719#
720# We can't depend on any specific call existing (pthread_create, for example),
721# as it may be #defined in an include file -- OSF/1 (Tru64) has this problem.
722
723AC_SUBST(thread_h_decl)
724AC_SUBST(db_threadid_t_decl)
725db_threadid_t_decl=notset
726
727case "$db_cv_mutex" in
728POSIX/pthread*|Solaris/lwp*)
729	thread_h_decl="#include <pthread.h>"
730	db_threadid_t_decl="typedef pthread_t db_threadid_t;"
731	AC_HAVE_LIBRARY(pthread, LIBSO_LIBS="$LIBSO_LIBS -lpthread");;
732UI/threads*)
733	thread_h_decl="#include <thread.h>"
734	db_threadid_t_decl="typedef thread_t db_threadid_t;"
735	AC_HAVE_LIBRARY(thread, LIBSO_LIBS="$LIBSO_LIBS -lthread");;
736*)
737	if test "$db_cv_pthread_api" = yes; then
738		thread_h_decl="#include <pthread.h>"
739		db_threadid_t_decl="typedef pthread_t db_threadid_t;"
740		AC_HAVE_LIBRARY(pthread, LIBSO_LIBS="$LIBSO_LIBS -lpthread")
741	fi;;
742esac
743
744# We need to know if the thread ID type will fit into an integral type and we
745# can compare it for equality and generally treat it like an int, or if it's a
746# non-integral type and we have to treat it like a structure or other untyped
747# block of bytes.  For example, MVS typedef's pthread_t to a structure.
748AH_TEMPLATE(HAVE_SIMPLE_THREAD_TYPE,
749    [Define to 1 if thread identifier type db_threadid_t is integral.])
750if test "$db_threadid_t_decl" = notset; then
751	db_threadid_t_decl="typedef uintmax_t db_threadid_t;"
752	AC_DEFINE(HAVE_SIMPLE_THREAD_TYPE)
753else
754	AC_TRY_COMPILE(
755	#include <sys/types.h>
756	$thread_h_decl, [
757	$db_threadid_t_decl
758	db_threadid_t a;
759	a = 0;
760	], AC_DEFINE(HAVE_SIMPLE_THREAD_TYPE))
761fi
762
763# If we're building replication and configured with POSIX pthreads or were
764# told to build using the POSIX API, build the replication manager framework.
765db_cv_build_replication_mgr=no
766if test "$db_cv_build_replication" = yes; then
767	AH_TEMPLATE(HAVE_REPLICATION_THREADS,
768	    [Define to 1 if building the Berkeley DB replication framework.])
769	case "$db_cv_mutex" in
770	POSIX/pthread*|Solaris/lwp)
771		db_cv_build_replication_mgr=yes;;
772	esac
773	if test "$db_cv_pthread_api" = yes; then
774		db_cv_build_replication_mgr=yes
775	fi
776
777	if test "$db_cv_build_replication_mgr" = yes; then
778		AC_DEFINE(HAVE_REPLICATION_THREADS)
779
780		# Solaris requires the socket and nsl libraries to build the
781		# replication manager.  Don't add nsl regardless of the OS,
782		# it causes RPC to fail on AIX 4.3.3.
783		case "$host_os" in
784		solaris*)
785			AC_HAVE_LIBRARY(nsl, LIBSO_LIBS="$LIBSO_LIBS -lnsl")
786			AC_HAVE_LIBRARY(socket,
787			    LIBSO_LIBS="$LIBSO_LIBS -lsocket");;
788		esac
789	fi
790fi
791
792# There are 3 classes of mutexes:
793#
794# 1: Mutexes requiring no cleanup, for example, test-and-set mutexes.
795# 2: Mutexes that must be destroyed, but which don't hold permanent system
796#    resources, for example, pthread mutexes on MVS aka OS/390 aka z/OS.
797# 3: Mutexes that must be destroyed, even after the process is gone, for
798#    example, pthread mutexes on QNX and binary semaphores on VxWorks.
799#
800# DB cannot currently distinguish between #2 and #3 because DB does not know
801# if the application is running environment recovery as part of startup and
802# does not need to do cleanup, or if the environment is being removed and/or
803# recovered in a loop in the application, and so does need to clean up.  If
804# we get it wrong, we're going to call the mutex destroy routine on a random
805# piece of memory, which usually works, but just might drop core.  For now,
806# we group #2 and #3 into the HAVE_MUTEX_SYSTEM_RESOURCES define, until we
807# have a better solution or reason to solve this in a general way -- so far,
808# the places we've needed to handle this are few.
809AH_TEMPLATE(HAVE_MUTEX_SYSTEM_RESOURCES,
810    [Define to 1 if mutexes hold system resources.])
811
812case "$host_os$db_cv_mutex" in
813*qnx*POSIX/pthread*|openedition*POSIX/pthread*)
814	AC_DEFINE(HAVE_MUTEX_SYSTEM_RESOURCES);;
815esac])
816