/ Check-in [2170e803]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Clean up comments in os_unix.c. (CVS 2950)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2170e803ad48cffa6dddf8b591e0c085a7e66c86
User & Date: drh 2006-01-15 17:27:18
Context
2006-01-15
18:29
Prepare for the 3.3.1 alpha release. (CVS 2951) check-in: 3e32bcf0 user: drh tags: trunk
17:27
Clean up comments in os_unix.c. (CVS 2950) check-in: 2170e803 user: drh tags: trunk
14:11
Correctly set the length of the string in bytes when transforming an OP_String8 to OP_String in a utf-16 vdbe program. (CVS 2949) check-in: 69f996e0 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/os_unix.c.

    15     15   #include "sqliteInt.h"
    16     16   #include "os.h"
    17     17   #if OS_UNIX              /* This file is used on unix only */
    18     18   
    19     19   /*
    20     20   ** These #defines should enable >2GB file support on Posix if the
    21     21   ** underlying operating system supports it.  If the OS lacks
    22         -** large file support, or if the OS is windows, these should be no-ops.
           22  +** large file support, these should be no-ops.
    23     23   **
    24     24   ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
    25     25   ** on the compiler command line.  This is necessary if you are compiling
    26     26   ** on a recent machine (ex: RedHat 7.2) but you want your code to work
    27     27   ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
    28     28   ** without this option, LFS is enable.  But LFS does not exist in the kernel
    29     29   ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
    30     30   ** portability you should omit LFS.
    31         -**
    32         -** Similar is true for MacOS.  LFS is only supported on MacOS 9 and later.
    33     31   */
    34     32   #ifndef SQLITE_DISABLE_LFS
    35     33   # define _LARGE_FILE       1
    36     34   # ifndef _FILE_OFFSET_BITS
    37     35   #   define _FILE_OFFSET_BITS 64
    38     36   # endif
    39     37   # define _LARGEFILE_SOURCE 1
................................................................................
    47     45   #include <fcntl.h>
    48     46   #include <unistd.h>
    49     47   #include <time.h>
    50     48   #include <sys/time.h>
    51     49   #include <errno.h>
    52     50   
    53     51   /*
    54         -** Macros used to determine whether or not to use threads.  The
    55         -** SQLITE_UNIX_THREADS macro is defined if we are synchronizing for
    56         -** Posix threads and SQLITE_W32_THREADS is defined if we are
    57         -** synchronizing using Win32 threads.
           52  +** If we are to be thread-safe, include the pthreads header and define
           53  +** the SQLITE_UNIX_THREADS macro.
    58     54   */
    59     55   #if defined(THREADSAFE) && THREADSAFE
    60     56   # include <pthread.h>
    61     57   # define SQLITE_UNIX_THREADS 1
    62     58   #endif
    63     59   
    64     60   /*
................................................................................
    81     77     struct lockInfo *pLock;   /* Info about locks on this inode */
    82     78     int h;                    /* The file descriptor */
    83     79     unsigned char locktype;   /* The type of lock held on this fd */
    84     80     unsigned char isOpen;     /* True if needs to be closed */
    85     81     unsigned char fullSync;   /* Use F_FULLSYNC if available */
    86     82     int dirfd;                /* File descriptor for the directory */
    87     83   #ifdef SQLITE_UNIX_THREADS
    88         -  pthread_t tid;            /* The thread authorized to use this OsFile */
           84  +  pthread_t tid;            /* The thread that "owns" this OsFile */
    89     85   #endif
    90     86   };
    91     87   
    92     88   /*
    93     89   ** Provide the ability to override some OS-layer functions during
    94     90   ** testing.  This is used to simulate OS crashes to verify that 
    95     91   ** commits are atomic even in the event of an OS crash.
................................................................................
   109    105   /*
   110    106   ** Include code that is common to all os_*.c files
   111    107   */
   112    108   #include "os_common.h"
   113    109   
   114    110   /*
   115    111   ** Do not include any of the File I/O interface procedures if the
   116         -** SQLITE_OMIT_DISKIO macro is defined (indicating that there database
          112  +** SQLITE_OMIT_DISKIO macro is defined (indicating that the database
   117    113   ** will be in-memory only)
   118    114   */
   119    115   #ifndef SQLITE_OMIT_DISKIO
   120    116   
   121    117   
   122    118   /*
   123    119   ** Define various macros that are missing from some systems.
................................................................................
   160    156   ** Set or check the OsFile.tid field.  This field is set when an OsFile
   161    157   ** is first opened.  All subsequent uses of the OsFile verify that the
   162    158   ** same thread is operating on the OsFile.  Some operating systems do
   163    159   ** not allow locks to be overridden by other threads and that restriction
   164    160   ** means that sqlite3* database handles cannot be moved from one thread
   165    161   ** to another.  This logic makes sure a user does not try to do that
   166    162   ** by mistake.
          163  +**
          164  +** Version 3.3.1 (2006-01-15):  OsFiles can be moved from one thread to
          165  +** another as long as we are running on a system that supports threads
          166  +** overriding each others locks (which now the most common behavior)
          167  +** or if no locks are held.  But the OsFile.pLock field needs to be
          168  +** recomputed because its key includes the thread-id.  See the 
          169  +** transferOwnership() function below for additional information
   167    170   */
   168    171   #if defined(SQLITE_UNIX_THREADS)
   169    172   # define SET_THREADID(X)   (X)->tid = pthread_self()
   170    173   # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
   171    174                               !pthread_equal((X)->tid, pthread_self()))
   172    175   #else
   173    176   # define SET_THREADID(X)
