| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>Chapter 16. The Locking Subsystem</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 11 | <link rel="prev" href="program_faq.html" title="Programmer notes FAQ" /> |
| 12 | <link rel="next" href="lock_config.html" title="Configuring locking" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Chapter 16. |
| 22 | The Locking Subsystem |
| 23 | </th> |
| 24 | </tr> |
| 25 | <tr> |
| 26 | <td width="20%" align="left"><a accesskey="p" href="program_faq.html">Prev</a> </td> |
| 27 | <th width="60%" align="center"> </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="lock_config.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="chapter" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title"><a id="lock"></a>Chapter 16. |
| 38 | The Locking Subsystem |
| 39 | </h2> |
| 40 | </div> |
| 41 | </div> |
| 42 | </div> |
| 43 | <div class="toc"> |
| 44 | <p> |
| 45 | <b>Table of Contents</b> |
| 46 | </p> |
| 47 | <dl> |
| 48 | <dt> |
| 49 | <span class="sect1"> |
| 50 | <a href="lock.html#lock_intro">Introduction to the locking subsystem</a> |
| 51 | </span> |
| 52 | </dt> |
| 53 | <dt> |
| 54 | <span class="sect1"> |
| 55 | <a href="lock_config.html">Configuring locking</a> |
| 56 | </span> |
| 57 | </dt> |
| 58 | <dt> |
| 59 | <span class="sect1"> |
| 60 | <a href="lock_max.html">Configuring locking: sizing the system</a> |
| 61 | </span> |
| 62 | </dt> |
| 63 | <dt> |
| 64 | <span class="sect1"> |
| 65 | <a href="lock_stdmode.html">Standard lock modes</a> |
| 66 | </span> |
| 67 | </dt> |
| 68 | <dt> |
| 69 | <span class="sect1"> |
| 70 | <a href="lock_dead.html">Deadlock detection</a> |
| 71 | </span> |
| 72 | </dt> |
| 73 | <dt> |
| 74 | <span class="sect1"> |
| 75 | <a href="lock_timeout.html">Deadlock detection using timers</a> |
| 76 | </span> |
| 77 | </dt> |
| 78 | <dt> |
| 79 | <span class="sect1"> |
| 80 | <a href="lock_deaddbg.html">Deadlock debugging</a> |
| 81 | </span> |
| 82 | </dt> |
| 83 | <dt> |
| 84 | <span class="sect1"> |
| 85 | <a href="lock_page.html">Locking granularity</a> |
| 86 | </span> |
| 87 | </dt> |
| 88 | <dt> |
| 89 | <span class="sect1"> |
| 90 | <a href="lock_notxn.html">Locking without transactions</a> |
| 91 | </span> |
| 92 | </dt> |
| 93 | <dt> |
| 94 | <span class="sect1"> |
| 95 | <a href="lock_twopl.html">Locking with transactions: two-phase locking</a> |
| 96 | </span> |
| 97 | </dt> |
| 98 | <dt> |
| 99 | <span class="sect1"> |
| 100 | <a href="lock_cam_conv.html">Berkeley DB Concurrent Data Store locking conventions</a> |
| 101 | </span> |
| 102 | </dt> |
| 103 | <dt> |
| 104 | <span class="sect1"> |
| 105 | <a href="lock_am_conv.html">Berkeley DB Transactional Data Store locking conventions</a> |
| 106 | </span> |
| 107 | </dt> |
| 108 | <dt> |
| 109 | <span class="sect1"> |
| 110 | <a href="lock_nondb.html">Locking and non-Berkeley DB applications</a> |
| 111 | </span> |
| 112 | </dt> |
| 113 | </dl> |
| 114 | </div> |
| 115 | <div class="sect1" lang="en" xml:lang="en"> |
| 116 | <div class="titlepage"> |
| 117 | <div> |
| 118 | <div> |
| 119 | <h2 class="title" style="clear: both"><a id="lock_intro"></a>Introduction to the locking subsystem</h2> |
| 120 | </div> |
| 121 | </div> |
| 122 | </div> |
| 123 | <p>The locking subsystem provides interprocess and intraprocess concurrency |
| 124 | control mechanisms. Although the lock system is used extensively by |
| 125 | the Berkeley DB access methods and transaction system, it may also be used as |
| 126 | a standalone subsystem to provide concurrency control to any set of |
| 127 | designated resources.</p> |
| 128 | <p>The Lock subsystem is created, initialized, and opened by calls to |
| 129 | <a href="../api_reference/C/envopen.html" class="olink">DB_ENV->open()</a> with the <a href="../api_reference/C/envopen.html#envopen_DB_INIT_LOCK" class="olink">DB_INIT_LOCK</a> or <a href="../api_reference/C/envopen.html#envopen_DB_INIT_CDB" class="olink">DB_INIT_CDB</a> |
| 130 | flags specified.</p> |
| 131 | <p>The <a href="../api_reference/C/lockvec.html" class="olink">DB_ENV->lock_vec()</a> method is used to acquire and release locks. The |
| 132 | <a href="../api_reference/C/lockvec.html" class="olink">DB_ENV->lock_vec()</a> method performs any number of lock operations atomically. It |
| 133 | also provides the capability to release all locks held by a particular |
| 134 | locker and release all the locks on a particular object. (Performing |
| 135 | multiple lock operations atomically is useful in performing Btree |
| 136 | traversals -- you want to acquire a lock on a child page and once |
| 137 | acquired, immediately release the lock on its parent. This is |
| 138 | traditionally referred to as <span class="emphasis"><em>lock-coupling</em></span>). Two additional |
| 139 | methods, <a href="../api_reference/C/lockget.html" class="olink">DB_ENV->lock_get()</a> and <a href="../api_reference/C/lockput.html" class="olink">DB_ENV->lock_put()</a>, are provided. These |
| 140 | methods are simpler front-ends to the <a href="../api_reference/C/lockvec.html" class="olink">DB_ENV->lock_vec()</a> functionality, |
| 141 | where <a href="../api_reference/C/lockget.html" class="olink">DB_ENV->lock_get()</a> acquires a lock, and <a href="../api_reference/C/lockput.html" class="olink">DB_ENV->lock_put()</a> releases a |
| 142 | lock that was acquired using <a href="../api_reference/C/lockget.html" class="olink">DB_ENV->lock_get()</a> or <a href="../api_reference/C/lockvec.html" class="olink">DB_ENV->lock_vec()</a>. All |
| 143 | locks explicitly requested by an application should be released via |
| 144 | calls to <a href="../api_reference/C/lockput.html" class="olink">DB_ENV->lock_put()</a> or <a href="../api_reference/C/lockvec.html" class="olink">DB_ENV->lock_vec()</a>. Using <a href="../api_reference/C/lockvec.html" class="olink">DB_ENV->lock_vec()</a> |
| 145 | instead of separate calls to <a href="../api_reference/C/lockput.html" class="olink">DB_ENV->lock_put()</a> and <a href="../api_reference/C/lockget.html" class="olink">DB_ENV->lock_get()</a> also |
| 146 | reduces the synchronization overhead between multiple threads or |
| 147 | processes. The three methods are fully compatible, and may be used |
| 148 | interchangeably.</p> |
| 149 | <p>Applications must specify lockers and lock objects appropriately. When |
| 150 | used with the Berkeley DB access methods, lockers and objects are handled |
| 151 | completely internally, but an application using the lock manager |
| 152 | directly must either use the same conventions as the access methods or |
| 153 | define its own convention to which it adheres. If an application is |
| 154 | using the access methods with locking at the same time that it is |
| 155 | calling the lock manager directly, the application must follow a |
| 156 | convention that is compatible with the access methods' use of the |
| 157 | locking subsystem. See <a class="xref" href="lock_am_conv.html" title="Berkeley DB Transactional Data Store locking conventions">Berkeley DB Transactional Data Store locking conventions</a> for more information.</p> |
| 158 | <p>The <a href="../api_reference/C/lockid.html" class="olink">DB_ENV->lock_id()</a> function returns a unique ID that may safely be used |
| 159 | as the locker parameter to the <a href="../api_reference/C/lockvec.html" class="olink">DB_ENV->lock_vec()</a> method. The access methods |
| 160 | use <a href="../api_reference/C/lockid.html" class="olink">DB_ENV->lock_id()</a> to generate unique lockers for the cursors |
| 161 | associated with a database.</p> |
| 162 | <p>The <a href="../api_reference/C/lockdetect.html" class="olink">DB_ENV->lock_detect()</a> function provides the programmatic interface to |
| 163 | the Berkeley DB deadlock detector. Whenever two threads of control issue lock |
| 164 | requests concurrently, the possibility for deadlock arises. A deadlock |
| 165 | occurs when two or more threads of control are blocked, waiting for |
| 166 | actions that another one of the blocked threads must take. For example, |
| 167 | assume that threads A and B have each obtained read locks on object X. |
| 168 | Now suppose that both threads want to obtain write locks on object X. |
| 169 | Neither thread can be granted its write lock (because of the other |
| 170 | thread's read lock). Both threads block and will never unblock because |
| 171 | the event for which they are waiting can never happen.</p> |
| 172 | <p>The deadlock detector examines all the locks held in the environment, |
| 173 | and identifies situations where no thread can make forward progress. |
| 174 | It then selects one of the participants in the deadlock (according to |
| 175 | the argument that was specified to <a href="../api_reference/C/envset_lk_detect.html" class="olink">DB_ENV->set_lk_detect()</a>), and |
| 176 | forces it to return the value <a class="link" href="program_errorret.html#program_errorret.DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a>, which indicates |
| 177 | that a deadlock occurred. The thread receiving such an error must |
| 178 | release all of its locks and undo any incomplete modifications to the |
| 179 | locked resource. Locks are typically released, and modifications |
| 180 | undone, by closing any cursors involved in the operation and aborting |
| 181 | any transaction enclosing the operation. The operation may optionally |
| 182 | be retried.</p> |
| 183 | <p>The <a href="../api_reference/C/lockstat.html" class="olink">DB_ENV->lock_stat()</a> function returns information about the status of |
| 184 | the lock subsystem. It is the programmatic interface used by the |
| 185 | <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility.</p> |
| 186 | <p>The locking subsystem is closed by the call to <a href="../api_reference/C/envclose.html" class="olink">DB_ENV->close()</a>.</p> |
| 187 | <p>Finally, the entire locking subsystem may be discarded using the |
| 188 | <a href="../api_reference/C/envremove.html" class="olink">DB_ENV->remove()</a> method.</p> |
| 189 | <p>For more information on the locking subsystem methods, see the <a href="../api_reference/C/lock.html#locklist" class="olink">Locking Subsystem and Related Methods</a> section in the <em class="citetitle">Berkeley DB C API Reference Guide.</em> </p> |
| 190 | </div> |
| 191 | </div> |
| 192 | <div class="navfooter"> |
| 193 | <hr /> |
| 194 | <table width="100%" summary="Navigation footer"> |
| 195 | <tr> |
| 196 | <td width="40%" align="left"><a accesskey="p" href="program_faq.html">Prev</a> </td> |
| 197 | <td width="20%" align="center"> </td> |
| 198 | <td width="40%" align="right"> <a accesskey="n" href="lock_config.html">Next</a></td> |
| 199 | </tr> |
| 200 | <tr> |
| 201 | <td width="40%" align="left" valign="top">Programmer notes FAQ </td> |
| 202 | <td width="20%" align="center"> |
| 203 | <a accesskey="h" href="index.html">Home</a> |
| 204 | </td> |
| 205 | <td width="40%" align="right" valign="top"> Configuring locking</td> |
| 206 | </tr> |
| 207 | </table> |
| 208 | </div> |
| 209 | </body> |
| 210 | </html> |