Update.
[glibc.git] / db2 / os / os_spin.c
blobcbde58894ac21c67f31cadc43c1b17b56241fe16
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "@(#)os_spin.c 10.10 (Sleepycat) 10/12/98";
12 #endif /* not lint */
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
16 #if defined(HAVE_PSTAT_GETDYNAMIC)
17 #include <sys/pstat.h>
18 #endif
20 #include <limits.h>
21 #include <unistd.h>
22 #endif
24 #include "db_int.h"
25 #include "os_jump.h"
27 #if defined(HAVE_PSTAT_GETDYNAMIC)
29 * __os_pstat_getdynamic --
30 * HP/UX.
32 static int
33 __os_pstat_getdynamic()
35 struct pst_dynamic psd;
37 return (pstat_getdynamic(&psd,
38 sizeof(psd), (size_t)1, 0) == -1 ? 1 : psd.psd_proc_cnt);
40 #endif
42 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
44 * __os_sysconf --
45 * Solaris, Linux.
47 static int
48 __os_sysconf(void)
50 int nproc;
52 return ((nproc = sysconf(_SC_NPROCESSORS_ONLN)) > 1 ? nproc : 1);
54 #endif
57 * __os_spin --
58 * Return the number of default spins before blocking.
60 * PUBLIC: int __os_spin __P((void));
62 int
63 __os_spin()
66 * If the application specified a value or we've already figured it
67 * out, return it.
69 * XXX
70 * We don't want to repeatedly call the underlying function because
71 * it can be expensive (e.g., requiring multiple filesystem accesses
72 * under Debian Linux).
74 if (DB_GLOBAL(db_tsl_spins) != 0)
75 return (DB_GLOBAL(db_tsl_spins));
77 DB_GLOBAL(db_tsl_spins) = 1;
78 #if defined(HAVE_PSTAT_GETDYNAMIC)
79 DB_GLOBAL(db_tsl_spins) = __os_pstat_getdynamic();
80 #endif
81 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
82 DB_GLOBAL(db_tsl_spins) = __os_sysconf();
83 #endif
86 * Spin 50 times per processor, we have anecdotal evidence that this
87 * is a reasonable value.
89 DB_GLOBAL(db_tsl_spins) *= 50;
91 return (DB_GLOBAL(db_tsl_spins));
95 * __os_yield --
96 * Yield the processor.
98 * PUBLIC: void __os_yield __P((u_long));
100 void
101 __os_yield(usecs)
102 u_long usecs;
104 if (__db_jump.j_yield != NULL && __db_jump.j_yield() == 0)
105 return;
106 __os_sleep(0, usecs);