sqlite3async.h 9.2 KB

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