................................................................................
   276    279   
   277    280   /*
   278    281   ** An instance of the following structure serves as the key used
   279    282   ** to locate a particular lockInfo structure given its inode.
   280    283   **
   281    284   ** If threads cannot override each others locks, then we set the
   282    285   ** lockKey.tid field to the thread ID.  If threads can override
   283         -** each others locks then tid is always set to zero.  tid is also
   284         -** set to zero if we compile without threading support.
          286  +** each others locks then tid is always set to zero.  tid is omitted
          287  +** if we compile without threading support.
   285    288   */
   286    289   struct lockKey {
   287    290     dev_t dev;       /* Device number */
   288    291     ino_t ino;       /* Inode number */
   289    292   #ifdef SQLITE_UNIX_THREADS
   290    293     pthread_t tid;   /* Thread ID or zero if threads can override each other */
   291    294   #endif
................................................................................
   329    332     int nRef;             /* Number of pointers to this structure */
   330    333     int nLock;            /* Number of outstanding locks */
   331    334     int nPending;         /* Number of pending close() operations */
   332    335     int *aPending;        /* Malloced space holding fd's awaiting a close() */
   333    336   };
   334    337   
   335    338   /* 
   336         -** These hash table maps inodes and process IDs into lockInfo and openCnt
   337         -** structures.  Access to these hash tables must be protected by a mutex.
          339  +** These hash tables map inodes and file descriptors (really, lockKey and
          340  +** openKey structures) into lockInfo and openCnt structures.  Access to 
          341  +** these hash tables must be protected by a mutex.
   338    342   */
   339    343   static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
   340    344   static Hash openHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
   341    345   
   342    346   
   343    347   #ifdef SQLITE_UNIX_THREADS
   344    348   /*
   345    349   ** This variable records whether or not threads can override each others
   346    350   ** locks.
   347    351   **
   348    352   **    0:  No.  Threads cannot override each others locks.
   349    353   **    1:  Yes.  Threads can override each others locks.
   350    354   **   -1:  We don't know yet.
          355  +**
          356  +** This variable normally has file scope only.  But during testing, we make
          357  +** it a global so that the test code can change its value in order to verify
          358  +** that the right stuff happens in either case.
   351    359   */
   352    360   #ifdef SQLITE_TEST
   353    361   int threadsOverrideEachOthersLocks = -1;
   354    362   #else
   355    363   static int threadsOverrideEachOthersLocks = -1;
   356    364   #endif
   357    365   
