Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Simplifications to the SHM implementation in os_unix.c, taking advantage of the removal of the LinuxThreads mess. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
d1debe5def82a6bc72f11b8787176ac6 |
User & Date: | drh 2010-05-14 14:52:25.000 |
Context
2010-05-14
| ||
19:24 | Make sure the value of an INTEGER PRIMARY KEY column supplied to triggers and especially to FK constraints really contains the ROWID and not the NULL that is stored in the column itself. Ticket [dd08e5a988d00dec]. (check-in: 636f86095e user: drh tags: trunk) | |
16:34 | Pull in all the latest changes from the trunk. Update the win32 SHM methods to work with the new interface design. (Closed-Leaf check-in: 4b69f2cd31 user: drh tags: wal-win32) | |
14:52 | Simplifications to the SHM implementation in os_unix.c, taking advantage of the removal of the LinuxThreads mess. (check-in: d1debe5def user: drh tags: trunk) | |
12:43 | Simplify os_unix.c by removing support for LinuxThreads. Linux systems must either use NPTL or else not share database connections across threads. (check-in: e294b696ba user: drh tags: trunk) | |
Changes
Changes to src/os_unix.c.
︙ | ︙ | |||
171 172 173 174 175 176 177 | /* ** Only set the lastErrno if the error code is a real error and not ** a normal expected return code of SQLITE_BUSY or SQLITE_OK */ #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) | | | | > > < | | 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | /* ** Only set the lastErrno if the error code is a real error and not ** a normal expected return code of SQLITE_BUSY or SQLITE_OK */ #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) /* Forward references */ typedef struct unixShm unixShm; /* Connection shared memory */ typedef struct unixShmNode unixShmNode; /* Shared memory instance */ typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ /* ** Sometimes, after a file handle is closed by SQLite, the file descriptor ** cannot be closed immediately. In these cases, instances of the following ** structure are used to store the file descriptor while waiting for an ** opportunity to either close or reuse it. */ struct UnixUnusedFd { int fd; /* File descriptor to close */ int flags; /* Flags this file descriptor was opened with */ UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ }; /* ** The unixFile structure is subclass of sqlite3_file specific to the unix ** VFS implementations. */ typedef struct unixFile unixFile; struct unixFile { sqlite3_io_methods const *pMethod; /* Always the first entry */ unixInodeInfo *pInode; /* Info about locks on this inode */ int h; /* The file descriptor */ int dirfd; /* File descriptor for the directory */ unsigned char eFileLock; /* The type of lock held on this fd */ int lastErrno; /* The unix errno from last I/O error */ void *lockingContext; /* Locking style specific state */ UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ int fileFlags; /* Miscellanous flags */ |
︙ | ︙ | |||
711 712 713 714 715 716 717 718 719 720 | ** object keeps a count of the number of unixFile pointing to it. */ struct unixInodeInfo { struct unixFileId fileId; /* The lookup key */ int nShared; /* Number of SHARED locks held */ int eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ int nRef; /* Number of pointers to this structure */ #if defined(SQLITE_ENABLE_LOCKING_STYLE) unsigned long long sharedByte; /* for AFP simulated shared lock */ #endif | > > > > > < < < < | | > | 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | ** object keeps a count of the number of unixFile pointing to it. */ struct unixInodeInfo { struct unixFileId fileId; /* The lookup key */ int nShared; /* Number of SHARED locks held */ int eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ int nRef; /* Number of pointers to this structure */ unixShmNode *pShmNode; /* Shared memory associated with this inode */ int nLock; /* Number of outstanding file locks */ UnixUnusedFd *pUnused; /* Unused file descriptors to close */ unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ unixInodeInfo *pPrev; /* .... doubly linked */ #if defined(SQLITE_ENABLE_LOCKING_STYLE) unsigned long long sharedByte; /* for AFP simulated shared lock */ #endif #if OS_VXWORKS sem_t *pSem; /* Named POSIX semaphore */ char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ #endif }; /* ** A lists of all unixInodeInfo objects. */ static unixInodeInfo *inodeList = 0; /* ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). ** ** The mutex entered using the unixEnterMutex() function must be held ** when this function is called. */ static void releaseInodeInfo(unixInodeInfo *pInode){ assert( unixMutexHeld() ); if( pInode ){ pInode->nRef--; if( pInode->nRef==0 ){ assert( pInode->pShmNode==0 ); if( pInode->pPrev ){ assert( pInode->pPrev->pNext==pInode ); pInode->pPrev->pNext = pInode->pNext; }else{ assert( inodeList==pInode ); inodeList = pInode->pNext; } |
︙ | ︙ | |||
768 769 770 771 772 773 774 | ** The mutex entered using the unixEnterMutex() function must be held ** when this function is called. ** ** Return an appropriate error code. */ static int findInodeInfo( unixFile *pFile, /* Unix file with file desc used in the key */ | | | | | | 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 | ** The mutex entered using the unixEnterMutex() function must be held ** when this function is called. ** ** Return an appropriate error code. */ static int findInodeInfo( unixFile *pFile, /* Unix file with file desc used in the key */ unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ ){ int rc; /* System call return code */ int fd; /* The file descriptor for pFile */ struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ struct stat statbuf; /* Low-level file information */ unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ assert( unixMutexHeld() ); /* Get low-level information about the file that we can used to ** create a unique name for the file. */ fd = pFile->h; |
︙ | ︙ | |||
959 960 961 962 963 964 965 | ** The reason a single byte cannot be used instead of the 'shared byte ** range' is that some versions of windows do not support read-locks. By ** locking a random byte from a range, concurrent SHARED locks may exist ** even if the locking primitive used is always a write-lock. */ int rc = SQLITE_OK; unixFile *pFile = (unixFile*)id; | | | 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 | ** The reason a single byte cannot be used instead of the 'shared byte ** range' is that some versions of windows do not support read-locks. By ** locking a random byte from a range, concurrent SHARED locks may exist ** even if the locking primitive used is always a write-lock. */ int rc = SQLITE_OK; unixFile *pFile = (unixFile*)id; unixInodeInfo *pInode = pFile->pInode; struct flock lock; int s = 0; int tErrno = 0; assert( pFile ); OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
︙ | ︙ | |||
1156 1157 1158 1159 1160 1161 1162 | ** ** Otherwise, if an error occurs, then successfully closed file descriptor ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned. ** not deleted and SQLITE_IOERR_CLOSE returned. */ static int closePendingFds(unixFile *pFile){ int rc = SQLITE_OK; | | | 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 | ** ** Otherwise, if an error occurs, then successfully closed file descriptor ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned. ** not deleted and SQLITE_IOERR_CLOSE returned. */ static int closePendingFds(unixFile *pFile){ int rc = SQLITE_OK; unixInodeInfo *pInode = pFile->pInode; UnixUnusedFd *pError = 0; UnixUnusedFd *p; UnixUnusedFd *pNext; for(p=pInode->pUnused; p; p=pNext){ pNext = p->pNext; if( close(p->fd) ){ pFile->lastErrno = errno; |
︙ | ︙ | |||
1180 1181 1182 1183 1184 1185 1186 | } /* ** Add the file descriptor used by file handle pFile to the corresponding ** pUnused list. */ static void setPendingFd(unixFile *pFile){ | | | 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 | } /* ** Add the file descriptor used by file handle pFile to the corresponding ** pUnused list. */ static void setPendingFd(unixFile *pFile){ unixInodeInfo *pInode = pFile->pInode; UnixUnusedFd *p = pFile->pUnused; p->pNext = pInode->pUnused; pInode->pUnused = p; pFile->h = -1; pFile->pUnused = 0; } |
︙ | ︙ | |||
1203 1204 1205 1206 1207 1208 1209 | ** the byte range is divided into 2 parts and the first part is unlocked then ** set to a read lock, then the other part is simply unlocked. This works ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to ** remove the write lock on a region when a read lock is set. */ static int _posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ unixFile *pFile = (unixFile*)id; | | | 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 | ** the byte range is divided into 2 parts and the first part is unlocked then ** set to a read lock, then the other part is simply unlocked. This works ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to ** remove the write lock on a region when a read lock is set. */ static int _posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ unixFile *pFile = (unixFile*)id; unixInodeInfo *pInode; struct flock lock; int rc = SQLITE_OK; int h; int tErrno; /* Error code from system call errors */ assert( pFile ); OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
︙ | ︙ | |||
2230 2231 2232 2233 2234 2235 2236 | ** ** This routine will only increase a lock. Use the sqlite3OsUnlock() ** routine to lower a locking level. */ static int afpLock(sqlite3_file *id, int eFileLock){ int rc = SQLITE_OK; unixFile *pFile = (unixFile*)id; | | | 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 | ** ** This routine will only increase a lock. Use the sqlite3OsUnlock() ** routine to lower a locking level. */ static int afpLock(sqlite3_file *id, int eFileLock){ int rc = SQLITE_OK; unixFile *pFile = (unixFile*)id; unixInodeInfo *pInode = pFile->pInode; afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; assert( pFile ); OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, azFileLock(eFileLock), azFileLock(pFile->eFileLock), azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
︙ | ︙ | |||
2412 2413 2414 2415 2416 2417 2418 | ** ** If the locking level of the file descriptor is already at or below ** the requested locking level, this routine is a no-op. */ static int afpUnlock(sqlite3_file *id, int eFileLock) { int rc = SQLITE_OK; unixFile *pFile = (unixFile*)id; | | | 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 | ** ** If the locking level of the file descriptor is already at or below ** the requested locking level, this routine is a no-op. */ static int afpUnlock(sqlite3_file *id, int eFileLock) { int rc = SQLITE_OK; unixFile *pFile = (unixFile*)id; unixInodeInfo *pInode; afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; int skipShared = 0; #ifdef SQLITE_TEST int h = pFile->h; #endif assert( pFile ); |
︙ | ︙ | |||
3086 3087 3088 3089 3090 3091 3092 | return 0; } #ifndef SQLITE_OMIT_WAL /* | | > | | | | > > > > > > > < | | | | < < < < < < < < > > > | | | | | | | | | 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 | return 0; } #ifndef SQLITE_OMIT_WAL /* ** Object used to represent an shared memory buffer. ** ** When multiple threads all reference the same wal-index, each thread ** has its own unixShm object, but they all point to a single instance ** of this unixShmNode object. In other words, each wal-index is opened ** only once per process. ** ** Each unixShmNode object is connected to a single unixInodeInfo object. ** We could coalesce this object into unixInodeInfo, but that would mean ** every open file that does not use shared memory (in other words, most ** open files) would have to carry around this extra information. So ** the unixInodeInfo object contains a pointer to this unixShmNode object ** and the unixShmNode object is created only when needed. ** ** unixMutexHeld() must be true when creating or destroying ** this object or while reading or writing the following fields: ** ** nRef ** ** The following fields are read-only after the object is created: ** ** fid ** zFilename ** ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and ** unixMutexHeld() is true when reading or writing any other field ** in this structure. ** ** To avoid deadlocks, mutex and mutexBuf are always released in the ** reverse order that they are acquired. mutexBuf is always acquired ** first and released last. This invariant is check by asserting ** sqlite3_mutex_notheld() on mutex whenever mutexBuf is acquired or ** released. */ struct unixShmNode { unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ sqlite3_mutex *mutex; /* Mutex to access this object */ sqlite3_mutex *mutexBuf; /* Mutex to access zBuf[] */ char *zFilename; /* Name of the mmapped file */ int h; /* Open file descriptor */ int szMap; /* Size of the mapping into memory */ char *pMMapBuf; /* Where currently mmapped(). NULL if unmapped */ int nRef; /* Number of unixShm objects pointing to this */ unixShm *pFirst; /* All unixShm objects pointing to this */ #ifdef SQLITE_DEBUG u8 exclMask; /* Mask of exclusive locks held */ u8 sharedMask; /* Mask of shared locks held */ u8 nextShmId; /* Next available unixShm.id value */ #endif }; /* ** Structure used internally by this VFS to record the state of an ** open shared memory connection. ** ** The following fields are initialized when this object is created and ** are read-only thereafter: ** ** unixShm.pFile ** unixShm.id ** ** All other fields are read/write. The unixShm.pFile->mutex must be held ** while accessing any read/write fields. */ struct unixShm { unixShmNode *pShmNode; /* The underlying unixShmNode object */ unixShm *pNext; /* Next unixShm with the same unixShmNode */ u8 lockState; /* Current lock state */ u8 hasMutex; /* True if holding the unixShmNode mutex */ u8 hasMutexBuf; /* True if holding pFile->mutexBuf */ u8 sharedMask; /* Mask of shared locks held */ u8 exclMask; /* Mask of exclusive locks held */ #ifdef SQLITE_DEBUG u8 id; /* Id of this connection within its unixShmNode */ #endif }; /* ** Size increment by which shared memory grows */ #define SQLITE_UNIX_SHM_INCR 4096 |
︙ | ︙ | |||
3217 3218 3219 3220 3221 3222 3223 | ** lockMask might contain multiple bits but all bits are guaranteed ** to be contiguous. ** ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking ** otherwise. */ static int unixShmSystemLock( | | | | | | | 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 | ** lockMask might contain multiple bits but all bits are guaranteed ** to be contiguous. ** ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking ** otherwise. */ static int unixShmSystemLock( unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ u8 lockMask /* Which bytes to lock or unlock */ ){ struct flock f; /* The posix advisory locking structure */ int lockOp; /* The opcode for fcntl() */ int i; /* Offset into the locking byte range */ int rc; /* Result code form fcntl() */ u8 mask; /* Mask of bits in lockMask */ /* Access to the unixShmNode object is serialized by the caller */ assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); /* Initialize the locking parameters */ memset(&f, 0, sizeof(f)); f.l_type = lockType; f.l_whence = SEEK_SET; if( lockMask==UNIX_SHM_C && lockType!=F_UNLCK ){ lockOp = F_SETLKW; |
︙ | ︙ | |||
3258 3259 3260 3261 3262 3263 3264 | mask <<= 1; } /* Verify that all bits set in lockMask are contiguous */ assert( mask==0 || (lockMask & ~(mask | (mask-1)))==0 ); /* Acquire the system-level lock */ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | < | < < | > | | | | | < < < | 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 | mask <<= 1; } /* Verify that all bits set in lockMask are contiguous */ assert( mask==0 || (lockMask & ~(mask | (mask-1)))==0 ); /* Acquire the system-level lock */ rc = fcntl(pShmNode->h, lockOp, &f); rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; /* Update the global lock state and do debug tracing */ #ifdef SQLITE_DEBUG OSTRACE(("SHM-LOCK ")); if( rc==SQLITE_OK ){ if( lockType==F_UNLCK ){ OSTRACE(("unlock ok")); pShmNode->exclMask &= ~lockMask; pShmNode->sharedMask &= ~lockMask; }else if( lockType==F_RDLCK ){ OSTRACE(("read-lock ok")); pShmNode->exclMask &= ~lockMask; pShmNode->sharedMask |= lockMask; }else{ assert( lockType==F_WRLCK ); OSTRACE(("write-lock ok")); pShmNode->exclMask |= lockMask; pShmNode->sharedMask &= ~lockMask; } }else{ if( lockType==F_UNLCK ){ OSTRACE(("unlock failed")); }else if( lockType==F_RDLCK ){ OSTRACE(("read-lock failed")); }else{ assert( lockType==F_WRLCK ); OSTRACE(("write-lock failed")); } } OSTRACE((" - change requested %s - afterwards %s:%s\n", unixShmLockString(lockMask), unixShmLockString(pShmNode->sharedMask), unixShmLockString(pShmNode->exclMask))); #endif return rc; } /* ** For connection p, unlock all of the locks identified by the unlockMask ** parameter. */ static int unixShmUnlock( unixShmNode *pShmNode, /* The underlying shared-memory file */ unixShm *p, /* The connection to be unlocked */ u8 unlockMask /* Mask of locks to be unlocked */ ){ int rc; /* Result code */ unixShm *pX; /* For looping over all sibling connections */ u8 allMask; /* Union of locks held by connections other than "p" */ /* Access to the unixShmNode object is serialized by the caller */ assert( sqlite3_mutex_held(pShmNode->mutex) ); /* Compute locks held by sibling connections */ allMask = 0; for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ if( pX==p ) continue; assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); allMask |= pX->sharedMask; } /* Unlock the system-level locks */ if( (unlockMask & allMask)!=unlockMask ){ rc = unixShmSystemLock(pShmNode, F_UNLCK, unlockMask & ~allMask); }else{ rc = SQLITE_OK; } /* Undo the local locks */ if( rc==SQLITE_OK ){ p->exclMask &= ~unlockMask; p->sharedMask &= ~unlockMask; } return rc; } /* ** Get reader locks for connection p on all locks in the readMask parameter. */ static int unixShmSharedLock( unixShmNode *pShmNode, /* The underlying shared-memory file */ unixShm *p, /* The connection to get the shared locks */ u8 readMask /* Mask of shared locks to be acquired */ ){ int rc; /* Result code */ unixShm *pX; /* For looping over all sibling connections */ u8 allShared; /* Union of locks held by connections other than "p" */ /* Access to the unixShmNode object is serialized by the caller */ assert( sqlite3_mutex_held(pShmNode->mutex) ); /* Find out which shared locks are already held by sibling connections. ** If any sibling already holds an exclusive lock, go ahead and return ** SQLITE_BUSY. */ allShared = 0; for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ if( pX==p ) continue; if( (pX->exclMask & readMask)!=0 ) return SQLITE_BUSY; allShared |= pX->sharedMask; } /* Get shared locks at the system level, if necessary */ if( (~allShared) & readMask ){ rc = unixShmSystemLock(pShmNode, F_RDLCK, readMask); }else{ rc = SQLITE_OK; } /* Get the local shared locks */ if( rc==SQLITE_OK ){ p->sharedMask |= readMask; } return rc; } /* ** For connection p, get an exclusive lock on all locks identified in ** the writeMask parameter. */ static int unixShmExclusiveLock( unixShmNode *pShmNode, /* The underlying shared-memory file */ unixShm *p, /* The connection to get the exclusive locks */ u8 writeMask /* Mask of exclusive locks to be acquired */ ){ int rc; /* Result code */ unixShm *pX; /* For looping over all sibling connections */ /* Access to the unixShmNode object is serialized by the caller */ assert( sqlite3_mutex_held(pShmNode->mutex) ); /* Make sure no sibling connections hold locks that will block this ** lock. If any do, return SQLITE_BUSY right away. */ for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ if( pX==p ) continue; if( (pX->exclMask & writeMask)!=0 ) return SQLITE_BUSY; if( (pX->sharedMask & writeMask)!=0 ) return SQLITE_BUSY; } /* Get the exclusive locks at the system level. Then if successful ** also mark the local connection as being locked. */ rc = unixShmSystemLock(pShmNode, F_WRLCK, writeMask); if( rc==SQLITE_OK ){ p->sharedMask &= ~writeMask; p->exclMask |= writeMask; } return rc; } /* ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. ** ** This is not a VFS shared-memory method; it is a utility function called ** by VFS shared-memory methods. */ static void unixShmPurge(unixFile *pFd){ unixShmNode *p = pFd->pInode->pShmNode; assert( unixMutexHeld() ); if( p && p->nRef==0 ){ assert( p->pInode==pFd->pInode ); if( p->mutex ) sqlite3_mutex_free(p->mutex); if( p->mutexBuf ) sqlite3_mutex_free(p->mutexBuf); if( p->h>=0 ) close(p->h); p->pInode->pShmNode = 0; sqlite3_free(p); } } /* ** Open a shared-memory area. This particular implementation uses ** mmapped files. ** |
︙ | ︙ | |||
3455 3456 3457 3458 3459 3460 3461 | ** file are currently open, in this process or in other processes, then ** the file must be truncated to zero length or have its header cleared. */ static int unixShmOpen( sqlite3_file *fd /* The file descriptor of the associated database */ ){ struct unixShm *p = 0; /* The connection to be opened */ | | < < < | < < < < < < | < < < | < | < > | > | | | < < > < > | | < | < | < | | | | | | | | < < < < < < < < > | | | | | | | | | | < | | > > > | > | | | | | | | | | | 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 | ** file are currently open, in this process or in other processes, then ** the file must be truncated to zero length or have its header cleared. */ static int unixShmOpen( sqlite3_file *fd /* The file descriptor of the associated database */ ){ struct unixShm *p = 0; /* The connection to be opened */ struct unixShmNode *pShmNode = 0; /* The underlying mmapped file */ int rc; /* Result code */ struct unixFile *pDbFd; /* Underlying database file */ int nPath; /* Size of pDbFd->zPath in bytes */ /* Allocate space for the new sqlite3_shm object. */ p = sqlite3_malloc( sizeof(*p) ); if( p==0 ) return SQLITE_NOMEM; memset(p, 0, sizeof(*p)); pDbFd = (struct unixFile*)fd; assert( pDbFd->pShm==0 ); /* Check to see if a unixShmNode object already exists. Reuse an existing ** one if present. Create a new one if necessary. */ unixEnterMutex(); pShmNode = pDbFd->pInode->pShmNode; if( pShmNode==0 ){ nPath = strlen(pDbFd->zPath); pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nPath + 15 ); if( pShmNode==0 ){ rc = SQLITE_NOMEM; goto shm_open_err; } memset(pShmNode, 0, sizeof(*pShmNode)); pShmNode->zFilename = (char*)&pShmNode[1]; sqlite3_snprintf(nPath+15, pShmNode->zFilename, "%s-wal-index", pDbFd->zPath); pShmNode->h = -1; pDbFd->pInode->pShmNode = pShmNode; pShmNode->pInode = pDbFd->pInode; pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); if( pShmNode->mutex==0 ){ rc = SQLITE_NOMEM; goto shm_open_err; } pShmNode->mutexBuf = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); if( pShmNode->mutexBuf==0 ){ rc = SQLITE_NOMEM; goto shm_open_err; } pShmNode->h = open(pShmNode->zFilename, O_RDWR|O_CREAT, 0664); if( pShmNode->h<0 ){ rc = SQLITE_CANTOPEN_BKPT; goto shm_open_err; } /* Check to see if another process is holding the dead-man switch. ** If not, truncate the file to zero length. */ rc = SQLITE_OK; if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS)==SQLITE_OK ){ if( ftruncate(pShmNode->h, 0) ){ rc = SQLITE_IOERR; } } if( rc==SQLITE_OK ){ rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS); } if( rc ) goto shm_open_err; } /* Make the new connection a child of the unixShmNode */ p->pShmNode = pShmNode; p->pNext = pShmNode->pFirst; #ifdef SQLITE_DEBUG p->id = pShmNode->nextShmId++; #endif pShmNode->pFirst = p; pShmNode->nRef++; pDbFd->pShm = p; unixLeaveMutex(); return SQLITE_OK; /* Jump here on any error */ shm_open_err: unixShmPurge(pDbFd); /* This call frees pShmNode if required */ sqlite3_free(p); unixLeaveMutex(); return rc; } /* ** Close a connection to shared-memory. Delete the underlying ** storage if deleteFlag is true. */ static int unixShmClose( sqlite3_file *fd, /* The underlying database file */ int deleteFlag /* Delete shared-memory if true */ ){ unixShm *p; /* The connection to be closed */ unixShmNode *pShmNode; /* The underlying shared-memory file */ unixShm **pp; /* For looping over sibling connections */ unixFile *pDbFd; /* The underlying database file */ pDbFd = (unixFile*)fd; p = pDbFd->pShm; if( p==0 ) return SQLITE_OK; pShmNode = p->pShmNode; assert( pShmNode==pDbFd->pInode->pShmNode ); assert( pShmNode->pInode==pDbFd->pInode ); /* Verify that the connection being closed holds no locks */ assert( p->exclMask==0 ); assert( p->sharedMask==0 ); /* Remove connection p from the set of connections associated ** with pShmNode */ sqlite3_mutex_enter(pShmNode->mutex); for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} *pp = p->pNext; /* Free the connection p */ sqlite3_free(p); pDbFd->pShm = 0; sqlite3_mutex_leave(pShmNode->mutex); /* If pShmNode->nRef has reached 0, then close the underlying ** shared-memory file, too */ unixEnterMutex(); assert( pShmNode->nRef>0 ); pShmNode->nRef--; if( pShmNode->nRef==0 ){ if( deleteFlag ) unlink(pShmNode->zFilename); unixShmPurge(pDbFd); } unixLeaveMutex(); return SQLITE_OK; } /* |
︙ | ︙ | |||
3630 3631 3632 3633 3634 3635 3636 | static int unixShmSize( sqlite3_file *fd, /* The open database file holding SHM */ int reqSize, /* Requested size. -1 for query only */ int *pNewSize /* Write new size here */ ){ unixFile *pDbFd = (unixFile*)fd; unixShm *p = pDbFd->pShm; | | > > > | | | 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 | static int unixShmSize( sqlite3_file *fd, /* The open database file holding SHM */ int reqSize, /* Requested size. -1 for query only */ int *pNewSize /* Write new size here */ ){ unixFile *pDbFd = (unixFile*)fd; unixShm *p = pDbFd->pShm; unixShmNode *pShmNode = p->pShmNode; int rc = SQLITE_OK; struct stat sStat; assert( pShmNode==pDbFd->pInode->pShmNode ); assert( pShmNode->pInode==pDbFd->pInode ); /* On a query, this loop runs once. When reqSize>=0, the loop potentially ** runs twice, except if the actual size is already greater than or equal ** to the requested size, reqSize is set to -1 on the first iteration and ** the loop only runs once. */ while( 1 ){ if( fstat(pShmNode->h, &sStat)==0 ){ *pNewSize = (int)sStat.st_size; if( reqSize>=0 && reqSize<=(int)sStat.st_size ) break; }else{ *pNewSize = 0; rc = SQLITE_IOERR; break; } if( reqSize<0 ) break; reqSize = (reqSize + SQLITE_UNIX_SHM_INCR - 1)/SQLITE_UNIX_SHM_INCR; reqSize *= SQLITE_UNIX_SHM_INCR; rc = ftruncate(pShmNode->h, reqSize); reqSize = -1; } return rc; } /* |
︙ | ︙ | |||
3692 3693 3694 3695 3696 3697 3698 | sqlite3_file *fd, /* Database file holding shared memory */ int reqMapSize, /* Requested size of mapping. -1 means don't care */ int *pNewMapSize, /* Write new size of mapping here */ void **ppBuf /* Write mapping buffer origin here */ ){ unixFile *pDbFd = (unixFile*)fd; unixShm *p = pDbFd->pShm; | | > > > | | | | | | | | | | | | | | | 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 | sqlite3_file *fd, /* Database file holding shared memory */ int reqMapSize, /* Requested size of mapping. -1 means don't care */ int *pNewMapSize, /* Write new size of mapping here */ void **ppBuf /* Write mapping buffer origin here */ ){ unixFile *pDbFd = (unixFile*)fd; unixShm *p = pDbFd->pShm; unixShmNode *pShmNode = p->pShmNode; int rc = SQLITE_OK; assert( pShmNode==pDbFd->pInode->pShmNode ); assert( pShmNode->pInode==pDbFd->pInode ); if( p->lockState!=SQLITE_SHM_CHECKPOINT && p->hasMutexBuf==0 ){ assert( sqlite3_mutex_notheld(pShmNode->mutex) ); sqlite3_mutex_enter(pShmNode->mutexBuf); p->hasMutexBuf = 1; } sqlite3_mutex_enter(pShmNode->mutex); if( pShmNode->szMap==0 || reqMapSize>pShmNode->szMap ){ int actualSize; if( unixShmSize(fd, -1, &actualSize)==SQLITE_OK && reqMapSize<actualSize ){ reqMapSize = actualSize; } if( pShmNode->pMMapBuf ){ munmap(pShmNode->pMMapBuf, pShmNode->szMap); } pShmNode->pMMapBuf = mmap(0, reqMapSize, PROT_READ|PROT_WRITE, MAP_SHARED, pShmNode->h, 0); pShmNode->szMap = pShmNode->pMMapBuf ? reqMapSize : 0; } *pNewMapSize = pShmNode->szMap; *ppBuf = pShmNode->pMMapBuf; sqlite3_mutex_leave(pShmNode->mutex); return rc; } /* ** Release the lock held on the shared memory segment to that other ** threads are free to resize it if necessary. ** ** If the lock is not currently held, this routine is a harmless no-op. ** ** If the shared-memory object is in lock state RECOVER, then we do not ** really want to release the lock, so in that case too, this routine ** is a no-op. */ static int unixShmRelease(sqlite3_file *fd){ unixFile *pDbFd = (unixFile*)fd; unixShm *p = pDbFd->pShm; if( p->hasMutexBuf && p->lockState!=SQLITE_SHM_RECOVER ){ assert( sqlite3_mutex_notheld(p->pShmNode->mutex) ); sqlite3_mutex_leave(p->pShmNode->mutexBuf); p->hasMutexBuf = 0; } return SQLITE_OK; } /* ** Symbolic names for LOCK states used for debugging. |
︙ | ︙ | |||
3769 3770 3771 3772 3773 3774 3775 | static int unixShmLock( sqlite3_file *fd, /* Database file holding the shared memory */ int desiredLock, /* One of SQLITE_SHM_xxxxx locking states */ int *pGotLock /* The lock you actually got */ ){ unixFile *pDbFd = (unixFile*)fd; unixShm *p = pDbFd->pShm; | | > > > | 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 | static int unixShmLock( sqlite3_file *fd, /* Database file holding the shared memory */ int desiredLock, /* One of SQLITE_SHM_xxxxx locking states */ int *pGotLock /* The lock you actually got */ ){ unixFile *pDbFd = (unixFile*)fd; unixShm *p = pDbFd->pShm; unixShmNode *pShmNode = p->pShmNode; int rc = SQLITE_PROTOCOL; assert( pShmNode==pDbFd->pInode->pShmNode ); assert( pShmNode->pInode==pDbFd->pInode ); /* Note that SQLITE_SHM_READ_FULL and SQLITE_SHM_PENDING are never ** directly requested; they are side effects from requesting ** SQLITE_SHM_READ and SQLITE_SHM_CHECKPOINT, respectively. */ assert( desiredLock==SQLITE_SHM_UNLOCK || desiredLock==SQLITE_SHM_READ |
︙ | ︙ | |||
3798 3799 3800 3801 3802 3803 3804 | return SQLITE_OK; } OSTRACE(("SHM-LOCK shmid-%d, pid-%d request %s->%s\n", p->id, getpid(), azLkName[p->lockState], azLkName[desiredLock])); if( desiredLock==SQLITE_SHM_RECOVER && !p->hasMutexBuf ){ | | | | | | | | | | | | | | | | | 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 | return SQLITE_OK; } OSTRACE(("SHM-LOCK shmid-%d, pid-%d request %s->%s\n", p->id, getpid(), azLkName[p->lockState], azLkName[desiredLock])); if( desiredLock==SQLITE_SHM_RECOVER && !p->hasMutexBuf ){ assert( sqlite3_mutex_notheld(pShmNode->mutex) ); sqlite3_mutex_enter(pShmNode->mutexBuf); p->hasMutexBuf = 1; } sqlite3_mutex_enter(pShmNode->mutex); switch( desiredLock ){ case SQLITE_SHM_UNLOCK: { assert( p->lockState!=SQLITE_SHM_RECOVER ); unixShmUnlock(pShmNode, p, UNIX_SHM_A|UNIX_SHM_B|UNIX_SHM_C|UNIX_SHM_D); rc = SQLITE_OK; p->lockState = SQLITE_SHM_UNLOCK; break; } case SQLITE_SHM_READ: { if( p->lockState==SQLITE_SHM_UNLOCK ){ int nAttempt; rc = SQLITE_BUSY; assert( p->lockState==SQLITE_SHM_UNLOCK ); for(nAttempt=0; nAttempt<5 && rc==SQLITE_BUSY; nAttempt++){ rc = unixShmSharedLock(pShmNode, p, UNIX_SHM_A|UNIX_SHM_B); if( rc==SQLITE_BUSY ){ rc = unixShmSharedLock(pShmNode, p, UNIX_SHM_D); if( rc==SQLITE_OK ){ p->lockState = SQLITE_SHM_READ_FULL; } }else{ unixShmUnlock(pShmNode, p, UNIX_SHM_B); p->lockState = SQLITE_SHM_READ; } } }else{ assert( p->lockState==SQLITE_SHM_WRITE || p->lockState==SQLITE_SHM_RECOVER ); rc = unixShmSharedLock(pShmNode, p, UNIX_SHM_A); unixShmUnlock(pShmNode, p, UNIX_SHM_C|UNIX_SHM_D); p->lockState = SQLITE_SHM_READ; } break; } case SQLITE_SHM_WRITE: { assert( p->lockState==SQLITE_SHM_READ || p->lockState==SQLITE_SHM_READ_FULL ); rc = unixShmExclusiveLock(pShmNode, p, UNIX_SHM_C|UNIX_SHM_D); if( rc==SQLITE_OK ){ p->lockState = SQLITE_SHM_WRITE; } break; } case SQLITE_SHM_CHECKPOINT: { assert( p->lockState==SQLITE_SHM_UNLOCK || p->lockState==SQLITE_SHM_PENDING ); if( p->lockState==SQLITE_SHM_UNLOCK ){ rc = unixShmExclusiveLock(pShmNode, p, UNIX_SHM_B|UNIX_SHM_C); if( rc==SQLITE_OK ){ p->lockState = SQLITE_SHM_PENDING; } } if( p->lockState==SQLITE_SHM_PENDING ){ rc = unixShmExclusiveLock(pShmNode, p, UNIX_SHM_A); if( rc==SQLITE_OK ){ p->lockState = SQLITE_SHM_CHECKPOINT; } } break; } default: { assert( desiredLock==SQLITE_SHM_RECOVER ); assert( p->lockState==SQLITE_SHM_READ || p->lockState==SQLITE_SHM_READ_FULL ); assert( sqlite3_mutex_held(pShmNode->mutexBuf) ); rc = unixShmExclusiveLock(pShmNode, p, UNIX_SHM_C); if( rc==SQLITE_OK ){ p->lockState = SQLITE_SHM_RECOVER; } break; } } sqlite3_mutex_leave(pShmNode->mutex); OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %s\n", p->id, getpid(), azLkName[p->lockState])); if( pGotLock ) *pGotLock = p->lockState; return rc; } #else |
︙ | ︙ | |||
4493 4494 4495 4496 4497 4498 4499 | ** For this reason, if an error occurs in the stat() call here, it is ** ignored and -1 is returned. The caller will try to open a new file ** descriptor on the same path, fail, and return an error to SQLite. ** ** Even if a subsequent open() call does succeed, the consequences of ** not searching for a resusable file descriptor are not dire. */ if( 0==stat(zPath, &sStat) ){ | | | 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 | ** For this reason, if an error occurs in the stat() call here, it is ** ignored and -1 is returned. The caller will try to open a new file ** descriptor on the same path, fail, and return an error to SQLite. ** ** Even if a subsequent open() call does succeed, the consequences of ** not searching for a resusable file descriptor are not dire. */ if( 0==stat(zPath, &sStat) ){ unixInodeInfo *pInode; unixEnterMutex(); pInode = inodeList; while( pInode && (pInode->fileId.dev!=sStat.st_dev || pInode->fileId.ino!=sStat.st_ino) ){ pInode = pInode->pNext; } |
︙ | ︙ |