The bt-semaphore Reference Manual

This is the bt-semaphore Reference Manual, version 0.6.3, generated automatically by Declt version 4.0 beta 2 "William Riker" on Sat Dec 03 19:39:34 2022 GMT+0.

Table of Contents


1 Introduction


2 Systems

The main system appears first, followed by any subsystem dependency.


2.1 bt-semaphore

A simple semaphore class for bordeaux-threads inspired by SBCL’s semaphore.

Author

Ralph Möritz

License

MIT

Long Description

# bt-semaphore

A simple semaphore class for bordeaux-threads inspired by SBCL’s semaphore.

**Obsolete!** bordeaux-threads has its own built-in semaphores since version 0.8.6, so you should definitely use that instead of bt-semaphore.

## Installation

‘bt-semaphore‘ is available via [Quicklisp](http://www.quicklisp.org/beta/). You can also clone the Git repo if you’re curious:

“‘
cd ~/quicklisp/local-projects
git clone https://github.com/rmoritz/bt-semaphore
“‘

## Usage

There are seven functions of interest at the moment:

- ‘make-semaphore‘ creates a semaphore instance
- ‘wait-on-semaphore‘ blocks until the semaphore can be decremented (ie. its count > 0) or the timeout has expired
- ‘signal-semaphore‘ increments the semaphore & wakes n waiting threads
- ‘try-semaphore‘ decrements the semaphore without blocking
- ‘semaphore-count‘ returns the current count of the semaphore
- ‘semaphore-waiters‘ returns the number of threads waiting on semaphore
- ‘semaphore-name‘ is an accessor for the semaphore’s name slot

To illustrate, here’s a tiny example:

“‘common-lisp
(ql:quickload :bt-semaphore)

(defun semaphore-demo ()
(defparameter sem (bt-sem:make-semaphore))
(defparameter lock (bt:make-lock))
(defparameter num 0)

(format t "spawn 20 threads with 4s timeout~%")
(loop
repeat 20
do (bt:make-thread
(lambda ()
(if (bt-sem:wait-on-semaphore sem :timeout 4)
(bt:with-lock-held (lock)
(incf num))))))
(format t "num is ~d~%" num)
(sleep 0.33)
(format t "there are ~d waiting threads~%~%" (bt-sem:semaphore-waiters sem))

(format t "signal 5 threads~%")
(bt-sem:signal-semaphore sem 5)
(sleep 0.33)
(bt:with-lock-held (lock)
(format t "num is ~d~%" num))
(format t "there are ~d waiting threads~%~%" (bt-sem:semaphore-waiters sem))

(format t "signal 10 threads~%")
(bt-sem:signal-semaphore sem 10)
(sleep 0.33)
(bt:with-lock-held (lock)
(format t "num is ~d~%" num))
(format t "there are ~d waiting threads~%~%" (bt-sem:semaphore-waiters sem))

(format t "4s sleep~%")
(sleep 4)
(bt:with-lock-held (lock)
(format t "num is ~d~%" num))
(format t "there are ~d waiting threads~%~%" (bt-sem:semaphore-waiters sem))) “‘

Calling ‘SEMAPHORE-DEMO‘ at the REPL should produce the following output:

“‘
spawn 20 threads with 4s timeout
num is 0
there are 20 waiting threads

signal 5 threads
num is 5
there are 15 waiting threads

signal 10 threads
num is 15
there are 5 waiting threads

4s sleep
num is 15
there are 0 waiting threads
“‘

## Status

Working, but obsolete: bordeaux-threads has semaphore support since version 0.8.6. You should definitely be using that instead of bt-semaphore.

You can run the test suites to verify that everything is working as it
should by invoking ‘(ql:quickload :bt-semaphore-test)‘ or ‘(asdf:test-system :bt-semaphore)‘.

## Bugs

Found one? Report it [here](https://github.com/rmoritz/bt-semaphore/issues), thanks.

## Author

* Ralph Moeritz (ralphmoritz@outlook.com)

## License

Copyright (c) Ralph Moeritz 2013.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

**THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.**

Version

0.6.3

Dependency

bordeaux-threads (system).

Source

bt-semaphore.asd.

Child Component

src (module).


3 Modules

Modules are listed depth-first from the system components tree.


3.1 bt-semaphore/src

Source

bt-semaphore.asd.

Parent Component

bt-semaphore (system).

Child Components

4 Files

Files are sorted by type and then listed depth-first from the systems components trees.


4.1 Lisp


4.1.1 bt-semaphore/bt-semaphore.asd

Source

bt-semaphore.asd.

Parent Component

bt-semaphore (system).

ASDF Systems

bt-semaphore.

Packages

bt-semaphore-asd.


4.1.2 bt-semaphore/src/package.lisp

Source

bt-semaphore.asd.

Parent Component

src (module).

Packages

bt-semaphore.


4.1.3 bt-semaphore/src/semaphore.lisp

Dependency

package.lisp (file).

Source

bt-semaphore.asd.

Parent Component

src (module).

Public Interface
Internals

semaphore (class).


5 Packages

Packages are listed by definition order.


5.1 bt-semaphore-asd

Source

bt-semaphore.asd.

Use List
  • asdf/interface.
  • common-lisp.

5.2 bt-semaphore

Source

package.lisp.

Nickname

bt-sem

Use List

common-lisp.

Public Interface
Internals

semaphore (class).


6 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


6.1 Public Interface


6.1.1 Ordinary functions

Function: make-semaphore (&key name count)

Create a semaphore with the supplied name and count.

Package

bt-semaphore.

Source

semaphore.lisp.


6.1.2 Generic functions

Generic Function: semaphore-count (instance)
Package

bt-semaphore.

Methods
Method: semaphore-count ((instance semaphore))

Return the count of the semaphore.

Source

semaphore.lisp.

Generic Reader: semaphore-name (object)
Package

bt-semaphore.

Methods
Reader Method: semaphore-name ((semaphore semaphore))

automatically generated reader method

Source

semaphore.lisp.

Target Slot

name.

Generic Writer: (setf semaphore-name) (object)
Package

bt-semaphore.

Methods
Writer Method: (setf semaphore-name) ((semaphore semaphore))

automatically generated writer method

Source

semaphore.lisp.

Target Slot

name.

Generic Function: semaphore-waiters (instance)
Package

bt-semaphore.

Methods
Method: semaphore-waiters ((instance semaphore))

Return the number of threads waiting on the semaphore.

Source

semaphore.lisp.

Generic Function: signal-semaphore (instance &optional n)
Package

bt-semaphore.

Methods
Method: signal-semaphore ((instance semaphore) &optional n)

Increment the count of the semaphore instance by n. If there are threads waiting on this semaphore, then n of them are woken up.

Source

semaphore.lisp.

Generic Function: try-semaphore (instance &optional n)
Package

bt-semaphore.

Methods
Method: try-semaphore ((instance semaphore) &optional n)

Try to decrement the count of semaphore by n. Returns nil if the count were to become negative, otherwise returns t.

Source

semaphore.lisp.

Generic Function: wait-on-semaphore (instance &key timeout)
Package

bt-semaphore.

Methods
Method: wait-on-semaphore ((instance semaphore) &key timeout)

Decrement the count of the semaphore instance if the count would not be negative, else block until the semaphore can be decremented. Returns t on success. If timeout is given, it is the maximum number of seconds to wait. If the count cannot be decremented in that time, return nil.

Source

semaphore.lisp.


6.2 Internals


6.2.1 Classes

Class: semaphore
Package

bt-semaphore.

Source

semaphore.lisp.

Direct methods
Direct slots
Slot: lock
Initform

(bordeaux-threads:make-lock)

Slot: condvar
Initform

(bordeaux-threads:make-condition-variable)

Slot: count
Package

common-lisp.

Initargs

:count

Slot: waiters
Initform

0

Slot: name
Initargs

:name

Readers

semaphore-name.

Writers

(setf semaphore-name).


Appendix A Indexes


A.1 Concepts


A.2 Functions

Jump to:   (  
F   G   M   S   T   W  
Index Entry  Section

(
(setf semaphore-name): Public generic functions
(setf semaphore-name): Public generic functions

F
Function, make-semaphore: Public ordinary functions

G
Generic Function, (setf semaphore-name): Public generic functions
Generic Function, semaphore-count: Public generic functions
Generic Function, semaphore-name: Public generic functions
Generic Function, semaphore-waiters: Public generic functions
Generic Function, signal-semaphore: Public generic functions
Generic Function, try-semaphore: Public generic functions
Generic Function, wait-on-semaphore: Public generic functions

M
make-semaphore: Public ordinary functions
Method, (setf semaphore-name): Public generic functions
Method, semaphore-count: Public generic functions
Method, semaphore-name: Public generic functions
Method, semaphore-waiters: Public generic functions
Method, signal-semaphore: Public generic functions
Method, try-semaphore: Public generic functions
Method, wait-on-semaphore: Public generic functions

S
semaphore-count: Public generic functions
semaphore-count: Public generic functions
semaphore-name: Public generic functions
semaphore-name: Public generic functions
semaphore-waiters: Public generic functions
semaphore-waiters: Public generic functions
signal-semaphore: Public generic functions
signal-semaphore: Public generic functions

T
try-semaphore: Public generic functions
try-semaphore: Public generic functions

W
wait-on-semaphore: Public generic functions
wait-on-semaphore: Public generic functions