blob: 3760583561145a9e4deb8ca461a8f00a5d8ca085 [file] [log] [blame]
drh23669402006-01-09 17:29:52 +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**
13** This file contains an example implementation of an asynchronous IO
drhfe0f75b2006-01-10 20:01:18 +000014** backend for SQLite.
15**
16** WHAT IS ASYNCHRONOUS I/O?
17**
18** With asynchronous I/O, write requests are handled by a separate thread
19** running in the background. This means that the thread that initiates
20** a database write does not have to wait for (sometimes slow) disk I/O
21** to occur. The write seems to happen very quickly, though in reality
22** it is happening at its usual slow pace in the background.
23**
24** Asynchronous I/O appears to give better responsiveness, but at a price.
25** You lose the Durable property. With the default I/O backend of SQLite,
26** once a write completes, you know that the information you wrote is
27** safely on disk. With the asynchronous I/O, this is no the case. If
28** your program crashes or if you take a power lose after the database
29** write but before the asynchronous write thread has completed, then the
30** database change might never make it to disk and the next user of the
31** database might not see your change.
32**
33** You lose Durability with asynchronous I/O, but you still retain the
34** other parts of ACID: Atomic, Consistent, and Isolated. Many
35** appliations get along fine without the Durablity.
36**
37** HOW IT WORKS
38**
39** Asynchronous I/O works by overloading the OS-layer disk I/O routines
40** with modified versions that store the data to be written in queue of
41** pending write operations. Look at the asyncEnable() subroutine to see
42** how overloading works. Six os-layer routines are overloaded:
43**
44** sqlite3OsOpenReadWrite;
45** sqlite3OsOpenReadOnly;
46** sqlite3OsOpenExclusive;
47** sqlite3OsDelete;
48** sqlite3OsFileExists;
49** sqlite3OsSyncDirectory;
50**
51** The original implementations of these routines are saved and are
52** used by the writer thread to do the real I/O. The substitute
53** implementations typically put the I/O operation on a queue
54** to be handled later by the writer thread, though read operations
55** must be handled right away, obviously.
56**
57** Asynchronous I/O is disabled by setting the os-layer interface routines
58** back to their original values.
59**
60** LIMITATIONS
61**
62** This demonstration code is deliberately kept simple in order to keep
63** the main ideas clear and easy to understand. Real applications that
64** want to do asynchronous I/O might want to add additional capabilities.
65** For example, in this demonstration if writes are happening at a steady
66** stream that exceeds the I/O capability of the background writer thread,
67** the queue of pending write operations will grow without bound until we
68** run out of memory. Users of this technique may want to keep track of
69** the quantity of pending writes and stop accepting new write requests
70** when the buffer gets to be too big.
drh23669402006-01-09 17:29:52 +000071*/
72
73#include "sqliteInt.h"
74#include "os.h"
75#include <tcl.h>
76
drhfe0f75b2006-01-10 20:01:18 +000077/* If the THREADSAFE macro is not set, assume that it is turned off. */
drh23669402006-01-09 17:29:52 +000078#ifndef THREADSAFE
79# define THREADSAFE 0
80#endif
81
82/*
83** This test uses pthreads and hence only works on unix and with
drhfe0f75b2006-01-10 20:01:18 +000084** a threadsafe build of SQLite. It also requires that the redefinable
85** I/O feature of SQLite be turned on. This feature is turned off by
86** default. If a required element is missing, almost all of the code
87** in this file is commented out.
drh23669402006-01-09 17:29:52 +000088*/
89#if OS_UNIX && THREADSAFE && defined(SQLITE_ENABLE_REDEF_IO)
90
drhfe0f75b2006-01-10 20:01:18 +000091/*
92** This demo uses pthreads. If you do not have a pthreads implementation
93** for your operating system, you will need to recode the threading
94** logic.
95*/
drh23669402006-01-09 17:29:52 +000096#include <pthread.h>
97#include <sched.h>
98
drhfe0f75b2006-01-10 20:01:18 +000099/* Useful macros used in several places */
drh23669402006-01-09 17:29:52 +0000100#define MIN(x,y) ((x)<(y)?(x):(y))
101#define MAX(x,y) ((x)>(y)?(x):(y))
102
drhfe0f75b2006-01-10 20:01:18 +0000103/* Forward references */
drh23669402006-01-09 17:29:52 +0000104typedef struct AsyncWrite AsyncWrite;
105typedef struct AsyncFile AsyncFile;
106
drhfe0f75b2006-01-10 20:01:18 +0000107/* Enable for debugging */
drh99681db2006-02-13 15:29:32 +0000108static int sqlite3async_trace = 0;
109# define TRACE(X) if( sqlite3async_trace ) asyncTrace X
drhfc8748a2006-02-13 14:49:38 +0000110static void asyncTrace(const char *zFormat, ...){
111 char *z;
112 va_list ap;
113 va_start(ap, zFormat);
114 z = sqlite3_vmprintf(zFormat, ap);
115 va_end(ap);
116 fprintf(stderr, "[%d] %s", (int)pthread_self(), z);
117 free(z);
118}
drh23669402006-01-09 17:29:52 +0000119
120/*
drh23669402006-01-09 17:29:52 +0000121** THREAD SAFETY NOTES
122**
123** Basic rules:
124**
125** * Both read and write access to the global write-op queue must be
126** protected by the async.queueMutex.
127**
128** * The file handles from the underlying system are assumed not to
129** be thread safe.
130**
drhfe0f75b2006-01-10 20:01:18 +0000131** * See the last two paragraphs under "The Writer Thread" for
drh23669402006-01-09 17:29:52 +0000132** an assumption to do with file-handle synchronization by the Os.
133**
134** File system operations (invoked by SQLite thread):
135**
136** xOpenXXX (three versions)
137** xDelete
138** xFileExists
139** xSyncDirectory
140**
141** File handle operations (invoked by SQLite thread):
142**
drh23669402006-01-09 17:29:52 +0000143** asyncWrite, asyncClose, asyncTruncate, asyncSync,
144** asyncSetFullSync, asyncOpenDirectory.
145**
drhfe0f75b2006-01-10 20:01:18 +0000146** The operations above add an entry to the global write-op list. They
147** prepare the entry, acquire the async.queueMutex momentarily while
148** list pointers are manipulated to insert the new entry, then release
149** the mutex and signal the writer thread to wake up in case it happens
150** to be asleep.
151**
drh23669402006-01-09 17:29:52 +0000152**
153** asyncRead, asyncFileSize.
drhfe0f75b2006-01-10 20:01:18 +0000154**
155** Read operations. Both of these read from both the underlying file
156** first then adjust their result based on pending writes in the
157** write-op queue. So async.queueMutex is held for the duration
158** of these operations to prevent other threads from changing the
159** queue in mid operation.
160**
161**
162** asyncLock, asyncUnlock, asyncLockState, asyncCheckReservedLock
drh23669402006-01-09 17:29:52 +0000163**
drh89ea9312006-02-13 17:03:47 +0000164** These primitives implement in-process locking using a hash table
165** on the file name. Files are locked correctly for connections coming
166** from the same process. But other processes cannot see these locks
167** and will therefore not honor them.
drhfe0f75b2006-01-10 20:01:18 +0000168**
169**
170** asyncFileHandle.
drh23669402006-01-09 17:29:52 +0000171**
172** The sqlite3OsFileHandle() function is currently only used when
173** debugging the pager module. Unless sqlite3OsClose() is called on the
174** file (shouldn't be possible for other reasons), the underlying
175** implementations are safe to call without grabbing any mutex. So we just
drhfe0f75b2006-01-10 20:01:18 +0000176** go ahead and call it no matter what any other threads are doing.
drh23669402006-01-09 17:29:52 +0000177**
drhfe0f75b2006-01-10 20:01:18 +0000178**
179** asyncSeek.
drh23669402006-01-09 17:29:52 +0000180**
181** Calling this method just manipulates the AsyncFile.iOffset variable.
182** Since this variable is never accessed by writer thread, this
183** function does not require the mutex. Actual calls to OsSeek() take
184** place just before OsWrite() or OsRead(), which are always protected by
185** the mutex.
drh23669402006-01-09 17:29:52 +0000186**
187** The writer thread:
188**
189** The async.writerMutex is used to make sure only there is only
190** a single writer thread running at a time.
191**
192** Inside the writer thread is a loop that works like this:
193**
194** WHILE (write-op list is not empty)
195** Do IO operation at head of write-op list
196** Remove entry from head of write-op list
197** END WHILE
198**
199** The async.queueMutex is always held during the <write-op list is
200** not empty> test, and when the entry is removed from the head
201** of the write-op list. Sometimes it is held for the interim
drhfe0f75b2006-01-10 20:01:18 +0000202** period (while the IO is performed), and sometimes it is
drh23669402006-01-09 17:29:52 +0000203** relinquished. It is relinquished if (a) the IO op is an
204** ASYNC_CLOSE or (b) when the file handle was opened, two of
205** the underlying systems handles were opened on the same
206** file-system entry.
207**
208** If condition (b) above is true, then one file-handle
209** (AsyncFile.pBaseRead) is used exclusively by sqlite threads to read the
210** file, the other (AsyncFile.pBaseWrite) by sqlite3_async_flush()
211** threads to perform write() operations. This means that read
212** operations are not blocked by asynchronous writes (although
213** asynchronous writes may still be blocked by reads).
214**
215** This assumes that the OS keeps two handles open on the same file
216** properly in sync. That is, any read operation that starts after a
217** write operation on the same file system entry has completed returns
218** data consistent with the write. We also assume that if one thread
219** reads a file while another is writing it all bytes other than the
220** ones actually being written contain valid data.
221**
222** If the above assumptions are not true, set the preprocessor symbol
223** SQLITE_ASYNC_TWO_FILEHANDLES to 0.
224*/
225
226#ifndef SQLITE_ASYNC_TWO_FILEHANDLES
227/* #define SQLITE_ASYNC_TWO_FILEHANDLES 0 */
228#define SQLITE_ASYNC_TWO_FILEHANDLES 1
229#endif
230
231/*
232** State information is held in the static variable "async" defined
233** as follows:
234*/
235static struct TestAsyncStaticData {
236 pthread_mutex_t queueMutex; /* Mutex for access to write operation queue */
237 pthread_mutex_t writerMutex; /* Prevents multiple writer threads */
drh89ea9312006-02-13 17:03:47 +0000238 pthread_mutex_t lockMutex; /* For access to aLock hash table */
drh23669402006-01-09 17:29:52 +0000239 pthread_cond_t queueSignal; /* For waking up sleeping writer thread */
240 pthread_cond_t emptySignal; /* Notify when the write queue is empty */
241 AsyncWrite *pQueueFirst; /* Next write operation to be processed */
242 AsyncWrite *pQueueLast; /* Last write operation on the list */
drh89ea9312006-02-13 17:03:47 +0000243 Hash aLock; /* Files locked */
drh23669402006-01-09 17:29:52 +0000244 volatile int ioDelay; /* Extra delay between write operations */
245 volatile int writerHaltWhenIdle; /* Writer thread halts when queue empty */
246 volatile int writerHaltNow; /* Writer thread halts after next op */
247} async = {
248 PTHREAD_MUTEX_INITIALIZER,
249 PTHREAD_MUTEX_INITIALIZER,
drh89ea9312006-02-13 17:03:47 +0000250 PTHREAD_MUTEX_INITIALIZER,
drh23669402006-01-09 17:29:52 +0000251 PTHREAD_COND_INITIALIZER,
252 PTHREAD_COND_INITIALIZER,
253};
254
255/* Possible values of AsyncWrite.op */
drh4b74b262006-02-13 13:50:55 +0000256#define ASYNC_NOOP 0
drh23669402006-01-09 17:29:52 +0000257#define ASYNC_WRITE 1
258#define ASYNC_SYNC 2
259#define ASYNC_TRUNCATE 3
260#define ASYNC_CLOSE 4
261#define ASYNC_OPENDIRECTORY 5
262#define ASYNC_SETFULLSYNC 6
drh23669402006-01-09 17:29:52 +0000263#define ASYNC_DELETE 7
264#define ASYNC_OPENEXCLUSIVE 8
265#define ASYNC_SYNCDIRECTORY 9
266
drh99681db2006-02-13 15:29:32 +0000267/* Names of opcodes. Used for debugging only.
268** Make sure these stay in sync with the macros above!
269*/
270static const char *azOpcodeName[] = {
271 "NOOP", "WRITE", "SYNC", "TRUNCATE", "CLOSE",
drh89ea9312006-02-13 17:03:47 +0000272 "OPENDIR", "SETFULLSYNC", "DELETE", "OPENEX", "SYNCDIR",
drh99681db2006-02-13 15:29:32 +0000273};
274
drh23669402006-01-09 17:29:52 +0000275/*
drhfe0f75b2006-01-10 20:01:18 +0000276** Entries on the write-op queue are instances of the AsyncWrite
277** structure, defined here.
278**
drh23669402006-01-09 17:29:52 +0000279** The interpretation of the iOffset and nByte variables varies depending
280** on the value of AsyncWrite.op:
281**
282** ASYNC_WRITE:
283** iOffset -> Offset in file to write to.
284** nByte -> Number of bytes of data to write (pointed to by zBuf).
285**
286** ASYNC_SYNC:
287** iOffset -> Unused.
288** nByte -> Value of "fullsync" flag to pass to sqlite3OsSync().
289**
290** ASYNC_TRUNCATE:
291** iOffset -> Size to truncate file to.
292** nByte -> Unused.
293**
294** ASYNC_CLOSE:
295** iOffset -> Unused.
296** nByte -> Unused.
297**
298** ASYNC_OPENDIRECTORY:
299** iOffset -> Unused.
300** nByte -> Number of bytes of zBuf points to (directory name).
301**
302** ASYNC_SETFULLSYNC:
303** iOffset -> Unused.
304** nByte -> New value for the full-sync flag.
305**
306**
307** ASYNC_DELETE:
308** iOffset -> Unused.
309** nByte -> Number of bytes of zBuf points to (file name).
310**
311** ASYNC_OPENEXCLUSIVE:
312** iOffset -> Value of "delflag".
313** nByte -> Number of bytes of zBuf points to (file name).
314**
drh89ea9312006-02-13 17:03:47 +0000315**
drh23669402006-01-09 17:29:52 +0000316** For an ASYNC_WRITE operation, zBuf points to the data to write to the file.
317** This space is sqliteMalloc()d along with the AsyncWrite structure in a
318** single blob, so is deleted when sqliteFree() is called on the parent
319** structure.
320*/
321struct AsyncWrite {
322 AsyncFile *pFile; /* File to write data to or sync */
323 int op; /* One of ASYNC_xxx etc. */
324 i64 iOffset; /* See above */
325 int nByte; /* See above */
326 char *zBuf; /* Data to write to file (or NULL if op!=ASYNC_WRITE) */
327 AsyncWrite *pNext; /* Next write operation (to any file) */
328};
329
330/*
331** The AsyncFile structure is a subclass of OsFile used for asynchronous IO.
332*/
333struct AsyncFile {
334 IoMethod *pMethod; /* Must be first */
drhfc8748a2006-02-13 14:49:38 +0000335 int ioError; /* Value of any asychronous error we have seen */
336 i64 iOffset; /* Current seek() offset in file */
drh89ea9312006-02-13 17:03:47 +0000337 char *zName; /* Underlying OS filename - used for debugging */
338 int nName; /* Number of characters in zName */
drh23669402006-01-09 17:29:52 +0000339 OsFile *pBaseRead; /* Read handle to the underlying Os file */
340 OsFile *pBaseWrite; /* Write handle to the underlying Os file */
341};
342
343/*
344** Add an entry to the end of the global write-op list. pWrite should point
drhfe0f75b2006-01-10 20:01:18 +0000345** to an AsyncWrite structure allocated using sqlite3OsMalloc(). The writer
346** thread will call sqlite3OsFree() to free the structure after the specified
347** operation has been completed.
drh23669402006-01-09 17:29:52 +0000348**
drhfe0f75b2006-01-10 20:01:18 +0000349** Once an AsyncWrite structure has been added to the list, it becomes the
350** property of the writer thread and must not be read or modified by the
351** caller.
drh23669402006-01-09 17:29:52 +0000352*/
353static void addAsyncWrite(AsyncWrite *pWrite){
drhfe0f75b2006-01-10 20:01:18 +0000354 /* We must hold the queue mutex in order to modify the queue pointers */
drh23669402006-01-09 17:29:52 +0000355 pthread_mutex_lock(&async.queueMutex);
drhfe0f75b2006-01-10 20:01:18 +0000356
357 /* Add the record to the end of the write-op queue */
drh23669402006-01-09 17:29:52 +0000358 assert( !pWrite->pNext );
359 if( async.pQueueLast ){
360 assert( async.pQueueFirst );
361 async.pQueueLast->pNext = pWrite;
362 }else{
363 async.pQueueFirst = pWrite;
364 }
365 async.pQueueLast = pWrite;
drh89ea9312006-02-13 17:03:47 +0000366 TRACE(("PUSH %p (%s %s)\n", pWrite, azOpcodeName[pWrite->op],
367 pWrite->pFile ? pWrite->pFile->zName : "-"));
drhfe0f75b2006-01-10 20:01:18 +0000368
369 /* Drop the queue mutex */
drh23669402006-01-09 17:29:52 +0000370 pthread_mutex_unlock(&async.queueMutex);
drhfe0f75b2006-01-10 20:01:18 +0000371
372 /* The writer thread might have been idle because there was nothing
373 ** on the write-op queue for it to do. So wake it up. */
drh23669402006-01-09 17:29:52 +0000374 pthread_cond_signal(&async.queueSignal);
375}
376
377/*
378** This is a utility function to allocate and populate a new AsyncWrite
379** structure and insert it (via addAsyncWrite() ) into the global list.
380*/
381static int addNewAsyncWrite(
382 AsyncFile *pFile,
383 int op,
384 i64 iOffset,
385 int nByte,
386 const char *zByte
387){
drh4b74b262006-02-13 13:50:55 +0000388 AsyncWrite *p;
389 if( pFile && pFile->ioError!=SQLITE_OK ){
390 return pFile->ioError;
391 }
392 p = sqlite3OsMalloc(sizeof(AsyncWrite) + (zByte?nByte:0));
drh23669402006-01-09 17:29:52 +0000393 if( !p ){
394 return SQLITE_NOMEM;
395 }
396 p->op = op;
397 p->iOffset = iOffset;
398 p->nByte = nByte;
399 p->pFile = pFile;
400 p->pNext = 0;
401 if( zByte ){
402 p->zBuf = (char *)&p[1];
403 memcpy(p->zBuf, zByte, nByte);
404 }else{
405 p->zBuf = 0;
406 }
407 addAsyncWrite(p);
408 return SQLITE_OK;
409}
410
411/*
412** Close the file. This just adds an entry to the write-op list, the file is
413** not actually closed.
414*/
415static int asyncClose(OsFile **pId){
416 return addNewAsyncWrite((AsyncFile *)*pId, ASYNC_CLOSE, 0, 0, 0);
417}
418
419/*
420** Implementation of sqlite3OsWrite() for asynchronous files. Instead of
421** writing to the underlying file, this function adds an entry to the end of
422** the global AsyncWrite list. Either SQLITE_OK or SQLITE_NOMEM may be
423** returned.
424*/
425static int asyncWrite(OsFile *id, const void *pBuf, int amt){
426 AsyncFile *pFile = (AsyncFile *)id;
427 int rc = addNewAsyncWrite(pFile, ASYNC_WRITE, pFile->iOffset, amt, pBuf);
428 pFile->iOffset += (i64)amt;
429 return rc;
430}
431
432/*
433** Truncate the file to nByte bytes in length. This just adds an entry to
434** the write-op list, no IO actually takes place.
435*/
436static int asyncTruncate(OsFile *id, i64 nByte){
437 return addNewAsyncWrite((AsyncFile *)id, ASYNC_TRUNCATE, nByte, 0, 0);
438}
439
440/*
441** Open the directory identified by zName and associate it with the
442** specified file. This just adds an entry to the write-op list, the
443** directory is opened later by sqlite3_async_flush().
444*/
445static int asyncOpenDirectory(OsFile *id, const char *zName){
446 AsyncFile *pFile = (AsyncFile *)id;
447 return addNewAsyncWrite(pFile, ASYNC_OPENDIRECTORY, 0, strlen(zName)+1,zName);
448}
449
450/*
451** Sync the file. This just adds an entry to the write-op list, the
452** sync() is done later by sqlite3_async_flush().
453*/
454static int asyncSync(OsFile *id, int fullsync){
455 return addNewAsyncWrite((AsyncFile *)id, ASYNC_SYNC, 0, fullsync, 0);
456}
457
458/*
459** Set (or clear) the full-sync flag on the underlying file. This operation
460** is queued and performed later by sqlite3_async_flush().
461*/
462static void asyncSetFullSync(OsFile *id, int value){
463 addNewAsyncWrite((AsyncFile *)id, ASYNC_SETFULLSYNC, 0, value, 0);
464}
465
466/*
467** Read data from the file. First we read from the filesystem, then adjust
468** the contents of the buffer based on ASYNC_WRITE operations in the
drhfe0f75b2006-01-10 20:01:18 +0000469** write-op queue.
drh23669402006-01-09 17:29:52 +0000470**
471** This method holds the mutex from start to finish.
472*/
473static int asyncRead(OsFile *id, void *obuf, int amt){
474 int rc = SQLITE_OK;
475 i64 filesize;
476 int nRead;
477 AsyncFile *pFile = (AsyncFile *)id;
danielk1977750b03e2006-02-14 10:48:39 +0000478 OsFile *pBase = pFile->pBaseRead;
479
480 if( !pBase ){
481 pBase = pFile->pBaseWrite;
482 }
drh23669402006-01-09 17:29:52 +0000483
drh4b74b262006-02-13 13:50:55 +0000484 /* If an I/O error has previously occurred on this file, then all
485 ** subsequent operations fail.
486 */
487 if( pFile->ioError!=SQLITE_OK ){
488 return pFile->ioError;
489 }
490
drh23669402006-01-09 17:29:52 +0000491 /* Grab the write queue mutex for the duration of the call */
492 pthread_mutex_lock(&async.queueMutex);
493
danielk1977750b03e2006-02-14 10:48:39 +0000494 if( pBase ){
495 rc = sqlite3OsFileSize(pBase, &filesize);
drh23669402006-01-09 17:29:52 +0000496 if( rc!=SQLITE_OK ){
497 goto asyncread_out;
498 }
danielk1977750b03e2006-02-14 10:48:39 +0000499 rc = sqlite3OsSeek(pBase, pFile->iOffset);
drh23669402006-01-09 17:29:52 +0000500 if( rc!=SQLITE_OK ){
501 goto asyncread_out;
502 }
503 nRead = MIN(filesize - pFile->iOffset, amt);
504 if( nRead>0 ){
danielk1977750b03e2006-02-14 10:48:39 +0000505 rc = sqlite3OsRead(pBase, obuf, nRead);
drh97bbdc02006-02-13 18:35:06 +0000506 TRACE(("READ %s %d bytes at %d\n", pFile->zName, nRead, pFile->iOffset));
drh23669402006-01-09 17:29:52 +0000507 }
508 }
509
510 if( rc==SQLITE_OK ){
511 AsyncWrite *p;
512 i64 iOffset = pFile->iOffset; /* Current seek offset */
513
514 for(p=async.pQueueFirst; p; p = p->pNext){
515 if( p->pFile==pFile && p->op==ASYNC_WRITE ){
drh44528382006-02-13 13:30:19 +0000516 int iBeginOut = (p->iOffset - iOffset);
517 int iBeginIn = -iBeginOut;
drh23669402006-01-09 17:29:52 +0000518 int nCopy;
519
520 if( iBeginIn<0 ) iBeginIn = 0;
521 if( iBeginOut<0 ) iBeginOut = 0;
522 nCopy = MIN(p->nByte-iBeginIn, amt-iBeginOut);
523
524 if( nCopy>0 ){
525 memcpy(&((char *)obuf)[iBeginOut], &p->zBuf[iBeginIn], nCopy);
drh97bbdc02006-02-13 18:35:06 +0000526 TRACE(("OVERREAD %d bytes at %d\n", nCopy, iBeginOut+iOffset));
drh23669402006-01-09 17:29:52 +0000527 }
528 }
529 }
530
531 pFile->iOffset += (i64)amt;
532 }
533
534asyncread_out:
535 pthread_mutex_unlock(&async.queueMutex);
536 return rc;
537}
538
539/*
540** Seek to the specified offset. This just adjusts the AsyncFile.iOffset
541** variable - calling seek() on the underlying file is defered until the
542** next read() or write() operation.
543*/
544static int asyncSeek(OsFile *id, i64 offset){
545 AsyncFile *pFile = (AsyncFile *)id;
546 pFile->iOffset = offset;
547 return SQLITE_OK;
548}
549
550/*
551** Read the size of the file. First we read the size of the file system
552** entry, then adjust for any ASYNC_WRITE or ASYNC_TRUNCATE operations
553** currently in the write-op list.
554**
555** This method holds the mutex from start to finish.
556*/
557int asyncFileSize(OsFile *id, i64 *pSize){
558 int rc = SQLITE_OK;
559 i64 s = 0;
560 OsFile *pBase;
561
562 pthread_mutex_lock(&async.queueMutex);
563
564 /* Read the filesystem size from the base file. If pBaseRead is NULL, this
565 ** means the file hasn't been opened yet. In this case all relevant data
566 ** must be in the write-op queue anyway, so we can omit reading from the
567 ** file-system.
568 */
569 pBase = ((AsyncFile *)id)->pBaseRead;
danielk1977750b03e2006-02-14 10:48:39 +0000570 if( !pBase ){
571 pBase = ((AsyncFile *)id)->pBaseWrite;
572 }
drh23669402006-01-09 17:29:52 +0000573 if( pBase ){
574 rc = sqlite3OsFileSize(pBase, &s);
575 }
576
577 if( rc==SQLITE_OK ){
578 AsyncWrite *p;
579 for(p=async.pQueueFirst; p; p = p->pNext){
580 if( p->pFile==(AsyncFile *)id ){
581 switch( p->op ){
582 case ASYNC_WRITE:
583 s = MAX(p->iOffset + (i64)(p->nByte), s);
584 break;
585 case ASYNC_TRUNCATE:
586 s = MIN(s, p->nByte);
587 break;
588 }
589 }
590 }
591 *pSize = s;
592 }
593 pthread_mutex_unlock(&async.queueMutex);
594 return rc;
595}
596
597/*
598** Return the operating system file handle. This is only used for debugging
599** at the moment anyway.
600*/
601static int asyncFileHandle(OsFile *id){
602 return sqlite3OsFileHandle(((AsyncFile *)id)->pBaseRead);
603}
604
drhfe0f75b2006-01-10 20:01:18 +0000605/*
drh89ea9312006-02-13 17:03:47 +0000606** No disk locking is performed. We keep track of locks locally in
607** the async.aLock hash table. Locking should appear to work the same
608** as with standard (unmodified) SQLite as long as all connections
609** come from this one process. Connections from external processes
610** cannot see our internal hash table (obviously) and will thus not
611** honor our locks.
drhfe0f75b2006-01-10 20:01:18 +0000612*/
drh23669402006-01-09 17:29:52 +0000613static int asyncLock(OsFile *id, int lockType){
drh89ea9312006-02-13 17:03:47 +0000614 AsyncFile *pFile = (AsyncFile*)id;
615 TRACE(("LOCK %d (%s)\n", lockType, pFile->zName));
616 pthread_mutex_lock(&async.lockMutex);
617 sqlite3HashInsert(&async.aLock, pFile->zName, pFile->nName, (void*)lockType);
618 pthread_mutex_unlock(&async.lockMutex);
drh23669402006-01-09 17:29:52 +0000619 return SQLITE_OK;
620}
621static int asyncUnlock(OsFile *id, int lockType){
drh89ea9312006-02-13 17:03:47 +0000622 return asyncLock(id, lockType);
drh23669402006-01-09 17:29:52 +0000623}
624
625/*
626** This function is called when the pager layer first opens a database file
627** and is checking for a hot-journal.
628*/
629static int asyncCheckReservedLock(OsFile *id){
drh89ea9312006-02-13 17:03:47 +0000630 AsyncFile *pFile = (AsyncFile*)id;
631 int rc;
632 pthread_mutex_lock(&async.lockMutex);
633 rc = (int)sqlite3HashFind(&async.aLock, pFile->zName, pFile->nName);
634 pthread_mutex_unlock(&async.lockMutex);
635 TRACE(("CHECK-LOCK %d (%s)\n", rc, pFile->zName));
drh97bbdc02006-02-13 18:35:06 +0000636 return rc>SHARED_LOCK;
drh23669402006-01-09 17:29:52 +0000637}
638
639/*
640** This is broken. But sqlite3OsLockState() is only used for testing anyway.
641*/
642static int asyncLockState(OsFile *id){
643 return SQLITE_OK;
644}
645
646/*
647** The following variables hold pointers to the original versions of
drhfe0f75b2006-01-10 20:01:18 +0000648** OS-layer interface routines that are overloaded in order to create
649** the asynchronous I/O backend.
drh23669402006-01-09 17:29:52 +0000650*/
651static int (*xOrigOpenReadWrite)(const char*, OsFile**, int*) = 0;
652static int (*xOrigOpenExclusive)(const char*, OsFile**, int) = 0;
653static int (*xOrigOpenReadOnly)(const char*, OsFile**) = 0;
654static int (*xOrigDelete)(const char*) = 0;
655static int (*xOrigFileExists)(const char*) = 0;
656static int (*xOrigSyncDirectory)(const char*) = 0;
657
drhfe0f75b2006-01-10 20:01:18 +0000658/*
659** This routine does most of the work of opening a file and building
660** the OsFile structure.
661*/
drh23669402006-01-09 17:29:52 +0000662static int asyncOpenFile(
drhfe0f75b2006-01-10 20:01:18 +0000663 const char *zName, /* The name of the file to be opened */
664 OsFile **pFile, /* Put the OsFile structure here */
665 OsFile *pBaseRead, /* The real OsFile from the real I/O routine */
666 int openForWriting /* Open a second file handle for writing if true */
drh23669402006-01-09 17:29:52 +0000667){
drh89ea9312006-02-13 17:03:47 +0000668 int rc, i, n;
drh23669402006-01-09 17:29:52 +0000669 AsyncFile *p;
670 OsFile *pBaseWrite = 0;
671
672 static IoMethod iomethod = {
673 asyncClose,
674 asyncOpenDirectory,
675 asyncRead,
676 asyncWrite,
677 asyncSeek,
678 asyncTruncate,
679 asyncSync,
680 asyncSetFullSync,
681 asyncFileHandle,
682 asyncFileSize,
683 asyncLock,
684 asyncUnlock,
685 asyncLockState,
686 asyncCheckReservedLock
687 };
688
drhfe0f75b2006-01-10 20:01:18 +0000689 if( openForWriting && SQLITE_ASYNC_TWO_FILEHANDLES ){
drh23669402006-01-09 17:29:52 +0000690 int dummy;
691 rc = xOrigOpenReadWrite(zName, &pBaseWrite, &dummy);
692 if( rc!=SQLITE_OK ){
693 goto error_out;
694 }
695 }
696
drh89ea9312006-02-13 17:03:47 +0000697 n = strlen(zName);
698 for(i=n-1; i>=0 && zName[i]!='/'; i--){}
699 p = (AsyncFile *)sqlite3OsMalloc(sizeof(AsyncFile) + n - i);
drh23669402006-01-09 17:29:52 +0000700 if( !p ){
701 rc = SQLITE_NOMEM;
702 goto error_out;
703 }
704 memset(p, 0, sizeof(AsyncFile));
drh89ea9312006-02-13 17:03:47 +0000705 p->zName = (char*)&p[1];
706 strcpy(p->zName, &zName[i+1]);
707 p->nName = n - i;
drh23669402006-01-09 17:29:52 +0000708 p->pMethod = &iomethod;
709 p->pBaseRead = pBaseRead;
710 p->pBaseWrite = pBaseWrite;
drh4b74b262006-02-13 13:50:55 +0000711 p->ioError = SQLITE_OK;
drh23669402006-01-09 17:29:52 +0000712
713 *pFile = (OsFile *)p;
714 return SQLITE_OK;
715
716error_out:
717 assert(!p);
718 sqlite3OsClose(&pBaseRead);
719 sqlite3OsClose(&pBaseWrite);
720 *pFile = 0;
721 return rc;
722}
723
724/*
725** The async-IO backends implementation of the three functions used to open
726** a file (xOpenExclusive, xOpenReadWrite and xOpenReadOnly). Most of the
727** work is done in function asyncOpenFile() - see above.
728*/
729static int asyncOpenExclusive(const char *z, OsFile **ppFile, int delFlag){
730 int rc = asyncOpenFile(z, ppFile, 0, 0);
731 if( rc==SQLITE_OK ){
732 AsyncFile *pFile = (AsyncFile *)(*ppFile);
733 int nByte = strlen(z)+1;
734 i64 i = (i64)(delFlag);
735 rc = addNewAsyncWrite(pFile, ASYNC_OPENEXCLUSIVE, i, nByte, z);
736 if( rc!=SQLITE_OK ){
737 sqlite3OsFree(pFile);
738 *ppFile = 0;
739 }
740 }
741 return rc;
742}
743static int asyncOpenReadOnly(const char *z, OsFile **ppFile){
744 OsFile *pBase = 0;
745 int rc = xOrigOpenReadOnly(z, &pBase);
746 if( rc==SQLITE_OK ){
747 rc = asyncOpenFile(z, ppFile, pBase, 0);
748 }
749 return rc;
750}
751static int asyncOpenReadWrite(const char *z, OsFile **ppFile, int *pReadOnly){
752 OsFile *pBase = 0;
753 int rc = xOrigOpenReadWrite(z, &pBase, pReadOnly);
754 if( rc==SQLITE_OK ){
755 rc = asyncOpenFile(z, ppFile, pBase, (*pReadOnly ? 0 : 1));
756 }
757 return rc;
758}
759
760/*
761** Implementation of sqlite3OsDelete. Add an entry to the end of the
762** write-op queue to perform the delete.
763*/
764static int asyncDelete(const char *z){
765 return addNewAsyncWrite(0, ASYNC_DELETE, 0, strlen(z)+1, z);
766}
767
768/*
769** Implementation of sqlite3OsSyncDirectory. Add an entry to the end of the
770** write-op queue to perform the directory sync.
771*/
772static int asyncSyncDirectory(const char *z){
773 return addNewAsyncWrite(0, ASYNC_SYNCDIRECTORY, 0, strlen(z)+1, z);
774}
775
776/*
777** Implementation of sqlite3OsFileExists. Return true if file 'z' exists
778** in the file system.
779**
780** This method holds the mutex from start to finish.
781*/
782static int asyncFileExists(const char *z){
783 int ret;
784 AsyncWrite *p;
785
786 pthread_mutex_lock(&async.queueMutex);
787
788 /* See if the real file system contains the specified file. */
789 ret = xOrigFileExists(z);
790
791 for(p=async.pQueueFirst; p; p = p->pNext){
792 if( p->op==ASYNC_DELETE && 0==strcmp(p->zBuf, z) ){
793 ret = 0;
794 }else if( p->op==ASYNC_OPENEXCLUSIVE && 0==strcmp(p->zBuf, z) ){
795 ret = 1;
796 }
797 }
798
drh89ea9312006-02-13 17:03:47 +0000799 TRACE(("EXISTS: %s = %d\n", z, ret));
drh23669402006-01-09 17:29:52 +0000800 pthread_mutex_unlock(&async.queueMutex);
801 return ret;
802}
803
804/*
805** Call this routine to enable or disable the
806** asynchronous IO features implemented in this file.
807**
808** This routine is not even remotely threadsafe. Do not call
809** this routine while any SQLite database connections are open.
810*/
811static void asyncEnable(int enable){
812 if( enable && xOrigOpenReadWrite==0 ){
danielk1977750b03e2006-02-14 10:48:39 +0000813 assert(sqlite3Os.xOpenReadWrite);
drh89ea9312006-02-13 17:03:47 +0000814 sqlite3HashInit(&async.aLock, SQLITE_HASH_BINARY, 1);
drh23669402006-01-09 17:29:52 +0000815 xOrigOpenReadWrite = sqlite3Os.xOpenReadWrite;
816 xOrigOpenReadOnly = sqlite3Os.xOpenReadOnly;
817 xOrigOpenExclusive = sqlite3Os.xOpenExclusive;
818 xOrigDelete = sqlite3Os.xDelete;
819 xOrigFileExists = sqlite3Os.xFileExists;
820 xOrigSyncDirectory = sqlite3Os.xSyncDirectory;
821
822 sqlite3Os.xOpenReadWrite = asyncOpenReadWrite;
823 sqlite3Os.xOpenReadOnly = asyncOpenReadOnly;
824 sqlite3Os.xOpenExclusive = asyncOpenExclusive;
825 sqlite3Os.xDelete = asyncDelete;
826 sqlite3Os.xFileExists = asyncFileExists;
827 sqlite3Os.xSyncDirectory = asyncSyncDirectory;
danielk1977750b03e2006-02-14 10:48:39 +0000828 assert(sqlite3Os.xOpenReadWrite);
drh23669402006-01-09 17:29:52 +0000829 }
830 if( !enable && xOrigOpenReadWrite!=0 ){
danielk1977750b03e2006-02-14 10:48:39 +0000831 assert(sqlite3Os.xOpenReadWrite);
drh89ea9312006-02-13 17:03:47 +0000832 sqlite3HashClear(&async.aLock);
drh23669402006-01-09 17:29:52 +0000833 sqlite3Os.xOpenReadWrite = xOrigOpenReadWrite;
834 sqlite3Os.xOpenReadOnly = xOrigOpenReadOnly;
835 sqlite3Os.xOpenExclusive = xOrigOpenExclusive;
836 sqlite3Os.xDelete = xOrigDelete;
837 sqlite3Os.xFileExists = xOrigFileExists;
838 sqlite3Os.xSyncDirectory = xOrigSyncDirectory;
839
840 xOrigOpenReadWrite = 0;
841 xOrigOpenReadOnly = 0;
842 xOrigOpenExclusive = 0;
843 xOrigDelete = 0;
844 xOrigFileExists = 0;
845 xOrigSyncDirectory = 0;
danielk1977750b03e2006-02-14 10:48:39 +0000846 assert(sqlite3Os.xOpenReadWrite);
drh23669402006-01-09 17:29:52 +0000847 }
848}
849
850/*
851** This procedure runs in a separate thread, reading messages off of the
852** write queue and processing them one by one.
853**
854** If async.writerHaltNow is true, then this procedure exits
855** after processing a single message.
856**
857** If async.writerHaltWhenIdle is true, then this procedure exits when
858** the write queue is empty.
859**
860** If both of the above variables are false, this procedure runs
861** indefinately, waiting for operations to be added to the write queue
862** and processing them in the order in which they arrive.
863**
864** An artifical delay of async.ioDelay milliseconds is inserted before
865** each write operation in order to simulate the effect of a slow disk.
866**
867** Only one instance of this procedure may be running at a time.
868*/
869static void *asyncWriterThread(void *NotUsed){
870 AsyncWrite *p = 0;
871 int rc = SQLITE_OK;
872
873 if( pthread_mutex_trylock(&async.writerMutex) ){
874 return 0;
875 }
876 while( async.writerHaltNow==0 ){
877 int holdingMutex;
878 OsFile *pBase = 0;
879
880 pthread_mutex_lock(&async.queueMutex);
881 holdingMutex = 1;
882 while( (p = async.pQueueFirst)==0 ){
883 pthread_cond_broadcast(&async.emptySignal);
884 if( async.writerHaltWhenIdle ){
885 pthread_mutex_unlock(&async.queueMutex);
886 break;
887 }else{
drhfc8748a2006-02-13 14:49:38 +0000888 TRACE(("IDLE\n"));
drh23669402006-01-09 17:29:52 +0000889 pthread_cond_wait(&async.queueSignal, &async.queueMutex);
drhfc8748a2006-02-13 14:49:38 +0000890 TRACE(("WAKEUP\n"));
drh23669402006-01-09 17:29:52 +0000891 }
892 }
893 if( p==0 ) break;
drh23669402006-01-09 17:29:52 +0000894
895 /* Right now this thread is holding the mutex on the write-op queue.
896 ** Variable 'p' points to the first entry in the write-op queue. In
897 ** the general case, we hold on to the mutex for the entire body of
898 ** the loop.
899 **
900 ** However in the cases enumerated below, we relinquish the mutex,
901 ** perform the IO, and then re-request the mutex before removing 'p' from
902 ** the head of the write-op queue. The idea is to increase concurrency with
903 ** sqlite threads.
904 **
905 ** * An ASYNC_CLOSE operation.
906 ** * An ASYNC_OPENEXCLUSIVE operation. For this one, we relinquish
907 ** the mutex, call the underlying xOpenExclusive() function, then
908 ** re-aquire the mutex before seting the AsyncFile.pBaseRead
909 ** variable.
910 ** * ASYNC_SYNC and ASYNC_WRITE operations, if
911 ** SQLITE_ASYNC_TWO_FILEHANDLES was set at compile time and two
912 ** file-handles are open for the particular file being "synced".
913 */
914 if( p->pFile ){
915 pBase = p->pFile->pBaseWrite;
drh4b74b262006-02-13 13:50:55 +0000916 if( p->pFile->ioError!=SQLITE_OK && p->op!=ASYNC_CLOSE ){
917 p->op = ASYNC_NOOP;
918 }
drh23669402006-01-09 17:29:52 +0000919 if(
920 p->op==ASYNC_CLOSE ||
921 p->op==ASYNC_OPENEXCLUSIVE ||
922 (pBase && (p->op==ASYNC_SYNC || p->op==ASYNC_WRITE) )
923 ){
924 pthread_mutex_unlock(&async.queueMutex);
925 holdingMutex = 0;
926 }
927 if( !pBase ){
928 pBase = p->pFile->pBaseRead;
929 }
930 }
931
932 switch( p->op ){
drh4b74b262006-02-13 13:50:55 +0000933 case ASYNC_NOOP:
934 break;
935
drh23669402006-01-09 17:29:52 +0000936 case ASYNC_WRITE:
937 assert( pBase );
drh97bbdc02006-02-13 18:35:06 +0000938 TRACE(("WRITE %s %d bytes at %d\n",
939 p->pFile->zName, p->nByte, p->iOffset));
drh23669402006-01-09 17:29:52 +0000940 rc = sqlite3OsSeek(pBase, p->iOffset);
941 if( rc==SQLITE_OK ){
942 rc = sqlite3OsWrite(pBase, (const void *)(p->zBuf), p->nByte);
943 }
944 break;
945
946 case ASYNC_SYNC:
947 assert( pBase );
drh97bbdc02006-02-13 18:35:06 +0000948 TRACE(("SYNC %s\n", p->pFile->zName));
drh23669402006-01-09 17:29:52 +0000949 rc = sqlite3OsSync(pBase, p->nByte);
950 break;
951
952 case ASYNC_TRUNCATE:
953 assert( pBase );
drh97bbdc02006-02-13 18:35:06 +0000954 TRACE(("TRUNCATE %s to %d bytes\n", p->pFile->zName, p->iOffset));
955 rc = sqlite3OsTruncate(pBase, p->iOffset);
drh23669402006-01-09 17:29:52 +0000956 break;
957
958 case ASYNC_CLOSE:
drh97bbdc02006-02-13 18:35:06 +0000959 TRACE(("CLOSE %s\n", p->pFile->zName));
drh23669402006-01-09 17:29:52 +0000960 sqlite3OsClose(&p->pFile->pBaseWrite);
danielk1977750b03e2006-02-14 10:48:39 +0000961 sqlite3OsClose(&p->pFile->pBaseRead);
drh23669402006-01-09 17:29:52 +0000962 sqlite3OsFree(p->pFile);
963 break;
964
965 case ASYNC_OPENDIRECTORY:
966 assert( pBase );
drh97bbdc02006-02-13 18:35:06 +0000967 TRACE(("OPENDIR %s\n", p->zBuf));
drh23669402006-01-09 17:29:52 +0000968 sqlite3OsOpenDirectory(pBase, p->zBuf);
969 break;
970
971 case ASYNC_SETFULLSYNC:
972 assert( pBase );
drh97bbdc02006-02-13 18:35:06 +0000973 TRACE(("SETFULLSYNC %s %d\n", p->pFile->zName, p->nByte));
drh23669402006-01-09 17:29:52 +0000974 sqlite3OsSetFullSync(pBase, p->nByte);
975 break;
976
977 case ASYNC_DELETE:
drh97bbdc02006-02-13 18:35:06 +0000978 TRACE(("DELETE %s\n", p->zBuf));
drh23669402006-01-09 17:29:52 +0000979 rc = xOrigDelete(p->zBuf);
980 break;
981
982 case ASYNC_SYNCDIRECTORY:
drh97bbdc02006-02-13 18:35:06 +0000983 TRACE(("SYNCDIR %s\n", p->zBuf));
drh23669402006-01-09 17:29:52 +0000984 rc = xOrigSyncDirectory(p->zBuf);
985 break;
986
987 case ASYNC_OPENEXCLUSIVE: {
988 AsyncFile *pFile = p->pFile;
989 int delFlag = ((p->iOffset)?1:0);
990 OsFile *pBase = 0;
drh97bbdc02006-02-13 18:35:06 +0000991 TRACE(("OPEN %s delFlag=%d\n", p->zBuf, delFlag));
danielk1977750b03e2006-02-14 10:48:39 +0000992 assert(pFile->pBaseRead==0 && pFile->pBaseWrite==0);
drh23669402006-01-09 17:29:52 +0000993 rc = xOrigOpenExclusive(p->zBuf, &pBase, delFlag);
994 assert( holdingMutex==0 );
995 pthread_mutex_lock(&async.queueMutex);
996 holdingMutex = 1;
997 if( rc==SQLITE_OK ){
danielk1977750b03e2006-02-14 10:48:39 +0000998 pFile->pBaseWrite = pBase;
drh23669402006-01-09 17:29:52 +0000999 }
1000 break;
1001 }
1002
1003 default: assert(!"Illegal value for AsyncWrite.op");
1004 }
1005
drh4b74b262006-02-13 13:50:55 +00001006 /* If an error happens, store the error code in the pFile.ioError
1007 ** field. This will prevent any future operations on that file,
1008 ** other than closing it.
1009 **
1010 ** We cannot report the error back to the connection that requested
1011 ** the I/O since the error happened asynchronously. The connection has
1012 ** already moved on. There really is nobody to report the error to.
1013 */
1014 if( rc!=SQLITE_OK ){
1015 p->pFile->ioError = rc;
1016 rc = SQLITE_OK;
1017 }
1018
drh23669402006-01-09 17:29:52 +00001019 /* If we didn't hang on to the mutex during the IO op, obtain it now
1020 ** so that the AsyncWrite structure can be safely removed from the
1021 ** global write-op queue.
1022 */
1023 if( !holdingMutex ){
1024 pthread_mutex_lock(&async.queueMutex);
1025 holdingMutex = 1;
1026 }
drh99681db2006-02-13 15:29:32 +00001027 /* TRACE(("UNLINK %p\n", p)); */
drh4b74b262006-02-13 13:50:55 +00001028 if( p==async.pQueueLast ){
1029 async.pQueueLast = 0;
drh23669402006-01-09 17:29:52 +00001030 }
drh4b74b262006-02-13 13:50:55 +00001031 async.pQueueFirst = p->pNext;
drh5c323542006-02-13 13:23:57 +00001032 sqlite3OsFree(p);
drh23669402006-01-09 17:29:52 +00001033 assert( holdingMutex );
1034
1035 /* Drop the queue mutex before continuing to the next write operation
1036 ** in order to give other threads a chance to work with the write queue.
1037 */
1038 pthread_mutex_unlock(&async.queueMutex);
1039 if( async.ioDelay>0 ){
1040 sqlite3OsSleep(async.ioDelay);
1041 }else{
1042 sched_yield();
1043 }
1044 }
1045 pthread_mutex_unlock(&async.writerMutex);
1046 return 0;
1047}
1048
1049/**************************************************************************
1050** The remaining code defines a Tcl interface for testing the asynchronous
1051** IO implementation in this file.
1052**
1053** To adapt the code to a non-TCL environment, delete or comment out
1054** the code that follows.
1055*/
1056
1057/*
1058** sqlite3async_enable ?YES/NO?
1059**
1060** Enable or disable the asynchronous I/O backend. This command is
1061** not thread-safe. Do not call it while any database connections
1062** are open.
1063*/
1064static int testAsyncEnable(
1065 void * clientData,
1066 Tcl_Interp *interp,
1067 int objc,
1068 Tcl_Obj *CONST objv[]
1069){
1070 if( objc!=1 && objc!=2 ){
1071 Tcl_WrongNumArgs(interp, 1, objv, "?YES/NO?");
1072 return TCL_ERROR;
1073 }
1074 if( objc==1 ){
1075 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(xOrigOpenReadWrite!=0));
1076 }else{
1077 int en;
1078 if( Tcl_GetBooleanFromObj(interp, objv[1], &en) ) return TCL_ERROR;
1079 asyncEnable(en);
1080 }
1081 return TCL_OK;
1082}
1083
1084/*
1085** sqlite3async_halt "now"|"idle"|"never"
1086**
1087** Set the conditions at which the writer thread will halt.
1088*/
1089static int testAsyncHalt(
1090 void * clientData,
1091 Tcl_Interp *interp,
1092 int objc,
1093 Tcl_Obj *CONST objv[]
1094){
1095 const char *zCond;
1096 if( objc!=2 ){
1097 Tcl_WrongNumArgs(interp, 1, objv, "\"now\"|\"idle\"|\"never\"");
1098 return TCL_ERROR;
1099 }
1100 zCond = Tcl_GetString(objv[1]);
1101 if( strcmp(zCond, "now")==0 ){
1102 async.writerHaltNow = 1;
1103 pthread_cond_broadcast(&async.queueSignal);
1104 }else if( strcmp(zCond, "idle")==0 ){
1105 async.writerHaltWhenIdle = 1;
1106 async.writerHaltNow = 0;
1107 pthread_cond_broadcast(&async.queueSignal);
1108 }else if( strcmp(zCond, "never")==0 ){
1109 async.writerHaltWhenIdle = 0;
1110 async.writerHaltNow = 0;
1111 }else{
1112 Tcl_AppendResult(interp,
1113 "should be one of: \"now\", \"idle\", or \"never\"", (char*)0);
1114 return TCL_ERROR;
1115 }
1116 return TCL_OK;
1117}
1118
1119/*
1120** sqlite3async_delay ?MS?
1121**
1122** Query or set the number of milliseconds of delay in the writer
1123** thread after each write operation. The default is 0. By increasing
1124** the memory delay we can simulate the effect of slow disk I/O.
1125*/
1126static int testAsyncDelay(
1127 void * clientData,
1128 Tcl_Interp *interp,
1129 int objc,
1130 Tcl_Obj *CONST objv[]
1131){
1132 if( objc!=1 && objc!=2 ){
1133 Tcl_WrongNumArgs(interp, 1, objv, "?MS?");
1134 return TCL_ERROR;
1135 }
1136 if( objc==1 ){
1137 Tcl_SetObjResult(interp, Tcl_NewIntObj(async.ioDelay));
1138 }else{
1139 int ioDelay;
1140 if( Tcl_GetIntFromObj(interp, objv[1], &ioDelay) ) return TCL_ERROR;
1141 async.ioDelay = ioDelay;
1142 }
1143 return TCL_OK;
1144}
1145
1146/*
1147** sqlite3async_start
1148**
1149** Start a new writer thread.
1150*/
1151static int testAsyncStart(
1152 void * clientData,
1153 Tcl_Interp *interp,
1154 int objc,
1155 Tcl_Obj *CONST objv[]
1156){
1157 pthread_t x;
1158 int rc;
1159 rc = pthread_create(&x, 0, asyncWriterThread, 0);
1160 if( rc ){
1161 Tcl_AppendResult(interp, "failed to create the thread", 0);
1162 return TCL_ERROR;
1163 }
1164 pthread_detach(x);
1165 return TCL_OK;
1166}
1167
1168/*
1169** sqlite3async_wait
1170**
1171** Wait for the current writer thread to terminate.
1172**
1173** If the current writer thread is set to run forever then this
1174** command would block forever. To prevent that, an error is returned.
1175*/
1176static int testAsyncWait(
1177 void * clientData,
1178 Tcl_Interp *interp,
1179 int objc,
1180 Tcl_Obj *CONST objv[]
1181){
drh89ea9312006-02-13 17:03:47 +00001182 int cnt = 10;
drh23669402006-01-09 17:29:52 +00001183 if( async.writerHaltNow==0 && async.writerHaltWhenIdle==0 ){
1184 Tcl_AppendResult(interp, "would block forever", (char*)0);
1185 return TCL_ERROR;
1186 }
danielk1977750b03e2006-02-14 10:48:39 +00001187
drh89ea9312006-02-13 17:03:47 +00001188 while( cnt-- && !pthread_mutex_trylock(&async.writerMutex) ){
1189 pthread_mutex_unlock(&async.writerMutex);
1190 sched_yield();
1191 }
1192 if( cnt>=0 ){
1193 TRACE(("WAIT\n"));
1194 pthread_mutex_lock(&async.queueMutex);
1195 pthread_cond_broadcast(&async.queueSignal);
1196 pthread_mutex_unlock(&async.queueMutex);
1197 pthread_mutex_lock(&async.writerMutex);
1198 pthread_mutex_unlock(&async.writerMutex);
1199 }else{
drh97bbdc02006-02-13 18:35:06 +00001200 TRACE(("NO-WAIT\n"));
drh89ea9312006-02-13 17:03:47 +00001201 }
drh23669402006-01-09 17:29:52 +00001202 return TCL_OK;
1203}
1204
1205
1206#endif /* OS_UNIX and THREADSAFE and defined(SQLITE_ENABLE_REDEF_IO) */
1207
1208/*
1209** This routine registers the custom TCL commands defined in this
1210** module. This should be the only procedure visible from outside
1211** of this module.
1212*/
1213int Sqlitetestasync_Init(Tcl_Interp *interp){
1214#if OS_UNIX && THREADSAFE && defined(SQLITE_ENABLE_REDEF_IO)
1215 Tcl_CreateObjCommand(interp,"sqlite3async_enable",testAsyncEnable,0,0);
1216 Tcl_CreateObjCommand(interp,"sqlite3async_halt",testAsyncHalt,0,0);
1217 Tcl_CreateObjCommand(interp,"sqlite3async_delay",testAsyncDelay,0,0);
1218 Tcl_CreateObjCommand(interp,"sqlite3async_start",testAsyncStart,0,0);
1219 Tcl_CreateObjCommand(interp,"sqlite3async_wait",testAsyncWait,0,0);
drh99681db2006-02-13 15:29:32 +00001220 Tcl_LinkVar(interp, "sqlite3async_trace",
1221 (char*)&sqlite3async_trace, TCL_LINK_INT);
drh23669402006-01-09 17:29:52 +00001222#endif /* OS_UNIX and THREADSAFE and defined(SQLITE_ENABLE_REDEF_IO) */
1223 return TCL_OK;
1224}