Commit ced3c5e2 authored by Daniel Kochmański's avatar Daniel Kochmański
Browse files

mp: semaphores: add tests and the documentation

Functions wait-on-semaphore and try-get-semaphore are deprecated in
favour of the new function.
parent 7c2fb4a1
......@@ -34,6 +34,15 @@
(Windows always uses the utf-16 format provided by the OS).
- Add hook functions for cl:ed via ext:*ed-functions* list)
- Signal type-error in condition readers for WSCL conformance.
- Remove homegrown mutex implementation - all multithreading
synchronization objects are now implemented on top of mutex and condition
variable primitives from the operating system (no api changes)
- Timeouts for condition variables and mutex locking and semaphore wait
operations
** API changes
- A new function ~(mp:semaphore-wait semaphore count timeout)~
- Deprecate Functions ~mp:wait-on-semaphore~ and ~mp:try-get-semphore~
* 21.2.1 changes since 20.4.24
** Announcement
Dear Community,
......
......@@ -61,6 +61,25 @@ Returns the resource count of @var{semaphore}.
Returns the number of threads waiting on @var{semaphore}.
@end defun
@cppdef mp_semaphore_wait
@lspdef mp:semaphore-wait
@deftypefun cl_object mp_sempahore_wait(cl_object semaphore, cl_object count, cl_object timeout)
@end deftypefun
@defun mp:semaphore-wait semaphore count timeout
Decrement the count of @var{semaphore} by @var{count} if the count
would not be negative.
Else blocks until the semaphore can be decremented. Returns the new
count of @var{semaphore} on success.
If timeout is not @code{niL}, it is the maximum number of seconds to
wait. If the count cannot be decremented in that time, returns
@code{nil} without decrementing the count.
@end defun
@cppdef mp_wait_on_semaphore
@lspdef mp:wait-on-semaphore
......@@ -71,6 +90,9 @@ Returns the number of threads waiting on @var{semaphore}.
@defun mp:wait-on-semaphore semaphore
Waits on semaphore until it can grab the resource (blocking). Returns
resource count before semaphore was acquired.
@strong{DEPRECATED} this function is deprecated, it is equivalent to
@code{(mp:semaphore-wait semaphore 1 nil)}
@end defun
......@@ -84,6 +106,9 @@ resource count before semaphore was acquired.
Tries to get a semaphore (non-blocking). If there is no resource left
returns @code{nil}, otherwise returns resource count before semaphore
was acquired.
@strong{DEPRECATED} this function is deprecated, it is equivalent to
@code{(mp:semaphore-wait semaphore 1 0)}
@end defun
......
......@@ -411,6 +411,25 @@
(is (zerop (mp:semaphore-count sem)))
(is (zerop (mp:semaphore-wait-count sem)))))
;;; Date: 2021-10-19
;;;
;;; A smoke test for the new function wait-semaphore.
;;;
(test-with-timeout mp.sem.semaphore-wait/smoke
(let ((sem (mp:make-semaphore :name "sem.semaphore-wait" :count 3)))
(flet ((signal-after-fn (count seconds)
(lambda ()
(sleep seconds)
(mp:signal-semaphore sem count))))
(is (null (mp:semaphore-wait sem 4 0)))
(is (null (mp:semaphore-wait sem 4 0.1)))
(is (= 1 (mp:semaphore-wait sem 2 nil)))
(mp:process-run-function nil (signal-after-fn 1 0.2))
(is (null (mp:semaphore-wait sem 2 0.1)))
(is (= 0 (mp:semaphore-wait sem 2 0.2)))
(mp:process-run-function nil (signal-after-fn 2 0.2))
(is (= 1 (mp:semaphore-wait sem 1 nil))))))
;; Mailbox
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment