| 1 |
<!DOCTYPE html> |
| 2 |
<html><head> |
| 3 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 4 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 5 |
<link href="sqlite.css" rel="stylesheet"> |
| 6 |
<title>SQLite Unlock-Notify API</title> |
| 7 |
<!-- path= --> |
| 8 |
</head> |
| 9 |
<body> |
| 10 |
<div class=nosearch> |
| 11 |
<a href="index.html"> |
| 12 |
<img class="logo" src="images/sqlite370_banner.svg" alt="SQLite" border="0"> |
| 13 |
</a> |
| 14 |
<div><!-- IE hack to prevent disappearing logo --></div> |
| 15 |
<div class="tagline desktoponly"> |
| 16 |
Small. Fast. Reliable.<br>Choose any three. |
| 17 |
</div> |
| 18 |
<div class="menu mainmenu"> |
| 19 |
<ul> |
| 20 |
<li><a href="index.html">Home</a> |
| 21 |
<li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a> |
| 22 |
<li class='wideonly'><a href='about.html'>About</a> |
| 23 |
<li class='desktoponly'><a href="docs.html">Documentation</a> |
| 24 |
<li class='desktoponly'><a href="download.html">Download</a> |
| 25 |
<li class='wideonly'><a href='copyright.html'>License</a> |
| 26 |
<li class='desktoponly'><a href="support.html">Support</a> |
| 27 |
<li class='desktoponly'><a href="prosupport.html">Purchase</a> |
| 28 |
<li class='search' id='search_menubutton'> |
| 29 |
<a href="javascript:void(0)" onclick='toggle_search()'>Search</a> |
| 30 |
</ul> |
| 31 |
</div> |
| 32 |
<div class="menu submenu" id="submenu"> |
| 33 |
<ul> |
| 34 |
<li><a href='about.html'>About</a> |
| 35 |
<li><a href='docs.html'>Documentation</a> |
| 36 |
<li><a href='download.html'>Download</a> |
| 37 |
<li><a href='support.html'>Support</a> |
| 38 |
<li><a href='prosupport.html'>Purchase</a> |
| 39 |
</ul> |
| 40 |
</div> |
| 41 |
<div class="searchmenu" id="searchmenu"> |
| 42 |
<form method="GET" action="search"> |
| 43 |
<select name="s" id="searchtype"> |
| 44 |
<option value="d">Search Documentation</option> |
| 45 |
<option value="c">Search Changelog</option> |
| 46 |
</select> |
| 47 |
<input type="text" name="q" id="searchbox" value=""> |
| 48 |
<input type="submit" value="Go"> |
| 49 |
</form> |
| 50 |
</div> |
| 51 |
</div> |
| 52 |
<script> |
| 53 |
function toggle_div(nm) { |
| 54 |
var w = document.getElementById(nm); |
| 55 |
if( w.style.display=="block" ){ |
| 56 |
w.style.display = "none"; |
| 57 |
}else{ |
| 58 |
w.style.display = "block"; |
| 59 |
} |
| 60 |
} |
| 61 |
function toggle_search() { |
| 62 |
var w = document.getElementById("searchmenu"); |
| 63 |
if( w.style.display=="block" ){ |
| 64 |
w.style.display = "none"; |
| 65 |
} else { |
| 66 |
w.style.display = "block"; |
| 67 |
setTimeout(function(){ |
| 68 |
document.getElementById("searchbox").focus() |
| 69 |
}, 30); |
| 70 |
} |
| 71 |
} |
| 72 |
function div_off(nm){document.getElementById(nm).style.display="none";} |
| 73 |
window.onbeforeunload = function(e){div_off("submenu");} |
| 74 |
/* Disable the Search feature if we are not operating from CGI, since */ |
| 75 |
/* Search is accomplished using CGI and will not work without it. */ |
| 76 |
if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){ |
| 77 |
document.getElementById("search_menubutton").style.display = "none"; |
| 78 |
} |
| 79 |
/* Used by the Hide/Show button beside syntax diagrams, to toggle the */ |
| 80 |
function hideorshow(btn,obj){ |
| 81 |
var x = document.getElementById(obj); |
| 82 |
var b = document.getElementById(btn); |
| 83 |
if( x.style.display!='none' ){ |
| 84 |
x.style.display = 'none'; |
| 85 |
b.innerHTML='show'; |
| 86 |
}else{ |
| 87 |
x.style.display = ''; |
| 88 |
b.innerHTML='hide'; |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
var antiRobot = 0; |
| 93 |
function antiRobotGo(){ |
| 94 |
if( antiRobot!=3 ) return; |
| 95 |
antiRobot = 7; |
| 96 |
var j = document.getElementById("mtimelink"); |
| 97 |
if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href"); |
| 98 |
} |
| 99 |
function antiRobotDefense(){ |
| 100 |
document.body.onmousedown=function(){ |
| 101 |
antiRobot |= 2; |
| 102 |
antiRobotGo(); |
| 103 |
document.body.onmousedown=null; |
| 104 |
} |
| 105 |
document.body.onmousemove=function(){ |
| 106 |
antiRobot |= 2; |
| 107 |
antiRobotGo(); |
| 108 |
document.body.onmousemove=null; |
| 109 |
} |
| 110 |
setTimeout(function(){ |
| 111 |
antiRobot |= 1; |
| 112 |
antiRobotGo(); |
| 113 |
}, 100) |
| 114 |
antiRobotGo(); |
| 115 |
} |
| 116 |
antiRobotDefense(); |
| 117 |
</script> |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
<style> |
| 122 |
pre a:visited, pre a:link { text-decoration: none ; color: #40534b } |
| 123 |
pre { |
| 124 |
background: #F3F3F3; |
| 125 |
float: right; |
| 126 |
padding: 1ex 2ex; |
| 127 |
margin-left: 1em; |
| 128 |
border: solid black 1px; |
| 129 |
} |
| 130 |
h1,h2 { clear: both ; text-align: center } |
| 131 |
</style> |
| 132 |
|
| 133 |
<h1>Using the sqlite3_unlock_notify() API</h1> |
| 134 |
|
| 135 |
<pre> |
| 136 |
<span style="color:blue;font-style:italic">/* This example uses the pthreads API */</span> |
| 137 |
#include <pthread.h> |
| 138 |
|
| 139 |
<span style="color:blue;font-style:italic">/*</span> |
| 140 |
<span style="color:blue;font-style:italic">** A pointer to an instance of this structure is passed as the user-context</span> |
| 141 |
<span style="color:blue;font-style:italic">** pointer when registering for an unlock-notify callback.</span> |
| 142 |
<span style="color:blue;font-style:italic">*/</span> |
| 143 |
typedef struct UnlockNotification UnlockNotification; |
| 144 |
struct UnlockNotification { |
| 145 |
int fired; <span style="color:blue;font-style:italic">/* True after unlock event has occurred */</span> |
| 146 |
pthread_cond_t cond; <span style="color:blue;font-style:italic">/* Condition variable to wait on */</span> |
| 147 |
pthread_mutex_t mutex; <span style="color:blue;font-style:italic">/* Mutex to protect structure */</span> |
| 148 |
}; |
| 149 |
|
| 150 |
<span style="color:blue;font-style:italic">/*</span> |
| 151 |
<span style="color:blue;font-style:italic">** This function is an unlock-notify callback registered with SQLite.</span> |
| 152 |
<span style="color:blue;font-style:italic">*/</span> |
| 153 |
static void unlock_notify_cb(void **apArg, int nArg){ |
| 154 |
int i; |
| 155 |
for(i=0; i<nArg; i++){ |
| 156 |
UnlockNotification *p = (UnlockNotification *)apArg[i]; |
| 157 |
pthread_mutex_lock(&p->mutex); |
| 158 |
p->fired = 1; |
| 159 |
pthread_cond_signal(&p->cond); |
| 160 |
pthread_mutex_unlock(&p->mutex); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
<span style="color:blue;font-style:italic">/*</span> |
| 165 |
<span style="color:blue;font-style:italic">** This function assumes that an SQLite API call (either <a href="c3ref/prepare.html">sqlite3_prepare_v2</a>() </span> |
| 166 |
<span style="color:blue;font-style:italic">** or <a href="c3ref/step.html">sqlite3_step</a>()) has just returned SQLITE_LOCKED. The argument is the</span> |
| 167 |
<span style="color:blue;font-style:italic">** associated database connection.</span> |
| 168 |
<span style="color:blue;font-style:italic">**</span> |
| 169 |
<span style="color:blue;font-style:italic">** This function calls <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify</a>() to register for an </span> |
| 170 |
<span style="color:blue;font-style:italic">** unlock-notify callback, then blocks until that callback is delivered </span> |
| 171 |
<span style="color:blue;font-style:italic">** and returns SQLITE_OK. The caller should then retry the failed operation.</span> |
| 172 |
<span style="color:blue;font-style:italic">**</span> |
| 173 |
<span style="color:blue;font-style:italic">** Or, if <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify</a>() indicates that to block would deadlock </span> |
| 174 |
<span style="color:blue;font-style:italic">** the system, then this function returns SQLITE_LOCKED immediately. In </span> |
| 175 |
<span style="color:blue;font-style:italic">** this case the caller should not retry the operation and should roll </span> |
| 176 |
<span style="color:blue;font-style:italic">** back the current transaction (if any).</span> |
| 177 |
<span style="color:blue;font-style:italic">*/</span> |
| 178 |
static int wait_for_unlock_notify(<a href="c3ref/sqlite3.html">sqlite3</a> *db){ |
| 179 |
int rc; |
| 180 |
UnlockNotification un; |
| 181 |
|
| 182 |
<span style="color:blue;font-style:italic">/* Initialize the UnlockNotification structure. */</span> |
| 183 |
un.fired = 0; |
| 184 |
pthread_mutex_init(&un.mutex, 0); |
| 185 |
pthread_cond_init(&un.cond, 0); |
| 186 |
|
| 187 |
<span style="color:blue;font-style:italic">/* Register for an unlock-notify callback. */</span> |
| 188 |
rc = <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify</a>(db, unlock_notify_cb, (void *)&un); |
| 189 |
assert( rc==SQLITE_LOCKED || rc==SQLITE_OK ); |
| 190 |
|
| 191 |
<span style="color:blue;font-style:italic">/* The call to <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify</a>() always returns either SQLITE_LOCKED </span> |
| 192 |
<span style="color:blue;font-style:italic"> ** or SQLITE_OK. </span> |
| 193 |
<span style="color:blue;font-style:italic"> **</span> |
| 194 |
<span style="color:blue;font-style:italic"> ** If SQLITE_LOCKED was returned, then the system is deadlocked. In this</span> |
| 195 |
<span style="color:blue;font-style:italic"> ** case this function needs to return SQLITE_LOCKED to the caller so </span> |
| 196 |
<span style="color:blue;font-style:italic"> ** that the current transaction can be rolled back. Otherwise, block</span> |
| 197 |
<span style="color:blue;font-style:italic"> ** until the unlock-notify callback is invoked, then return SQLITE_OK.</span> |
| 198 |
<span style="color:blue;font-style:italic">*/</span> |
| 199 |
if( rc==SQLITE_OK ){ |
| 200 |
pthread_mutex_lock(&un.mutex); |
| 201 |
if( !un.fired ){ |
| 202 |
pthread_cond_wait(&un.cond, &un.mutex); |
| 203 |
} |
| 204 |
pthread_mutex_unlock(&un.mutex); |
| 205 |
} |
| 206 |
|
| 207 |
<span style="color:blue;font-style:italic">/* Destroy the mutex and condition variables. */</span> |
| 208 |
pthread_cond_destroy(&un.cond); |
| 209 |
pthread_mutex_destroy(&un.mutex); |
| 210 |
|
| 211 |
return rc; |
| 212 |
} |
| 213 |
|
| 214 |
<span style="color:blue;font-style:italic">/*</span> |
| 215 |
<span style="color:blue;font-style:italic">** This function is a wrapper around the SQLite function <a href="c3ref/step.html">sqlite3_step</a>().</span> |
| 216 |
<span style="color:blue;font-style:italic">** It functions in the same way as step(), except that if a required</span> |
| 217 |
<span style="color:blue;font-style:italic">** shared-cache lock cannot be obtained, this function may block waiting for</span> |
| 218 |
<span style="color:blue;font-style:italic">** the lock to become available. In this scenario the normal API step()</span> |
| 219 |
<span style="color:blue;font-style:italic">** function always returns SQLITE_LOCKED.</span> |
| 220 |
<span style="color:blue;font-style:italic">**</span> |
| 221 |
<span style="color:blue;font-style:italic">** If this function returns SQLITE_LOCKED, the caller should rollback</span> |
| 222 |
<span style="color:blue;font-style:italic">** the current transaction (if any) and try again later. Otherwise, the</span> |
| 223 |
<span style="color:blue;font-style:italic">** system may become deadlocked.</span> |
| 224 |
<span style="color:blue;font-style:italic">*/</span> |
| 225 |
int sqlite3_blocking_step(<a href="c3ref/stmt.html">sqlite3_stmt</a> *pStmt){ |
| 226 |
int rc; |
| 227 |
while( SQLITE_LOCKED==(rc = <a href="c3ref/step.html">sqlite3_step</a>(pStmt)) ){ |
| 228 |
rc = wait_for_unlock_notify(<a href="c3ref/db_handle.html">sqlite3_db_handle</a>(pStmt)); |
| 229 |
if( rc!=SQLITE_OK ) break; |
| 230 |
<a href="c3ref/reset.html">sqlite3_reset</a>(pStmt); |
| 231 |
} |
| 232 |
return rc; |
| 233 |
} |
| 234 |
|
| 235 |
<span style="color:blue;font-style:italic">/*</span> |
| 236 |
<span style="color:blue;font-style:italic">** This function is a wrapper around the SQLite function <a href="c3ref/prepare.html">sqlite3_prepare_v2</a>().</span> |
| 237 |
<span style="color:blue;font-style:italic">** It functions in the same way as prepare_v2(), except that if a required</span> |
| 238 |
<span style="color:blue;font-style:italic">** shared-cache lock cannot be obtained, this function may block waiting for</span> |
| 239 |
<span style="color:blue;font-style:italic">** the lock to become available. In this scenario the normal API prepare_v2()</span> |
| 240 |
<span style="color:blue;font-style:italic">** function always returns SQLITE_LOCKED.</span> |
| 241 |
<span style="color:blue;font-style:italic">**</span> |
| 242 |
<span style="color:blue;font-style:italic">** If this function returns SQLITE_LOCKED, the caller should rollback</span> |
| 243 |
<span style="color:blue;font-style:italic">** the current transaction (if any) and try again later. Otherwise, the</span> |
| 244 |
<span style="color:blue;font-style:italic">** system may become deadlocked.</span> |
| 245 |
<span style="color:blue;font-style:italic">*/</span> |
| 246 |
int sqlite3_blocking_prepare_v2( |
| 247 |
<a href="c3ref/sqlite3.html">sqlite3</a> *db, <span style="color:blue;font-style:italic">/* Database handle. */</span> |
| 248 |
const char *zSql, <span style="color:blue;font-style:italic">/* UTF-8 encoded SQL statement. */</span> |
| 249 |
int nSql, <span style="color:blue;font-style:italic">/* Length of zSql in bytes. */</span> |
| 250 |
<a href="c3ref/stmt.html">sqlite3_stmt</a> **ppStmt, <span style="color:blue;font-style:italic">/* OUT: A pointer to the prepared statement */</span> |
| 251 |
const char **pz <span style="color:blue;font-style:italic">/* OUT: End of parsed string */</span> |
| 252 |
){ |
| 253 |
int rc; |
| 254 |
while( SQLITE_LOCKED==(rc = <a href="c3ref/prepare.html">sqlite3_prepare_v2</a>(db, zSql, nSql, ppStmt, pz)) ){ |
| 255 |
rc = wait_for_unlock_notify(db); |
| 256 |
if( rc!=SQLITE_OK ) break; |
| 257 |
} |
| 258 |
return rc; |
| 259 |
} |
| 260 |
</pre> |
| 261 |
|
| 262 |
|
| 263 |
<p> |
| 264 |
When two or more connections access the same database in shared-cache |
| 265 |
mode, read and write (shared and exclusive) locks on individual tables |
| 266 |
are used to ensure that concurrently executing transactions are kept |
| 267 |
isolated. Before writing to a table, a write (exclusive) lock must be |
| 268 |
obtained on that table. Before reading, a read (shared) lock must be |
| 269 |
obtained. A connection releases all held table locks when it concludes |
| 270 |
its transaction. If a connection cannot obtain a required lock, then |
| 271 |
the call to <a href="c3ref/step.html">sqlite3_step()</a> returns SQLITE_LOCKED. |
| 272 |
|
| 273 |
<p> |
| 274 |
Although it is less common, a call to <a href="c3ref/prepare.html">sqlite3_prepare()</a> or |
| 275 |
<a href="c3ref/prepare.html">sqlite3_prepare_v2()</a> may also return SQLITE_LOCKED if it cannot obtain |
| 276 |
a read-lock on the <a href="schematab.html">sqlite_schema table</a> of each attached database. These |
| 277 |
APIs need to read the schema data contained in the sqlite_schema table |
| 278 |
in order to compile SQL statements to <a href="c3ref/stmt.html">sqlite3_stmt*</a> objects. |
| 279 |
|
| 280 |
<p> |
| 281 |
This article presents a technique using the SQLite <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> |
| 282 |
interface such that calls to <a href="c3ref/step.html">sqlite3_step()</a> and <a href="c3ref/prepare.html">sqlite3_prepare_v2()</a> |
| 283 |
block until the required locks are available instead of returning |
| 284 |
SQLITE_LOCKED immediately. If the |
| 285 |
sqlite3_blocking_step() or sqlite3_blocking_prepare_v2() functions presented |
| 286 |
to the right return SQLITE_LOCKED, this indicates that to block would |
| 287 |
deadlock the system. |
| 288 |
|
| 289 |
<p> |
| 290 |
The <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> API, which is only available if the library is |
| 291 |
compiled with the pre-processor symbol <a href="compile.html#enable_unlock_notify">SQLITE_ENABLE_UNLOCK_NOTIFY</a> defined, |
| 292 |
is <a href="c3ref/unlock_notify.html">documented here</a>. This article is not a |
| 293 |
substitute for reading the full API documentation! |
| 294 |
|
| 295 |
<p> |
| 296 |
The <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> interface is designed for use in systems |
| 297 |
that have a separate thread assigned to each <a href="c3ref/sqlite3.html">database connection</a>. There |
| 298 |
is nothing in the implementation that prevents a single thread from running |
| 299 |
multiple database connections. However, the <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> |
| 300 |
interface only works on a single connection at a time, so the lock |
| 301 |
resolution logic presented here will only work for a single |
| 302 |
database connection per thread. |
| 303 |
|
| 304 |
<p><b>The sqlite3_unlock_notify() API</b> |
| 305 |
|
| 306 |
<p> |
| 307 |
After a call to <a href="c3ref/step.html">sqlite3_step()</a> or <a href="c3ref/prepare.html">sqlite3_prepare_v2()</a> returns |
| 308 |
SQLITE_LOCKED, the <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> API may be invoked to register |
| 309 |
for an unlock-notify callback. The unlock-notify callback is invoked by |
| 310 |
SQLite after the database connection holding the table-lock that prevented |
| 311 |
the call to <a href="c3ref/step.html">sqlite3_step()</a> or <a href="c3ref/prepare.html">sqlite3_prepare_v2()</a> from succeeding has |
| 312 |
finished its transaction and released all locks. For example, if a call to |
| 313 |
sqlite3_step() is an attempt to read from table X, and some other connection |
| 314 |
Y is holding a write-lock on table X, then sqlite3_step() will return |
| 315 |
SQLITE_LOCKED. If <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> is then called, the unlock-notify |
| 316 |
callback will be invoked after connection Y's transaction is concluded. The |
| 317 |
connection that the unlock-notify callback is waiting on, in this case |
| 318 |
connection Y, is known as the "blocking connection". |
| 319 |
|
| 320 |
<p> |
| 321 |
If a call to sqlite3_step() that attempts to write to a database table |
| 322 |
returns SQLITE_LOCKED, then more than one other connection may be holding |
| 323 |
a read-lock on the database table in question. In this case SQLite simply |
| 324 |
selects one of those other connections arbitrarily and issues the |
| 325 |
unlock-notify callback when that connection's transaction is finished. |
| 326 |
Whether the call to sqlite3_step() was blocked by one or many connections, |
| 327 |
when the corresponding unlock-notify callback is issued it is not |
| 328 |
guaranteed that the required lock is available, only that it may be. |
| 329 |
|
| 330 |
<p> |
| 331 |
When the unlock-notify callback is issued, it is issued from within a |
| 332 |
call to sqlite3_step() (or sqlite3_close()) associated with the blocking |
| 333 |
connection. It is illegal to invoke any sqlite3_XXX() API functions from |
| 334 |
within an unlock-notify callback. The expected use is that the unlock-notify |
| 335 |
callback will signal some other waiting thread or schedule some action |
| 336 |
to take place later. |
| 337 |
|
| 338 |
<p> |
| 339 |
The algorithm used by the sqlite3_blocking_step() function is as follows: |
| 340 |
|
| 341 |
<ol> |
| 342 |
<li><p> Call sqlite3_step() on the supplied statement handle. If the call |
| 343 |
returns anything other than SQLITE_LOCKED, then return this value |
| 344 |
to the caller. Otherwise, continue. |
| 345 |
|
| 346 |
<li><p> Invoke <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> on the database connection handle |
| 347 |
associated with the supplied statement handle to register for an |
| 348 |
unlock-notify callback. If the call to unlock_notify() returns |
| 349 |
SQLITE_LOCKED, then return this value to the caller. |
| 350 |
|
| 351 |
<li><p> Block until the unlock-notify callback is invoked by another thread. |
| 352 |
|
| 353 |
<li><p> Call sqlite3_reset() on the statement handle. Since an |
| 354 |
SQLITE_LOCKED error may only occur on the first call to sqlite3_step() |
| 355 |
(it is not possible for one call to sqlite3_step() to return |
| 356 |
SQLITE_ROW and then the next SQLITE_LOCKED), the statement handle may |
| 357 |
be reset at this point without affecting the results of the query |
| 358 |
from the point of view of the caller. If sqlite3_reset() were not |
| 359 |
called at this point, the next call to sqlite3_step() would return |
| 360 |
SQLITE_MISUSE. |
| 361 |
|
| 362 |
<li><p> Return to step 1. |
| 363 |
</ol> |
| 364 |
|
| 365 |
<p> |
| 366 |
The algorithm used by the sqlite3_blocking_prepare_v2() function is similar, |
| 367 |
except that step 4 (resetting the statement handle) is omitted. |
| 368 |
|
| 369 |
|
| 370 |
<p><b>Writer Starvation</b> |
| 371 |
|
| 372 |
<p> |
| 373 |
Multiple connections may hold a read-lock simultaneously. If many threads |
| 374 |
are acquiring overlapping read-locks, it might be the case that at least |
| 375 |
one thread is always holding a read lock. |
| 376 |
Then a table waiting for a write-lock will wait forever. |
| 377 |
This scenario is called "writer starvation." |
| 378 |
|
| 379 |
<p> |
| 380 |
SQLite helps applications avoid writer starvation. After any attempt to |
| 381 |
obtain a write-lock on a table fails (because one or more other |
| 382 |
connections are holding read-locks), all attempts to open new transactions |
| 383 |
on the shared-cache fail until one of the following is true: |
| 384 |
|
| 385 |
<ul> |
| 386 |
<li> The current writer concludes its transaction, OR |
| 387 |
<li> The number of open read-transactions on the shared-cache drops to zero. |
| 388 |
</ul> |
| 389 |
|
| 390 |
<p> |
| 391 |
Failed attempts to open new read-transactions return SQLITE_LOCKED to the |
| 392 |
caller. If the caller then calls <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> to register for |
| 393 |
an unlock-notify callback, the blocking connection is the connection that |
| 394 |
currently has an open write-transaction on the shared-cache. This prevents |
| 395 |
writer-starvation since if no new read-transactions may be opened and |
| 396 |
assuming all existing read-transactions are eventually concluded, the |
| 397 |
writer will eventually have an opportunity to obtain the required |
| 398 |
write-lock. |
| 399 |
|
| 400 |
<p><b>The pthreads API</b> |
| 401 |
|
| 402 |
<p> By the time <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> is invoked by |
| 403 |
wait_for_unlock_notify(), it is possible that the blocking connection |
| 404 |
that prevented the sqlite3_step() or sqlite3_prepare_v2() call from |
| 405 |
succeeding has already finished its transaction. In this case, the |
| 406 |
unlock-notify callback is invoked immediately, before |
| 407 |
<a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> returns. Or, it is possible that the |
| 408 |
unlock-notify callback is invoked by a second thread after |
| 409 |
<a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> is called but before the thread starts waiting |
| 410 |
to be asynchronously signaled. |
| 411 |
|
| 412 |
<p> Exactly how such a potential race-condition is handled depends on the |
| 413 |
threads and synchronization primitives interface used by the application. |
| 414 |
This example uses pthreads, the interface provided by modern UNIX-like |
| 415 |
systems, including Linux. |
| 416 |
|
| 417 |
<p> The pthreads interface provides the pthread_cond_wait() function. |
| 418 |
This function allows the caller to simultaneously release a mutex |
| 419 |
and start waiting for an asynchronous signal. Using this function, |
| 420 |
a "fired" flag and a mutex, the race-condition described above may |
| 421 |
be eliminated as follows: |
| 422 |
|
| 423 |
<p> When the unlock-notify callback is invoked, which may be before the |
| 424 |
thread that called <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> begins waiting for the |
| 425 |
asynchronous signal, it does the following: |
| 426 |
|
| 427 |
<ol> |
| 428 |
<li> Obtains the mutex. |
| 429 |
<li> Sets the "fired" flag to true. |
| 430 |
<li> Attempts to signal a waiting thread. |
| 431 |
<li> Releases the mutex. |
| 432 |
</ol> |
| 433 |
|
| 434 |
<p> When the wait_for_unlock_notify() thread is ready to begin waiting for |
| 435 |
the unlock-notify callback to arrive, it: |
| 436 |
|
| 437 |
<ol> |
| 438 |
<li> Obtains the mutex. |
| 439 |
<li> Checks if the "fired" flag has been set. If so, the unlock-notify |
| 440 |
callback has already been invoked. Release the mutex and continue. |
| 441 |
<li> Atomically releases the mutex and begins waiting for the |
| 442 |
asynchronous signal. When the signal arrives, continue. |
| 443 |
</ol> |
| 444 |
|
| 445 |
<p> This way, it doesn't matter if the unlock-notify callback has already |
| 446 |
been invoked, or is being invoked, when the wait_for_unlock_notify() |
| 447 |
thread begins blocking. |
| 448 |
|
| 449 |
<p><b>Possible Enhancements</b> |
| 450 |
|
| 451 |
<p> The code in this article could be improved in at least two ways: |
| 452 |
|
| 453 |
<ul> |
| 454 |
<li> It could manage thread priorities. |
| 455 |
<li> It could handle a special case of SQLITE_LOCKED that can occur |
| 456 |
when dropping a table or index. |
| 457 |
</ul> |
| 458 |
|
| 459 |
<p> |
| 460 |
Even though the <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> function only allows the caller |
| 461 |
to specify a single user-context pointer, an unlock-notify callback |
| 462 |
is passed an array of such context pointers. This is because if when |
| 463 |
a blocking connection concludes its transaction, if there is more |
| 464 |
than one unlock-notify registered to call the same C function, the |
| 465 |
context-pointers are marshaled into an array and a single callback |
| 466 |
issued. If each thread were assigned a priority, then instead of just |
| 467 |
signaling the threads in arbitrary order as this implementation does, |
| 468 |
higher priority threads could be signaled before lower priority threads. |
| 469 |
|
| 470 |
<p> |
| 471 |
If a "DROP TABLE" or "DROP INDEX" SQL command is executed, and the |
| 472 |
same database connection currently has one or more actively |
| 473 |
executing SELECT statements, then SQLITE_LOCKED is returned. If |
| 474 |
<a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a> is called in this case, then the specified |
| 475 |
callback will be invoked immediately. Re-attempting the "DROP |
| 476 |
TABLE" or "DROP INDEX" statement will return another SQLITE_LOCKED |
| 477 |
error. In the implementation of sqlite3_blocking_step() shown to the |
| 478 |
right, this could cause an infinite loop. |
| 479 |
|
| 480 |
<p> |
| 481 |
The caller could distinguish between this special "DROP TABLE|INDEX" |
| 482 |
case and other cases by using <a href="rescode.html#extrc">extended error codes</a>. When it is appropriate |
| 483 |
to call <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a>, the extended error code is |
| 484 |
SQLITE_LOCKED_SHAREDCACHE. Otherwise, in the "DROP TABLE|INDEX" case, |
| 485 |
it is just plain SQLITE_LOCKED. Another solution might be to limit |
| 486 |
the number of times that any single query could be reattempted (to say |
| 487 |
100). Although this might be less efficient than one might wish, the |
| 488 |
situation in question is not likely to occur often. |
| 489 |
|
| 490 |
<div style="clear:both"></div> |
| 491 |
<p align="center"><small><i>This page was last updated on 2025-06-12 09:10:23Z </small></i></p> |
| 492 |
|