Add notification object for waiting on semaphores

Bug #565419 reported by David L. Rager
12
This bug affects 2 people
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-section()
  (let ((notification nil))
    (unwind-protect-disable-interrupts-during-cleanup
     (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)
            (think-hard))
     (when notification
       (signal-semaphore *sem*)))))

(defun better-critical-section()
  (let ((notification (make-semaphore-notification)))
    (unwind-protect-disable-interrupts-during-cleanup
     (progn (wait-on-semaphore *sem* :notification notification)
            (think-hard))
     (when (semaphore-notification-status notification)
       (signal-semaphore *sem*)))))

(dotimes (i 8)
  (run-thread "my thread" #'naive-critical-section))

Tags: threads
Revision history for this message
David L. Rager (ragerdl) wrote :

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
 semaphore-notification-status
 clear-semaphore-notification-status
 set-semaphore-notification-status

 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
 wait-for-children-to-finish

Changed in sbcl:
importance: Undecided → Wishlist
status: New → Triaged
tags: added: threads
removed: concurrency multi-threading
Changed in sbcl:
importance: Wishlist → Medium
Revision history for this message
Nikodemus Siivola (nikodemus) wrote :

commit 9f1903c072936fb15f03f93182c57beb4621b39d
Author: Nikodemus Siivola <email address hidden>
Date: Thu Nov 17 14:11:18 2011 +0200

    semaphore notification objects

Changed in sbcl:
status: Triaged → Fix Committed
Changed in sbcl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.