................................................................................
   368    376   #ifdef SQLITE_LOCK_TRACE
   369    377   /*
   370    378   ** Print out information about all locking operations.
   371    379   **
   372    380   ** This routine is used for troubleshooting locks on multithreaded
   373    381   ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
   374    382   ** command-line option on the compiler.  This code is normally
   375         -** turnned off.
          383  +** turned off.
   376    384   */
   377    385   static int lockTrace(int fd, int op, struct flock *p){
   378    386     char *zOpName, *zType;
   379    387     int s;
   380    388     int savedErrno;
   381    389     if( op==F_GETLK ){
   382    390       zOpName = "GETLK";
................................................................................
   606    614   **
   607    615   ** A unixFile is only owned by a thread on systems where one thread is
   608    616   ** unable to override locks created by a different thread.  RedHat9 is
   609    617   ** an example of such a system.
   610    618   **
   611    619   ** Ownership transfer is only allowed if the unixFile is currently unlocked.
   612    620   ** If the unixFile is locked and an ownership is wrong, then return
   613         -** SQLITE_MISUSE.  Otherwise return SQLITE_OK.
          621  +** SQLITE_MISUSE.  SQLITE_OK is returned if everything works.
   614    622   */
   615    623   #ifdef SQLITE_UNIX_THREADS
   616    624   static int transferOwnership(unixFile *pFile){
   617    625     int rc;
   618    626     pthread_t hSelf;
   619    627     if( threadsOverrideEachOthersLocks ){
   620    628       /* Ownership transfers not needed on this system */
................................................................................
   636    644     rc = findLockInfo(pFile->h, &pFile->pLock, 0);
   637    645     TRACE5("LOCK    %d is now %s(%s,%d)\n", pFile->h,
   638    646        locktypeName(pFile->locktype),
   639    647        locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
   640    648     return rc;
   641    649   }
   642    650   #else
          651  +  /* On single-threaded builds, ownership transfer is a no-op */
   643    652   # define transferOwnership(X) SQLITE_OK
   644    653   #endif
   645    654   
   646    655   /*
   647    656   ** Delete the named file
   648    657   */
   649    658   int sqlite3UnixDelete(const char *zFilename){
................................................................................
   830    839     return SQLITE_OK;
   831    840   }
   832    841   
   833    842   /*
   834    843   ** If the following global variable points to a string which is the
   835    844   ** name of a directory, then that directory will be used to store
   836    845   ** temporary files.
          846  +**
          847  +** See also the "PRAGMA temp_store_directory" SQL command.
   837    848   */
   838    849   char *sqlite3_temp_directory = 0;
   839    850   
   840    851   /*
   841    852   ** Create a temporary file name in zBuf.  zBuf must be big enough to
   842    853   ** hold at least SQLITE_TEMPNAME_SIZE characters.
   843    854   */
................................................................................
  1367   1378   
  1368   1379   /*
  1369   1380   ** Lower the locking level on file descriptor pFile to locktype.  locktype
  1370   1381   ** must be either NO_LOCK or SHARED_LOCK.
  1371   1382   **
  1372   1383   ** If the locking level of the file descriptor is already at or below
  1373   1384   ** the requested locking level, this routine is a no-op.
  1374         -**
  1375         -** It is not possible for this routine to fail if the second argument
  1376         -** is NO_LOCK.  If the second argument is SHARED_LOCK, this routine
  1377         -** might return SQLITE_IOERR instead of SQLITE_OK.
  1378   1385   */
  1379   1386   static int unixUnlock(OsFile *id, int locktype){
  1380   1387     struct lockInfo *pLock;
  1381   1388     struct flock lock;
  1382   1389     int rc = SQLITE_OK;
  1383   1390     unixFile *pFile = (unixFile*)id;
  1384   1391   
................................................................................
  1386   1393     TRACE7("UNLOCK  %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
  1387   1394         pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
  1388   1395   
  1389   1396     assert( locktype<=SHARED_LOCK );
  1390   1397     if( pFile->locktype<=locktype ){
  1391   1398       return SQLITE_OK;
  1392   1399     }
  1393         -  if( CHECK_THREADID(pFile) ) return SQLITE_MISUSE;
         1400  +  if( CHECK_THREADID(pFile) ){
         1401  +    return SQLITE_MISUSE;
         1402  +  }
  1394   1403     sqlite3OsEnterMutex();
  1395   1404     pLock = pFile->pLock;
  1396   1405     assert( pLock->cnt!=0 );
  1397   1406     if( pFile->locktype>SHARED_LOCK ){
  1398   1407       assert( pLock->locktype==pFile->locktype );
  1399   1408       if( locktype==SHARED_LOCK ){
  1400   1409         lock.l_type = F_RDLCK;
................................................................................
  1612   1621     ** prefer that the randomness be increased by making use of the
  1613   1622     ** uninitialized space in zBuf - but valgrind errors tend to worry
  1614   1623     ** some users.  Rather than argue, it seems easier just to initialize
  1615   1624     ** the whole array and silence valgrind, even if that means less randomness
  1616   1625     ** in the random seed.
  1617   1626     **
  1618   1627     ** When testing, initializing zBuf[] to zero is all we do.  That means
  1619         -  ** that we always use the same random number sequence.* This makes the
         1628  +  ** that we always use the same random number sequence.  This makes the
  1620   1629     ** tests repeatable.
  1621   1630     */
  1622   1631     memset(zBuf, 0, 256);
  1623   1632   #if !defined(SQLITE_TEST)
  1624   1633     {
  1625   1634       int pid, fd;
  1626   1635       fd = open("/dev/urandom", O_RDONLY);
................................................................................
  1637   1646     }
  1638   1647   #endif
  1639   1648     return SQLITE_OK;
  1640   1649   }
  1641   1650   
  1642   1651   /*
  1643   1652   ** Sleep for a little while.  Return the amount of time slept.
         1653  +** The argument is the number of milliseconds we want to sleep.
  1644   1654   */
  1645   1655   int sqlite3UnixSleep(int ms){
  1646   1656   #if defined(HAVE_USLEEP) && HAVE_USLEEP
  1647   1657     usleep(ms*1000);
  1648   1658     return ms;
  1649   1659   #else
  1650   1660     sleep((ms+999)/1000);
................................................................................
  1663   1673   /*
  1664   1674   ** The following pair of routine implement mutual exclusion for
  1665   1675   ** multi-threaded processes.  Only a single thread is allowed to
  1666   1676   ** executed code that is surrounded by EnterMutex() and LeaveMutex().
  1667   1677   **
  1668   1678   ** SQLite uses only a single Mutex.  There is not much critical
  1669   1679   ** code and what little there is executes quickly and without blocking.
         1680  +**
         1681  +** This mutex is not recursive.
  1670   1682   */
  1671   1683   void sqlite3UnixEnterMutex(){
  1672   1684   #ifdef SQLITE_UNIX_THREADS
  1673   1685     pthread_mutex_lock(&mutex);
  1674   1686   #endif
  1675   1687     assert( !inMutex );
  1676   1688     inMutex = 1;
................................................................................
  1708   1720   # endif
  1709   1721   #else
  1710   1722   # define TSD_COUNTER(N)  /* no-op */
  1711   1723   #endif
  1712   1724   
  1713   1725   
  1714   1726   /*
  1715         -** If called with allocateFlag>1, then return a pointer to thread
         1727  +** If called with allocateFlag>0, then return a pointer to thread
  1716   1728   ** specific data for the current thread.  Allocate and zero the
  1717         -** thread-specific data if it does not already exist necessary.
         1729  +** thread-specific data if it does not already exist.
  1718   1730   **
  1719   1731   ** If called with allocateFlag==0, then check the current thread
  1720   1732   ** specific data.  Return it if it exists.  If it does not exist,
  1721   1733   ** then return NULL.
  1722   1734   **
  1723   1735   ** If called with allocateFlag<0, check to see if the thread specific
  1724   1736   ** data is allocated and is all zero.  If it is then deallocate it.