blob: 127942bcac78a2f6743fa4b0952474ec1469b34f [file] [log] [blame]
danielk1977a3f06592009-04-23 14:58:39 +00001/*
2** 2005 December 14
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
danielk19775368f292009-07-18 11:52:04 +000013** $Id: sqlite3async.c,v 1.7 2009/07/18 11:52:04 danielk1977 Exp $
danielk1977a3f06592009-04-23 14:58:39 +000014**
danielk1977debcfd22009-04-24 09:27:16 +000015** This file contains the implementation of an asynchronous IO backend
16** for SQLite.
danielk1977a3f06592009-04-23 14:58:39 +000017*/
18
19#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ASYNCIO)
20
21#include "sqlite3async.h"
shaneeb4ac062009-04-30 17:45:33 +000022#include "sqlite3.h"
danielk19774598b8e2009-04-24 10:13:05 +000023#include <stdarg.h>
24#include <string.h>
25#include <assert.h>
danielk1977a3f06592009-04-23 14:58:39 +000026
danielk1977a3f06592009-04-23 14:58:39 +000027/* Useful macros used in several places */
28#define MIN(x,y) ((x)<(y)?(x):(y))
29#define MAX(x,y) ((x)>(y)?(x):(y))
30
shaneeb4ac062009-04-30 17:45:33 +000031#ifndef SQLITE_AMALGAMATION
32/* Macro to mark parameters as unused and silence compiler warnings. */
33#define UNUSED_PARAMETER(x) (void)(x)
34#endif
35
danielk1977a3f06592009-04-23 14:58:39 +000036/* Forward references */
37typedef struct AsyncWrite AsyncWrite;
38typedef struct AsyncFile AsyncFile;
39typedef struct AsyncFileData AsyncFileData;
40typedef struct AsyncFileLock AsyncFileLock;
41typedef struct AsyncLock AsyncLock;
42
43/* Enable for debugging */
danielk19774598b8e2009-04-24 10:13:05 +000044#ifndef NDEBUG
45#include <stdio.h>
danielk1977a3f06592009-04-23 14:58:39 +000046static int sqlite3async_trace = 0;
47# define ASYNC_TRACE(X) if( sqlite3async_trace ) asyncTrace X
48static void asyncTrace(const char *zFormat, ...){
49 char *z;
50 va_list ap;
51 va_start(ap, zFormat);
52 z = sqlite3_vmprintf(zFormat, ap);
53 va_end(ap);
54 fprintf(stderr, "[%d] %s", 0 /* (int)pthread_self() */, z);
55 sqlite3_free(z);
56}
shanea3628d12009-04-29 18:11:59 +000057#else
58# define ASYNC_TRACE(X)
danielk19774598b8e2009-04-24 10:13:05 +000059#endif
danielk1977a3f06592009-04-23 14:58:39 +000060
61/*
62** THREAD SAFETY NOTES
63**
64** Basic rules:
65**
66** * Both read and write access to the global write-op queue must be
67** protected by the async.queueMutex. As are the async.ioError and
68** async.nFile variables.
69**
70** * The async.pLock list and all AsyncLock and AsyncFileLock
71** structures must be protected by the async.lockMutex mutex.
72**
73** * The file handles from the underlying system are not assumed to
74** be thread safe.
75**
76** * See the last two paragraphs under "The Writer Thread" for
77** an assumption to do with file-handle synchronization by the Os.
78**
79** Deadlock prevention:
80**
81** There are three mutex used by the system: the "writer" mutex,
82** the "queue" mutex and the "lock" mutex. Rules are:
83**
84** * It is illegal to block on the writer mutex when any other mutex
85** are held, and
86**
87** * It is illegal to block on the queue mutex when the lock mutex
88** is held.
89**
90** i.e. mutex's must be grabbed in the order "writer", "queue", "lock".
91**
92** File system operations (invoked by SQLite thread):
93**
94** xOpen
95** xDelete
96** xFileExists
97**
98** File handle operations (invoked by SQLite thread):
99**
100** asyncWrite, asyncClose, asyncTruncate, asyncSync
101**
102** The operations above add an entry to the global write-op list. They
103** prepare the entry, acquire the async.queueMutex momentarily while
104** list pointers are manipulated to insert the new entry, then release
105** the mutex and signal the writer thread to wake up in case it happens
106** to be asleep.
107**
108**
109** asyncRead, asyncFileSize.
110**
111** Read operations. Both of these read from both the underlying file
112** first then adjust their result based on pending writes in the
113** write-op queue. So async.queueMutex is held for the duration
114** of these operations to prevent other threads from changing the
115** queue in mid operation.
116**
117**
118** asyncLock, asyncUnlock, asyncCheckReservedLock
119**
120** These primitives implement in-process locking using a hash table
121** on the file name. Files are locked correctly for connections coming
122** from the same process. But other processes cannot see these locks
123** and will therefore not honor them.
124**
125**
126** The writer thread:
127**
128** The async.writerMutex is used to make sure only there is only
129** a single writer thread running at a time.
130**
131** Inside the writer thread is a loop that works like this:
132**
133** WHILE (write-op list is not empty)
134** Do IO operation at head of write-op list
135** Remove entry from head of write-op list
136** END WHILE
137**
138** The async.queueMutex is always held during the <write-op list is
139** not empty> test, and when the entry is removed from the head
140** of the write-op list. Sometimes it is held for the interim
141** period (while the IO is performed), and sometimes it is
142** relinquished. It is relinquished if (a) the IO op is an
143** ASYNC_CLOSE or (b) when the file handle was opened, two of
144** the underlying systems handles were opened on the same
145** file-system entry.
146**
147** If condition (b) above is true, then one file-handle
148** (AsyncFile.pBaseRead) is used exclusively by sqlite threads to read the
149** file, the other (AsyncFile.pBaseWrite) by sqlite3_async_flush()
150** threads to perform write() operations. This means that read
151** operations are not blocked by asynchronous writes (although
152** asynchronous writes may still be blocked by reads).
153**
154** This assumes that the OS keeps two handles open on the same file
155** properly in sync. That is, any read operation that starts after a
156** write operation on the same file system entry has completed returns
157** data consistent with the write. We also assume that if one thread
158** reads a file while another is writing it all bytes other than the
159** ones actually being written contain valid data.
160**
161** If the above assumptions are not true, set the preprocessor symbol
162** SQLITE_ASYNC_TWO_FILEHANDLES to 0.
163*/
164
165
166#ifndef NDEBUG
167# define TESTONLY( X ) X
168#else
169# define TESTONLY( X )
170#endif
171
172/*
danielk1977debcfd22009-04-24 09:27:16 +0000173** PORTING FUNCTIONS
174**
danielk1977a3f06592009-04-23 14:58:39 +0000175** There are two definitions of the following functions. One for pthreads
176** compatible systems and one for Win32. These functions isolate the OS
177** specific code required by each platform.
178**
179** The system uses three mutexes and a single condition variable. To
180** block on a mutex, async_mutex_enter() is called. The parameter passed
181** to async_mutex_enter(), which must be one of ASYNC_MUTEX_LOCK,
182** ASYNC_MUTEX_QUEUE or ASYNC_MUTEX_WRITER, identifies which of the three
183** mutexes to lock. Similarly, to unlock a mutex, async_mutex_leave() is
184** called with a parameter identifying the mutex being unlocked. Mutexes
185** are not recursive - it is an error to call async_mutex_enter() to
186** lock a mutex that is already locked, or to call async_mutex_leave()
187** to unlock a mutex that is not currently locked.
188**
189** The async_cond_wait() and async_cond_signal() functions are modelled
190** on the pthreads functions with similar names. The first parameter to
191** both functions is always ASYNC_COND_QUEUE. When async_cond_wait()
192** is called the mutex identified by the second parameter must be held.
193** The mutex is unlocked, and the calling thread simultaneously begins
194** waiting for the condition variable to be signalled by another thread.
195** After another thread signals the condition variable, the calling
196** thread stops waiting, locks mutex eMutex and returns. The
197** async_cond_signal() function is used to signal the condition variable.
198** It is assumed that the mutex used by the thread calling async_cond_wait()
199** is held by the caller of async_cond_signal() (otherwise there would be
200** a race condition).
201**
202** It is guaranteed that no other thread will call async_cond_wait() when
203** there is already a thread waiting on the condition variable.
204**
205** The async_sched_yield() function is called to suggest to the operating
206** system that it would be a good time to shift the current thread off the
207** CPU. The system will still work if this function is not implemented
208** (it is not currently implemented for win32), but it might be marginally
209** more efficient if it is.
210*/
211static void async_mutex_enter(int eMutex);
212static void async_mutex_leave(int eMutex);
213static void async_cond_wait(int eCond, int eMutex);
214static void async_cond_signal(int eCond);
215static void async_sched_yield(void);
216
217/*
218** There are also two definitions of the following. async_os_initialize()
219** is called when the asynchronous VFS is first installed, and os_shutdown()
220** is called when it is uninstalled (from within sqlite3async_shutdown()).
221**
222** For pthreads builds, both of these functions are no-ops. For win32,
223** they provide an opportunity to initialize and finalize the required
224** mutex and condition variables.
225**
226** If async_os_initialize() returns other than zero, then the initialization
227** fails and SQLITE_ERROR is returned to the user.
228*/
229static int async_os_initialize(void);
230static void async_os_shutdown(void);
231
232/* Values for use as the 'eMutex' argument of the above functions. The
233** integer values assigned to these constants are important for assert()
234** statements that verify that mutexes are locked in the correct order.
235** Specifically, it is unsafe to try to lock mutex N while holding a lock
236** on mutex M if (M<=N).
237*/
238#define ASYNC_MUTEX_LOCK 0
239#define ASYNC_MUTEX_QUEUE 1
240#define ASYNC_MUTEX_WRITER 2
241
242/* Values for use as the 'eCond' argument of the above functions. */
243#define ASYNC_COND_QUEUE 0
244
245/*************************************************************************
246** Start of OS specific code.
247*/
248#if SQLITE_OS_WIN || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
249
shaneeb4ac062009-04-30 17:45:33 +0000250#include <windows.h>
251
danielk1977a3f06592009-04-23 14:58:39 +0000252/* The following block contains the win32 specific code. */
253
254#define mutex_held(X) (GetCurrentThreadId()==primitives.aHolder[X])
255
256static struct AsyncPrimitives {
257 int isInit;
258 DWORD aHolder[3];
259 CRITICAL_SECTION aMutex[3];
260 HANDLE aCond[1];
261} primitives = { 0 };
262
263static int async_os_initialize(void){
264 if( !primitives.isInit ){
265 primitives.aCond[0] = CreateEvent(NULL, TRUE, FALSE, 0);
266 if( primitives.aCond[0]==NULL ){
267 return 1;
268 }
269 InitializeCriticalSection(&primitives.aMutex[0]);
270 InitializeCriticalSection(&primitives.aMutex[1]);
271 InitializeCriticalSection(&primitives.aMutex[2]);
272 primitives.isInit = 1;
273 }
274 return 0;
275}
276static void async_os_shutdown(void){
277 if( primitives.isInit ){
278 DeleteCriticalSection(&primitives.aMutex[0]);
279 DeleteCriticalSection(&primitives.aMutex[1]);
280 DeleteCriticalSection(&primitives.aMutex[2]);
281 CloseHandle(primitives.aCond[0]);
282 primitives.isInit = 0;
283 }
284}
285
286/* The following block contains the Win32 specific code. */
287static void async_mutex_enter(int eMutex){
288 assert( eMutex==0 || eMutex==1 || eMutex==2 );
289 assert( eMutex!=2 || (!mutex_held(0) && !mutex_held(1) && !mutex_held(2)) );
290 assert( eMutex!=1 || (!mutex_held(0) && !mutex_held(1)) );
291 assert( eMutex!=0 || (!mutex_held(0)) );
292 EnterCriticalSection(&primitives.aMutex[eMutex]);
293 TESTONLY( primitives.aHolder[eMutex] = GetCurrentThreadId(); )
294}
295static void async_mutex_leave(int eMutex){
296 assert( eMutex==0 || eMutex==1 || eMutex==2 );
297 assert( mutex_held(eMutex) );
298 TESTONLY( primitives.aHolder[eMutex] = 0; )
299 LeaveCriticalSection(&primitives.aMutex[eMutex]);
300}
301static void async_cond_wait(int eCond, int eMutex){
302 ResetEvent(primitives.aCond[eCond]);
303 async_mutex_leave(eMutex);
304 WaitForSingleObject(primitives.aCond[eCond], INFINITE);
305 async_mutex_enter(eMutex);
306}
307static void async_cond_signal(int eCond){
308 assert( mutex_held(ASYNC_MUTEX_QUEUE) );
309 SetEvent(primitives.aCond[eCond]);
310}
311static void async_sched_yield(void){
shanea3628d12009-04-29 18:11:59 +0000312 Sleep(0);
danielk1977a3f06592009-04-23 14:58:39 +0000313}
314#else
315
316/* The following block contains the pthreads specific code. */
317#include <pthread.h>
318#include <sched.h>
319
320#define mutex_held(X) pthread_equal(primitives.aHolder[X], pthread_self())
321
322static int async_os_initialize(void) {return 0;}
323static void async_os_shutdown(void) {}
324
325static struct AsyncPrimitives {
326 pthread_mutex_t aMutex[3];
327 pthread_cond_t aCond[1];
328 pthread_t aHolder[3];
329} primitives = {
330 { PTHREAD_MUTEX_INITIALIZER,
331 PTHREAD_MUTEX_INITIALIZER,
332 PTHREAD_MUTEX_INITIALIZER
333 } , {
334 PTHREAD_COND_INITIALIZER
335 } , { 0, 0, 0 }
336};
337
338static void async_mutex_enter(int eMutex){
339 assert( eMutex==0 || eMutex==1 || eMutex==2 );
340 assert( eMutex!=2 || (!mutex_held(0) && !mutex_held(1) && !mutex_held(2)) );
341 assert( eMutex!=1 || (!mutex_held(0) && !mutex_held(1)) );
342 assert( eMutex!=0 || (!mutex_held(0)) );
343 pthread_mutex_lock(&primitives.aMutex[eMutex]);
344 TESTONLY( primitives.aHolder[eMutex] = pthread_self(); )
345}
346static void async_mutex_leave(int eMutex){
347 assert( eMutex==0 || eMutex==1 || eMutex==2 );
348 assert( mutex_held(eMutex) );
349 TESTONLY( primitives.aHolder[eMutex] = 0; )
350 pthread_mutex_unlock(&primitives.aMutex[eMutex]);
351}
352static void async_cond_wait(int eCond, int eMutex){
353 assert( eMutex==0 || eMutex==1 || eMutex==2 );
354 assert( mutex_held(eMutex) );
355 TESTONLY( primitives.aHolder[eMutex] = 0; )
356 pthread_cond_wait(&primitives.aCond[eCond], &primitives.aMutex[eMutex]);
357 TESTONLY( primitives.aHolder[eMutex] = pthread_self(); )
358}
359static void async_cond_signal(int eCond){
360 assert( mutex_held(ASYNC_MUTEX_QUEUE) );
361 pthread_cond_signal(&primitives.aCond[eCond]);
362}
363static void async_sched_yield(void){
364 sched_yield();
365}
366#endif
367/*
368** End of OS specific code.
369*************************************************************************/
370
371#define assert_mutex_is_held(X) assert( mutex_held(X) )
372
373
374#ifndef SQLITE_ASYNC_TWO_FILEHANDLES
375/* #define SQLITE_ASYNC_TWO_FILEHANDLES 0 */
376#define SQLITE_ASYNC_TWO_FILEHANDLES 1
377#endif
378
379/*
380** State information is held in the static variable "async" defined
381** as the following structure.
382**
383** Both async.ioError and async.nFile are protected by async.queueMutex.
384*/
385static struct TestAsyncStaticData {
386 AsyncWrite *pQueueFirst; /* Next write operation to be processed */
387 AsyncWrite *pQueueLast; /* Last write operation on the list */
388 AsyncLock *pLock; /* Linked list of all AsyncLock structures */
389 volatile int ioDelay; /* Extra delay between write operations */
390 volatile int eHalt; /* One of the SQLITEASYNC_HALT_XXX values */
danielk19774598b8e2009-04-24 10:13:05 +0000391 volatile int bLockFiles; /* Current value of "lockfiles" parameter */
danielk1977a3f06592009-04-23 14:58:39 +0000392 int ioError; /* True if an IO error has occurred */
393 int nFile; /* Number of open files (from sqlite pov) */
danielk19774598b8e2009-04-24 10:13:05 +0000394} async = { 0,0,0,0,0,1,0,0 };
danielk1977a3f06592009-04-23 14:58:39 +0000395
396/* Possible values of AsyncWrite.op */
397#define ASYNC_NOOP 0
398#define ASYNC_WRITE 1
399#define ASYNC_SYNC 2
400#define ASYNC_TRUNCATE 3
401#define ASYNC_CLOSE 4
402#define ASYNC_DELETE 5
403#define ASYNC_OPENEXCLUSIVE 6
404#define ASYNC_UNLOCK 7
405
406/* Names of opcodes. Used for debugging only.
407** Make sure these stay in sync with the macros above!
408*/
409static const char *azOpcodeName[] = {
410 "NOOP", "WRITE", "SYNC", "TRUNCATE", "CLOSE", "DELETE", "OPENEX", "UNLOCK"
411};
412
413/*
414** Entries on the write-op queue are instances of the AsyncWrite
415** structure, defined here.
416**
417** The interpretation of the iOffset and nByte variables varies depending
418** on the value of AsyncWrite.op:
419**
420** ASYNC_NOOP:
421** No values used.
422**
423** ASYNC_WRITE:
424** iOffset -> Offset in file to write to.
425** nByte -> Number of bytes of data to write (pointed to by zBuf).
426**
427** ASYNC_SYNC:
428** nByte -> flags to pass to sqlite3OsSync().
429**
430** ASYNC_TRUNCATE:
431** iOffset -> Size to truncate file to.
432** nByte -> Unused.
433**
434** ASYNC_CLOSE:
435** iOffset -> Unused.
436** nByte -> Unused.
437**
438** ASYNC_DELETE:
439** iOffset -> Contains the "syncDir" flag.
440** nByte -> Number of bytes of zBuf points to (file name).
441**
442** ASYNC_OPENEXCLUSIVE:
443** iOffset -> Value of "delflag".
444** nByte -> Number of bytes of zBuf points to (file name).
445**
446** ASYNC_UNLOCK:
447** nByte -> Argument to sqlite3OsUnlock().
448**
449**
450** For an ASYNC_WRITE operation, zBuf points to the data to write to the file.
451** This space is sqlite3_malloc()d along with the AsyncWrite structure in a
452** single blob, so is deleted when sqlite3_free() is called on the parent
453** structure.
454*/
455struct AsyncWrite {
456 AsyncFileData *pFileData; /* File to write data to or sync */
457 int op; /* One of ASYNC_xxx etc. */
458 sqlite_int64 iOffset; /* See above */
459 int nByte; /* See above */
460 char *zBuf; /* Data to write to file (or NULL if op!=ASYNC_WRITE) */
461 AsyncWrite *pNext; /* Next write operation (to any file) */
462};
463
464/*
465** An instance of this structure is created for each distinct open file
466** (i.e. if two handles are opened on the one file, only one of these
467** structures is allocated) and stored in the async.aLock hash table. The
468** keys for async.aLock are the full pathnames of the opened files.
469**
470** AsyncLock.pList points to the head of a linked list of AsyncFileLock
471** structures, one for each handle currently open on the file.
472**
473** If the opened file is not a main-database (the SQLITE_OPEN_MAIN_DB is
danielk19774598b8e2009-04-24 10:13:05 +0000474** not passed to the sqlite3OsOpen() call), or if async.bLockFiles is
475** false, variables AsyncLock.pFile and AsyncLock.eLock are never used.
476** Otherwise, pFile is a file handle opened on the file in question and
477** used to obtain the file-system locks required by database connections
478** within this process.
danielk1977a3f06592009-04-23 14:58:39 +0000479**
480** See comments above the asyncLock() function for more details on
481** the implementation of database locking used by this backend.
482*/
483struct AsyncLock {
484 char *zFile;
485 int nFile;
486 sqlite3_file *pFile;
487 int eLock;
488 AsyncFileLock *pList;
489 AsyncLock *pNext; /* Next in linked list headed by async.pLock */
490};
491
492/*
493** An instance of the following structure is allocated along with each
494** AsyncFileData structure (see AsyncFileData.lock), but is only used if the
495** file was opened with the SQLITE_OPEN_MAIN_DB.
496*/
497struct AsyncFileLock {
498 int eLock; /* Internally visible lock state (sqlite pov) */
499 int eAsyncLock; /* Lock-state with write-queue unlock */
500 AsyncFileLock *pNext;
501};
502
503/*
504** The AsyncFile structure is a subclass of sqlite3_file used for
505** asynchronous IO.
506**
507** All of the actual data for the structure is stored in the structure
508** pointed to by AsyncFile.pData, which is allocated as part of the
509** sqlite3OsOpen() using sqlite3_malloc(). The reason for this is that the
510** lifetime of the AsyncFile structure is ended by the caller after OsClose()
511** is called, but the data in AsyncFileData may be required by the
512** writer thread after that point.
513*/
514struct AsyncFile {
515 sqlite3_io_methods *pMethod;
516 AsyncFileData *pData;
517};
518struct AsyncFileData {
519 char *zName; /* Underlying OS filename - used for debugging */
520 int nName; /* Number of characters in zName */
521 sqlite3_file *pBaseRead; /* Read handle to the underlying Os file */
522 sqlite3_file *pBaseWrite; /* Write handle to the underlying Os file */
523 AsyncFileLock lock; /* Lock state for this handle */
524 AsyncLock *pLock; /* AsyncLock object for this file system entry */
525 AsyncWrite closeOp; /* Preallocated close operation */
526};
527
528/*
529** Add an entry to the end of the global write-op list. pWrite should point
530** to an AsyncWrite structure allocated using sqlite3_malloc(). The writer
531** thread will call sqlite3_free() to free the structure after the specified
532** operation has been completed.
533**
534** Once an AsyncWrite structure has been added to the list, it becomes the
535** property of the writer thread and must not be read or modified by the
536** caller.
537*/
538static void addAsyncWrite(AsyncWrite *pWrite){
539 /* We must hold the queue mutex in order to modify the queue pointers */
540 if( pWrite->op!=ASYNC_UNLOCK ){
541 async_mutex_enter(ASYNC_MUTEX_QUEUE);
542 }
543
544 /* Add the record to the end of the write-op queue */
545 assert( !pWrite->pNext );
546 if( async.pQueueLast ){
547 assert( async.pQueueFirst );
548 async.pQueueLast->pNext = pWrite;
549 }else{
550 async.pQueueFirst = pWrite;
551 }
552 async.pQueueLast = pWrite;
553 ASYNC_TRACE(("PUSH %p (%s %s %d)\n", pWrite, azOpcodeName[pWrite->op],
554 pWrite->pFileData ? pWrite->pFileData->zName : "-", pWrite->iOffset));
555
556 if( pWrite->op==ASYNC_CLOSE ){
557 async.nFile--;
558 }
559
560 /* The writer thread might have been idle because there was nothing
561 ** on the write-op queue for it to do. So wake it up. */
562 async_cond_signal(ASYNC_COND_QUEUE);
563
564 /* Drop the queue mutex */
565 if( pWrite->op!=ASYNC_UNLOCK ){
566 async_mutex_leave(ASYNC_MUTEX_QUEUE);
567 }
568}
569
570/*
571** Increment async.nFile in a thread-safe manner.
572*/
573static void incrOpenFileCount(void){
574 /* We must hold the queue mutex in order to modify async.nFile */
575 async_mutex_enter(ASYNC_MUTEX_QUEUE);
576 if( async.nFile==0 ){
577 async.ioError = SQLITE_OK;
578 }
579 async.nFile++;
580 async_mutex_leave(ASYNC_MUTEX_QUEUE);
581}
582
583/*
584** This is a utility function to allocate and populate a new AsyncWrite
585** structure and insert it (via addAsyncWrite() ) into the global list.
586*/
587static int addNewAsyncWrite(
588 AsyncFileData *pFileData,
589 int op,
590 sqlite3_int64 iOffset,
591 int nByte,
592 const char *zByte
593){
594 AsyncWrite *p;
595 if( op!=ASYNC_CLOSE && async.ioError ){
596 return async.ioError;
597 }
598 p = sqlite3_malloc(sizeof(AsyncWrite) + (zByte?nByte:0));
599 if( !p ){
600 /* The upper layer does not expect operations like OsWrite() to
601 ** return SQLITE_NOMEM. This is partly because under normal conditions
602 ** SQLite is required to do rollback without calling malloc(). So
603 ** if malloc() fails here, treat it as an I/O error. The above
604 ** layer knows how to handle that.
605 */
606 return SQLITE_IOERR;
607 }
608 p->op = op;
609 p->iOffset = iOffset;
610 p->nByte = nByte;
611 p->pFileData = pFileData;
612 p->pNext = 0;
613 if( zByte ){
614 p->zBuf = (char *)&p[1];
615 memcpy(p->zBuf, zByte, nByte);
616 }else{
617 p->zBuf = 0;
618 }
619 addAsyncWrite(p);
620 return SQLITE_OK;
621}
622
623/*
624** Close the file. This just adds an entry to the write-op list, the file is
625** not actually closed.
626*/
627static int asyncClose(sqlite3_file *pFile){
628 AsyncFileData *p = ((AsyncFile *)pFile)->pData;
629
630 /* Unlock the file, if it is locked */
631 async_mutex_enter(ASYNC_MUTEX_LOCK);
632 p->lock.eLock = 0;
633 async_mutex_leave(ASYNC_MUTEX_LOCK);
634
635 addAsyncWrite(&p->closeOp);
636 return SQLITE_OK;
637}
638
639/*
640** Implementation of sqlite3OsWrite() for asynchronous files. Instead of
641** writing to the underlying file, this function adds an entry to the end of
642** the global AsyncWrite list. Either SQLITE_OK or SQLITE_NOMEM may be
643** returned.
644*/
645static int asyncWrite(
646 sqlite3_file *pFile,
647 const void *pBuf,
648 int amt,
649 sqlite3_int64 iOff
650){
651 AsyncFileData *p = ((AsyncFile *)pFile)->pData;
652 return addNewAsyncWrite(p, ASYNC_WRITE, iOff, amt, pBuf);
653}
654
655/*
656** Read data from the file. First we read from the filesystem, then adjust
657** the contents of the buffer based on ASYNC_WRITE operations in the
658** write-op queue.
659**
660** This method holds the mutex from start to finish.
661*/
662static int asyncRead(
663 sqlite3_file *pFile,
664 void *zOut,
665 int iAmt,
666 sqlite3_int64 iOffset
667){
668 AsyncFileData *p = ((AsyncFile *)pFile)->pData;
669 int rc = SQLITE_OK;
670 sqlite3_int64 filesize;
danielk1977a3f06592009-04-23 14:58:39 +0000671 sqlite3_file *pBase = p->pBaseRead;
danfd3b2222009-10-19 07:50:25 +0000672 sqlite3_int64 iAmt64 = (sqlite3_int64)iAmt;
danielk1977a3f06592009-04-23 14:58:39 +0000673
674 /* Grab the write queue mutex for the duration of the call */
675 async_mutex_enter(ASYNC_MUTEX_QUEUE);
676
677 /* If an I/O error has previously occurred in this virtual file
678 ** system, then all subsequent operations fail.
679 */
680 if( async.ioError!=SQLITE_OK ){
681 rc = async.ioError;
682 goto asyncread_out;
683 }
684
685 if( pBase->pMethods ){
danfd3b2222009-10-19 07:50:25 +0000686 sqlite3_int64 nRead;
danielk1977a3f06592009-04-23 14:58:39 +0000687 rc = pBase->pMethods->xFileSize(pBase, &filesize);
688 if( rc!=SQLITE_OK ){
689 goto asyncread_out;
690 }
danfd3b2222009-10-19 07:50:25 +0000691 nRead = MIN(filesize - iOffset, iAmt64);
danielk1977a3f06592009-04-23 14:58:39 +0000692 if( nRead>0 ){
693 rc = pBase->pMethods->xRead(pBase, zOut, nRead, iOffset);
694 ASYNC_TRACE(("READ %s %d bytes at %d\n", p->zName, nRead, iOffset));
695 }
696 }
697
698 if( rc==SQLITE_OK ){
699 AsyncWrite *pWrite;
700 char *zName = p->zName;
701
702 for(pWrite=async.pQueueFirst; pWrite; pWrite = pWrite->pNext){
703 if( pWrite->op==ASYNC_WRITE && (
704 (pWrite->pFileData==p) ||
705 (zName && pWrite->pFileData->zName==zName)
706 )){
danfd3b2222009-10-19 07:50:25 +0000707 sqlite3_int64 nCopy;
708 sqlite3_int64 nByte64 = (sqlite3_int64)pWrite->nByte;
709
710 /* Set variable iBeginIn to the offset in buffer pWrite->zBuf[] from
711 ** which data should be copied. Set iBeginOut to the offset within
712 ** the output buffer to which data should be copied. If either of
713 ** these offsets is a negative number, set them to 0.
714 */
shanea3628d12009-04-29 18:11:59 +0000715 sqlite3_int64 iBeginOut = (pWrite->iOffset-iOffset);
716 sqlite3_int64 iBeginIn = -iBeginOut;
danielk1977a3f06592009-04-23 14:58:39 +0000717 if( iBeginIn<0 ) iBeginIn = 0;
718 if( iBeginOut<0 ) iBeginOut = 0;
danielk1977a3f06592009-04-23 14:58:39 +0000719
danfd3b2222009-10-19 07:50:25 +0000720 nCopy = MIN(nByte64-iBeginIn, iAmt64-iBeginOut);
danielk1977a3f06592009-04-23 14:58:39 +0000721 if( nCopy>0 ){
722 memcpy(&((char *)zOut)[iBeginOut], &pWrite->zBuf[iBeginIn], nCopy);
723 ASYNC_TRACE(("OVERREAD %d bytes at %d\n", nCopy, iBeginOut+iOffset));
724 }
725 }
726 }
727 }
728
729asyncread_out:
730 async_mutex_leave(ASYNC_MUTEX_QUEUE);
731 return rc;
732}
733
734/*
735** Truncate the file to nByte bytes in length. This just adds an entry to
736** the write-op list, no IO actually takes place.
737*/
738static int asyncTruncate(sqlite3_file *pFile, sqlite3_int64 nByte){
739 AsyncFileData *p = ((AsyncFile *)pFile)->pData;
740 return addNewAsyncWrite(p, ASYNC_TRUNCATE, nByte, 0, 0);
741}
742
743/*
744** Sync the file. This just adds an entry to the write-op list, the
745** sync() is done later by sqlite3_async_flush().
746*/
747static int asyncSync(sqlite3_file *pFile, int flags){
748 AsyncFileData *p = ((AsyncFile *)pFile)->pData;
749 return addNewAsyncWrite(p, ASYNC_SYNC, 0, flags, 0);
750}
751
752/*
753** Read the size of the file. First we read the size of the file system
754** entry, then adjust for any ASYNC_WRITE or ASYNC_TRUNCATE operations
755** currently in the write-op list.
756**
757** This method holds the mutex from start to finish.
758*/
759int asyncFileSize(sqlite3_file *pFile, sqlite3_int64 *piSize){
760 AsyncFileData *p = ((AsyncFile *)pFile)->pData;
761 int rc = SQLITE_OK;
762 sqlite3_int64 s = 0;
763 sqlite3_file *pBase;
764
765 async_mutex_enter(ASYNC_MUTEX_QUEUE);
766
shaneeb4ac062009-04-30 17:45:33 +0000767 /* Read the filesystem size from the base file. If pMethods is NULL, this
danielk1977a3f06592009-04-23 14:58:39 +0000768 ** means the file hasn't been opened yet. In this case all relevant data
769 ** must be in the write-op queue anyway, so we can omit reading from the
770 ** file-system.
771 */
772 pBase = p->pBaseRead;
773 if( pBase->pMethods ){
774 rc = pBase->pMethods->xFileSize(pBase, &s);
775 }
776
777 if( rc==SQLITE_OK ){
778 AsyncWrite *pWrite;
779 for(pWrite=async.pQueueFirst; pWrite; pWrite = pWrite->pNext){
780 if( pWrite->op==ASYNC_DELETE
781 && p->zName
782 && strcmp(p->zName, pWrite->zBuf)==0
783 ){
784 s = 0;
785 }else if( pWrite->pFileData && (
786 (pWrite->pFileData==p)
787 || (p->zName && pWrite->pFileData->zName==p->zName)
788 )){
789 switch( pWrite->op ){
790 case ASYNC_WRITE:
791 s = MAX(pWrite->iOffset + (sqlite3_int64)(pWrite->nByte), s);
792 break;
793 case ASYNC_TRUNCATE:
794 s = MIN(s, pWrite->iOffset);
795 break;
796 }
797 }
798 }
799 *piSize = s;
800 }
801 async_mutex_leave(ASYNC_MUTEX_QUEUE);
802 return rc;
803}
804
805/*
806** Lock or unlock the actual file-system entry.
807*/
808static int getFileLock(AsyncLock *pLock){
809 int rc = SQLITE_OK;
810 AsyncFileLock *pIter;
811 int eRequired = 0;
812
813 if( pLock->pFile ){
814 for(pIter=pLock->pList; pIter; pIter=pIter->pNext){
815 assert(pIter->eAsyncLock>=pIter->eLock);
816 if( pIter->eAsyncLock>eRequired ){
817 eRequired = pIter->eAsyncLock;
818 assert(eRequired>=0 && eRequired<=SQLITE_LOCK_EXCLUSIVE);
819 }
820 }
821
822 if( eRequired>pLock->eLock ){
823 rc = pLock->pFile->pMethods->xLock(pLock->pFile, eRequired);
824 if( rc==SQLITE_OK ){
825 pLock->eLock = eRequired;
826 }
827 }
828 else if( eRequired<pLock->eLock && eRequired<=SQLITE_LOCK_SHARED ){
829 rc = pLock->pFile->pMethods->xUnlock(pLock->pFile, eRequired);
830 if( rc==SQLITE_OK ){
831 pLock->eLock = eRequired;
832 }
833 }
834 }
835
836 return rc;
837}
838
839/*
840** Return the AsyncLock structure from the global async.pLock list
841** associated with the file-system entry identified by path zName
842** (a string of nName bytes). If no such structure exists, return 0.
843*/
844static AsyncLock *findLock(const char *zName, int nName){
845 AsyncLock *p = async.pLock;
846 while( p && (p->nFile!=nName || memcmp(p->zFile, zName, nName)) ){
847 p = p->pNext;
848 }
849 return p;
850}
851
852/*
853** The following two methods - asyncLock() and asyncUnlock() - are used
854** to obtain and release locks on database files opened with the
855** asynchronous backend.
856*/
857static int asyncLock(sqlite3_file *pFile, int eLock){
858 int rc = SQLITE_OK;
859 AsyncFileData *p = ((AsyncFile *)pFile)->pData;
860
861 if( p->zName ){
862 async_mutex_enter(ASYNC_MUTEX_LOCK);
863 if( p->lock.eLock<eLock ){
864 AsyncLock *pLock = p->pLock;
865 AsyncFileLock *pIter;
866 assert(pLock && pLock->pList);
867 for(pIter=pLock->pList; pIter; pIter=pIter->pNext){
868 if( pIter!=&p->lock && (
869 (eLock==SQLITE_LOCK_EXCLUSIVE && pIter->eLock>=SQLITE_LOCK_SHARED) ||
870 (eLock==SQLITE_LOCK_PENDING && pIter->eLock>=SQLITE_LOCK_RESERVED) ||
871 (eLock==SQLITE_LOCK_RESERVED && pIter->eLock>=SQLITE_LOCK_RESERVED) ||
872 (eLock==SQLITE_LOCK_SHARED && pIter->eLock>=SQLITE_LOCK_PENDING)
873 )){
874 rc = SQLITE_BUSY;
875 }
876 }
877 if( rc==SQLITE_OK ){
878 p->lock.eLock = eLock;
879 p->lock.eAsyncLock = MAX(p->lock.eAsyncLock, eLock);
880 }
881 assert(p->lock.eAsyncLock>=p->lock.eLock);
882 if( rc==SQLITE_OK ){
883 rc = getFileLock(pLock);
884 }
885 }
886 async_mutex_leave(ASYNC_MUTEX_LOCK);
887 }
888
889 ASYNC_TRACE(("LOCK %d (%s) rc=%d\n", eLock, p->zName, rc));
890 return rc;
891}
892static int asyncUnlock(sqlite3_file *pFile, int eLock){
893 int rc = SQLITE_OK;
894 AsyncFileData *p = ((AsyncFile *)pFile)->pData;
895 if( p->zName ){
896 AsyncFileLock *pLock = &p->lock;
897 async_mutex_enter(ASYNC_MUTEX_QUEUE);
898 async_mutex_enter(ASYNC_MUTEX_LOCK);
899 pLock->eLock = MIN(pLock->eLock, eLock);
900 rc = addNewAsyncWrite(p, ASYNC_UNLOCK, 0, eLock, 0);
901 async_mutex_leave(ASYNC_MUTEX_LOCK);
902 async_mutex_leave(ASYNC_MUTEX_QUEUE);
903 }
904 return rc;
905}
906
907/*
908** This function is called when the pager layer first opens a database file
909** and is checking for a hot-journal.
910*/
911static int asyncCheckReservedLock(sqlite3_file *pFile, int *pResOut){
912 int ret = 0;
913 AsyncFileLock *pIter;
914 AsyncFileData *p = ((AsyncFile *)pFile)->pData;
915
916 async_mutex_enter(ASYNC_MUTEX_LOCK);
917 for(pIter=p->pLock->pList; pIter; pIter=pIter->pNext){
918 if( pIter->eLock>=SQLITE_LOCK_RESERVED ){
919 ret = 1;
shanea3628d12009-04-29 18:11:59 +0000920 break;
danielk1977a3f06592009-04-23 14:58:39 +0000921 }
922 }
923 async_mutex_leave(ASYNC_MUTEX_LOCK);
924
925 ASYNC_TRACE(("CHECK-LOCK %d (%s)\n", ret, p->zName));
926 *pResOut = ret;
927 return SQLITE_OK;
928}
929
930/*
931** sqlite3_file_control() implementation.
932*/
933static int asyncFileControl(sqlite3_file *id, int op, void *pArg){
934 switch( op ){
935 case SQLITE_FCNTL_LOCKSTATE: {
936 async_mutex_enter(ASYNC_MUTEX_LOCK);
937 *(int*)pArg = ((AsyncFile*)id)->pData->lock.eLock;
938 async_mutex_leave(ASYNC_MUTEX_LOCK);
939 return SQLITE_OK;
940 }
941 }
942 return SQLITE_ERROR;
943}
944
945/*
946** Return the device characteristics and sector-size of the device. It
shanea3628d12009-04-29 18:11:59 +0000947** is tricky to implement these correctly, as this backend might
danielk1977a3f06592009-04-23 14:58:39 +0000948** not have an open file handle at this point.
949*/
950static int asyncSectorSize(sqlite3_file *pFile){
shanea3628d12009-04-29 18:11:59 +0000951 UNUSED_PARAMETER(pFile);
danielk1977a3f06592009-04-23 14:58:39 +0000952 return 512;
953}
954static int asyncDeviceCharacteristics(sqlite3_file *pFile){
shanea3628d12009-04-29 18:11:59 +0000955 UNUSED_PARAMETER(pFile);
danielk1977a3f06592009-04-23 14:58:39 +0000956 return 0;
957}
958
959static int unlinkAsyncFile(AsyncFileData *pData){
960 AsyncFileLock **ppIter;
961 int rc = SQLITE_OK;
962
963 if( pData->zName ){
964 AsyncLock *pLock = pData->pLock;
965 for(ppIter=&pLock->pList; *ppIter; ppIter=&((*ppIter)->pNext)){
966 if( (*ppIter)==&pData->lock ){
967 *ppIter = pData->lock.pNext;
968 break;
969 }
970 }
971 if( !pLock->pList ){
972 AsyncLock **pp;
973 if( pLock->pFile ){
974 pLock->pFile->pMethods->xClose(pLock->pFile);
975 }
976 for(pp=&async.pLock; *pp!=pLock; pp=&((*pp)->pNext));
977 *pp = pLock->pNext;
978 sqlite3_free(pLock);
979 }else{
980 rc = getFileLock(pLock);
981 }
982 }
983
984 return rc;
985}
986
987/*
988** The parameter passed to this function is a copy of a 'flags' parameter
989** passed to this modules xOpen() method. This function returns true
990** if the file should be opened asynchronously, or false if it should
991** be opened immediately.
992**
993** If the file is to be opened asynchronously, then asyncOpen() will add
994** an entry to the event queue and the file will not actually be opened
995** until the event is processed. Otherwise, the file is opened directly
996** by the caller.
997*/
998static int doAsynchronousOpen(int flags){
999 return (flags&SQLITE_OPEN_CREATE) && (
1000 (flags&SQLITE_OPEN_MAIN_JOURNAL) ||
1001 (flags&SQLITE_OPEN_TEMP_JOURNAL) ||
1002 (flags&SQLITE_OPEN_DELETEONCLOSE)
1003 );
1004}
1005
1006/*
1007** Open a file.
1008*/
1009static int asyncOpen(
1010 sqlite3_vfs *pAsyncVfs,
1011 const char *zName,
1012 sqlite3_file *pFile,
1013 int flags,
1014 int *pOutFlags
1015){
1016 static sqlite3_io_methods async_methods = {
1017 1, /* iVersion */
1018 asyncClose, /* xClose */
1019 asyncRead, /* xRead */
1020 asyncWrite, /* xWrite */
1021 asyncTruncate, /* xTruncate */
1022 asyncSync, /* xSync */
1023 asyncFileSize, /* xFileSize */
1024 asyncLock, /* xLock */
1025 asyncUnlock, /* xUnlock */
1026 asyncCheckReservedLock, /* xCheckReservedLock */
1027 asyncFileControl, /* xFileControl */
1028 asyncSectorSize, /* xSectorSize */
1029 asyncDeviceCharacteristics /* xDeviceCharacteristics */
1030 };
1031
1032 sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1033 AsyncFile *p = (AsyncFile *)pFile;
1034 int nName = 0;
1035 int rc = SQLITE_OK;
1036 int nByte;
1037 AsyncFileData *pData;
1038 AsyncLock *pLock = 0;
1039 char *z;
1040 int isAsyncOpen = doAsynchronousOpen(flags);
1041
1042 /* If zName is NULL, then the upper layer is requesting an anonymous file */
1043 if( zName ){
shanea3628d12009-04-29 18:11:59 +00001044 nName = (int)strlen(zName)+1;
danielk1977a3f06592009-04-23 14:58:39 +00001045 }
1046
1047 nByte = (
1048 sizeof(AsyncFileData) + /* AsyncFileData structure */
1049 2 * pVfs->szOsFile + /* AsyncFileData.pBaseRead and pBaseWrite */
1050 nName /* AsyncFileData.zName */
1051 );
1052 z = sqlite3_malloc(nByte);
1053 if( !z ){
1054 return SQLITE_NOMEM;
1055 }
1056 memset(z, 0, nByte);
1057 pData = (AsyncFileData*)z;
1058 z += sizeof(pData[0]);
1059 pData->pBaseRead = (sqlite3_file*)z;
1060 z += pVfs->szOsFile;
1061 pData->pBaseWrite = (sqlite3_file*)z;
1062 pData->closeOp.pFileData = pData;
1063 pData->closeOp.op = ASYNC_CLOSE;
1064
1065 if( zName ){
1066 z += pVfs->szOsFile;
1067 pData->zName = z;
1068 pData->nName = nName;
1069 memcpy(pData->zName, zName, nName);
1070 }
1071
1072 if( !isAsyncOpen ){
1073 int flagsout;
1074 rc = pVfs->xOpen(pVfs, pData->zName, pData->pBaseRead, flags, &flagsout);
danielk19775368f292009-07-18 11:52:04 +00001075 if( rc==SQLITE_OK
1076 && (flagsout&SQLITE_OPEN_READWRITE)
1077 && (flags&SQLITE_OPEN_EXCLUSIVE)==0
1078 ){
danielk1977a3f06592009-04-23 14:58:39 +00001079 rc = pVfs->xOpen(pVfs, pData->zName, pData->pBaseWrite, flags, 0);
1080 }
1081 if( pOutFlags ){
1082 *pOutFlags = flagsout;
1083 }
1084 }
1085
1086 async_mutex_enter(ASYNC_MUTEX_LOCK);
1087
1088 if( zName && rc==SQLITE_OK ){
1089 pLock = findLock(pData->zName, pData->nName);
1090 if( !pLock ){
1091 int nByte = pVfs->szOsFile + sizeof(AsyncLock) + pData->nName + 1;
1092 pLock = (AsyncLock *)sqlite3_malloc(nByte);
1093 if( pLock ){
1094 memset(pLock, 0, nByte);
danielk19774598b8e2009-04-24 10:13:05 +00001095 if( async.bLockFiles && (flags&SQLITE_OPEN_MAIN_DB) ){
danielk1977a3f06592009-04-23 14:58:39 +00001096 pLock->pFile = (sqlite3_file *)&pLock[1];
1097 rc = pVfs->xOpen(pVfs, pData->zName, pLock->pFile, flags, 0);
1098 if( rc!=SQLITE_OK ){
1099 sqlite3_free(pLock);
1100 pLock = 0;
1101 }
1102 }
danielk1977a3f06592009-04-23 14:58:39 +00001103 if( pLock ){
1104 pLock->nFile = pData->nName;
1105 pLock->zFile = &((char *)(&pLock[1]))[pVfs->szOsFile];
1106 memcpy(pLock->zFile, pData->zName, pLock->nFile);
1107 pLock->pNext = async.pLock;
1108 async.pLock = pLock;
1109 }
1110 }else{
1111 rc = SQLITE_NOMEM;
1112 }
1113 }
1114 }
1115
1116 if( rc==SQLITE_OK ){
1117 p->pMethod = &async_methods;
1118 p->pData = pData;
1119
1120 /* Link AsyncFileData.lock into the linked list of
1121 ** AsyncFileLock structures for this file.
1122 */
1123 if( zName ){
1124 pData->lock.pNext = pLock->pList;
1125 pLock->pList = &pData->lock;
1126 pData->zName = pLock->zFile;
1127 }
1128 }else{
1129 if( pData->pBaseRead->pMethods ){
1130 pData->pBaseRead->pMethods->xClose(pData->pBaseRead);
1131 }
1132 if( pData->pBaseWrite->pMethods ){
1133 pData->pBaseWrite->pMethods->xClose(pData->pBaseWrite);
1134 }
1135 sqlite3_free(pData);
1136 }
1137
1138 async_mutex_leave(ASYNC_MUTEX_LOCK);
1139
1140 if( rc==SQLITE_OK ){
1141 incrOpenFileCount();
1142 pData->pLock = pLock;
1143 }
1144
1145 if( rc==SQLITE_OK && isAsyncOpen ){
1146 rc = addNewAsyncWrite(pData, ASYNC_OPENEXCLUSIVE, (sqlite3_int64)flags,0,0);
1147 if( rc==SQLITE_OK ){
1148 if( pOutFlags ) *pOutFlags = flags;
1149 }else{
1150 async_mutex_enter(ASYNC_MUTEX_LOCK);
1151 unlinkAsyncFile(pData);
1152 async_mutex_leave(ASYNC_MUTEX_LOCK);
1153 sqlite3_free(pData);
1154 }
1155 }
1156 if( rc!=SQLITE_OK ){
1157 p->pMethod = 0;
1158 }
1159 return rc;
1160}
1161
1162/*
1163** Implementation of sqlite3OsDelete. Add an entry to the end of the
1164** write-op queue to perform the delete.
1165*/
1166static int asyncDelete(sqlite3_vfs *pAsyncVfs, const char *z, int syncDir){
shanea3628d12009-04-29 18:11:59 +00001167 UNUSED_PARAMETER(pAsyncVfs);
1168 return addNewAsyncWrite(0, ASYNC_DELETE, syncDir, (int)strlen(z)+1, z);
danielk1977a3f06592009-04-23 14:58:39 +00001169}
1170
1171/*
1172** Implementation of sqlite3OsAccess. This method holds the mutex from
1173** start to finish.
1174*/
1175static int asyncAccess(
1176 sqlite3_vfs *pAsyncVfs,
1177 const char *zName,
1178 int flags,
1179 int *pResOut
1180){
1181 int rc;
1182 int ret;
1183 AsyncWrite *p;
1184 sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1185
1186 assert(flags==SQLITE_ACCESS_READWRITE
1187 || flags==SQLITE_ACCESS_READ
1188 || flags==SQLITE_ACCESS_EXISTS
1189 );
1190
1191 async_mutex_enter(ASYNC_MUTEX_QUEUE);
1192 rc = pVfs->xAccess(pVfs, zName, flags, &ret);
1193 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
1194 for(p=async.pQueueFirst; p; p = p->pNext){
1195 if( p->op==ASYNC_DELETE && 0==strcmp(p->zBuf, zName) ){
1196 ret = 0;
1197 }else if( p->op==ASYNC_OPENEXCLUSIVE
1198 && p->pFileData->zName
1199 && 0==strcmp(p->pFileData->zName, zName)
1200 ){
1201 ret = 1;
1202 }
1203 }
1204 }
1205 ASYNC_TRACE(("ACCESS(%s): %s = %d\n",
1206 flags==SQLITE_ACCESS_READWRITE?"read-write":
1207 flags==SQLITE_ACCESS_READ?"read":"exists"
1208 , zName, ret)
1209 );
1210 async_mutex_leave(ASYNC_MUTEX_QUEUE);
1211 *pResOut = ret;
1212 return rc;
1213}
1214
1215/*
1216** Fill in zPathOut with the full path to the file identified by zPath.
1217*/
1218static int asyncFullPathname(
1219 sqlite3_vfs *pAsyncVfs,
1220 const char *zPath,
1221 int nPathOut,
1222 char *zPathOut
1223){
1224 int rc;
1225 sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1226 rc = pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
1227
1228 /* Because of the way intra-process file locking works, this backend
1229 ** needs to return a canonical path. The following block assumes the
1230 ** file-system uses unix style paths.
1231 */
1232 if( rc==SQLITE_OK ){
1233 int i, j;
danielk1977a3f06592009-04-23 14:58:39 +00001234 char *z = zPathOut;
dan19125aa2009-12-02 18:16:56 +00001235 int n = strlen(z);
danielk1977a3f06592009-04-23 14:58:39 +00001236 while( n>1 && z[n-1]=='/' ){ n--; }
1237 for(i=j=0; i<n; i++){
1238 if( z[i]=='/' ){
1239 if( z[i+1]=='/' ) continue;
1240 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
1241 i += 1;
1242 continue;
1243 }
1244 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
1245 while( j>0 && z[j-1]!='/' ){ j--; }
1246 if( j>0 ){ j--; }
1247 i += 2;
1248 continue;
1249 }
1250 }
1251 z[j++] = z[i];
1252 }
1253 z[j] = 0;
1254 }
1255
1256 return rc;
1257}
1258static void *asyncDlOpen(sqlite3_vfs *pAsyncVfs, const char *zPath){
1259 sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1260 return pVfs->xDlOpen(pVfs, zPath);
1261}
1262static void asyncDlError(sqlite3_vfs *pAsyncVfs, int nByte, char *zErrMsg){
1263 sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1264 pVfs->xDlError(pVfs, nByte, zErrMsg);
1265}
1266static void (*asyncDlSym(
1267 sqlite3_vfs *pAsyncVfs,
1268 void *pHandle,
1269 const char *zSymbol
1270))(void){
1271 sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1272 return pVfs->xDlSym(pVfs, pHandle, zSymbol);
1273}
1274static void asyncDlClose(sqlite3_vfs *pAsyncVfs, void *pHandle){
1275 sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1276 pVfs->xDlClose(pVfs, pHandle);
1277}
1278static int asyncRandomness(sqlite3_vfs *pAsyncVfs, int nByte, char *zBufOut){
1279 sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1280 return pVfs->xRandomness(pVfs, nByte, zBufOut);
1281}
1282static int asyncSleep(sqlite3_vfs *pAsyncVfs, int nMicro){
1283 sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1284 return pVfs->xSleep(pVfs, nMicro);
1285}
1286static int asyncCurrentTime(sqlite3_vfs *pAsyncVfs, double *pTimeOut){
1287 sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1288 return pVfs->xCurrentTime(pVfs, pTimeOut);
1289}
1290
1291static sqlite3_vfs async_vfs = {
1292 1, /* iVersion */
1293 sizeof(AsyncFile), /* szOsFile */
1294 0, /* mxPathname */
1295 0, /* pNext */
1296 SQLITEASYNC_VFSNAME, /* zName */
1297 0, /* pAppData */
1298 asyncOpen, /* xOpen */
1299 asyncDelete, /* xDelete */
1300 asyncAccess, /* xAccess */
1301 asyncFullPathname, /* xFullPathname */
1302 asyncDlOpen, /* xDlOpen */
1303 asyncDlError, /* xDlError */
1304 asyncDlSym, /* xDlSym */
1305 asyncDlClose, /* xDlClose */
1306 asyncRandomness, /* xDlError */
1307 asyncSleep, /* xDlSym */
1308 asyncCurrentTime /* xDlClose */
1309};
1310
1311/*
1312** This procedure runs in a separate thread, reading messages off of the
1313** write queue and processing them one by one.
1314**
1315** If async.writerHaltNow is true, then this procedure exits
1316** after processing a single message.
1317**
1318** If async.writerHaltWhenIdle is true, then this procedure exits when
1319** the write queue is empty.
1320**
1321** If both of the above variables are false, this procedure runs
1322** indefinately, waiting for operations to be added to the write queue
1323** and processing them in the order in which they arrive.
1324**
1325** An artifical delay of async.ioDelay milliseconds is inserted before
1326** each write operation in order to simulate the effect of a slow disk.
1327**
1328** Only one instance of this procedure may be running at a time.
1329*/
1330static void asyncWriterThread(void){
1331 sqlite3_vfs *pVfs = (sqlite3_vfs *)(async_vfs.pAppData);
1332 AsyncWrite *p = 0;
1333 int rc = SQLITE_OK;
1334 int holdingMutex = 0;
1335
1336 async_mutex_enter(ASYNC_MUTEX_WRITER);
1337
1338 while( async.eHalt!=SQLITEASYNC_HALT_NOW ){
1339 int doNotFree = 0;
1340 sqlite3_file *pBase = 0;
1341
1342 if( !holdingMutex ){
1343 async_mutex_enter(ASYNC_MUTEX_QUEUE);
1344 }
1345 while( (p = async.pQueueFirst)==0 ){
1346 if( async.eHalt!=SQLITEASYNC_HALT_NEVER ){
1347 async_mutex_leave(ASYNC_MUTEX_QUEUE);
1348 break;
1349 }else{
1350 ASYNC_TRACE(("IDLE\n"));
1351 async_cond_wait(ASYNC_COND_QUEUE, ASYNC_MUTEX_QUEUE);
1352 ASYNC_TRACE(("WAKEUP\n"));
1353 }
1354 }
1355 if( p==0 ) break;
1356 holdingMutex = 1;
1357
1358 /* Right now this thread is holding the mutex on the write-op queue.
1359 ** Variable 'p' points to the first entry in the write-op queue. In
1360 ** the general case, we hold on to the mutex for the entire body of
1361 ** the loop.
1362 **
1363 ** However in the cases enumerated below, we relinquish the mutex,
1364 ** perform the IO, and then re-request the mutex before removing 'p' from
1365 ** the head of the write-op queue. The idea is to increase concurrency with
1366 ** sqlite threads.
1367 **
1368 ** * An ASYNC_CLOSE operation.
1369 ** * An ASYNC_OPENEXCLUSIVE operation. For this one, we relinquish
1370 ** the mutex, call the underlying xOpenExclusive() function, then
1371 ** re-aquire the mutex before seting the AsyncFile.pBaseRead
1372 ** variable.
1373 ** * ASYNC_SYNC and ASYNC_WRITE operations, if
1374 ** SQLITE_ASYNC_TWO_FILEHANDLES was set at compile time and two
1375 ** file-handles are open for the particular file being "synced".
1376 */
1377 if( async.ioError!=SQLITE_OK && p->op!=ASYNC_CLOSE ){
1378 p->op = ASYNC_NOOP;
1379 }
1380 if( p->pFileData ){
1381 pBase = p->pFileData->pBaseWrite;
1382 if(
1383 p->op==ASYNC_CLOSE ||
1384 p->op==ASYNC_OPENEXCLUSIVE ||
1385 (pBase->pMethods && (p->op==ASYNC_SYNC || p->op==ASYNC_WRITE) )
1386 ){
1387 async_mutex_leave(ASYNC_MUTEX_QUEUE);
1388 holdingMutex = 0;
1389 }
1390 if( !pBase->pMethods ){
1391 pBase = p->pFileData->pBaseRead;
1392 }
1393 }
1394
1395 switch( p->op ){
1396 case ASYNC_NOOP:
1397 break;
1398
1399 case ASYNC_WRITE:
1400 assert( pBase );
1401 ASYNC_TRACE(("WRITE %s %d bytes at %d\n",
1402 p->pFileData->zName, p->nByte, p->iOffset));
1403 rc = pBase->pMethods->xWrite(pBase, (void *)(p->zBuf), p->nByte, p->iOffset);
1404 break;
1405
1406 case ASYNC_SYNC:
1407 assert( pBase );
1408 ASYNC_TRACE(("SYNC %s\n", p->pFileData->zName));
1409 rc = pBase->pMethods->xSync(pBase, p->nByte);
1410 break;
1411
1412 case ASYNC_TRUNCATE:
1413 assert( pBase );
1414 ASYNC_TRACE(("TRUNCATE %s to %d bytes\n",
1415 p->pFileData->zName, p->iOffset));
1416 rc = pBase->pMethods->xTruncate(pBase, p->iOffset);
1417 break;
1418
1419 case ASYNC_CLOSE: {
1420 AsyncFileData *pData = p->pFileData;
1421 ASYNC_TRACE(("CLOSE %s\n", p->pFileData->zName));
1422 if( pData->pBaseWrite->pMethods ){
1423 pData->pBaseWrite->pMethods->xClose(pData->pBaseWrite);
1424 }
1425 if( pData->pBaseRead->pMethods ){
1426 pData->pBaseRead->pMethods->xClose(pData->pBaseRead);
1427 }
1428
1429 /* Unlink AsyncFileData.lock from the linked list of AsyncFileLock
1430 ** structures for this file. Obtain the async.lockMutex mutex
1431 ** before doing so.
1432 */
1433 async_mutex_enter(ASYNC_MUTEX_LOCK);
1434 rc = unlinkAsyncFile(pData);
1435 async_mutex_leave(ASYNC_MUTEX_LOCK);
1436
1437 if( !holdingMutex ){
1438 async_mutex_enter(ASYNC_MUTEX_QUEUE);
1439 holdingMutex = 1;
1440 }
1441 assert_mutex_is_held(ASYNC_MUTEX_QUEUE);
1442 async.pQueueFirst = p->pNext;
1443 sqlite3_free(pData);
1444 doNotFree = 1;
1445 break;
1446 }
1447
1448 case ASYNC_UNLOCK: {
1449 AsyncWrite *pIter;
1450 AsyncFileData *pData = p->pFileData;
1451 int eLock = p->nByte;
1452
1453 /* When a file is locked by SQLite using the async backend, it is
1454 ** locked within the 'real' file-system synchronously. When it is
1455 ** unlocked, an ASYNC_UNLOCK event is added to the write-queue to
1456 ** unlock the file asynchronously. The design of the async backend
1457 ** requires that the 'real' file-system file be locked from the
1458 ** time that SQLite first locks it (and probably reads from it)
1459 ** until all asynchronous write events that were scheduled before
1460 ** SQLite unlocked the file have been processed.
1461 **
1462 ** This is more complex if SQLite locks and unlocks the file multiple
1463 ** times in quick succession. For example, if SQLite does:
1464 **
1465 ** lock, write, unlock, lock, write, unlock
1466 **
1467 ** Each "lock" operation locks the file immediately. Each "write"
1468 ** and "unlock" operation adds an event to the event queue. If the
1469 ** second "lock" operation is performed before the first "unlock"
1470 ** operation has been processed asynchronously, then the first
1471 ** "unlock" cannot be safely processed as is, since this would mean
1472 ** the file was unlocked when the second "write" operation is
1473 ** processed. To work around this, when processing an ASYNC_UNLOCK
1474 ** operation, SQLite:
1475 **
1476 ** 1) Unlocks the file to the minimum of the argument passed to
1477 ** the xUnlock() call and the current lock from SQLite's point
1478 ** of view, and
1479 **
1480 ** 2) Only unlocks the file at all if this event is the last
1481 ** ASYNC_UNLOCK event on this file in the write-queue.
1482 */
1483 assert( holdingMutex==1 );
1484 assert( async.pQueueFirst==p );
1485 for(pIter=async.pQueueFirst->pNext; pIter; pIter=pIter->pNext){
1486 if( pIter->pFileData==pData && pIter->op==ASYNC_UNLOCK ) break;
1487 }
1488 if( !pIter ){
1489 async_mutex_enter(ASYNC_MUTEX_LOCK);
1490 pData->lock.eAsyncLock = MIN(
1491 pData->lock.eAsyncLock, MAX(pData->lock.eLock, eLock)
1492 );
1493 assert(pData->lock.eAsyncLock>=pData->lock.eLock);
1494 rc = getFileLock(pData->pLock);
1495 async_mutex_leave(ASYNC_MUTEX_LOCK);
1496 }
1497 break;
1498 }
1499
1500 case ASYNC_DELETE:
1501 ASYNC_TRACE(("DELETE %s\n", p->zBuf));
1502 rc = pVfs->xDelete(pVfs, p->zBuf, (int)p->iOffset);
1503 break;
1504
1505 case ASYNC_OPENEXCLUSIVE: {
1506 int flags = (int)p->iOffset;
1507 AsyncFileData *pData = p->pFileData;
1508 ASYNC_TRACE(("OPEN %s flags=%d\n", p->zBuf, (int)p->iOffset));
1509 assert(pData->pBaseRead->pMethods==0 && pData->pBaseWrite->pMethods==0);
1510 rc = pVfs->xOpen(pVfs, pData->zName, pData->pBaseRead, flags, 0);
1511 assert( holdingMutex==0 );
1512 async_mutex_enter(ASYNC_MUTEX_QUEUE);
1513 holdingMutex = 1;
1514 break;
1515 }
1516
1517 default: assert(!"Illegal value for AsyncWrite.op");
1518 }
1519
1520 /* If we didn't hang on to the mutex during the IO op, obtain it now
1521 ** so that the AsyncWrite structure can be safely removed from the
1522 ** global write-op queue.
1523 */
1524 if( !holdingMutex ){
1525 async_mutex_enter(ASYNC_MUTEX_QUEUE);
1526 holdingMutex = 1;
1527 }
1528 /* ASYNC_TRACE(("UNLINK %p\n", p)); */
1529 if( p==async.pQueueLast ){
1530 async.pQueueLast = 0;
1531 }
1532 if( !doNotFree ){
1533 assert_mutex_is_held(ASYNC_MUTEX_QUEUE);
1534 async.pQueueFirst = p->pNext;
1535 sqlite3_free(p);
1536 }
1537 assert( holdingMutex );
1538
1539 /* An IO error has occurred. We cannot report the error back to the
1540 ** connection that requested the I/O since the error happened
1541 ** asynchronously. The connection has already moved on. There
1542 ** really is nobody to report the error to.
1543 **
1544 ** The file for which the error occurred may have been a database or
1545 ** journal file. Regardless, none of the currently queued operations
1546 ** associated with the same database should now be performed. Nor should
1547 ** any subsequently requested IO on either a database or journal file
1548 ** handle for the same database be accepted until the main database
1549 ** file handle has been closed and reopened.
1550 **
1551 ** Furthermore, no further IO should be queued or performed on any file
1552 ** handle associated with a database that may have been part of a
1553 ** multi-file transaction that included the database associated with
1554 ** the IO error (i.e. a database ATTACHed to the same handle at some
1555 ** point in time).
1556 */
1557 if( rc!=SQLITE_OK ){
1558 async.ioError = rc;
1559 }
1560
1561 if( async.ioError && !async.pQueueFirst ){
1562 async_mutex_enter(ASYNC_MUTEX_LOCK);
1563 if( 0==async.pLock ){
1564 async.ioError = SQLITE_OK;
1565 }
1566 async_mutex_leave(ASYNC_MUTEX_LOCK);
1567 }
1568
1569 /* Drop the queue mutex before continuing to the next write operation
1570 ** in order to give other threads a chance to work with the write queue.
1571 */
1572 if( !async.pQueueFirst || !async.ioError ){
1573 async_mutex_leave(ASYNC_MUTEX_QUEUE);
1574 holdingMutex = 0;
1575 if( async.ioDelay>0 ){
danielk19776f050aa2009-04-25 08:39:14 +00001576 pVfs->xSleep(pVfs, async.ioDelay*1000);
danielk1977a3f06592009-04-23 14:58:39 +00001577 }else{
1578 async_sched_yield();
1579 }
1580 }
1581 }
1582
1583 async_mutex_leave(ASYNC_MUTEX_WRITER);
1584 return;
1585}
1586
1587/*
1588** Install the asynchronous VFS.
1589*/
1590int sqlite3async_initialize(const char *zParent, int isDefault){
1591 int rc = SQLITE_OK;
1592 if( async_vfs.pAppData==0 ){
1593 sqlite3_vfs *pParent = sqlite3_vfs_find(zParent);
1594 if( !pParent || async_os_initialize() ){
1595 rc = SQLITE_ERROR;
1596 }else if( SQLITE_OK!=(rc = sqlite3_vfs_register(&async_vfs, isDefault)) ){
1597 async_os_shutdown();
1598 }else{
1599 async_vfs.pAppData = (void *)pParent;
1600 async_vfs.mxPathname = ((sqlite3_vfs *)async_vfs.pAppData)->mxPathname;
1601 }
1602 }
1603 return rc;
1604}
1605
1606/*
1607** Uninstall the asynchronous VFS.
1608*/
1609void sqlite3async_shutdown(void){
1610 if( async_vfs.pAppData ){
1611 async_os_shutdown();
1612 sqlite3_vfs_unregister((sqlite3_vfs *)&async_vfs);
1613 async_vfs.pAppData = 0;
1614 }
1615}
1616
1617/*
1618** Process events on the write-queue.
1619*/
1620void sqlite3async_run(void){
1621 asyncWriterThread();
1622}
1623
1624/*
1625** Control/configure the asynchronous IO system.
1626*/
1627int sqlite3async_control(int op, ...){
1628 va_list ap;
1629 va_start(ap, op);
1630 switch( op ){
1631 case SQLITEASYNC_HALT: {
1632 int eWhen = va_arg(ap, int);
1633 if( eWhen!=SQLITEASYNC_HALT_NEVER
1634 && eWhen!=SQLITEASYNC_HALT_NOW
1635 && eWhen!=SQLITEASYNC_HALT_IDLE
1636 ){
danielk19774598b8e2009-04-24 10:13:05 +00001637 return SQLITE_MISUSE;
danielk1977a3f06592009-04-23 14:58:39 +00001638 }
1639 async.eHalt = eWhen;
1640 async_mutex_enter(ASYNC_MUTEX_QUEUE);
1641 async_cond_signal(ASYNC_COND_QUEUE);
1642 async_mutex_leave(ASYNC_MUTEX_QUEUE);
1643 break;
1644 }
1645
1646 case SQLITEASYNC_DELAY: {
1647 int iDelay = va_arg(ap, int);
danielk19774598b8e2009-04-24 10:13:05 +00001648 if( iDelay<0 ){
1649 return SQLITE_MISUSE;
1650 }
danielk1977a3f06592009-04-23 14:58:39 +00001651 async.ioDelay = iDelay;
1652 break;
1653 }
danielk19774598b8e2009-04-24 10:13:05 +00001654
1655 case SQLITEASYNC_LOCKFILES: {
1656 int bLock = va_arg(ap, int);
1657 async_mutex_enter(ASYNC_MUTEX_QUEUE);
1658 if( async.nFile || async.pQueueFirst ){
1659 async_mutex_leave(ASYNC_MUTEX_QUEUE);
1660 return SQLITE_MISUSE;
1661 }
1662 async.bLockFiles = bLock;
1663 async_mutex_leave(ASYNC_MUTEX_QUEUE);
1664 break;
1665 }
danielk1977a3f06592009-04-23 14:58:39 +00001666
1667 case SQLITEASYNC_GET_HALT: {
1668 int *peWhen = va_arg(ap, int *);
1669 *peWhen = async.eHalt;
1670 break;
1671 }
1672 case SQLITEASYNC_GET_DELAY: {
1673 int *piDelay = va_arg(ap, int *);
1674 *piDelay = async.ioDelay;
1675 break;
1676 }
danielk19774598b8e2009-04-24 10:13:05 +00001677 case SQLITEASYNC_GET_LOCKFILES: {
1678 int *piDelay = va_arg(ap, int *);
1679 *piDelay = async.bLockFiles;
1680 break;
1681 }
danielk1977a3f06592009-04-23 14:58:39 +00001682
1683 default:
1684 return SQLITE_ERROR;
1685 }
1686 return SQLITE_OK;
1687}
1688
1689#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ASYNCIO) */
1690