[Openmcl-devel] Managing multi-threaded access to list
David L. Rager
ragerdl at cs.utexas.edu
Thu May 25 16:14:38 UTC 2006
You are missing a basic building block: the semaphore. Also, you should use
the exported function (ccl:make-lock) instead of the non-exported
A semaphore allows for efficient (fast) signaling between threads. From the
"Creates and returns an object of type CCL:SEMAPHORE. A semaphore has an
associated "count" which may be incremented and decremented atomically;
incrementing it represents sending a signal, and decrementing it represents
handling that signal. semaphore has an initial count of 0."
When you "increment" a semaphore's count, exactly one thread that's waiting
to decrement it is immediately awakened and decrements the count. This fast
awakening is how you "signal" that a resource may be available. Also,
semaphores effectively "store" signals. If you signal a semaphore 10 times,
a thread can "wait" on that semaphore and immediately progress in its code.
If an 11th thread "waits" on the semaphore, it will have to wait for it to
be signaled by the producer.
Instead of a semaphore, you could use condition variables. Semaphores can
act as both locks and condition variables, so OpenMCL hasn't implemented
I inserted the semaphore signaling and semaphore waiting that you need.
It's possible I missed some details, like misnaming a function, but your
code looks like you can figure those out. Also, since you use semaphores,
you probably won't need the data-available function. Here's the
(defparameter *packet-list-lock* (ccl::make-lock))
(defparameter *packet-list* nil)
(defparameter *packet-list-semaphore* (ccl:make-semaphore))
(defun reading-packets ()
"Function to read packets and putting them on *packet-list*"
(let ((pkt (read-packet-from-socket)))
(when (acceptable-packet pkt)
(setf *packet-list* (append *packet-list* (list pkt))))
(defun start-reader-process ()
"Start a producer thread"
(ccl:process-run-function "Network Reader Process" #'reading-packets))
(defun consume-packet-hang ()
"Pick a packet off the packet list, or return nil if none is available"
(progn (ccl:wait-on-semaphore *packet-list-semaphore*)
More information about the Openmcl-devel