xref: /sqlite-3.40.0/ext/async/sqlite3async.h (revision debcfd2d)
1a3f06598Sdanielk1977 
2a3f06598Sdanielk1977 #ifndef __SQLITEASYNC_H_
3a3f06598Sdanielk1977 #define __SQLITEASYNC_H_ 1
4a3f06598Sdanielk1977 
5a3f06598Sdanielk1977 #define SQLITEASYNC_VFSNAME "sqlite3async"
6a3f06598Sdanielk1977 
7a3f06598Sdanielk1977 /*
8*debcfd2dSdanielk1977 ** THREAD SAFETY NOTES:
9*debcfd2dSdanielk1977 **
10*debcfd2dSdanielk1977 ** Of the four API functions in this file, the following are not threadsafe:
11*debcfd2dSdanielk1977 **
12*debcfd2dSdanielk1977 **   sqlite3async_initialize()
13*debcfd2dSdanielk1977 **   sqlite3async_shutdown()
14*debcfd2dSdanielk1977 **
15*debcfd2dSdanielk1977 ** Care must be taken that neither of these functions is called while
16*debcfd2dSdanielk1977 ** another thread may be calling either any sqlite3async_XXX() function
17*debcfd2dSdanielk1977 ** or an sqlite3_XXX() API function related to a database handle that
18*debcfd2dSdanielk1977 ** is using the asynchronous IO VFS.
19*debcfd2dSdanielk1977 **
20*debcfd2dSdanielk1977 ** These functions:
21*debcfd2dSdanielk1977 **
22*debcfd2dSdanielk1977 **   sqlite3async_run()
23*debcfd2dSdanielk1977 **   sqlite3async_control()
24*debcfd2dSdanielk1977 **
25*debcfd2dSdanielk1977 ** are threadsafe. It is quite safe to call either of these functions even
26*debcfd2dSdanielk1977 ** if another thread may also be calling one of them or an sqlite3_XXX()
27*debcfd2dSdanielk1977 ** function related to a database handle that uses the asynchronous IO VFS.
28*debcfd2dSdanielk1977 */
29*debcfd2dSdanielk1977 
30*debcfd2dSdanielk1977 /*
31*debcfd2dSdanielk1977 ** Initialize the asynchronous IO VFS and register it with SQLite using
32*debcfd2dSdanielk1977 ** sqlite3_vfs_register(). If the asynchronous VFS is already initialized
33*debcfd2dSdanielk1977 ** and registered, this function is a no-op. The asynchronous IO VFS
34*debcfd2dSdanielk1977 ** is registered as "sqlite3async".
35*debcfd2dSdanielk1977 **
36*debcfd2dSdanielk1977 ** The asynchronous IO VFS does not make operating system IO requests
37*debcfd2dSdanielk1977 ** directly. Instead, it uses an existing VFS implementation for all
38*debcfd2dSdanielk1977 ** required file-system operations. If the first parameter to this function
39*debcfd2dSdanielk1977 ** is NULL, then the current default VFS is used for IO. If it is not
40*debcfd2dSdanielk1977 ** NULL, then it must be the name of an existing VFS. In other words, the
41*debcfd2dSdanielk1977 ** first argument to this function is passed to sqlite3_vfs_find() to
42*debcfd2dSdanielk1977 ** locate the VFS to use for all real IO operations. This VFS is known
43*debcfd2dSdanielk1977 ** as the "parent VFS".
44*debcfd2dSdanielk1977 **
45*debcfd2dSdanielk1977 ** If the second parameter to this function is non-zero, then the
46*debcfd2dSdanielk1977 ** asynchronous IO VFS is registered as the default VFS for all SQLite
47*debcfd2dSdanielk1977 ** database connections within the process. Otherwise, the asynchronous IO
48*debcfd2dSdanielk1977 ** VFS is only used by connections opened using sqlite3_open_v2() that
49*debcfd2dSdanielk1977 ** specifically request VFS "sqlite3async".
50*debcfd2dSdanielk1977 **
51*debcfd2dSdanielk1977 ** If a parent VFS cannot be located, then SQLITE_ERROR is returned.
52*debcfd2dSdanielk1977 ** In the unlikely event that operating system specific initialization
53*debcfd2dSdanielk1977 ** fails (win32 systems create the required critical section and event
54*debcfd2dSdanielk1977 ** objects within this function), then SQLITE_ERROR is also returned.
55*debcfd2dSdanielk1977 ** Finally, if the call to sqlite3_vfs_register() returns an error, then
56*debcfd2dSdanielk1977 ** the error code is returned to the user by this function. In all three
57*debcfd2dSdanielk1977 ** of these cases, intialization has failed and the asynchronous IO VFS
58*debcfd2dSdanielk1977 ** is not registered with SQLite.
59*debcfd2dSdanielk1977 **
60*debcfd2dSdanielk1977 ** Otherwise, if no error occurs, SQLITE_OK is returned.
61a3f06598Sdanielk1977 */
62a3f06598Sdanielk1977 int sqlite3async_initialize(const char *zParent, int isDefault);
63a3f06598Sdanielk1977 
64a3f06598Sdanielk1977 /*
65*debcfd2dSdanielk1977 ** This function unregisters the asynchronous IO VFS using
66*debcfd2dSdanielk1977 ** sqlite3_vfs_unregister().
67*debcfd2dSdanielk1977 **
68*debcfd2dSdanielk1977 ** On win32 platforms, this function also releases the small number of
69*debcfd2dSdanielk1977 ** critical section and event objects created by sqlite3async_initialize().
70a3f06598Sdanielk1977 */
71a3f06598Sdanielk1977 void sqlite3async_shutdown();
72a3f06598Sdanielk1977 
73a3f06598Sdanielk1977 /*
74*debcfd2dSdanielk1977 ** This function may only be called when the asynchronous IO VFS is
75*debcfd2dSdanielk1977 ** installed (after a call to sqlite3async_initialize()). It processes
76*debcfd2dSdanielk1977 ** zero or more queued write operations before returning. It is expected
77*debcfd2dSdanielk1977 ** (but not required) that this function will be called by a different
78*debcfd2dSdanielk1977 ** thread than those threads that use SQLite. The "background thread"
79*debcfd2dSdanielk1977 ** that performs IO.
80*debcfd2dSdanielk1977 **
81*debcfd2dSdanielk1977 ** How many queued write operations are performed before returning
82*debcfd2dSdanielk1977 ** depends on the global setting configured by passing the SQLITEASYNC_HALT
83*debcfd2dSdanielk1977 ** verb to sqlite3async_control() (see below for details). By default
84*debcfd2dSdanielk1977 ** this function never returns - it processes all pending operations and
85*debcfd2dSdanielk1977 ** then blocks waiting for new ones.
86*debcfd2dSdanielk1977 **
87*debcfd2dSdanielk1977 ** If multiple simultaneous calls are made to sqlite3async_run() from two
88*debcfd2dSdanielk1977 ** or more threads, then the calls are serialized internally.
89a3f06598Sdanielk1977 */
90a3f06598Sdanielk1977 void sqlite3async_run();
91a3f06598Sdanielk1977 
92a3f06598Sdanielk1977 /*
93*debcfd2dSdanielk1977 ** This function may only be called when the asynchronous IO VFS is
94*debcfd2dSdanielk1977 ** installed (after a call to sqlite3async_initialize()). It is used
95*debcfd2dSdanielk1977 ** to query or configure various parameters that affect the operation
96*debcfd2dSdanielk1977 ** of the asynchronous IO VFS. At present there are three parameters
97*debcfd2dSdanielk1977 ** supported:
98*debcfd2dSdanielk1977 **
99*debcfd2dSdanielk1977 **   * The "halt" parameter, which configures the circumstances under
100*debcfd2dSdanielk1977 **     which the sqlite3async_run() parameter is configured.
101*debcfd2dSdanielk1977 **
102*debcfd2dSdanielk1977 **   * The "delay" parameter. Setting the delay parameter to a non-zero
103*debcfd2dSdanielk1977 **     value causes the sqlite3async_run() function to sleep for the
104*debcfd2dSdanielk1977 **     configured number of milliseconds between each queued write
105*debcfd2dSdanielk1977 **     operation.
106*debcfd2dSdanielk1977 **
107*debcfd2dSdanielk1977 **   * The "lockfiles" parameter. This parameter determines whether or
108*debcfd2dSdanielk1977 **     not the asynchronous IO VFS locks the database files it operates
109*debcfd2dSdanielk1977 **     on. Disabling file locking can improve throughput.
110*debcfd2dSdanielk1977 **
111*debcfd2dSdanielk1977 ** This function is always passed two arguments. When setting the value
112*debcfd2dSdanielk1977 ** of a parameter, the first argument must be one of SQLITEASYNC_HALT,
113*debcfd2dSdanielk1977 ** SQLITEASYNC_DELAY or SQLITEASYNC_LOCKFILES. The second argument must
114*debcfd2dSdanielk1977 ** be passed the new value for the parameter as type "int".
115*debcfd2dSdanielk1977 **
116*debcfd2dSdanielk1977 ** When querying the current value of a paramter, the first argument must
117*debcfd2dSdanielk1977 ** be one of SQLITEASYNC_GET_HALT, GET_DELAY or GET_LOCKFILES. The second
118*debcfd2dSdanielk1977 ** argument to this function must be of type (int *). The current value
119*debcfd2dSdanielk1977 ** of the queried parameter is copied to the memory pointed to by the
120*debcfd2dSdanielk1977 ** second argument. For example:
121*debcfd2dSdanielk1977 **
122*debcfd2dSdanielk1977 **   int eCurrentHalt;
123*debcfd2dSdanielk1977 **   int eNewHalt = SQLITEASYNC_HALT_IDLE;
124*debcfd2dSdanielk1977 **
125*debcfd2dSdanielk1977 **   sqlite3async_control(SQLITEASYNC_HALT, eNewHalt);
126*debcfd2dSdanielk1977 **   sqlite3async_control(SQLITEASYNC_GET_HALT, &eCurrentHalt);
127*debcfd2dSdanielk1977 **   assert( eNewHalt==eCurrentHalt );
128*debcfd2dSdanielk1977 **
129*debcfd2dSdanielk1977 ** See below for more detail on each configuration parameter.
130*debcfd2dSdanielk1977 **
131*debcfd2dSdanielk1977 ** SQLITEASYNC_HALT:
132*debcfd2dSdanielk1977 **
133*debcfd2dSdanielk1977 **   This is used to set the value of the "halt" parameter. The second
134*debcfd2dSdanielk1977 **   argument must be one of the SQLITEASYNC_HALT_XXX symbols defined
135*debcfd2dSdanielk1977 **   below (either NEVER, IDLE and NOW).
136*debcfd2dSdanielk1977 **
137*debcfd2dSdanielk1977 **   If the parameter is set to NEVER, then calls to sqlite3async_run()
138*debcfd2dSdanielk1977 **   never return. This is the default setting. If the parameter is set
139*debcfd2dSdanielk1977 **   to IDLE, then calls to sqlite3async_run() return as soon as the
140*debcfd2dSdanielk1977 **   queue of pending write operations is empty. If the parameter is set
141*debcfd2dSdanielk1977 **   to NOW, then calls to sqlite3async_run() return as quickly as
142*debcfd2dSdanielk1977 **   possible, without processing any pending write requests.
143*debcfd2dSdanielk1977 **
144*debcfd2dSdanielk1977 **   If an attempt is made to set this parameter to an integer value other
145*debcfd2dSdanielk1977 **   than SQLITEASYNC_HALT_NEVER, IDLE or NOW, then sqlite3async_control()
146*debcfd2dSdanielk1977 **   returns SQLITE_MISUSE and the current value of the parameter is not
147*debcfd2dSdanielk1977 **   modified.
148*debcfd2dSdanielk1977 **
149*debcfd2dSdanielk1977 **   Modifying the "halt" parameter affects calls to sqlite3async_run()
150*debcfd2dSdanielk1977 **   made by other threads that are currently in progress.
151*debcfd2dSdanielk1977 **
152*debcfd2dSdanielk1977 ** SQLITEASYNC_DELAY:
153*debcfd2dSdanielk1977 **
154*debcfd2dSdanielk1977 **   This is used to set the value of the "delay" parameter. If set to
155*debcfd2dSdanielk1977 **   a non-zero value, then after completing a pending write request, the
156*debcfd2dSdanielk1977 **   sqlite3async_run() function sleeps for the configured number of
157*debcfd2dSdanielk1977 **   milliseconds.
158*debcfd2dSdanielk1977 **
159*debcfd2dSdanielk1977 **   If an attempt is made to set this parameter to a negative value,
160*debcfd2dSdanielk1977 **   sqlite3async_control() returns SQLITE_MISUSE and the current value
161*debcfd2dSdanielk1977 **   of the parameter is not modified.
162*debcfd2dSdanielk1977 **
163*debcfd2dSdanielk1977 **   Modifying the "delay" parameter affects calls to sqlite3async_run()
164*debcfd2dSdanielk1977 **   made by other threads that are currently in progress.
165*debcfd2dSdanielk1977 **
166*debcfd2dSdanielk1977 ** SQLITEASYNC_LOCKFILES:
167*debcfd2dSdanielk1977 **
168*debcfd2dSdanielk1977 **   This is used to set the value of the "lockfiles" parameter. This
169*debcfd2dSdanielk1977 **   parameter must be set to either 0 or 1. If set to 1, then the
170*debcfd2dSdanielk1977 **   asynchronous IO VFS uses the xLock() and xUnlock() methods of the
171*debcfd2dSdanielk1977 **   parent VFS to lock database files being read and/or written. If
172*debcfd2dSdanielk1977 **   the parameter is set to 0, then these locks are omitted.
173*debcfd2dSdanielk1977 **
174*debcfd2dSdanielk1977 **   This parameter may only be set when there are no open database
175*debcfd2dSdanielk1977 **   connections using the VFS and the queue of pending write requests
176*debcfd2dSdanielk1977 **   is empty. Attempting to set it when this is not true, or to set it
177*debcfd2dSdanielk1977 **   to a value other than 0 or 1 causes sqlite3async_control() to return
178*debcfd2dSdanielk1977 **   SQLITE_MISUSE and the value of the parameter to remain unchanged.
179*debcfd2dSdanielk1977 **
180*debcfd2dSdanielk1977 **   If this parameter is set to zero, then it is only safe to access the
181*debcfd2dSdanielk1977 **   database via the asynchronous IO VFS from within a single process. If
182*debcfd2dSdanielk1977 **   while writing to the database via the asynchronous IO VFS the database
183*debcfd2dSdanielk1977 **   is also read or written from within another process, or via another
184*debcfd2dSdanielk1977 **   connection that does not use the asynchronous IO VFS within the same
185*debcfd2dSdanielk1977 **   process, the results are undefined (and may include crashes or database
186*debcfd2dSdanielk1977 **   corruption).
187*debcfd2dSdanielk1977 **
188*debcfd2dSdanielk1977 **   Alternatively, if this parameter is set to 1, then it is safe to access
189*debcfd2dSdanielk1977 **   the database from multiple connections within multiple processes using
190*debcfd2dSdanielk1977 **   either the asynchronous IO VFS or the parent VFS directly.
191a3f06598Sdanielk1977 */
192a3f06598Sdanielk1977 int sqlite3async_control(int op, ...);
193a3f06598Sdanielk1977 
194a3f06598Sdanielk1977 /*
195a3f06598Sdanielk1977 ** Values that can be used as the first argument to sqlite3async_control().
196a3f06598Sdanielk1977 */
197a3f06598Sdanielk1977 #define SQLITEASYNC_HALT          1
198*debcfd2dSdanielk1977 #define SQLITEASYNC_GET_HALT      2
199*debcfd2dSdanielk1977 #define SQLITEASYNC_DELAY         3
200a3f06598Sdanielk1977 #define SQLITEASYNC_GET_DELAY     4
201*debcfd2dSdanielk1977 #define SQLITEASYNC_LOCKFILES     5
202*debcfd2dSdanielk1977 #define SQLITEASYNC_GET_LOCKFILES 6
203a3f06598Sdanielk1977 
204a3f06598Sdanielk1977 /*
205a3f06598Sdanielk1977 ** If the first argument to sqlite3async_control() is SQLITEASYNC_HALT,
206a3f06598Sdanielk1977 ** the second argument should be one of the following.
207a3f06598Sdanielk1977 */
208a3f06598Sdanielk1977 #define SQLITEASYNC_HALT_NEVER 0       /* Never halt (default value) */
209a3f06598Sdanielk1977 #define SQLITEASYNC_HALT_NOW   1       /* Halt as soon as possible */
210a3f06598Sdanielk1977 #define SQLITEASYNC_HALT_IDLE  2       /* Halt when write-queue is empty */
211a3f06598Sdanielk1977 
212a3f06598Sdanielk1977 #endif        /* ifndef __SQLITEASYNC_H_ */
213a3f06598Sdanielk1977 
214