xref: /onnv-gate/usr/src/cmd/sendmail/db/os/os_spin.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*-
2*0Sstevel@tonic-gate  * See the file LICENSE for redistribution information.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * Copyright (c) 1997, 1998
5*0Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #include "config.h"
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate #ifndef lint
11*0Sstevel@tonic-gate static const char sccsid[] = "@(#)os_spin.c	10.10 (Sleepycat) 10/12/98";
12*0Sstevel@tonic-gate #endif /* not lint */
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
15*0Sstevel@tonic-gate #include <sys/types.h>
16*0Sstevel@tonic-gate #if defined(HAVE_PSTAT_GETDYNAMIC)
17*0Sstevel@tonic-gate #include <sys/pstat.h>
18*0Sstevel@tonic-gate #endif
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate #include <limits.h>
21*0Sstevel@tonic-gate #include <unistd.h>
22*0Sstevel@tonic-gate #endif
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate #include "db_int.h"
25*0Sstevel@tonic-gate #include "os_jump.h"
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #if defined(HAVE_PSTAT_GETDYNAMIC)
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate  * __os_pstat_getdynamic --
30*0Sstevel@tonic-gate  *	HP/UX.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate static int
__os_pstat_getdynamic()33*0Sstevel@tonic-gate __os_pstat_getdynamic()
34*0Sstevel@tonic-gate {
35*0Sstevel@tonic-gate 	struct pst_dynamic psd;
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate 	return (pstat_getdynamic(&psd,
38*0Sstevel@tonic-gate 	    sizeof(psd), (size_t)1, 0) == -1 ? 1 : psd.psd_proc_cnt);
39*0Sstevel@tonic-gate }
40*0Sstevel@tonic-gate #endif
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
43*0Sstevel@tonic-gate /*
44*0Sstevel@tonic-gate  * __os_sysconf --
45*0Sstevel@tonic-gate  *	Solaris, Linux.
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate static int
__os_sysconf()48*0Sstevel@tonic-gate __os_sysconf()
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate 	int nproc;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	return ((nproc = sysconf(_SC_NPROCESSORS_ONLN)) > 1 ? nproc : 1);
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate #endif
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate  * __os_spin --
58*0Sstevel@tonic-gate  *	Return the number of default spins before blocking.
59*0Sstevel@tonic-gate  *
60*0Sstevel@tonic-gate  * PUBLIC: int __os_spin __P((void));
61*0Sstevel@tonic-gate  */
62*0Sstevel@tonic-gate int
__os_spin()63*0Sstevel@tonic-gate __os_spin()
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate 	/*
66*0Sstevel@tonic-gate 	 * If the application specified a value or we've already figured it
67*0Sstevel@tonic-gate 	 * out, return it.
68*0Sstevel@tonic-gate 	 *
69*0Sstevel@tonic-gate 	 * XXX
70*0Sstevel@tonic-gate 	 * We don't want to repeatedly call the underlying function because
71*0Sstevel@tonic-gate 	 * it can be expensive (e.g., requiring multiple filesystem accesses
72*0Sstevel@tonic-gate 	 * under Debian Linux).
73*0Sstevel@tonic-gate 	 */
74*0Sstevel@tonic-gate 	if (DB_GLOBAL(db_tsl_spins) != 0)
75*0Sstevel@tonic-gate 		return (DB_GLOBAL(db_tsl_spins));
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	DB_GLOBAL(db_tsl_spins) = 1;
78*0Sstevel@tonic-gate #if defined(HAVE_PSTAT_GETDYNAMIC)
79*0Sstevel@tonic-gate 	DB_GLOBAL(db_tsl_spins) = __os_pstat_getdynamic();
80*0Sstevel@tonic-gate #endif
81*0Sstevel@tonic-gate #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
82*0Sstevel@tonic-gate 	DB_GLOBAL(db_tsl_spins) = __os_sysconf();
83*0Sstevel@tonic-gate #endif
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/*
86*0Sstevel@tonic-gate 	 * Spin 50 times per processor, we have anecdotal evidence that this
87*0Sstevel@tonic-gate 	 * is a reasonable value.
88*0Sstevel@tonic-gate 	 */
89*0Sstevel@tonic-gate 	DB_GLOBAL(db_tsl_spins) *= 50;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	return (DB_GLOBAL(db_tsl_spins));
92*0Sstevel@tonic-gate }
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate /*
95*0Sstevel@tonic-gate  * __os_yield --
96*0Sstevel@tonic-gate  *	Yield the processor.
97*0Sstevel@tonic-gate  *
98*0Sstevel@tonic-gate  * PUBLIC: void __os_yield __P((u_long));
99*0Sstevel@tonic-gate  */
100*0Sstevel@tonic-gate void
__os_yield(usecs)101*0Sstevel@tonic-gate __os_yield(usecs)
102*0Sstevel@tonic-gate 	u_long usecs;
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	if (__db_jump.j_yield != NULL && __db_jump.j_yield() == 0)
105*0Sstevel@tonic-gate 		return;
106*0Sstevel@tonic-gate 	__os_sleep(0, usecs);
107*0Sstevel@tonic-gate }
108