Skip to content

Commit

Permalink
Test the return value of CONDITION-NOTIFY and CONDITION-BROADCAST
Browse files Browse the repository at this point in the history
  • Loading branch information
sionescu committed Jan 13, 2022
1 parent fd1b0cf commit df9748c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
9 changes: 4 additions & 5 deletions apiv2/api-condition-variables.lisp
Expand Up @@ -83,17 +83,16 @@
It is unspecified which thread gets a wakeup and does not
necessarily relate to the order that the threads went to sleep in.
CONDITION-NOTIFY returns T if a thread was woken up, otherwise NIL."
CONDITION-NOTIFY returns always NIL."
(%condition-notify condition-variable)
t)
nil)

(defun condition-broadcast (condition-variable)
"Notify all threads waiting for CONDITION-VARIABLE.
The order of wakeup is unspecified and does not necessarily relate
to the order that the threads went to sleep in.
CONDITION-BROADCAST returns T if at least one thread was woken up,
otherwise NIL."
CONDITION-BROADCAST returns always NIL."
(%condition-broadcast condition-variable)
t)
nil)
9 changes: 6 additions & 3 deletions docs/content/condition-variables/condition-broadcast.md
Expand Up @@ -19,9 +19,7 @@ boolean](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_g.htm#gene

Notify all the threads waiting for `condition-variable`.

Returns
[true](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_t.htm#true)
if at least one thread was woken up, otherwise
Returns always
[false](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#false).

#### Exceptional situations:
Expand All @@ -40,3 +38,8 @@ if `condition-variable` is not a

The order of wakeup is unspecified and does not necessarily relate to
the order in which the threads went to sleep.

**condition-broadcast** always returns
[false](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#false)
because not all implementations' primitives can tell whether or not
some threads were indeed woken up.
9 changes: 6 additions & 3 deletions docs/content/condition-variables/condition-notify.md
Expand Up @@ -19,9 +19,7 @@ boolean](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_g.htm#gene

Notify one of the threads waiting for `condition-variable`.

Returns
[true](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_t.htm#true)
if a thread was woken up, otherwise
Returns always
[false](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#false).

#### Exceptional situations:
Expand All @@ -40,3 +38,8 @@ if `condition-variable` is not a

It is unspecified which thread gets a wakeup and does not necessarily
relate to the order in which the threads went to sleep.

**condition-notify** always returns
[false](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#false)
because not all implementations' primitives can tell whether or not
some threads were indeed woken up.
29 changes: 29 additions & 0 deletions test/tests-v2.lisp
Expand Up @@ -430,6 +430,35 @@ lock."
(with-lock-held (res-lock)
(condition-wait res-cv res-lock)
(is-false lock-was-acquired-p)))))))

#+#.(bt2::implemented-p* 'bt2:make-condition-variable)
(test condition-notify.no-waiting-threads
"Test that `CONDITION-NOTIFY` returns NIL whether or not there are
threads waiting."
(let ((lock (make-lock :name "Test lock"))
(cv (make-condition-variable :name "Test condition variable")))
(is-false (condition-notify cv))
(make-thread (lambda ()
(with-lock-held (lock)
(condition-wait cv lock))))
(is-false (condition-notify cv))))

#+#.(bt2::implemented-p* 'bt2:make-condition-variable)
(test condition-broadcast.return-value
"Test that `CONDITION-BROADCAST` returns NIL whether or not there
are threads waiting."
(let ((lock (make-lock :name "Test lock"))
(cv (make-condition-variable :name "Test condition variable")))
(is-false (condition-notify cv))
(make-thread (lambda ()
(with-lock-held (lock)
(condition-wait cv lock)))
:name "Waiting thread 1")
(make-thread (lambda ()
(with-lock-held (lock)
(condition-wait cv lock)))
:name "Waiting thread 2")
(is-false (condition-broadcast cv))))


;;;
Expand Down

0 comments on commit df9748c

Please sign in to comment.