Add notification object for waiting on semaphores
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
SBCL |
Fix Released
|
Medium
|
Unassigned |
Bug Description
This is a feature request for adding a "semaphore notification object" to the ability to wait on a semaphore. Such an object provides a reliable way to know whether a semaphore's signal was received. A naive implementation of failing to implement this type of user functionality comes first. Following that, is what usage of the CCL interface looks like. I'm using the language of ACL2's multi-threading interface, because it strikes me as easier to read; the code can also be read as a type of pseudo-code.
It's possible to implement something usefully equivalent in user space with condition variables and locks, but it would be nice to have this feature as a first class object.
(defvar *sem* (make-semaphore))
(signal-semaphore *sem*)
(defun think-hard()
(sleep 2)
(print "done"))
(defun naive-critical-
(let ((notification nil))
(unwind-
(progn (wait-on-semaphore *sem*)
; Race condition here -- it's possible to receive the semaphore, be interrupted
; with a function that throws, and not record the notification.
(setf notification t)
(when notification
(defun better-
(let ((notification (make-semaphore
(unwind-
(progn (wait-on-semaphore *sem* :notification notification)
(when (semaphore-
(dotimes (i 8)
(run-thread "my thread" #'naive-
Changed in sbcl: | |
importance: | Undecided → Wishlist |
status: | New → Triaged |
tags: |
added: threads removed: concurrency multi-threading |
Changed in sbcl: | |
importance: | Wishlist → Medium |
Changed in sbcl: | |
status: | Fix Committed → Fix Released |
If you're already motivated, you can skip this comment. This comment serves as a pointer to information that might be more convincing that this is a real use case.
======= ======= ======= ======= ======= ======= ===
All of this code is from an older version of ACL2's parallel-raw.lsp, which can be found at http:// userweb. cs.utexas. edu/users/ ragerdl/ parallelism/ parallel. lsp . I've since reworked the condition variables and used keywords instead of optional arguments.
Rather than copy + paste code here, I think it might be easier for you if you search for "notification" in that file and just
peruse at your leisure. You can assume feature acl2-par to always be in *features*.
As a guide, I will offer some starting points. The following functions are relevant to defining a notification-object in user space:
make-semaphore -notification notification- status semaphore- notification- status notification- status
semaphore-
clear-
set-semaphore-
wait-on-semaphore
For an example usage, you can visit the following functions. The second function is a good example where variable "children-left-ptr" is used for bookkeeping and we use a notification-object to ensure that if the semaphore is signaled, that we record that signaling in our books.
decrement- children- left children- to-finish
wait-for-