Back to index

lightning-sunbird  0.9+nobinonly
os_beos.c
Go to the documentation of this file.
00001 /*
00002 ** 2004 May 22
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 ******************************************************************************
00012 **
00013 ** This file contains code that is specific to BeOS systems.
00014 ** Code is derived from sqlite3 os_unix.c, with BeOS modifications by DeadYak
00015 ** and mmu_man of the Haiku OS team.
00016 */
00017 #include "sqliteInt.h"
00018 #include "os.h"
00019 #if OS_BEOS              /* This file is used on BeOS and derivatives only */
00020 
00021 /*
00022 ** These #defines should enable >2GB file support on Posix if the
00023 ** underlying operating system supports it.  If the OS lacks
00024 ** large file support, these should be no-ops.
00025 **
00026 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
00027 ** on the compiler command line.  This is necessary if you are compiling
00028 ** on a recent machine (ex: RedHat 7.2) but you want your code to work
00029 ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
00030 ** without this option, LFS is enable.  But LFS does not exist in the kernel
00031 ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
00032 ** portability you should omit LFS.
00033 */
00034 #ifndef SQLITE_DISABLE_LFS
00035 # define _LARGE_FILE       1
00036 # ifndef _FILE_OFFSET_BITS
00037 #   define _FILE_OFFSET_BITS 64
00038 # endif
00039 # define _LARGEFILE_SOURCE 1
00040 #endif
00041 
00042 /*
00043 ** standard include files.
00044 */
00045 #include <sys/types.h>
00046 #include <sys/stat.h>
00047 #include <fcntl.h>
00048 #include <unistd.h>
00049 #include <time.h>
00050 #include <sys/time.h>
00051 #include <errno.h>
00052 #include <support/TLS.h>
00053 
00054 /*
00055 ** If we are to be thread-safe, include the BeOS os header that includes bthreads
00056 ** and define the SQLITE_BEOS_THREADS macro.
00057 */
00058 #if defined(THREADSAFE) && THREADSAFE
00059 # include <kernel/OS.h>
00060 # define SQLITE_BEOS_THREADS 1
00061 #endif
00062 
00063 /*
00064 ** Default permissions when creating a new file
00065 */
00066 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
00067 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
00068 #endif
00069 
00070 
00071 
00072 /*
00073 ** For sqlite, BeOS uses the same file structure as Unix.
00074 ** The unixFile structure is subclass of OsFile specific for the unix
00075 ** portability layer.
00076 */
00077 typedef struct unixFile unixFile;
00078 struct unixFile {
00079   IoMethod const *pMethod;  /* Always the first entry */
00080   struct openCnt *pOpen;    /* Info about all open fd's on this inode */
00081   struct lockInfo *pLock;   /* Info about locks on this inode */
00082   int h;                    /* The file descriptor */
00083   unsigned char locktype;   /* The type of lock held on this fd */
00084   unsigned char isOpen;     /* True if needs to be closed */
00085   unsigned char fullSync;   /* Use F_FULLSYNC if available */
00086   int dirfd;                /* File descriptor for the directory */
00087   i64 offset;               /* Seek offset */
00088 #ifdef SQLITE_BEOS_THREADS
00089   thread_id tid;            /* The thread that "owns" this OsFile */
00090 #endif
00091 };
00092 
00093 /*
00094 ** Provide the ability to override some OS-layer functions during
00095 ** testing.  This is used to simulate OS crashes to verify that 
00096 ** commits are atomic even in the event of an OS crash.
00097 */
00098 #ifdef SQLITE_CRASH_TEST
00099   extern int sqlite3CrashTestEnable;
00100   extern int sqlite3CrashOpenReadWrite(const char*, OsFile**, int*);
00101   extern int sqlite3CrashOpenExclusive(const char*, OsFile**, int);
00102   extern int sqlite3CrashOpenReadOnly(const char*, OsFile**, int);
00103 # define CRASH_TEST_OVERRIDE(X,A,B,C) \
00104   if(sqlite3CrashTestEnable){ return X(A,B,C); }
00105 #else
00106 # define CRASH_TEST_OVERRIDE(X,A,B,C)  /* no-op */
00107 #endif
00108 
00109 
00110 /*
00111 ** Include code that is common to all os_*.c files
00112 */
00113 #include "os_common.h"
00114 
00115 /*
00116 ** Do not include any of the File I/O interface procedures if the
00117 ** SQLITE_OMIT_DISKIO macro is defined (indicating that the database
00118 ** will be in-memory only)
00119 */
00120 #ifndef SQLITE_OMIT_DISKIO
00121 
00122 
00123 /*
00124 ** Define various macros that are missing from some systems.
00125 */
00126 #ifndef O_LARGEFILE
00127 # define O_LARGEFILE 0
00128 #endif
00129 #ifdef SQLITE_DISABLE_LFS
00130 # undef O_LARGEFILE
00131 # define O_LARGEFILE 0
00132 #endif
00133 #ifndef O_NOFOLLOW
00134 # define O_NOFOLLOW 0
00135 #endif
00136 #ifndef O_BINARY
00137 # define O_BINARY 0
00138 #endif
00139 
00140 /*
00141 ** The DJGPP compiler environment looks mostly like Unix, but it
00142 ** lacks the fcntl() system call.  So redefine fcntl() to be something
00143 ** that always succeeds.  This means that locking does not occur under
00144 ** DJGPP.  But it's DOS - what did you expect?
00145 */
00146 #ifdef __DJGPP__
00147 # define fcntl(A,B,C) 0
00148 #endif
00149 
00150 /*
00151 ** The threadid macro resolves to the thread-id or to 0.  Used for
00152 ** testing and debugging only.
00153 */
00154 #ifdef SQLITE_BEOS_THREADS
00155 #define threadid find_thread(NULL)
00156 #else
00157 #define threadid 0
00158 #endif
00159 
00160 /*
00161 ** Set or check the OsFile.tid field.  This field is set when an OsFile
00162 ** is first opened.  All subsequent uses of the OsFile verify that the
00163 ** same thread is operating on the OsFile.  Some operating systems do
00164 ** not allow locks to be overridden by other threads and that restriction
00165 ** means that sqlite3* database handles cannot be moved from one thread
00166 ** to another.  This logic makes sure a user does not try to do that
00167 ** by mistake.
00168 **
00169 ** Version 3.3.1 (2006-01-15):  OsFiles can be moved from one thread to
00170 ** another as long as we are running on a system that supports threads
00171 ** overriding each others locks (which now the most common behavior)
00172 ** or if no locks are held.  But the OsFile.pLock field needs to be
00173 ** recomputed because its key includes the thread-id.  See the 
00174 ** transferOwnership() function below for additional information
00175 **
00176 ** This code is BeOS-specific and differs from Unix.
00177 */
00178 #if defined(SQLITE_BEOS_THREADS)
00179 # define SET_THREADID(X)   (X)->tid = find_thread(NULL)
00180 # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
00181                              !((X)->tid == find_thread(NULL)))
00182 #else
00183 # define SET_THREADID(X)
00184 # define CHECK_THREADID(X) 0
00185 #endif
00186 
00187 /*
00188 ** Here is the dirt on POSIX advisory locks:  ANSI STD 1003.1 (1996)
00189 ** section 6.5.2.2 lines 483 through 490 specify that when a process
00190 ** sets or clears a lock, that operation overrides any prior locks set
00191 ** by the same process.  It does not explicitly say so, but this implies
00192 ** that it overrides locks set by the same process using a different
00193 ** file descriptor.  Consider this test case:
00194 **
00195 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
00196 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
00197 **
00198 ** Suppose ./file1 and ./file2 are really the same file (because
00199 ** one is a hard or symbolic link to the other) then if you set
00200 ** an exclusive lock on fd1, then try to get an exclusive lock
00201 ** on fd2, it works.  I would have expected the second lock to
00202 ** fail since there was already a lock on the file due to fd1.
00203 ** But not so.  Since both locks came from the same process, the
00204 ** second overrides the first, even though they were on different
00205 ** file descriptors opened on different file names.
00206 **
00207 ** Bummer.  If you ask me, this is broken.  Badly broken.  It means
00208 ** that we cannot use POSIX locks to synchronize file access among
00209 ** competing threads of the same process.  POSIX locks will work fine
00210 ** to synchronize access for threads in separate processes, but not
00211 ** threads within the same process.
00212 **
00213 ** To work around the problem, SQLite has to manage file locks internally
00214 ** on its own.  Whenever a new database is opened, we have to find the
00215 ** specific inode of the database file (the inode is determined by the
00216 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
00217 ** and check for locks already existing on that inode.  When locks are
00218 ** created or removed, we have to look at our own internal record of the
00219 ** locks to see if another thread has previously set a lock on that same
00220 ** inode.
00221 **
00222 ** The OsFile structure for POSIX is no longer just an integer file
00223 ** descriptor.  It is now a structure that holds the integer file
00224 ** descriptor and a pointer to a structure that describes the internal
00225 ** locks on the corresponding inode.  There is one locking structure
00226 ** per inode, so if the same inode is opened twice, both OsFile structures
00227 ** point to the same locking structure.  The locking structure keeps
00228 ** a reference count (so we will know when to delete it) and a "cnt"
00229 ** field that tells us its internal lock status.  cnt==0 means the
00230 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
00231 ** cnt>0 means there are cnt shared locks on the file.
00232 **
00233 ** Any attempt to lock or unlock a file first checks the locking
00234 ** structure.  The fcntl() system call is only invoked to set a 
00235 ** POSIX lock if the internal lock structure transitions between
00236 ** a locked and an unlocked state.
00237 **
00238 ** 2004-Jan-11:
00239 ** More recent discoveries about POSIX advisory locks.  (The more
00240 ** I discover, the more I realize the a POSIX advisory locks are
00241 ** an abomination.)
00242 **
00243 ** If you close a file descriptor that points to a file that has locks,
00244 ** all locks on that file that are owned by the current process are
00245 ** released.  To work around this problem, each OsFile structure contains
00246 ** a pointer to an openCnt structure.  There is one openCnt structure
00247 ** per open inode, which means that multiple OsFiles can point to a single
00248 ** openCnt.  When an attempt is made to close an OsFile, if there are
00249 ** other OsFiles open on the same inode that are holding locks, the call
00250 ** to close() the file descriptor is deferred until all of the locks clear.
00251 ** The openCnt structure keeps a list of file descriptors that need to
00252 ** be closed and that list is walked (and cleared) when the last lock
00253 ** clears.
00254 **
00255 ** First, under Linux threads, because each thread has a separate
00256 ** process ID, lock operations in one thread do not override locks
00257 ** to the same file in other threads.  Linux threads behave like
00258 ** separate processes in this respect.  But, if you close a file
00259 ** descriptor in linux threads, all locks are cleared, even locks
00260 ** on other threads and even though the other threads have different
00261 ** process IDs.  Linux threads is inconsistent in this respect.
00262 ** (I'm beginning to think that linux threads is an abomination too.)
00263 ** The consequence of this all is that the hash table for the lockInfo
00264 ** structure has to include the process id as part of its key because
00265 ** locks in different threads are treated as distinct.  But the 
00266 ** openCnt structure should not include the process id in its
00267 ** key because close() clears lock on all threads, not just the current
00268 ** thread.  Were it not for this goofiness in linux threads, we could
00269 ** combine the lockInfo and openCnt structures into a single structure.
00270 **
00271 ** 2004-Jun-28:
00272 ** On some versions of linux, threads can override each others locks.
00273 ** On others not.  Sometimes you can change the behavior on the same
00274 ** system by setting the LD_ASSUME_KERNEL environment variable.  The
00275 ** POSIX standard is silent as to which behavior is correct, as far
00276 ** as I can tell, so other versions of unix might show the same
00277 ** inconsistency.  There is no little doubt in my mind that posix
00278 ** advisory locks and linux threads are profoundly broken.
00279 **
00280 ** To work around the inconsistencies, we have to test at runtime 
00281 ** whether or not threads can override each others locks.  This test
00282 ** is run once, the first time any lock is attempted.  A static 
00283 ** variable is set to record the results of this test for future
00284 ** use.
00285 */
00286 
00287 /*
00288 ** An instance of the following structure serves as the key used
00289 ** to locate a particular lockInfo structure given its inode.
00290 **
00291 ** If threads cannot override each others locks, then we set the
00292 ** lockKey.tid field to the thread ID.  If threads can override
00293 ** each others locks then tid is always set to zero.  tid is omitted
00294 ** if we compile without threading support.
00295 */
00296 struct lockKey {
00297   dev_t dev;       /* Device number */
00298   ino_t ino;       /* Inode number */
00299 #ifdef SQLITE_BEOS_THREADS
00300   thread_id tid;   /* Thread ID or zero if threads can override each other */
00301 #endif
00302 };
00303 
00304 /*
00305 ** An instance of the following structure is allocated for each open
00306 ** inode on each thread with a different process ID.  (Threads have
00307 ** different process IDs on linux, but not on most other unixes.)
00308 **
00309 ** A single inode can have multiple file descriptors, so each OsFile
00310 ** structure contains a pointer to an instance of this object and this
00311 ** object keeps a count of the number of OsFiles pointing to it.
00312 */
00313 struct lockInfo {
00314   struct lockKey key;  /* The lookup key */
00315   int cnt;             /* Number of SHARED locks held */
00316   int locktype;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
00317   int nRef;            /* Number of pointers to this structure */
00318 };
00319 
00320 /*
00321 ** An instance of the following structure serves as the key used
00322 ** to locate a particular openCnt structure given its inode.  This
00323 ** is the same as the lockKey except that the thread ID is omitted.
00324 */
00325 struct openKey {
00326   dev_t dev;   /* Device number */
00327   ino_t ino;   /* Inode number */
00328 };
00329 
00330 /*
00331 ** An instance of the following structure is allocated for each open
00332 ** inode.  This structure keeps track of the number of locks on that
00333 ** inode.  If a close is attempted against an inode that is holding
00334 ** locks, the close is deferred until all locks clear by adding the
00335 ** file descriptor to be closed to the pending list.
00336 */
00337 struct openCnt {
00338   struct openKey key;   /* The lookup key */
00339   int nRef;             /* Number of pointers to this structure */
00340   int nLock;            /* Number of outstanding locks */
00341   int nPending;         /* Number of pending close() operations */
00342   int *aPending;        /* Malloced space holding fd's awaiting a close() */
00343 };
00344 
00345 /* 
00346 ** These hash tables map inodes and file descriptors (really, lockKey and
00347 ** openKey structures) into lockInfo and openCnt structures.  Access to 
00348 ** these hash tables must be protected by a mutex.
00349 */
00350 static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 
00351     sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0};
00352 static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 
00353     sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0};
00354 
00355 #ifdef SQLITE_BEOS_THREADS
00356 /*
00357 ** This variable records whether or not threads can override each others
00358 ** locks.
00359 **
00360 **    0:  No.  Threads cannot override each others locks.
00361 **    1:  Yes.  Threads can override each others locks.
00362 **   -1:  We don't know yet.
00363 **
00364 ** On some systems, we know at compile-time if threads can override each
00365 ** others locks.  On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
00366 ** will be set appropriately.  On other systems, we have to check at
00367 ** runtime.  On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
00368 ** undefined.
00369 **
00370 ** This variable normally has file scope only.  But during testing, we make
00371 ** it a global so that the test code can change its value in order to verify
00372 ** that the right stuff happens in either case.
00373 */
00374 #ifndef SQLITE_THREAD_OVERRIDE_LOCK
00375 # define SQLITE_THREAD_OVERRIDE_LOCK 1
00376 #endif
00377 #ifdef SQLITE_TEST
00378 int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
00379 #else
00380 static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
00381 #endif
00382 
00383 /*
00384 ** This structure holds information passed into individual test
00385 ** threads by the testThreadLockingBehavior() routine.
00386 */
00387 struct threadTestData {
00388   int fd;                /* File to be locked */
00389   struct flock lock;     /* The locking operation */
00390   int result;            /* Result of the locking operation */
00391 };
00392 
00393 #ifdef SQLITE_LOCK_TRACE
00394 /*
00395 ** Print out information about all locking operations.
00396 **
00397 ** This routine is used for troubleshooting locks on multithreaded
00398 ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
00399 ** command-line option on the compiler.  This code is normally
00400 ** turned off.
00401 */
00402 static int lockTrace(int fd, int op, struct flock *p){
00403   char *zOpName, *zType;
00404   int s;
00405   int savedErrno;
00406   if( op==F_GETLK ){
00407     zOpName = "GETLK";
00408   }else if( op==F_SETLK ){
00409     zOpName = "SETLK";
00410   }else{
00411     s = fcntl(fd, op, p);
00412     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
00413     return s;
00414   }
00415   if( p->l_type==F_RDLCK ){
00416     zType = "RDLCK";
00417   }else if( p->l_type==F_WRLCK ){
00418     zType = "WRLCK";
00419   }else if( p->l_type==F_UNLCK ){
00420     zType = "UNLCK";
00421   }else{
00422     assert( 0 );
00423   }
00424   assert( p->l_whence==SEEK_SET );
00425   s = fcntl(fd, op, p);
00426   savedErrno = errno;
00427   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
00428      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
00429      (int)p->l_pid, s);
00430   if( s && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
00431     struct flock l2;
00432     l2 = *p;
00433     fcntl(fd, F_GETLK, &l2);
00434     if( l2.l_type==F_RDLCK ){
00435       zType = "RDLCK";
00436     }else if( l2.l_type==F_WRLCK ){
00437       zType = "WRLCK";
00438     }else if( l2.l_type==F_UNLCK ){
00439       zType = "UNLCK";
00440     }else{
00441       assert( 0 );
00442     }
00443   sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
00444      zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
00445   }
00446   errno = savedErrno;
00447   return s;
00448 }
00449 #define fcntl lockTrace
00450 #endif /* SQLITE_LOCK_TRACE */
00451 
00452 /*
00453 ** The testThreadLockingBehavior() routine launches two separate
00454 ** threads on this routine.  This routine attempts to lock a file
00455 ** descriptor then returns.  The success or failure of that attempt
00456 ** allows the testThreadLockingBehavior() procedure to determine
00457 ** whether or not threads can override each others locks.
00458 */
00459 static void *threadLockingTest(void *pArg){
00460   struct threadTestData *pData = (struct threadTestData*)pArg;
00461   pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
00462   return pArg;
00463 }
00464 
00465 /*
00466 ** This procedure attempts to determine whether or not threads
00467 ** can override each others locks then sets the 
00468 ** threadsOverrideEachOthersLocks variable appropriately.
00469 */
00470 static void testThreadLockingBehavior(int fd_orig){
00471   threadsOverrideEachOthersLocks =  1;
00472 }
00473 #endif /* SQLITE_BEOS_THREADS */
00474 
00475 /*
00476 ** Release a lockInfo structure previously allocated by findLockInfo().
00477 */
00478 static void releaseLockInfo(struct lockInfo *pLock){
00479   assert( sqlite3OsInMutex(1) );
00480   pLock->nRef--;
00481   if( pLock->nRef==0 ){
00482     sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
00483     sqlite3ThreadSafeFree(pLock);
00484   }
00485 }
00486 
00487 /*
00488 ** Release a openCnt structure previously allocated by findLockInfo().
00489 */
00490 static void releaseOpenCnt(struct openCnt *pOpen){
00491   assert( sqlite3OsInMutex(1) );
00492   pOpen->nRef--;
00493   if( pOpen->nRef==0 ){
00494     sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
00495     free(pOpen->aPending);
00496     sqlite3ThreadSafeFree(pOpen);
00497   }
00498 }
00499 
00500 /*
00501 ** Given a file descriptor, locate lockInfo and openCnt structures that
00502 ** describes that file descriptor.  Create new ones if necessary.  The
00503 ** return values might be uninitialized if an error occurs.
00504 **
00505 ** Return the number of errors.
00506 */
00507 static int findLockInfo(
00508   int fd,                      /* The file descriptor used in the key */
00509   struct lockInfo **ppLock,    /* Return the lockInfo structure here */
00510   struct openCnt **ppOpen      /* Return the openCnt structure here */
00511 ){
00512   int rc;
00513   struct lockKey key1;
00514   struct openKey key2;
00515   struct stat statbuf;
00516   struct lockInfo *pLock;
00517   struct openCnt *pOpen;
00518   rc = fstat(fd, &statbuf);
00519   if( rc!=0 ) return 1;
00520 
00521   assert( sqlite3OsInMutex(1) );
00522   memset(&key1, 0, sizeof(key1));
00523   key1.dev = statbuf.st_dev;
00524   key1.ino = statbuf.st_ino;
00525   key1.tid = find_thread(NULL);
00526 
00527   memset(&key2, 0, sizeof(key2));
00528   key2.dev = statbuf.st_dev;
00529   key2.ino = statbuf.st_ino;
00530   pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
00531   if( pLock==0 ){
00532     struct lockInfo *pOld;
00533     pLock = sqlite3ThreadSafeMalloc( sizeof(*pLock) );
00534     if( pLock==0 ){
00535       rc = 1;
00536       goto exit_findlockinfo;
00537     }
00538     pLock->key = key1;
00539     pLock->nRef = 1;
00540     pLock->cnt = 0;
00541     pLock->locktype = 0;
00542     pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
00543     if( pOld!=0 ){
00544       assert( pOld==pLock );
00545       sqlite3ThreadSafeFree(pLock);
00546       rc = 1;
00547       goto exit_findlockinfo;
00548     }
00549   }else{
00550     pLock->nRef++;
00551   }
00552   *ppLock = pLock;
00553   if( ppOpen!=0 ){
00554     pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
00555     if( pOpen==0 ){
00556       struct openCnt *pOld;
00557       pOpen = sqlite3ThreadSafeMalloc( sizeof(*pOpen) );
00558       if( pOpen==0 ){
00559         releaseLockInfo(pLock);
00560         rc = 1;
00561         goto exit_findlockinfo;
00562       }
00563       pOpen->key = key2;
00564       pOpen->nRef = 1;
00565       pOpen->nLock = 0;
00566       pOpen->nPending = 0;
00567       pOpen->aPending = 0;
00568       pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
00569       if( pOld!=0 ){
00570         assert( pOld==pOpen );
00571         sqlite3ThreadSafeFree(pOpen);
00572         releaseLockInfo(pLock);
00573         rc = 1;
00574         goto exit_findlockinfo;
00575       }
00576     }else{
00577       pOpen->nRef++;
00578     }
00579     *ppOpen = pOpen;
00580   }
00581 
00582 exit_findlockinfo:
00583   return rc;
00584 }
00585 
00586 #ifdef SQLITE_DEBUG
00587 /*
00588 ** Helper function for printing out trace information from debugging
00589 ** binaries. This returns the string represetation of the supplied
00590 ** integer lock-type.
00591 */
00592 static const char *locktypeName(int locktype){
00593   switch( locktype ){
00594   case NO_LOCK: return "NONE";
00595   case SHARED_LOCK: return "SHARED";
00596   case RESERVED_LOCK: return "RESERVED";
00597   case PENDING_LOCK: return "PENDING";
00598   case EXCLUSIVE_LOCK: return "EXCLUSIVE";
00599   }
00600   return "ERROR";
00601 }
00602 #endif
00603 
00604 /*
00605 ** If we are currently in a different thread than the thread that the
00606 ** unixFile argument belongs to, then transfer ownership of the unixFile
00607 ** over to the current thread.
00608 **
00609 ** A unixFile is only owned by a thread on systems where one thread is
00610 ** unable to override locks created by a different thread.  RedHat9 is
00611 ** an example of such a system.
00612 **
00613 ** Ownership transfer is only allowed if the unixFile is currently unlocked.
00614 ** If the unixFile is locked and an ownership is wrong, then return
00615 ** SQLITE_MISUSE.  SQLITE_OK is returned if everything works.
00616 */
00617 #ifdef SQLITE_BEOS_THREADS
00618 static int transferOwnership(unixFile *pFile){
00619   int rc;
00620   thread_id hSelf;
00621   if( threadsOverrideEachOthersLocks ){
00622     /* Ownership transfers not needed on this system */
00623     return SQLITE_OK;
00624   }
00625   hSelf = find_thread(NULL);
00626   if( pFile->tid == hSelf){
00627     /* We are still in the same thread */
00628     TRACE1("No-transfer, same thread\n");
00629     return SQLITE_OK;
00630   }
00631   if( pFile->locktype!=NO_LOCK ){
00632     /* We cannot change ownership while we are holding a lock! */
00633     return SQLITE_MISUSE;
00634   }
00635   TRACE4("Transfer ownership of %d from %d to %d\n", pFile->h,pFile->tid,hSelf);
00636   pFile->tid = hSelf;
00637   releaseLockInfo(pFile->pLock);
00638   rc = findLockInfo(pFile->h, &pFile->pLock, 0);
00639   TRACE5("LOCK    %d is now %s(%s,%d)\n", pFile->h,
00640      locktypeName(pFile->locktype),
00641      locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
00642   return rc;
00643 }
00644 #else
00645   /* On single-threaded builds, ownership transfer is a no-op */
00646 # define transferOwnership(X) SQLITE_OK
00647 #endif
00648 
00649 /*
00650 ** Delete the named file
00651 */
00652 int sqlite3BeDelete(const char *zFilename){
00653   unlink(zFilename);
00654   return SQLITE_OK;
00655 }
00656 
00657 /*
00658 ** Return TRUE if the named file exists.
00659 */
00660 int sqlite3BeFileExists(const char *zFilename){
00661   return access(zFilename, 0)==0;
00662 }
00663 
00664 /* Forward declaration */
00665 static int allocateUnixFile(unixFile *pInit, OsFile **pId);
00666 
00667 /*
00668 ** Attempt to open a file for both reading and writing.  If that
00669 ** fails, try opening it read-only.  If the file does not exist,
00670 ** try to create it.
00671 **
00672 ** On success, a handle for the open file is written to *id
00673 ** and *pReadonly is set to 0 if the file was opened for reading and
00674 ** writing or 1 if the file was opened read-only.  The function returns
00675 ** SQLITE_OK.
00676 **
00677 ** On failure, the function returns SQLITE_CANTOPEN and leaves
00678 ** *id and *pReadonly unchanged.
00679 */
00680 int sqlite3BeOpenReadWrite(
00681   const char *zFilename,
00682   OsFile **pId,
00683   int *pReadonly
00684 ){
00685   int rc;
00686   unixFile f;
00687 
00688   CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadWrite, zFilename, pId, pReadonly);
00689   assert( 0==*pId );
00690   f.h = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY,
00691                           SQLITE_DEFAULT_FILE_PERMISSIONS);
00692   if( f.h<0 ){
00693 #ifdef EISDIR
00694     if( errno==EISDIR ){
00695       return SQLITE_CANTOPEN;
00696     }
00697 #endif
00698     f.h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
00699     if( f.h<0 ){
00700       return SQLITE_CANTOPEN; 
00701     }
00702     *pReadonly = 1;
00703   }else{
00704     *pReadonly = 0;
00705   }
00706   sqlite3OsEnterMutex();
00707   rc = findLockInfo(f.h, &f.pLock, &f.pOpen);
00708   sqlite3OsLeaveMutex();
00709   if( rc ){
00710     close(f.h);
00711     return SQLITE_NOMEM;
00712   }
00713   TRACE3("OPEN    %-3d %s\n", f.h, zFilename);
00714   return allocateUnixFile(&f, pId);
00715 }
00716 
00717 
00718 /*
00719 ** Attempt to open a new file for exclusive access by this process.
00720 ** The file will be opened for both reading and writing.  To avoid
00721 ** a potential security problem, we do not allow the file to have
00722 ** previously existed.  Nor do we allow the file to be a symbolic
00723 ** link.
00724 **
00725 ** If delFlag is true, then make arrangements to automatically delete
00726 ** the file when it is closed.
00727 **
00728 ** On success, write the file handle into *id and return SQLITE_OK.
00729 **
00730 ** On failure, return SQLITE_CANTOPEN.
00731 */
00732 int sqlite3BeOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){
00733   int rc;
00734   unixFile f;
00735 
00736   CRASH_TEST_OVERRIDE(sqlite3CrashOpenExclusive, zFilename, pId, delFlag);
00737   assert( 0==*pId );
00738   if( access(zFilename, 0)==0 ){
00739     return SQLITE_CANTOPEN;
00740   }
00741   f.h = open(zFilename,
00742                 O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY,
00743                 SQLITE_DEFAULT_FILE_PERMISSIONS);
00744   if( f.h<0 ){
00745     return SQLITE_CANTOPEN;
00746   }
00747   sqlite3OsEnterMutex();
00748   rc = findLockInfo(f.h, &f.pLock, &f.pOpen);
00749   sqlite3OsLeaveMutex();
00750   if( rc ){
00751     close(f.h);
00752     unlink(zFilename);
00753     return SQLITE_NOMEM;
00754   }
00755   if( delFlag ){
00756     unlink(zFilename);
00757   }
00758   TRACE3("OPEN-EX %-3d %s\n", f.h, zFilename);
00759   return allocateUnixFile(&f, pId);
00760 }
00761 
00762 /*
00763 ** Attempt to open a new file for read-only access.
00764 **
00765 ** On success, write the file handle into *id and return SQLITE_OK.
00766 **
00767 ** On failure, return SQLITE_CANTOPEN.
00768 */
00769 int sqlite3BeOpenReadOnly(const char *zFilename, OsFile **pId){
00770   int rc;
00771   unixFile f;
00772 
00773   CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadOnly, zFilename, pId, 0);
00774   assert( 0==*pId );
00775   f.h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
00776   if( f.h<0 ){
00777     return SQLITE_CANTOPEN;
00778   }
00779   sqlite3OsEnterMutex();
00780   rc = findLockInfo(f.h, &f.pLock, &f.pOpen);
00781   sqlite3OsLeaveMutex();
00782   if( rc ){
00783     close(f.h);
00784     return SQLITE_NOMEM;
00785   }
00786   TRACE3("OPEN-RO %-3d %s\n", f.h, zFilename);
00787   return allocateUnixFile(&f, pId);
00788 }
00789 
00790 /*
00791 ** Attempt to open a file descriptor for the directory that contains a
00792 ** file.  This file descriptor can be used to fsync() the directory
00793 ** in order to make sure the creation of a new file is actually written
00794 ** to disk.
00795 **
00796 ** This routine is only meaningful for Unix.  It is a no-op under
00797 ** windows since windows does not support hard links.
00798 **
00799 ** On success, a handle for a previously open file at *id is
00800 ** updated with the new directory file descriptor and SQLITE_OK is
00801 ** returned.
00802 **
00803 ** On failure, the function returns SQLITE_CANTOPEN and leaves
00804 ** *id unchanged.
00805 */
00806 static int unixOpenDirectory(
00807   OsFile *id,
00808   const char *zDirname
00809 ){
00810   unixFile *pFile = (unixFile*)id;
00811   if( pFile==0 ){
00812     /* Do not open the directory if the corresponding file is not already
00813     ** open. */
00814     return SQLITE_CANTOPEN;
00815   }
00816   SET_THREADID(pFile);
00817   assert( pFile->dirfd<0 );
00818   pFile->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0);
00819   if( pFile->dirfd<0 ){
00820     return SQLITE_CANTOPEN; 
00821   }
00822   TRACE3("OPENDIR %-3d %s\n", pFile->dirfd, zDirname);
00823   return SQLITE_OK;
00824 }
00825 
00826 /*
00827 ** If the following global variable points to a string which is the
00828 ** name of a directory, then that directory will be used to store
00829 ** temporary files.
00830 **
00831 ** See also the "PRAGMA temp_store_directory" SQL command.
00832 */
00833 char *sqlite3_temp_directory = 0;
00834 
00835 /*
00836 ** Create a temporary file name in zBuf.  zBuf must be big enough to
00837 ** hold at least SQLITE_TEMPNAME_SIZE characters.
00838 */
00839 int sqlite3BeTempFileName(char *zBuf){
00840   static const char *azDirs[] = {
00841      0,
00842      "/var/tmp",
00843      "/tmp",
00844      ".",
00845   };
00846   static const unsigned char zChars[] =
00847     "abcdefghijklmnopqrstuvwxyz"
00848     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00849     "0123456789";
00850   int i, j;
00851   struct stat buf;
00852   const char *zDir = ".";
00853   azDirs[0] = sqlite3_temp_directory;
00854   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
00855     if( azDirs[i]==0 ) continue;
00856     if( stat(azDirs[i], &buf) ) continue;
00857     if( !S_ISDIR(buf.st_mode) ) continue;
00858     if( access(azDirs[i], 07) ) continue;
00859     zDir = azDirs[i];
00860     break;
00861   }
00862   do{
00863     sprintf(zBuf, "%s/"TEMP_FILE_PREFIX, zDir);
00864     j = strlen(zBuf);
00865     sqlite3Randomness(15, &zBuf[j]);
00866     for(i=0; i<15; i++, j++){
00867       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
00868     }
00869     zBuf[j] = 0;
00870   }while( access(zBuf,0)==0 );
00871   return SQLITE_OK; 
00872 }
00873 
00874 /*
00875 ** Check that a given pathname is a directory and is writable 
00876 **
00877 */
00878 int sqlite3BeIsDirWritable(char *zBuf){
00879 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00880   struct stat buf;
00881   if( zBuf==0 ) return 0;
00882   if( zBuf[0]==0 ) return 0;
00883   if( stat(zBuf, &buf) ) return 0;
00884   if( !S_ISDIR(buf.st_mode) ) return 0;
00885   if( access(zBuf, 07) ) return 0;
00886 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
00887   return 1;
00888 }
00889 
00890 /*
00891 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
00892 ** Return the number of bytes actually read.  Update the offset.
00893 */
00894 static int seekAndRead(unixFile *id, void *pBuf, int cnt){
00895   int got;
00896 #ifdef USE_PREAD
00897   got = pread(id->h, pBuf, cnt, id->offset);
00898 #else
00899   lseek(id->h, id->offset, SEEK_SET);
00900   got = read(id->h, pBuf, cnt);
00901 #endif
00902   if( got>0 ){
00903     id->offset += got;
00904   }
00905   return got;
00906 }
00907 
00908 /*
00909 ** Read data from a file into a buffer.  Return SQLITE_OK if all
00910 ** bytes were read successfully and SQLITE_IOERR if anything goes
00911 ** wrong.
00912 */
00913 static int unixRead(OsFile *id, void *pBuf, int amt){
00914   int got;
00915   assert( id );
00916   SimulateIOError(SQLITE_IOERR);
00917   TIMER_START;
00918   got = seekAndRead((unixFile*)id, pBuf, amt);
00919   TIMER_END;
00920   TRACE5("READ    %-3d %5d %7d %d\n", ((unixFile*)id)->h, got,
00921           last_page, TIMER_ELAPSED);
00922   SEEK(0);
00923   /* if( got<0 ) got = 0; */
00924   if( got==amt ){
00925     return SQLITE_OK;
00926   }else{
00927     return SQLITE_IOERR;
00928   }
00929 }
00930 
00931 /*
00932 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
00933 ** Return the number of bytes actually read.  Update the offset.
00934 */
00935 static int seekAndWrite(unixFile *id, const void *pBuf, int cnt){
00936   int got;
00937 #ifdef USE_PREAD
00938   got = pwrite(id->h, pBuf, cnt, id->offset);
00939 #else
00940   lseek(id->h, id->offset, SEEK_SET);
00941   got = write(id->h, pBuf, cnt);
00942 #endif
00943   if( got>0 ){
00944     id->offset += got;
00945   }
00946   return got;
00947 }
00948 
00949 
00950 /*
00951 ** Write data from a buffer into a file.  Return SQLITE_OK on success
00952 ** or some other error code on failure.
00953 */
00954 static int unixWrite(OsFile *id, const void *pBuf, int amt){
00955   int wrote = 0;
00956   assert( id );
00957   assert( amt>0 );
00958   SimulateIOError(SQLITE_IOERR);
00959   SimulateDiskfullError;
00960   TIMER_START;
00961   while( amt>0 && (wrote = seekAndWrite((unixFile*)id, pBuf, amt))>0 ){
00962     amt -= wrote;
00963     pBuf = &((char*)pBuf)[wrote];
00964   }
00965   TIMER_END;
00966   TRACE5("WRITE   %-3d %5d %7d %d\n", ((unixFile*)id)->h, wrote,
00967           last_page, TIMER_ELAPSED);
00968   SEEK(0);
00969   if( amt>0 ){
00970     return SQLITE_FULL;
00971   }
00972   return SQLITE_OK;
00973 }
00974 
00975 /*
00976 ** Move the read/write pointer in a file.
00977 */
00978 static int unixSeek(OsFile *id, i64 offset){
00979   assert( id );
00980   SEEK(offset/1024 + 1);
00981 #ifdef SQLITE_TEST
00982   if( offset ) SimulateDiskfullError
00983 #endif
00984   ((unixFile*)id)->offset = offset;
00985   return SQLITE_OK;
00986 }
00987 
00988 #ifdef SQLITE_TEST
00989 /*
00990 ** Count the number of fullsyncs and normal syncs.  This is used to test
00991 ** that syncs and fullsyncs are occuring at the right times.
00992 */
00993 int sqlite3_sync_count = 0;
00994 int sqlite3_fullsync_count = 0;
00995 #endif
00996 
00997 /*
00998 ** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
00999 ** Otherwise use fsync() in its place.
01000 */
01001 #ifndef HAVE_FDATASYNC
01002 #  define fdatasync fsync
01003 #endif
01004 
01005 /*
01006 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
01007 ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
01008 ** only available on Mac OS X.  But that could change.
01009 */
01010 #ifdef F_FULLFSYNC
01011 #  define HAVE_FULLFSYNC 1
01012 #else
01013 #  define HAVE_FULLFSYNC 0
01014 #endif
01015 
01016 
01017 /*
01018 ** The fsync() system call does not work as advertised on many
01019 ** unix systems.  The following procedure is an attempt to make
01020 ** it work better.
01021 **
01022 ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
01023 ** for testing when we want to run through the test suite quickly.
01024 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
01025 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
01026 ** or power failure will likely corrupt the database file.
01027 */
01028 static int full_fsync(int fd, int fullSync, int dataOnly){
01029   int rc;
01030 
01031   /* Record the number of times that we do a normal fsync() and 
01032   ** FULLSYNC.  This is used during testing to verify that this procedure
01033   ** gets called with the correct arguments.
01034   */
01035 #ifdef SQLITE_TEST
01036   if( fullSync ) sqlite3_fullsync_count++;
01037   sqlite3_sync_count++;
01038 #endif
01039 
01040   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
01041   ** no-op
01042   */
01043 #ifdef SQLITE_NO_SYNC
01044   rc = SQLITE_OK;
01045 #else
01046 
01047 #if HAVE_FULLFSYNC
01048   if( fullSync ){
01049     rc = fcntl(fd, F_FULLFSYNC, 0);
01050   }else{
01051     rc = 1;
01052   }
01053   /* If the FULLSYNC failed, try to do a normal fsync() */
01054   if( rc ) rc = fsync(fd);
01055 
01056 #else /* if !defined(F_FULLSYNC) */
01057   if( dataOnly ){
01058     rc = fdatasync(fd);
01059   }else{
01060     rc = fsync(fd);
01061   }
01062 #endif /* defined(F_FULLFSYNC) */
01063 #endif /* defined(SQLITE_NO_SYNC) */
01064 
01065   return rc;
01066 }
01067 
01068 /*
01069 ** Make sure all writes to a particular file are committed to disk.
01070 **
01071 ** If dataOnly==0 then both the file itself and its metadata (file
01072 ** size, access time, etc) are synced.  If dataOnly!=0 then only the
01073 ** file data is synced.
01074 **
01075 ** Under Unix, also make sure that the directory entry for the file
01076 ** has been created by fsync-ing the directory that contains the file.
01077 ** If we do not do this and we encounter a power failure, the directory
01078 ** entry for the journal might not exist after we reboot.  The next
01079 ** SQLite to access the file will not know that the journal exists (because
01080 ** the directory entry for the journal was never created) and the transaction
01081 ** will not roll back - possibly leading to database corruption.
01082 */
01083 static int unixSync(OsFile *id, int dataOnly){
01084   unixFile *pFile = (unixFile*)id;
01085   assert( pFile );
01086   SimulateIOError(SQLITE_IOERR);
01087   TRACE2("SYNC    %-3d\n", pFile->h);
01088   if( full_fsync(pFile->h, pFile->fullSync, dataOnly) ){
01089     return SQLITE_IOERR;
01090   }
01091   if( pFile->dirfd>=0 ){
01092     TRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
01093             HAVE_FULLFSYNC, pFile->fullSync);
01094 #ifndef SQLITE_DISABLE_DIRSYNC
01095     /* The directory sync is only attempted if full_fsync is
01096     ** turned off or unavailable.  If a full_fsync occurred above,
01097     ** then the directory sync is superfluous.
01098     */
01099     if( (!HAVE_FULLFSYNC || !pFile->fullSync) && full_fsync(pFile->dirfd,0,0) ){
01100        /*
01101        ** We have received multiple reports of fsync() returning
01102        ** errors when applied to directories on certain file systems.
01103        ** A failed directory sync is not a big deal.  So it seems
01104        ** better to ignore the error.  Ticket #1657
01105        */
01106        /* return SQLITE_IOERR; */
01107     }
01108 #endif
01109     close(pFile->dirfd);  /* Only need to sync once, so close the directory */
01110     pFile->dirfd = -1;    /* when we are done. */
01111   }
01112   return SQLITE_OK;
01113 }
01114 
01115 /*
01116 ** Sync the directory zDirname. This is a no-op on operating systems other
01117 ** than UNIX.
01118 **
01119 ** This is used to make sure the master journal file has truely been deleted
01120 ** before making changes to individual journals on a multi-database commit.
01121 ** The F_FULLFSYNC option is not needed here.
01122 */
01123 int sqlite3BeSyncDirectory(const char *zDirname){
01124 #ifdef SQLITE_DISABLE_DIRSYNC
01125   return SQLITE_OK;
01126 #else
01127   int fd;
01128   int r;
01129   SimulateIOError(SQLITE_IOERR);
01130   fd = open(zDirname, O_RDONLY|O_BINARY, 0);
01131   TRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname);
01132   if( fd<0 ){
01133     return SQLITE_CANTOPEN; 
01134   }
01135   r = fsync(fd);
01136   close(fd);
01137   return ((r==0)?SQLITE_OK:SQLITE_IOERR);
01138 #endif
01139 }
01140 
01141 /*
01142 ** Truncate an open file to a specified size
01143 */
01144 static int unixTruncate(OsFile *id, i64 nByte){
01145   assert( id );
01146   SimulateIOError(SQLITE_IOERR);
01147   return ftruncate(((unixFile*)id)->h, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
01148 }
01149 
01150 /*
01151 ** Determine the current size of a file in bytes
01152 */
01153 static int unixFileSize(OsFile *id, i64 *pSize){
01154   struct stat buf;
01155   assert( id );
01156   SimulateIOError(SQLITE_IOERR);
01157   if( fstat(((unixFile*)id)->h, &buf)!=0 ){
01158     return SQLITE_IOERR;
01159   }
01160   *pSize = buf.st_size;
01161   return SQLITE_OK;
01162 }
01163 
01164 /*
01165 ** This routine checks if there is a RESERVED lock held on the specified
01166 ** file by this or any other process. If such a lock is held, return
01167 ** non-zero.  If the file is unlocked or holds only SHARED locks, then
01168 ** return zero.
01169 */
01170 static int unixCheckReservedLock(OsFile *id){
01171   int r = 0;
01172   unixFile *pFile = (unixFile*)id;
01173 
01174   assert( pFile );
01175   sqlite3OsEnterMutex(); /* Because pFile->pLock is shared across threads */
01176 
01177   /* Check if a thread in this process holds such a lock */
01178   if( pFile->pLock->locktype>SHARED_LOCK ){
01179     r = 1;
01180   }
01181 
01182   /* Otherwise see if some other process holds it.
01183   */
01184   if( !r ){
01185     struct flock lock;
01186     lock.l_whence = SEEK_SET;
01187     lock.l_start = RESERVED_BYTE;
01188     lock.l_len = 1;
01189     lock.l_type = F_WRLCK;
01190     fcntl(pFile->h, F_GETLK, &lock);
01191     if( lock.l_type!=F_UNLCK ){
01192       r = 1;
01193     }
01194   }
01195   
01196   sqlite3OsLeaveMutex();
01197   TRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
01198 
01199   return r;
01200 }
01201 
01202 /*
01203 ** Lock the file with the lock specified by parameter locktype - one
01204 ** of the following:
01205 **
01206 **     (1) SHARED_LOCK
01207 **     (2) RESERVED_LOCK
01208 **     (3) PENDING_LOCK
01209 **     (4) EXCLUSIVE_LOCK
01210 **
01211 ** Sometimes when requesting one lock state, additional lock states
01212 ** are inserted in between.  The locking might fail on one of the later
01213 ** transitions leaving the lock state different from what it started but
01214 ** still short of its goal.  The following chart shows the allowed
01215 ** transitions and the inserted intermediate states:
01216 **
01217 **    UNLOCKED -> SHARED
01218 **    SHARED -> RESERVED
01219 **    SHARED -> (PENDING) -> EXCLUSIVE
01220 **    RESERVED -> (PENDING) -> EXCLUSIVE
01221 **    PENDING -> EXCLUSIVE
01222 **
01223 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
01224 ** routine to lower a locking level.
01225 */
01226 static int unixLock(OsFile *id, int locktype){
01227   /* The following describes the implementation of the various locks and
01228   ** lock transitions in terms of the POSIX advisory shared and exclusive
01229   ** lock primitives (called read-locks and write-locks below, to avoid
01230   ** confusion with SQLite lock names). The algorithms are complicated
01231   ** slightly in order to be compatible with windows systems simultaneously
01232   ** accessing the same database file, in case that is ever required.
01233   **
01234   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
01235   ** byte', each single bytes at well known offsets, and the 'shared byte
01236   ** range', a range of 510 bytes at a well known offset.
01237   **
01238   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
01239   ** byte'.  If this is successful, a random byte from the 'shared byte
01240   ** range' is read-locked and the lock on the 'pending byte' released.
01241   **
01242   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
01243   ** A RESERVED lock is implemented by grabbing a write-lock on the
01244   ** 'reserved byte'. 
01245   **
01246   ** A process may only obtain a PENDING lock after it has obtained a
01247   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
01248   ** on the 'pending byte'. This ensures that no new SHARED locks can be
01249   ** obtained, but existing SHARED locks are allowed to persist. A process
01250   ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
01251   ** This property is used by the algorithm for rolling back a journal file
01252   ** after a crash.
01253   **
01254   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
01255   ** implemented by obtaining a write-lock on the entire 'shared byte
01256   ** range'. Since all other locks require a read-lock on one of the bytes
01257   ** within this range, this ensures that no other locks are held on the
01258   ** database. 
01259   **
01260   ** The reason a single byte cannot be used instead of the 'shared byte
01261   ** range' is that some versions of windows do not support read-locks. By
01262   ** locking a random byte from a range, concurrent SHARED locks may exist
01263   ** even if the locking primitive used is always a write-lock.
01264   */
01265   int rc = SQLITE_OK;
01266   unixFile *pFile = (unixFile*)id;
01267   struct lockInfo *pLock = pFile->pLock;
01268   struct flock lock;
01269   int s;
01270 
01271   assert( pFile );
01272   TRACE7("LOCK    %d %s was %s(%s,%d) pid=%d\n", pFile->h,
01273       locktypeName(locktype), locktypeName(pFile->locktype),
01274       locktypeName(pLock->locktype), pLock->cnt , getpid());
01275 
01276   /* If there is already a lock of this type or more restrictive on the
01277   ** OsFile, do nothing. Don't use the end_lock: exit path, as
01278   ** sqlite3OsEnterMutex() hasn't been called yet.
01279   */
01280   if( pFile->locktype>=locktype ){
01281     TRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
01282             locktypeName(locktype));
01283     return SQLITE_OK;
01284   }
01285 
01286   /* Make sure the locking sequence is correct
01287   */
01288   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
01289   assert( locktype!=PENDING_LOCK );
01290   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
01291 
01292   /* This mutex is needed because pFile->pLock is shared across threads
01293   */
01294   sqlite3OsEnterMutex();
01295 
01296   /* Make sure the current thread owns the pFile.
01297   */
01298   rc = transferOwnership(pFile);
01299   if( rc!=SQLITE_OK ){
01300     sqlite3OsLeaveMutex();
01301     return rc;
01302   }
01303   pLock = pFile->pLock;
01304 
01305   /* If some thread using this PID has a lock via a different OsFile*
01306   ** handle that precludes the requested lock, return BUSY.
01307   */
01308   if( (pFile->locktype!=pLock->locktype && 
01309           (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
01310   ){
01311     rc = SQLITE_BUSY;
01312     goto end_lock;
01313   }
01314 
01315   /* If a SHARED lock is requested, and some thread using this PID already
01316   ** has a SHARED or RESERVED lock, then increment reference counts and
01317   ** return SQLITE_OK.
01318   */
01319   if( locktype==SHARED_LOCK && 
01320       (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
01321     assert( locktype==SHARED_LOCK );
01322     assert( pFile->locktype==0 );
01323     assert( pLock->cnt>0 );
01324     pFile->locktype = SHARED_LOCK;
01325     pLock->cnt++;
01326     pFile->pOpen->nLock++;
01327     goto end_lock;
01328   }
01329 
01330   lock.l_len = 1L;
01331 
01332   lock.l_whence = SEEK_SET;
01333 
01334   /* A PENDING lock is needed before acquiring a SHARED lock and before
01335   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
01336   ** be released.
01337   */
01338   if( locktype==SHARED_LOCK 
01339       || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
01340   ){
01341     lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
01342     lock.l_start = PENDING_BYTE;
01343     s = fcntl(pFile->h, F_SETLK, &lock);
01344     if( s ){
01345       rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
01346       goto end_lock;
01347     }
01348   }
01349 
01350 
01351   /* If control gets to this point, then actually go ahead and make
01352   ** operating system calls for the specified lock.
01353   */
01354   if( locktype==SHARED_LOCK ){
01355     assert( pLock->cnt==0 );
01356     assert( pLock->locktype==0 );
01357 
01358     /* Now get the read-lock */
01359     lock.l_start = SHARED_FIRST;
01360     lock.l_len = SHARED_SIZE;
01361     s = fcntl(pFile->h, F_SETLK, &lock);
01362 
01363     /* Drop the temporary PENDING lock */
01364     lock.l_start = PENDING_BYTE;
01365     lock.l_len = 1L;
01366     lock.l_type = F_UNLCK;
01367     if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
01368       rc = SQLITE_IOERR;  /* This should never happen */
01369       goto end_lock;
01370     }
01371     if( s ){
01372       rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
01373     }else{
01374       pFile->locktype = SHARED_LOCK;
01375       pFile->pOpen->nLock++;
01376       pLock->cnt = 1;
01377     }
01378   }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
01379     /* We are trying for an exclusive lock but another thread in this
01380     ** same process is still holding a shared lock. */
01381     rc = SQLITE_BUSY;
01382   }else{
01383     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
01384     ** assumed that there is a SHARED or greater lock on the file
01385     ** already.
01386     */
01387     assert( 0!=pFile->locktype );
01388     lock.l_type = F_WRLCK;
01389     switch( locktype ){
01390       case RESERVED_LOCK:
01391         lock.l_start = RESERVED_BYTE;
01392         break;
01393       case EXCLUSIVE_LOCK:
01394         lock.l_start = SHARED_FIRST;
01395         lock.l_len = SHARED_SIZE;
01396         break;
01397       default:
01398         assert(0);
01399     }
01400     s = fcntl(pFile->h, F_SETLK, &lock);
01401     if( s ){
01402       rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
01403     }
01404   }
01405   
01406   if( rc==SQLITE_OK ){
01407     pFile->locktype = locktype;
01408     pLock->locktype = locktype;
01409   }else if( locktype==EXCLUSIVE_LOCK ){
01410     pFile->locktype = PENDING_LOCK;
01411     pLock->locktype = PENDING_LOCK;
01412   }
01413 
01414 end_lock:
01415   sqlite3OsLeaveMutex();
01416   TRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
01417       rc==SQLITE_OK ? "ok" : "failed");
01418   return rc;
01419 }
01420 
01421 /*
01422 ** Lower the locking level on file descriptor pFile to locktype.  locktype
01423 ** must be either NO_LOCK or SHARED_LOCK.
01424 **
01425 ** If the locking level of the file descriptor is already at or below
01426 ** the requested locking level, this routine is a no-op.
01427 */
01428 static int unixUnlock(OsFile *id, int locktype){
01429   struct lockInfo *pLock;
01430   struct flock lock;
01431   int rc = SQLITE_OK;
01432   unixFile *pFile = (unixFile*)id;
01433 
01434   assert( pFile );
01435   TRACE7("UNLOCK  %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
01436       pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
01437 
01438   assert( locktype<=SHARED_LOCK );
01439   if( pFile->locktype<=locktype ){
01440     return SQLITE_OK;
01441   }
01442   if( CHECK_THREADID(pFile) ){
01443     return SQLITE_MISUSE;
01444   }
01445   sqlite3OsEnterMutex();
01446   pLock = pFile->pLock;
01447   assert( pLock->cnt!=0 );
01448   if( pFile->locktype>SHARED_LOCK ){
01449     assert( pLock->locktype==pFile->locktype );
01450     if( locktype==SHARED_LOCK ){
01451       lock.l_type = F_RDLCK;
01452       lock.l_whence = SEEK_SET;
01453       lock.l_start = SHARED_FIRST;
01454       lock.l_len = SHARED_SIZE;
01455       if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
01456         /* This should never happen */
01457         rc = SQLITE_IOERR;
01458       }
01459     }
01460     lock.l_type = F_UNLCK;
01461     lock.l_whence = SEEK_SET;
01462     lock.l_start = PENDING_BYTE;
01463     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
01464     if( fcntl(pFile->h, F_SETLK, &lock)==0 ){
01465       pLock->locktype = SHARED_LOCK;
01466     }else{
01467       rc = SQLITE_IOERR;  /* This should never happen */
01468     }
01469   }
01470   if( locktype==NO_LOCK ){
01471     struct openCnt *pOpen;
01472 
01473     /* Decrement the shared lock counter.  Release the lock using an
01474     ** OS call only when all threads in this same process have released
01475     ** the lock.
01476     */
01477     pLock->cnt--;
01478     if( pLock->cnt==0 ){
01479       lock.l_type = F_UNLCK;
01480       lock.l_whence = SEEK_SET;
01481       lock.l_start = lock.l_len = 0L;
01482       if( fcntl(pFile->h, F_SETLK, &lock)==0 ){
01483         pLock->locktype = NO_LOCK;
01484       }else{
01485         rc = SQLITE_IOERR;  /* This should never happen */
01486       }
01487     }
01488 
01489     /* Decrement the count of locks against this same file.  When the
01490     ** count reaches zero, close any other file descriptors whose close
01491     ** was deferred because of outstanding locks.
01492     */
01493     pOpen = pFile->pOpen;
01494     pOpen->nLock--;
01495     assert( pOpen->nLock>=0 );
01496     if( pOpen->nLock==0 && pOpen->nPending>0 ){
01497       int i;
01498       for(i=0; i<pOpen->nPending; i++){
01499         close(pOpen->aPending[i]);
01500       }
01501       free(pOpen->aPending);
01502       pOpen->nPending = 0;
01503       pOpen->aPending = 0;
01504     }
01505   }
01506   sqlite3OsLeaveMutex();
01507   pFile->locktype = locktype;
01508   return rc;
01509 }
01510 
01511 /*
01512 ** Close a file.
01513 */
01514 static int unixClose(OsFile **pId){
01515   unixFile *id = (unixFile*)*pId;
01516 
01517   if( !id ) return SQLITE_OK;
01518   unixUnlock(*pId, NO_LOCK);
01519   if( id->dirfd>=0 ) close(id->dirfd);
01520   id->dirfd = -1;
01521   sqlite3OsEnterMutex();
01522 
01523   if( id->pOpen->nLock ){
01524     /* If there are outstanding locks, do not actually close the file just
01525     ** yet because that would clear those locks.  Instead, add the file
01526     ** descriptor to pOpen->aPending.  It will be automatically closed when
01527     ** the last lock is cleared.
01528     */
01529     int *aNew;
01530     struct openCnt *pOpen = id->pOpen;
01531     aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
01532     if( aNew==0 ){
01533       /* If a malloc fails, just leak the file descriptor */
01534     }else{
01535       pOpen->aPending = aNew;
01536       pOpen->aPending[pOpen->nPending] = id->h;
01537       pOpen->nPending++;
01538     }
01539   }else{
01540     /* There are no outstanding locks so we can close the file immediately */
01541     close(id->h);
01542   }
01543   releaseLockInfo(id->pLock);
01544   releaseOpenCnt(id->pOpen);
01545 
01546   sqlite3OsLeaveMutex();
01547   id->isOpen = 0;
01548   TRACE2("CLOSE   %-3d\n", id->h);
01549   OpenCounter(-1);
01550   sqlite3ThreadSafeFree(id);
01551   *pId = 0;
01552   return SQLITE_OK;
01553 }
01554 
01555 /*
01556 ** Turn a relative pathname into a full pathname.  Return a pointer
01557 ** to the full pathname stored in space obtained from sqliteMalloc().
01558 ** The calling function is responsible for freeing this space once it
01559 ** is no longer needed.
01560 */
01561 char *sqlite3BeFullPathname(const char *zRelative){
01562   char *zFull = 0;
01563   if( zRelative[0]=='/' ){
01564     sqlite3SetString(&zFull, zRelative, (char*)0);
01565   }else{
01566     char *zBuf = sqliteMalloc(5000);
01567     if( zBuf==0 ){
01568       return 0;
01569     }
01570     zBuf[0] = 0;
01571     sqlite3SetString(&zFull, getcwd(zBuf, 5000), "/", zRelative,
01572                     (char*)0);
01573     sqliteFree(zBuf);
01574   }
01575 
01576 #if 0
01577   /*
01578   ** Remove "/./" path elements and convert "/A/./" path elements
01579   ** to just "/".
01580   */
01581   if( zFull ){
01582     int i, j;
01583     for(i=j=0; zFull[i]; i++){
01584       if( zFull[i]=='/' ){
01585         if( zFull[i+1]=='/' ) continue;
01586         if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
01587           i += 1;
01588           continue;
01589         }
01590         if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
01591           while( j>0 && zFull[j-1]!='/' ){ j--; }
01592           i += 3;
01593           continue;
01594         }
01595       }
01596       zFull[j++] = zFull[i];
01597     }
01598     zFull[j] = 0;
01599   }
01600 #endif
01601 
01602   return zFull;
01603 }
01604 
01605 /*
01606 ** Change the value of the fullsync flag in the given file descriptor.
01607 */
01608 static void unixSetFullSync(OsFile *id, int v){
01609   ((unixFile*)id)->fullSync = v;
01610 }
01611 
01612 /*
01613 ** Return the underlying file handle for an OsFile
01614 */
01615 static int unixFileHandle(OsFile *id){
01616   return ((unixFile*)id)->h;
01617 }
01618 
01619 /*
01620 ** Return an integer that indices the type of lock currently held
01621 ** by this handle.  (Used for testing and analysis only.)
01622 */
01623 static int unixLockState(OsFile *id){
01624   return ((unixFile*)id)->locktype;
01625 }
01626 
01627 /*
01628 ** This vector defines all the methods that can operate on an OsFile
01629 ** for unix.
01630 */
01631 static const IoMethod sqlite3BeIoMethod = {
01632   unixClose,
01633   unixOpenDirectory,
01634   unixRead,
01635   unixWrite,
01636   unixSeek,
01637   unixTruncate,
01638   unixSync,
01639   unixSetFullSync,
01640   unixFileHandle,
01641   unixFileSize,
01642   unixLock,
01643   unixUnlock,
01644   unixLockState,
01645   unixCheckReservedLock,
01646 };
01647 
01648 /*
01649 ** Allocate memory for a unixFile.  Initialize the new unixFile
01650 ** to the value given in pInit and return a pointer to the new
01651 ** OsFile.  If we run out of memory, close the file and return NULL.
01652 */
01653 static int allocateUnixFile(unixFile *pInit, OsFile **pId){
01654   unixFile *pNew;
01655   pInit->dirfd = -1;
01656   pInit->fullSync = 0;
01657   pInit->locktype = 0;
01658   pInit->offset = 0;
01659   SET_THREADID(pInit);
01660   pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );
01661   if( pNew==0 ){
01662     close(pInit->h);
01663     sqlite3OsEnterMutex();
01664     releaseLockInfo(pInit->pLock);
01665     releaseOpenCnt(pInit->pOpen);
01666     sqlite3OsLeaveMutex();
01667     *pId = 0;
01668     return SQLITE_NOMEM;
01669   }else{
01670     *pNew = *pInit;
01671     pNew->pMethod = &sqlite3BeIoMethod;
01672     *pId = (OsFile*)pNew;
01673     OpenCounter(+1);
01674     return SQLITE_OK;
01675   }
01676 }
01677 
01678 
01679 #endif /* SQLITE_OMIT_DISKIO */
01680 /***************************************************************************
01681 ** Everything above deals with file I/O.  Everything that follows deals
01682 ** with other miscellanous aspects of the operating system interface
01683 ****************************************************************************/
01684 
01685 
01686 /*
01687 ** Get information to seed the random number generator.  The seed
01688 ** is written into the buffer zBuf[256].  The calling function must
01689 ** supply a sufficiently large buffer.
01690 */
01691 int sqlite3BeRandomSeed(char *zBuf){
01692   /* We have to initialize zBuf to prevent valgrind from reporting
01693   ** errors.  The reports issued by valgrind are incorrect - we would
01694   ** prefer that the randomness be increased by making use of the
01695   ** uninitialized space in zBuf - but valgrind errors tend to worry
01696   ** some users.  Rather than argue, it seems easier just to initialize
01697   ** the whole array and silence valgrind, even if that means less randomness
01698   ** in the random seed.
01699   **
01700   ** When testing, initializing zBuf[] to zero is all we do.  That means
01701   ** that we always use the same random number sequence.  This makes the
01702   ** tests repeatable.
01703   */
01704   memset(zBuf, 0, 256);
01705 #if !defined(SQLITE_TEST)
01706   {
01707     int pid, fd;
01708     fd = open("/dev/urandom", O_RDONLY);
01709     if( fd<0 ){
01710       time_t t;
01711       time(&t);
01712       memcpy(zBuf, &t, sizeof(t));
01713       pid = getpid();
01714       memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid));
01715     }else{
01716       read(fd, zBuf, 256);
01717       close(fd);
01718     }
01719   }
01720 #endif
01721   return SQLITE_OK;
01722 }
01723 
01724 /*
01725 ** Sleep for a little while.  Return the amount of time slept.
01726 ** The argument is the number of milliseconds we want to sleep.
01727 */
01728 int sqlite3BeSleep(int ms){
01729 #if defined(HAVE_USLEEP) && HAVE_USLEEP
01730   usleep(ms*1000);
01731   return ms;
01732 #else
01733   sleep((ms+999)/1000);
01734   return 1000*((ms+999)/1000);
01735 #endif
01736 }
01737 
01738 /*
01739 ** Static variables used for thread synchronization.
01740 **
01741 ** inMutex      the nesting depth of the recursive mutex.  The thread
01742 **              holding mutexMain can read this variable at any time.
01743 **              But is must hold mutexAux to change this variable.  Other
01744 **              threads must hold mutexAux to read the variable and can
01745 **              never write.
01746 **
01747 ** mutexOwner   The thread id of the thread holding mutexMain.  Same
01748 **              access rules as for inMutex.
01749 **
01750 ** mutexOwnerValid   True if the value in mutexOwner is valid.  The same
01751 **                   access rules apply as for inMutex.
01752 **
01753 ** mutexMain    The main mutex.  Hold this mutex in order to get exclusive
01754 **              access to SQLite data structures.
01755 **
01756 ** mutexAux     An auxiliary mutex needed to access variables defined above.
01757 **
01758 ** Mutexes are always acquired in this order: mutexMain mutexAux.   It
01759 ** is not necessary to acquire mutexMain in order to get mutexAux - just
01760 ** do not attempt to acquire them in the reverse order: mutexAux mutexMain.
01761 ** Either get the mutexes with mutexMain first or get mutexAux only.
01762 **
01763 ** When running on a platform where the three variables inMutex, mutexOwner,
01764 ** and mutexOwnerValid can be set atomically, the mutexAux is not required.
01765 ** On many systems, all three are 32-bit integers and writing to a 32-bit
01766 ** integer is atomic.  I think.  But there are no guarantees.  So it seems
01767 ** safer to protect them using mutexAux.
01768 */
01769 static int inMutex = 0;
01770 #ifdef SQLITE_BEOS_THREADS
01771 static thread_id mutexOwner;          /* Thread holding mutexMain */
01772 static int mutexOwnerValid = 0;       /* True if mutexOwner is valid */
01773 static sem_id mutexMain = -1; /* The mutex */
01774 static sem_id mutexAux = -1;  /* Aux mutex */
01775 #endif
01776 
01777 /*
01778 ** The following pair of routine implement mutual exclusion for
01779 ** multi-threaded processes.  Only a single thread is allowed to
01780 ** executed code that is surrounded by EnterMutex() and LeaveMutex().
01781 **
01782 ** SQLite uses only a single Mutex.  There is not much critical
01783 ** code and what little there is executes quickly and without blocking.
01784 **
01785 ** As of version 3.3.2, this mutex must be recursive.
01786 */
01787 void sqlite3BeEnterMutex(){
01788 #ifdef SQLITE_BEOS_THREADS
01789   if (mutexAux == -1 || mutexMain == -1)
01790   {
01791        mutexAux = create_sem(1, "mutexAux");
01792        mutexMain = create_sem(1, "mutexMain");
01793   }
01794   acquire_sem(mutexAux);
01795   if( !mutexOwnerValid || (mutexOwner != find_thread(NULL))) {
01796     release_sem(mutexAux);
01797     acquire_sem(mutexMain);
01798     assert( inMutex==0 );
01799     assert( !mutexOwnerValid );
01800     acquire_sem(mutexAux);
01801     mutexOwner = find_thread(NULL);
01802     mutexOwnerValid = 1;
01803   }
01804   inMutex++;
01805   release_sem(mutexAux);
01806 #else
01807   inMutex++;
01808 #endif
01809 }
01810 void sqlite3BeLeaveMutex(){
01811   assert( inMutex>0 );
01812 #ifdef SQLITE_BEOS_THREADS
01813   acquire_sem(mutexAux);
01814   inMutex--;
01815   assert( mutexOwner == find_thread(NULL) );
01816   if( inMutex==0 ){
01817     assert( mutexOwnerValid );
01818     mutexOwnerValid = 0;
01819     release_sem(mutexMain);
01820   }
01821   release_sem(mutexAux);
01822 #else
01823   inMutex--;
01824 #endif
01825 }
01826 
01827 /*
01828 ** Return TRUE if the mutex is currently held.
01829 **
01830 ** If the thisThrd parameter is true, return true only if the
01831 ** calling thread holds the mutex.  If the parameter is false, return
01832 ** true if any thread holds the mutex.
01833 */
01834 int sqlite3BeInMutex(int thisThrd){
01835 #ifdef SQLITE_BEOS_THREADS
01836   int rc;
01837   acquire_sem(mutexAux);
01838   rc = inMutex>0 && (thisThrd==0 || (mutexOwner == find_thread(NULL)));
01839   release_sem(mutexAux);
01840   return rc;
01841 #else
01842   return inMutex>0;
01843 #endif
01844 }
01845 
01846 /*
01847 ** Remember the number of thread-specific-data blocks allocated.
01848 ** Use this to verify that we are not leaking thread-specific-data.
01849 ** Ticket #1601
01850 */
01851 #ifdef SQLITE_TEST
01852 int sqlite3_tsd_count = 0;
01853 # ifdef SQLITE_BEOS_THREADS
01854     static sem_id tsd_counter_mutex = create_sem(1, "tsd_counter");
01855 #   define TSD_COUNTER(N) \
01856              acquire_sem(tsd_counter_mutex); \
01857              sqlite3_tsd_count += N; \
01858              release_sem(tsd_counter_mutex);
01859 # else
01860 #   define TSD_COUNTER(N)  sqlite3_tsd_count += N
01861 # endif
01862 #else
01863 # define TSD_COUNTER(N)  /* no-op */
01864 #endif
01865 
01866 /*
01867 ** If called with allocateFlag>0, then return a pointer to thread
01868 ** specific data for the current thread.  Allocate and zero the
01869 ** thread-specific data if it does not already exist.
01870 **
01871 ** If called with allocateFlag==0, then check the current thread
01872 ** specific data.  Return it if it exists.  If it does not exist,
01873 ** then return NULL.
01874 **
01875 ** If called with allocateFlag<0, check to see if the thread specific
01876 ** data is allocated and is all zero.  If it is then deallocate it.
01877 ** Return a pointer to the thread specific data or NULL if it is
01878 ** unallocated or gets deallocated.
01879 */
01880 ThreadData *sqlite3BeThreadSpecificData(int allocateFlag){
01881   static const ThreadData zeroData = {0};  /* Initializer to silence warnings
01882                                            ** from broken compilers */
01883 #ifdef SQLITE_BEOS_THREADS
01884   static int32 key;
01885   static int keyInit = 0;
01886   ThreadData *pTsd;
01887 
01888   if( !keyInit ){
01889     sqlite3OsEnterMutex();
01890     if( !keyInit ){
01891       key = tls_allocate();
01892       if( key < 0 ){
01893         sqlite3OsLeaveMutex();
01894         return 0;
01895       }
01896       keyInit = 1;
01897     }
01898     sqlite3OsLeaveMutex();
01899   }
01900 
01901   pTsd = tls_get(key);
01902   if( allocateFlag>0 ){
01903     if( pTsd==0 ){
01904       if( !sqlite3TestMallocFail() ){
01905         pTsd = sqlite3OsMalloc(sizeof(zeroData));
01906       }
01907 #ifdef SQLITE_MEMDEBUG
01908       sqlite3_isFail = 0;
01909 #endif
01910       if( pTsd ){
01911         *pTsd = zeroData;
01912         tls_set(key, pTsd);
01913         TSD_COUNTER(+1);
01914       }
01915     }
01916   }else if( pTsd!=0 && allocateFlag<0 
01917             && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
01918     sqlite3OsFree(pTsd);
01919     tls_set(key, 0);
01920     TSD_COUNTER(-1);
01921     pTsd = 0;
01922   }
01923   return pTsd;
01924 #else
01925   static ThreadData *pTsd = 0;
01926   if( allocateFlag>0 ){
01927     if( pTsd==0 ){
01928       if( !sqlite3TestMallocFail() ){
01929         pTsd = sqlite3OsMalloc( sizeof(zeroData) );
01930       }
01931 #ifdef SQLITE_MEMDEBUG
01932       sqlite3_isFail = 0;
01933 #endif
01934       if( pTsd ){
01935         *pTsd = zeroData;
01936         TSD_COUNTER(+1);
01937       }
01938     }
01939   }else if( pTsd!=0 && allocateFlag<0
01940             && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
01941     sqlite3OsFree(pTsd);
01942     TSD_COUNTER(-1);
01943     pTsd = 0;
01944   }
01945   return pTsd;
01946 #endif
01947 }
01948 
01949 /*
01950 ** The following variable, if set to a non-zero value, becomes the result
01951 ** returned from sqlite3OsCurrentTime().  This is used for testing.
01952 */
01953 #ifdef SQLITE_TEST
01954 int sqlite3_current_time = 0;
01955 #endif
01956 
01957 /*
01958 ** Find the current time (in Universal Coordinated Time).  Write the
01959 ** current time and date as a Julian Day number into *prNow and
01960 ** return 0.  Return 1 if the time and date cannot be found.
01961 */
01962 int sqlite3BeCurrentTime(double *prNow){
01963 #ifdef NO_GETTOD
01964   time_t t;
01965   time(&t);
01966   *prNow = t/86400.0 + 2440587.5;
01967 #else
01968   struct timeval sNow;
01969   struct timezone sTz;  /* Not used */
01970   gettimeofday(&sNow, &sTz);
01971   *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
01972 #endif
01973 #ifdef SQLITE_TEST
01974   if( sqlite3_current_time ){
01975     *prNow = sqlite3_current_time/86400.0 + 2440587.5;
01976   }
01977 #endif
01978   return 0;
01979 }
01980 
01981 #endif /* OS_BEOS */