Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

os_id.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2001-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: os_id.c,v 12.15 2005/11/10 18:44:02 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #include <stdlib.h>
00016 #endif
00017 
00018 #include "db_int.h"
00019 #include "dbinc/mutex_int.h"            /* Required to load appropriate
00020                                            header files for thread functions. */
00021 
00022 /*
00023  * __os_id --
00024  *      Return the current process ID.
00025  *
00026  * PUBLIC: void __os_id __P((DB_ENV *, pid_t *, db_threadid_t*));
00027  */
00028 void
00029 __os_id(dbenv, pidp, tidp)
00030         DB_ENV *dbenv;
00031         pid_t *pidp;
00032         db_threadid_t *tidp;
00033 {
00034         /*
00035          * We can't depend on dbenv not being NULL, this routine is called
00036          * from places where there's no DB_ENV handle.  It takes a DB_ENV
00037          * handle as an arg because it's the default DB_ENV->thread_id function.
00038          *
00039          * We cache the pid in the DB_ENV handle, it's a fairly slow call on
00040          * lots of systems.
00041          */
00042         if (pidp != NULL) {
00043                 if (dbenv == NULL) {
00044 #if defined(HAVE_VXWORKS)
00045                         *pidp = taskIdSelf();
00046 #else
00047                         *pidp = getpid();
00048 #endif
00049                 } else
00050                         *pidp = dbenv->pid_cache;
00051         }
00052 
00053         if (tidp != NULL) {
00054 #if defined(DB_WIN32)
00055                 *tidp = GetCurrentThreadId();
00056 #elif defined(HAVE_MUTEX_UI_THREADS)
00057                 *tidp = thr_self();
00058 #elif defined(HAVE_MUTEX_SOLARIS_LWP) || \
00059         defined(HAVE_MUTEX_PTHREADS) || defined(HAVE_PTHREAD_SELF)
00060                 *tidp = pthread_self();
00061 #else
00062                 /*
00063                  * Default to just getpid.
00064                  */
00065                 *tidp = 0;
00066 #endif
00067         }
00068 }
00069 
00070 /*
00071  * __os_unique_id --
00072  *      Return a unique 32-bit value.
00073  *
00074  * PUBLIC: void __os_unique_id __P((DB_ENV *, u_int32_t *));
00075  */
00076 void
00077 __os_unique_id(dbenv, idp)
00078         DB_ENV *dbenv;
00079         u_int32_t *idp;
00080 {
00081         static int first = 1;
00082         pid_t pid;
00083         db_threadid_t tid;
00084         u_int32_t id, sec, usec;
00085 
00086         *idp = 0;
00087 
00088         /*
00089          * Our randomized value is comprised of our process ID, the current
00090          * time of day and a couple of a stack addresses, all XOR'd together.
00091          */
00092         __os_id(dbenv, &pid, &tid);
00093         __os_clock(dbenv, &sec, &usec);
00094 
00095         id = (u_int32_t)pid ^ sec ^ usec ^ P_TO_UINT32(&pid);
00096 
00097         /*
00098          * We could try and find a reasonable random-number generator, but
00099          * that's not all that easy to do.  Seed and use srand()/rand(), if
00100          * we can find them.
00101          */
00102 #if HAVE_SRAND
00103         if (first == 1)
00104                 srand((u_int)id);
00105 #endif
00106         first = 0;
00107 
00108 #if HAVE_RAND
00109         id ^= (u_int)rand();
00110 #endif
00111 
00112         *idp = id;
00113 }

Generated on Sun Dec 25 12:14:42 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2