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