<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.visual-prolog.com/index.php?action=history&amp;feed=atom&amp;title=Threadsafe_facts</id>
	<title>Threadsafe facts - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.visual-prolog.com/index.php?action=history&amp;feed=atom&amp;title=Threadsafe_facts"/>
	<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Threadsafe_facts&amp;action=history"/>
	<updated>2026-04-09T05:00:56Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Threadsafe_facts&amp;diff=4605&amp;oldid=prev</id>
		<title>Thomas Linder Puls: release</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Threadsafe_facts&amp;diff=4605&amp;oldid=prev"/>
		<updated>2019-03-12T16:13:22Z</updated>

		<summary type="html">&lt;p&gt;release&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 18:13, 12 March 2019&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l47&quot;&gt;Line 47:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 47:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Nondeterm facts ===&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Nondeterm facts ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{{Template:NonReleased}}&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;While the fact types above are stored in a single cell and are therefore operated upon with atomic cell read and write operations, the same is not the case for nondeterm facts.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;While the fact types above are stored in a single cell and are therefore operated upon with atomic cell read and write operations, the same is not the case for nondeterm facts.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Thomas Linder Puls</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Threadsafe_facts&amp;diff=4561&amp;oldid=prev</id>
		<title>Thomas Linder Puls: category</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Threadsafe_facts&amp;diff=4561&amp;oldid=prev"/>
		<updated>2019-02-19T15:19:27Z</updated>

		<summary type="html">&lt;p&gt;category&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 17:19, 19 February 2019&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l170&quot;&gt;Line 170:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 170:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Due to the very similar performance properties, it has been decided that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;all&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; nondeterministic facts have this form of lock-free threadsafety.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Due to the very similar performance properties, it has been decided that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;all&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; nondeterministic facts have this form of lock-free threadsafety.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Tutorial&lt;/del&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Multi-threading|{{PAGENAME}}&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Thomas Linder Puls</name></author>
	</entry>
	<entry>
		<id>https://wiki.visual-prolog.com/index.php?title=Threadsafe_facts&amp;diff=4556&amp;oldid=prev</id>
		<title>Thomas Linder Puls: initial</title>
		<link rel="alternate" type="text/html" href="https://wiki.visual-prolog.com/index.php?title=Threadsafe_facts&amp;diff=4556&amp;oldid=prev"/>
		<updated>2019-02-19T14:49:12Z</updated>

		<summary type="html">&lt;p&gt;initial&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This article describes threading aspects of the fact databases in Visual Prolog, and especially the threadsafety of nondeterm fact databases.&lt;br /&gt;
