00001 /*- 00002 * See the file LICENSE for redistribution information. 00003 * 00004 * Copyright (c) 1997, 1998, 1999, 2000 00005 * Sleepycat Software. All rights reserved. 00006 */ 00007 00008 #include "config.h" 00009 00010 #ifndef lint 00011 static const char revid[] = "$Id: os__spin_8c-source.html,v 1.1 2008年06月08日 10:21:31 sebdiaz Exp $"; 00012 #endif /* not lint */ 00013 00014 #ifndef NO_SYSTEM_INCLUDES 00015 #include <sys/types.h> 00016 #if defined(HAVE_PSTAT_GETDYNAMIC) 00017 #include <sys/pstat.h> 00018 #endif 00019 00020 #include <limits.h> 00021 #include <unistd.h> 00022 #endif 00023 00024 #include "db_int.h" 00025 #include "os_jump.h" 00026 00027 #if defined(HAVE_PSTAT_GETDYNAMIC) 00028 /* 00029 * __os_pstat_getdynamic -- 00030 * HP/UX. 00031 */ 00032 static int 00033 __os_pstat_getdynamic() 00034 { 00035 struct pst_dynamic psd; 00036 00037 return (pstat_getdynamic(&psd, 00038 sizeof(psd), (size_t)1, 0) == -1 ? 1 : psd.psd_proc_cnt); 00039 } 00040 #endif 00041 00042 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) 00043 /* 00044 * __os_sysconf -- 00045 * Solaris, Linux. 00046 */ 00047 static int 00048 __os_sysconf() 00049 { 00050 int nproc; 00051 00052 return ((nproc = sysconf(_SC_NPROCESSORS_ONLN)) > 1 ? nproc : 1); 00053 } 00054 #endif 00055 00056 /* 00057 * CDB___os_spin -- 00058 * Return the number of default spins before blocking. 00059 * 00060 * PUBLIC: int CDB___os_spin __P((void)); 00061 */ 00062 int 00063 CDB___os_spin() 00064 { 00065 /* 00066 * If the application specified a value or we've already figured it 00067 * out, return it. 00068 * 00069 * XXX 00070 * We don't want to repeatedly call the underlying function because 00071 * it can be expensive (e.g., requiring multiple filesystem accesses 00072 * under Debian Linux). 00073 */ 00074 if (DB_GLOBAL(db_tas_spins) != 0) 00075 return (DB_GLOBAL(db_tas_spins)); 00076 00077 DB_GLOBAL(db_tas_spins) = 1; 00078 #if defined(HAVE_PSTAT_GETDYNAMIC) 00079 DB_GLOBAL(db_tas_spins) = __os_pstat_getdynamic(); 00080 #endif 00081 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) 00082 DB_GLOBAL(db_tas_spins) = __os_sysconf(); 00083 #endif 00084 00085 /* 00086 * Spin 50 times per processor, we have anecdotal evidence that this 00087 * is a reasonable value. 00088 */ 00089 if (DB_GLOBAL(db_tas_spins) != 1) 00090 DB_GLOBAL(db_tas_spins) *= 50; 00091 00092 return (DB_GLOBAL(db_tas_spins)); 00093 } 00094 00095 /* 00096 * CDB___os_yield -- 00097 * Yield the processor. 00098 * 00099 * PUBLIC: void CDB___os_yield __P((DB_ENV*, u_long)); 00100 */ 00101 void 00102 CDB___os_yield(dbenv, usecs) 00103 DB_ENV *dbenv; 00104 u_long usecs; 00105 { 00106 if (CDB___db_jump.j_yield != NULL && CDB___db_jump.j_yield() == 0) 00107 return; 00108 CDB___os_sleep(dbenv, 0, usecs); 00109 }