/usr/lib/swi-prolog/doc/Manual/threadsync.html is in swi-prolog-nox 5.10.4-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>SWI-Prolog 5.11.18 Reference Manual: Section 8.4</TITLE><LINK REL=home HREF="index.html">
<LINK REL=contents HREF="Contents.html">
<LINK REL=index HREF="DocIndex.html">
<LINK REL=summary HREF="summary.html">
<LINK REL=previous HREF="threadcom.html">
<LINK REL=next HREF="thutil.html">
<STYLE type="text/css">
/* Style sheet for SWI-Prolog latex2html
*/
dd.defbody
{ margin-bottom: 1em;
}
dt.pubdef
{ background-color: #c5e1ff;
}
dt.multidef
{ background-color: #c8ffc7;
}
.bib dd
{ margin-bottom: 1em;
}
.bib dt
{ float: left;
margin-right: 1.3ex;
}
pre.code
{ margin-left: 1.5em;
margin-right: 1.5em;
border: 1px dotted;
padding-top: 5px;
padding-left: 5px;
padding-bottom: 5px;
background-color: #f8f8f8;
}
div.navigate
{ text-align: center;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
}
div.title
{ text-align: center;
padding-bottom: 1em;
font-size: 200%;
font-weight: bold;
}
div.author
{ text-align: center;
font-style: italic;
}
div.abstract
{ margin-top: 2em;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
margin-left: 10%; margin-right:10%;
}
div.abstract-title
{ text-align: center;
padding: 5px;
font-size: 120%;
font-weight: bold;
}
div.toc-h1
{ font-size: 200%;
font-weight: bold;
}
div.toc-h2
{ font-size: 120%;
font-weight: bold;
margin-left: 2em;
}
div.toc-h3
{ font-size: 100%;
font-weight: bold;
margin-left: 4em;
}
div.toc-h4
{ font-size: 100%;
margin-left: 6em;
}
span.sec-nr
{
}
span.sec-title
{
}
span.pred-ext
{ font-weight: bold;
}
span.pred-tag
{ float: right;
padding-top: 0.2em;
font-size: 80%;
font-style: italic;
color: #202020;
}
/* Footnotes */
sup.fn { color: blue; text-decoration: underline; }
span.fn-text { display: none; }
sup.fn span {display: none;}
sup:hover span
{ display: block !important;
position: absolute; top: auto; left: auto; width: 80%;
color: #000; background: white;
border: 2px solid;
padding: 5px; margin: 10px; z-index: 100;
font-size: smaller;
}
</STYLE>
</HEAD>
<BODY BGCOLOR="white">
<DIV class="navigate"><A class="nav" href="index.html"><IMG SRC="home.gif" BORDER=0 ALT="Home"></A>
<A class="nav" href="Contents.html"><IMG SRC="index.gif" BORDER=0 ALT="Contents"></A>
<A class="nav" href="DocIndex.html"><IMG SRC="yellow_pages.gif" BORDER=0 ALT="Index"></A>
<A class="nav" href="summary.html"><IMG SRC="info.gif" BORDER=0 ALT="Summary"></A>
<A class="nav" href="threadcom.html"><IMG SRC="prev.gif" BORDER=0 ALT="Previous"></A>
<A class="nav" href="thutil.html"><IMG SRC="next.gif" BORDER=0 ALT="Next"></A>
</DIV>
<H2><A NAME="sec:8.4"><SPAN class="sec-nr">8.4</SPAN> <SPAN class="sec-title">Thread
synchronisation</SPAN></A></H2>
<A NAME="sec:threadsync"></A>
<P>All internal Prolog operations are thread-safe. This implies two
Prolog threads can operate on the same dynamic predicate without
corrupting the consistency of the predicate. This section deals with
user-level
<EM>mutexes</EM> (called <EM>monitors</EM> in ADA or
<EM>critical-sections</EM> by Microsoft). A mutex is a
<B>MUT</B>ual <B>EX</B>clusive device, which implies at most one thread
can <EM>hold</EM> a mutex.
<P>Mutexes are used to realise related updates to the Prolog database.
With `related', we refer to the situation where a `transaction' implies
two or more changes to the Prolog database. For example, we have a
predicate address/2 , representing the address of a person and we want
to change the address by retracting the old and asserting the new
address. Between these two operations the database is invalid: this
person has either no address or two addresses, depending on the
assert/retract order.
<P>Here is how to realise a correct update:
<PRE class="code">
:- initialization
mutex_create(addressbook).
change_address(Id, Address) :-
mutex_lock(addressbook),
retractall(address(Id, _)),
asserta(address(Id, Address)),
mutex_unlock(addressbook).
</PRE>
<DL class="latex">
<DT class="pubdef"><A NAME="mutex_create/1"><STRONG>mutex_create</STRONG>(<VAR>?MutexId</VAR>)</A></DT>
<DD class="defbody">
Create a mutex. If <VAR>MutexId</VAR> is an atom, a <EM>named</EM> mutex
is created. If it is a variable, an anonymous mutex reference is
returned. There is no limit to the number of mutexes that can be
created.</DD>
<DT class="pubdef"><A NAME="mutex_create/2"><STRONG>mutex_create</STRONG>(<VAR>-MutexId,
+Options</VAR>)</A></DT>
<DD class="defbody">
Create a mutex using options. Defined options are:
<DL class="latex">
<DT><STRONG>alias</STRONG>(<VAR>Alias</VAR>)</DT>
<DD class="defbody">
Set the alias name. Using <CODE>mutex_create(X, [alias(name)])</CODE> is
preferred over the equivalent <CODE>mutex_create(name)</CODE>.
</DD>
</DL>
</DD>
<DT class="pubdef"><A NAME="mutex_destroy/1"><STRONG>mutex_destroy</STRONG>(<VAR>+MutexId</VAR>)</A></DT>
<DD class="defbody">
Destroy a mutex. After this call, <VAR>MutexId</VAR> becomes invalid and
further references yield an <CODE>existence_error</CODE> exception.</DD>
<DT class="pubdef"><A NAME="with_mutex/2"><STRONG>with_mutex</STRONG>(<VAR>+MutexId,
:Goal</VAR>)</A></DT>
<DD class="defbody">
Execute <VAR>Goal</VAR> while holding <VAR>MutexId</VAR>. If <VAR>Goal</VAR>
leaves choice-points, these are destroyed (as in <A NAME="idx:once1:1423"></A><A class="pred" href="metacall.html#once/1">once/1</A>).
The mutex is unlocked regardless of whether <VAR>Goal</VAR> succeeds,
fails or raises an exception. An exception thrown by <VAR>Goal</VAR> is
re-thrown after the mutex has been successfully unlocked. See also <A NAME="idx:mutexcreate1:1424"></A><A class="pred" href="threadsync.html#mutex_create/1">mutex_create/1</A>
and <A NAME="idx:setupcallcleanup3:1425"></A><A class="pred" href="metacall.html#setup_call_cleanup/3">setup_call_cleanup/3</A>.
<P>Although described in the thread-section, this predicate is also
available in the single-threaded version, where it behaves simply as
<A NAME="idx:once1:1426"></A><A class="pred" href="metacall.html#once/1">once/1</A>.</DD>
<DT class="pubdef"><A NAME="mutex_lock/1"><STRONG>mutex_lock</STRONG>(<VAR>+MutexId</VAR>)</A></DT>
<DD class="defbody">
Lock the mutex. Prolog mutexes are <EM>recursive</EM> mutexes: they can
be locked multiple times by the same thread. Only after unlocking it as
many times as it is locked, the mutex becomes available for locking by
other threads. If another thread has locked the mutex the calling thread
is suspended until to mutex is unlocked.
<P>If <VAR>MutexId</VAR> is an atom, and there is no current mutex with
that name, the mutex is created automatically using <A NAME="idx:mutexcreate1:1427"></A><A class="pred" href="threadsync.html#mutex_create/1">mutex_create/1</A>.
This implies named mutexes need not be declared explicitly.
<P>Please note that locking and unlocking mutexes should be paired
carefully. Especially make sure to unlock mutexes even if the protected
code fails or raises an exception. For most common cases use
<A NAME="idx:withmutex2:1428"></A><A class="pred" href="threadsync.html#with_mutex/2">with_mutex/2</A>,
which provides a safer way for handling Prolog-level mutexes. The
predicate <A NAME="idx:setupcallcleanup3:1429"></A><A class="pred" href="metacall.html#setup_call_cleanup/3">setup_call_cleanup/3</A>
is another way to guarantee that the mutex is unlocked while retaining
non-determinism.</DD>
<DT class="pubdef"><A NAME="mutex_trylock/1"><STRONG>mutex_trylock</STRONG>(<VAR>+MutexId</VAR>)</A></DT>
<DD class="defbody">
As <A NAME="idx:mutexlock1:1430"></A><A class="pred" href="threadsync.html#mutex_lock/1">mutex_lock/1</A>,
but if the mutex is held by another thread, this predicates fails
immediately.</DD>
<DT class="pubdef"><A NAME="mutex_unlock/1"><STRONG>mutex_unlock</STRONG>(<VAR>+MutexId</VAR>)</A></DT>
<DD class="defbody">
Unlock the mutex. This can only be called if the mutex is held by the
calling thread. If this is not the case, a <CODE>permission_error</CODE>
exception is raised.</DD>
<DT class="pubdef"><A NAME="mutex_unlock_all/0"><STRONG>mutex_unlock_all</STRONG></A></DT>
<DD class="defbody">
Unlock all mutexes held by the current thread. This call is especially
useful to handle thread-termination using <A NAME="idx:abort0:1431"></A><A class="pred" href="toplevel.html#abort/0">abort/0</A>
or exceptions. See also <A NAME="idx:threadsignal2:1432"></A><A class="pred" href="threadcom.html#thread_signal/2">thread_signal/2</A>.</DD>
<DT class="pubdef"><A NAME="mutex_property/2"><STRONG>mutex_property</STRONG>(<VAR>?MutexId,
?Property</VAR>)</A></DT>
<DD class="defbody">
True if Property is a property of MutexId. Defined properties are:
<DL class="latex">
<DT><STRONG>alias</STRONG>(<VAR>Alias</VAR>)</DT>
<DD class="defbody">
Mutex has defined alias name. See <A NAME="idx:mutexcreate2:1433"></A><A class="pred" href="threadsync.html#mutex_create/2">mutex_create/2</A>
using the `alias' option.</DD>
<DT><STRONG>status</STRONG>(<VAR>Status</VAR>)</DT>
<DD class="defbody">
Current status of the mutex. One of <CODE>unlocked</CODE> if the mutex
is currently not locked or <CODE>locked(Owner, Count)</CODE> if mutex is
locked
<VAR>Count</VAR> times by threads <VAR>Owner</VAR>. Note that, unless <VAR>Owner</VAR>
is the calling thread, the locked status can change at any time. There
is no useful application of this property, except for diagnostic
purposes.<SUP class="fn">bug<SPAN class="fn-text">As <VAR>Owner</VAR>
and <VAR>Count</VAR> are fetched separately from the mutex, the values
may be inconsistent.</SPAN></SUP>
</DD>
</DL>
</DD>
</DL>
<P></BODY></HTML>
|