&lt;br /&gt;
Sharing data between threads is &amp;quot;delicate&amp;quot; and can cause problems.  The archetypical example is a counter.  To increment the count &lt;br /&gt;
&lt;br /&gt;
* you read the memory cell that holds the counter value&lt;br /&gt;
* add 1 to this value&lt;br /&gt;
* store the new value back to the memory cell that holds the counter value&lt;br /&gt;
&lt;br /&gt;
But if two threads perform the above three steps simultaneously, then they will both read the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;same&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; counter value from the memory, add one to it, and store the new value back.  As a result the counter will become &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;one&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; larger, even though we have performed &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;two&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; incrementations.&lt;br /&gt;
&lt;br /&gt;
This article does not describe how to deal with threading problems, it solely describes the threading properties of the fact databases.&lt;br /&gt;
&lt;br /&gt;
=== Atomic Operations ===&lt;br /&gt;
&lt;br /&gt;
To avoid dealing with the real timing complexity, we break the computation that each thread performs into atomic operations and consider the entire computation as a merge/mix/interleave of the atomic operations performed by the various threads.&lt;br /&gt;
&lt;br /&gt;
An &amp;#039;&amp;#039;atomic operation&amp;#039;&amp;#039; is a computer operation that functionally run completely independent of other threads.  Notice that it is the functionality that is independent; the timing may be affected.  The purpose of considering atomic operations is that we can disregard any overlap their execution may have in real time.  Atomic operations don&amp;#039;t overlap (i.e. can be considered such), given two operations one will happen before the other or vice versa.&lt;br /&gt;
&lt;br /&gt;
So instead of considering the real timing of the execution, you can consider atomic steps.  Each thread performs a sequence of atomic steps, and the steps of various threads can be seen as non-overlapping.  A certain atomic operation performed by some thread will either happen &amp;#039;&amp;#039;before or after&amp;#039;&amp;#039; a certain other atomic operation performed by some thread (even though their execution overlap in real time).&lt;br /&gt;
&lt;br /&gt;
Single-cell (i.e. memory-cell) reading and writing is atomic, furthermore each cell will undergo exactly one order of atomic operations, and all threads will see this order for that cell.  On the other hand, the order of atomic operations that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;different&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; cells undergo can look different from different threads.&lt;br /&gt;
&lt;br /&gt;
* On a 32bit CPU: a cell is a 32 bit word aligned on a 32 bit word boundary.&lt;br /&gt;
* On a 64bit CPU: a cell is a 64 bit word aligned on a 64 bit word boundary.&lt;br /&gt;
&lt;br /&gt;
{{Example|The thread &amp;lt;vp&amp;gt;T1&amp;lt;/vp&amp;gt; performs the atomic operation &amp;lt;vp&amp;gt;A1&amp;lt;/vp&amp;gt; and the thread &amp;lt;vp&amp;gt;T2&amp;lt;/vp&amp;gt; performs the atomic operation &amp;lt;vp&amp;gt;A2&amp;lt;/vp&amp;gt;.  When running on a single processor, the following two scenarios are possible&lt;br /&gt;
* &amp;lt;vp&amp;gt;A1&amp;lt;/vp&amp;gt; happens before &amp;lt;vp&amp;gt;A2&amp;lt;/vp&amp;gt;, or&lt;br /&gt;
* &amp;lt;vp&amp;gt;A2&amp;lt;/vp&amp;gt; happens before &amp;lt;vp&amp;gt;A1&amp;lt;/vp&amp;gt;&lt;br /&gt;
But when running on a multi processor, the following can also be the case:&lt;br /&gt;
* Some threads see &amp;lt;vp&amp;gt;A1&amp;lt;/vp&amp;gt; happen before &amp;lt;vp&amp;gt;A2&amp;lt;/vp&amp;gt; other threads see &amp;lt;vp&amp;gt;A2&amp;lt;/vp&amp;gt; happen before &amp;lt;vp&amp;gt;A1&amp;lt;/vp&amp;gt;&lt;br /&gt;
Notice however that the it is guarantied that if &amp;lt;vp&amp;gt;A1&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;A2&amp;lt;/vp&amp;gt; are both write operations to the same memory cell then all threads will see the same order of &amp;lt;vp&amp;gt;A1&amp;lt;/vp&amp;gt; and &amp;lt;vp&amp;gt;A2&amp;lt;/vp&amp;gt;.  Or put in another way, the memory cell will change according to one of the orders, and no thread will see another order.&lt;br /&gt;
&lt;br /&gt;
From a programming point of view anything else would (probably) seem very strange. But in the processors there are several layers of cache in which the same &amp;quot;logical memory cell&amp;quot; can be physically present many places at the same time.  So to the CPU manufacturers it is not trivial to fulfill such rules.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
In Visual Prolog much data is created once and never modified during its lifetime, furthermore it is not shared between threads before it is created.  Such data structures do not in itself change state, but they can be or become &amp;quot;old&amp;quot;.  In the counter example above none of the numbers were in a bad state, the problem was that both threads made their operations on the same input, where one should have used the output of the other instead.  Or to put it somewhat differently: when one of the threads performs the first write, the data in the other thread becomes &amp;quot;old&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
In Visual Prolog facts is the only kind of memory that can change after it has been created.&lt;br /&gt;
&lt;br /&gt;
* Single facts,&lt;br /&gt;
* determ facts and &lt;br /&gt;
* fact variables&lt;br /&gt;
&lt;br /&gt;
are stored in a single cell, and therefore all threads will see one (i.e. the same) order of updates of a certain fact. Notice that the value of a fact may reference objects, and these objects can contain facts which can also change.  The order of the nested changes are not related to the order of changes in the fact.&lt;br /&gt;
&lt;br /&gt;
Reads and writes of facts are atomic, but the data may still be &amp;quot;old&amp;quot; when it is read or become &amp;quot;old&amp;quot; after it has been read.  This article does not deal with such &amp;quot;being old&amp;quot; problems/issues.&lt;br /&gt;
&lt;br /&gt;
=== Nondeterm facts ===&lt;br /&gt;
&lt;br /&gt;
{{Template:NonReleased}}&lt;br /&gt;
&lt;br /&gt;
While the fact types above are stored in a single cell and are therefore operated upon with atomic cell read and write operations, the same is not the case for nondeterm facts.&lt;br /&gt;
&lt;br /&gt;
A nondeterm fact database contains a sequence of facts:&lt;br /&gt;
&lt;br /&gt;
* additional facts can be added before (&amp;lt;vp&amp;gt;asserta&amp;lt;/vp&amp;gt;) and after (&amp;lt;vp&amp;gt;assert&amp;lt;/vp&amp;gt;/&amp;lt;vp&amp;gt;assertz&amp;lt;/vp&amp;gt;) the current ones, and &lt;br /&gt;
* arbitrary facts can be removed (&amp;lt;vp&amp;gt;retract&amp;lt;/vp&amp;gt;/&amp;lt;vp&amp;gt;retractAll&amp;lt;/vp&amp;gt;)&lt;br /&gt;
* the facts can be traversed in the order they appear in the sequence.&lt;br /&gt;
&lt;br /&gt;
Visual Prolog fact databases are &amp;#039;&amp;#039;&amp;#039;lock-free&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;threadsafe&amp;#039;&amp;#039;&amp;#039; (since Visual Prolog version 9); in the sequel we will consider the details.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Lock-free&amp;#039;&amp;#039; means that fact operations made by one thread will not block/lock other threads. Instead the operations attempted by other threads may fail, and have to be performed/attempted again.  The operations that will have to be redone are very small and even for &amp;quot;busy&amp;quot; facts this will not give a significant overhead.&lt;br /&gt;
&lt;br /&gt;
The threadsafety ensures the integrity of the fact database, i.e. that&lt;br /&gt;
* it stays healthy and that &lt;br /&gt;
* its contents corresponds to the operations that have been performed on it.&lt;br /&gt;
&lt;br /&gt;
The integrity is ensured by making the following operations atomic:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;asserta&amp;lt;/vp&amp;gt;&lt;br /&gt;
* &amp;lt;vp&amp;gt;assert&amp;lt;/vp&amp;gt;/&amp;lt;vp&amp;gt;assertz&amp;lt;/vp&amp;gt;&lt;br /&gt;
* &amp;lt;vp&amp;gt;retract&amp;lt;/vp&amp;gt; of one fact&lt;br /&gt;
* traverse to the next fact and reading it&lt;br /&gt;
&lt;br /&gt;
This means that if several threads are performing several of the operations above then the fact database will undergo some interleave/mix of these operations.&lt;br /&gt;
&lt;br /&gt;
It is worth noticing that if two threads are both trying to retract the same fact then one of the threads will succeed; the other one will not be able to retract it. In fact, that thread will behave as if the fact was not present in the first place.&lt;br /&gt;
&lt;br /&gt;
Fact operations that operate on more than one fact are all implemented as sequences of the atomic operations mentioned above.  For example &amp;lt;vp&amp;gt;retractAll&amp;lt;/vp&amp;gt; will perform a number of individual &amp;lt;vp&amp;gt;retract&amp;lt;/vp&amp;gt;&amp;#039;s of facts (i.e. one at the time).  So &amp;lt;vp&amp;gt;retractAll&amp;lt;/vp&amp;gt; is not an atomic operation.&lt;br /&gt;
&lt;br /&gt;
Due to the atomicity of those operations it will be the case that:&lt;br /&gt;
&lt;br /&gt;
* An asserted fact will end up (once) in the in facts (i.e. there are losses or doubles of assert operations)&lt;br /&gt;
* A certain fact can be retraced exactly one time (by one thread) and will not be present in among the facts afterwards&lt;br /&gt;
* Traversal through the facts in atomic steps from fact to fact. Whether a certain fact is met requires that &lt;br /&gt;
** it has been asserted before the relevant step takes place&lt;br /&gt;
** it has not been retracted before the relevant step takes place&lt;br /&gt;
&lt;br /&gt;
=== Implementation details ===&lt;br /&gt;
&lt;br /&gt;
This section describes how the nondeterm facts are represented and how they are made threadsafe.&lt;br /&gt;
&lt;br /&gt;
Let us first consider a simplified non-threadsafe representation of a fact database (this is the representation that was used prior to Visual Prolog version 9).&lt;br /&gt;
&lt;br /&gt;
The facts are kept in a chain of fact cells each cell contains one fact as a pointer to the next fact cell in the chain we also have an &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; which points to the first fact call in the chain.  The &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; is the main entry to the facts.  When we want to traverse the fact we load the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; and then follow &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt;s from one cell to the next.&lt;br /&gt;
&lt;br /&gt;
To avoid having to traverse the entire chain when we want to perform an &amp;lt;vp&amp;gt;assertz&amp;lt;/vp&amp;gt; operation we also have a &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; that points to the last cell in the chain.&lt;br /&gt;
&lt;br /&gt;
To &amp;lt;vp&amp;gt;asserta&amp;lt;/vp&amp;gt; a fact we will create a new fact cell setting the fact to whatever should be asserted and setting the &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; to the current value of the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt;, i.e. the new cell will point to the old head of the chain.  Then we will update the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; to point to our new cell, because it is now the new head of the chain.  If the cell was empty before our operation we have now also created the last cell in the chain, so in that case we also update the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; to point to our new cell (it is both the first and the last).  If there were already facts in teh database then the z-pointer already point to the last fact in the chain, so it is just left unchanged.&lt;br /&gt;
&lt;br /&gt;
To &amp;lt;vp&amp;gt;assertz&amp;lt;/vp&amp;gt; a fact we will first create a new fact cell setting the fact to whatever should be asserted and setting the &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; to &amp;quot;nil&amp;quot; indicating that this cell is the last in the chain.  If there are no facts in the chain already we set both the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; and the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; to point to our new cell (because it is both the first and last cell in the database).  If there are already facts in the database then our operation does not affect what the first cell is, so the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; is left unchanged.  In this case the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; points to the &amp;quot;old&amp;quot; last cell, so we update the &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; in this cell to point to our new cell and we also we update the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; to point to our new cell.&lt;br /&gt;
&lt;br /&gt;
To retract facts we do quite the same as when we traverse the fact chain, the difference is that when we find a matching term we should remove it from the chain.  We do this by updating the pointer that pointed to the cell (this can either be the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; or a next pointer in another cell) with the value of this cells next pointer.  There is however a little extra complication if the retracted cell is the last cell because then we will also have to update the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt;.  We will skip the details about this, because it is not really relevant how this is done.&lt;br /&gt;
&lt;br /&gt;
To make this threadsafe we use &amp;lt;vp&amp;gt;interlockedCompareExchange&amp;lt;/vp&amp;gt; operations (also know as compare-and-swap operations).  These are atomic operations that will update the memory contents from to the value B provided that it currently has the value A, in any case the initial value that the cell had will be returned.  So the operation corresponds to performing this code as an atomic operation:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;vip&amp;gt;&lt;br /&gt;
clauses&lt;br /&gt;
    interlockedCompareExchange(Pointer, Old, New) = ActualOld :-&lt;br /&gt;
        ActualOld = getMemory(Pointer),&lt;br /&gt;
        if Old = ActualOld then&lt;br /&gt;
          setMemory(Pointer, New)&lt;br /&gt;
        end.&lt;br /&gt;
&amp;lt;/vip&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For our use it is not really important the the &amp;lt;vp&amp;gt;ActualOld&amp;lt;/vp&amp;gt; value is returned, what matters is that we can determine whether the exchange took place or not.&lt;br /&gt;
&lt;br /&gt;
If the fact database already contains facts then this is how we can asserta a fact.&lt;br /&gt;
&lt;br /&gt;
First we create a new fact cell setting the fact data to the new fact and the &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; to the current contents of the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt;, then we perform a &amp;lt;vp&amp;gt;interlockedCompareExchange&amp;lt;/vp&amp;gt; of the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; with the value that we put in our &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; as the &amp;lt;vp&amp;gt;Old&amp;lt;/vp&amp;gt; value and the address of our cell as &amp;lt;vp&amp;gt;New&amp;lt;/vp&amp;gt;.  If the &amp;lt;vp&amp;gt;interlockedCompareExchange&amp;lt;/vp&amp;gt; operation made the exchange then it was because the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; still pointed to the same cell that our new cells next-pointer also points to.  So atomically we have made a correct update of the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; and the fact database remains correct.  If the &amp;lt;vp&amp;gt;interlockedCompareExchange&amp;lt;/vp&amp;gt; operation didn&amp;#039;t make the exchange then it is because the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; has changed to something different than what we put in our cells &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt;.  This can either be because another thread have asserted a fact with &amp;lt;vp&amp;gt;asserta&amp;lt;/vp&amp;gt; or retracted the first fact in the chain.  In both cases we once more put the current &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; value in to the &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; in our cell and perform a corresponding &amp;lt;vp&amp;gt;interlockedCompareExchange&amp;lt;/vp&amp;gt; operation.&lt;br /&gt;
&lt;br /&gt;
We will continue this update the &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; + &amp;lt;vp&amp;gt;interlockedCompareExchange&amp;lt;/vp&amp;gt; until the exchange takes place. In principle this can go on forever, but notice that our thread is only delayed when another thread have successfully completed an assert or a retract.  So we are only delayed if some other thread have made progress.  So our operation is non-blocking (i.e. it is never suspended) and it is only delayed when some other thread have made progress. This is what is know as a &amp;#039;&amp;#039;lock-free&amp;#039;&amp;#039; algorithm: non-blocking and system wide progress, but individual threads may suffer from (periods with) lack of progress.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;vp&amp;gt;asserta&amp;lt;/vp&amp;gt; operation is the simplest operation to implement and but above we disregarded the update of the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; that must take place if there are initially no facts in the chain.  However, we need to consider all our operations, and here it turns out that retract is the real nut to crack.&lt;br /&gt;
&lt;br /&gt;
The problem with retract is that we &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;atomically&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; have to take the cell out of the chain.  And as described above taking the cell out of the chain means reading the cells &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; and writing that value in the previous cells &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; or in the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt;.  There are no operations that can atomically (and lock-free) copy the contents of one memory cell to another. All the levels of memory cache makes it impossible to implement such an operation without blocking other threads.  Moreover, retract will also sometimes have to update the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; (i.e. if the last fact is retracted).&lt;br /&gt;
&lt;br /&gt;
We handle the atomic retraction by means of a flag in the fact cells, i.e. each fact cell has a flag that is set whenever the cell is retracted.  Using the &amp;lt;vp&amp;gt;interlockedCompareExchange&amp;lt;/vp&amp;gt; it is easy to ensure that only one thread will retract a cell, we simply use &amp;lt;vp&amp;gt;unretracted&amp;lt;/vp&amp;gt; as &amp;lt;vp&amp;gt;Old&amp;lt;/vp&amp;gt; value and &amp;lt;vp&amp;gt;retracted&amp;lt;/vp&amp;gt; as &amp;lt;vp&amp;gt;New&amp;lt;/vp&amp;gt; value, if &amp;lt;vp&amp;gt;ActualOld&amp;lt;/vp&amp;gt; is &amp;lt;vp&amp;gt;unretracted&amp;lt;/vp&amp;gt; the cell was retracted.  The retracted cell is still in the chain after it has been retracted, therefore:&lt;br /&gt;
* Other operations will have to take into account that the chain can contain retracted cells.&lt;br /&gt;
* It should be removed from the chain.&lt;br /&gt;
&lt;br /&gt;
Before we go on let us imagine that we didn&amp;#039;t remove the retraced cells from the chain.  With that strategy the fact chain would only grow by adding new cells in front or behind the chain.  Keeping this super-chain in mind let us consider the operations:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;vp&amp;gt;asserta&amp;lt;/vp&amp;gt; means adding a new head to the super-chain. The current head can only be found from the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt;&lt;br /&gt;
* &amp;lt;vp&amp;gt;assertz&amp;lt;/vp&amp;gt; means adding a new tail to the super-chain. The current tail can be found by following &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt;s from any cell in the chain until you reach the cell whose &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; is null.&lt;br /&gt;
* removing a retracted cell means changing the &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; in a cell to point to another cell further ahead in the super-chain skipping a retracted cell.  Repeating the operation will always change pointers to point further ahead in the super-chain only skipping retracted cells.&lt;br /&gt;
&lt;br /&gt;
Well this is only almost true, there is one operation that could break the super-chain.  If we at some point remove the last cell in the current super-chain then the new tail cell will be one that once had a tail but now can get another tail.  So if we ever remove the last cell in the super-chain it will no longer be a single chain, but could have branches.&lt;br /&gt;
&lt;br /&gt;
The super-chain property is however what makes it possible to solve all the problems, so we will keep in mind that even if the last cell in the chain is retracted we will not remove it from the chain.  It can be removed if we at some point &amp;lt;vp&amp;gt;assertz&amp;lt;/vp&amp;gt; another cell after it, because then it is no longer the last cell.&lt;br /&gt;
&lt;br /&gt;
So provided that we never remove the current last cell in the chain the following will be true:&lt;br /&gt;
&lt;br /&gt;
* The head can be found from the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; (i.e. we have to make sure this is the case)&lt;br /&gt;
* The tail can be found from any cell by following &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt;s until you reach the cell whose &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; is &amp;lt;vp&amp;gt;null&amp;lt;/vp&amp;gt;.&lt;br /&gt;
* The removing retracted cells will change a &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; to point to a place further ahead in the super-chain only past retracted cells.&lt;br /&gt;
&lt;br /&gt;
From the last property we conclude the following:&lt;br /&gt;
&lt;br /&gt;
At any point in time all &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt;s will point to some cell further ahead in the super-chain only passing retracted cells.  Therefore at any point in time it is valid to move a next-pointer past a retracted cell and into the preceding cells next pointer.  This is very important because it means that any threads can perform that operation at any time it wishes without breaking the correctness.  So in general such an operation will reduce the the actual numbers of cells in the chain (by removing some that are retracted).  When two threads both are updating the same next-pointer then it can be the case that the thread with the &amp;quot;worst&amp;quot; value is that one that makes the last update, but while such updates may therefore not always be optimal they will never be wrong, and intermediate states will also be correct.&lt;br /&gt;
&lt;br /&gt;
The operation that performs at actual retract will not unchain the retraced cell (it will reset the fact data for the purpose of garbage collection); the unchaining of cells is only done when traversing the chain.&lt;br /&gt;
&lt;br /&gt;
The final thing we need to handle is the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt;, this cannot be maintained as part of the atomic parts of the operations, but also in this case we can make sure that we have a correct way to locate the last cell in the chain also when the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; is in sync with the operations (i.e. &amp;quot;old&amp;quot;).  We can always locate the last cell from any other cell (retracted or not and also if it is removed from the chain), so if the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; points to a cell we can just follow the &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt;s to the tail.  This will reach the correct tail regardless of whether other threads have asserted or retracted cells.  The only problematic time is initially when something has been asserted into the chain, but the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; is &amp;quot;old&amp;quot; and therefore still nil (and therefore does not point to a cell in the chain).  So if the &amp;lt;vp&amp;gt;z-pointer&amp;lt;/vp&amp;gt; is nil then the last cell should be found starting with the &amp;lt;vp&amp;gt;a-pointer&amp;lt;/vp&amp;gt; instead.&lt;br /&gt;
&lt;br /&gt;
=== Performance Considerations ===&lt;br /&gt;
&lt;br /&gt;
It is preferable if is such a feature (i.e. threadsafety) is used for all facts instead of being something that should be controlled by the programmer.  But that obviously requires that the overhead is insignificant/tolerable.&lt;br /&gt;
&lt;br /&gt;
Compared to the non-threadsafe version we need a flag for retracted in the fact cells, to avoid this giving a memory overhead we have packed this flag together with the &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt;.  This is possible because the least significant bits in the &amp;lt;vp&amp;gt;next-pointer&amp;lt;/vp&amp;gt; are always zero (due to memory alignment of the fact cells).  Furthermore, it is relatively simple to handle updates using interlockedCompareExchange operations of this combined value, especially because the retracted flag can only change from &amp;lt;vp&amp;gt;unretracted&amp;lt;/vp&amp;gt; to &amp;lt;vp&amp;gt;retracted&amp;lt;/vp&amp;gt; there is never a change the other way.&lt;br /&gt;
&lt;br /&gt;
So the memory usage is the same as for the non-threadsafe implementation.&lt;br /&gt;
&lt;br /&gt;
When considering execution time overhead, we have measured the time usage for various single-thread scenarios (the non-threadsafe version only works in such scenarios).  It is always difficult to trust that such scenarios are really good and representative.  But the scenarios show that the performance of the threadsafe facts is within 5% of the non-threadsafe facts.  There are some issues about comparing retract, the non-threadsafe retract is fully performed but the retract operation itself, but in the threadsafe version the cell is not removed from the chain until the chain is traversed again.  The non-threadsafe facts have also have an complexity and overhead caused by traversing a fact and retracting from it during that traversal, the complexity arise when retracting the cell that is the &amp;quot;current&amp;quot; cell in the traversal.  So for &amp;lt;vp&amp;gt;retract&amp;lt;/vp&amp;gt; is quite difficult to measure directly against each other.  The mentioned complexity for non-threadsafe facts makes it possible to create scenarios where retract is &amp;#039;&amp;#039;&amp;#039;much&amp;#039;&amp;#039;&amp;#039; slower with non-threadsafe than with threadsafe facts.  But even in the scenario which is most favorable for non-threadsafe facts retraction is still less than 5% better.&lt;br /&gt;
&lt;br /&gt;
The code for facts is a mixture of in line code generated during compilation and calls runtime routines.  During the implementation of the threadsafe facts we also optimized the generated code and the runtime system. So even though the threadsafe facts are up to 5% slower than the corresponding non-threadsafe ones, they are actually faster than the non-threadsafe facts used to be.  Furthermore, retract of non-threadsafe facts have some problems that can make that operation much slower than the threadsafe version.&lt;br /&gt;
&lt;br /&gt;
When running real programs we have not noticed any performance changes at all.&lt;br /&gt;
&lt;br /&gt;
Due to the very similar performance properties, it has been decided that &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;all&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; nondeterministic facts have this form of lock-free threadsafety.&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorial]]&lt;/div&gt;</summary>
		<author><name>Thomas Linder Puls</name></author>
	</entry>
</feed>