Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. (defstruct (semaphore (:conc-name sem-)
  2. (:constructor make-semaphore))
  3. (lock (make-lock))
  4. (wait (make-condition-variable))
  5. (count 0))
  6.  
  7. (defun semaphore-signal (self)
  8. "Increments SELF and wakes up waiting thread if any"
  9. (with-lock-held ((sem-lock self))
  10. (when (<= (incf (sem-count self)) 0)
  11. (condition-notify (sem-wait self)))))
  12.  
  13. (defun semaphore-wait (self)
  14. "Decrements SELF and blocks thread if negative"
  15. (with-lock-held ((sem-lock self))
  16. (when (< (decf (sem-count self)) 0)
  17. (condition-wait (sem-wait self) (sem-lock self)))))
  18.  
  19. (defun semaphore-count (self)
  20. "Returns count (signals - waits) of SELF"
  21. (with-lock-held ((sem-lock self))
  22. (sem-count self)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement