<p>
To acknowledge the disconnect, W ran code like:
-{% highlight c %}
+<pre>
qlock(&conn->lk);
conn->writer_done = 1;
qsignal(&conn->writer_ack);
qunlock(&conn->lk);
thread_exit();
-{% endhighlight %}
+</pre>
<p>
And to wait for the acknowledgement, R ran code like:
-{% highlight c %}
+<pre>
qlock(&conn->lk);
while(!conn->writer_done)
qwait(&conn->writer_ack);
// The writer is done, and so are we:
// free the connection.
free(conn);
-{% endhighlight %}
+</pre>
<p>
This is a standard locks and condition variables piece of code:
<p>
A simple spin lock implementation looks like:
-{% highlight c %}
+<pre>
struct SpinLock
{
int bit;
{
atomic_set(&lk->bit, 0);
}
-{% endhighlight %}
+</pre>
<p>
The spin lock’s core is the <code>bit</code> field.
The queue lock’s queue needs its own lock, almost always a spin lock.
In the library I was using, <code>qlock</code> and <code>qunlock</code> were implemented as:
-{% highlight c %}
+<pre>
struct QLock
{
SpinLock spin;
os_wakeup(t);
spinunlock(&lk->spin);
}
-{% endhighlight %}
+</pre>
<p>
The queue lock’s core is the <code>owner</code> field.