-
Notifications
You must be signed in to change notification settings - Fork 9
/
registry.lisp
61 lines (42 loc) · 1.57 KB
/
registry.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
;;;; Named agent registry.
(in-package :erlangen.registry)
(defvar *registry* (make-hash-table :lock-free t))
(defvar *registry*/lock (make-read-write-lock))
(defun register (name &optional (agent (agent)))
"*Arguments and Values:*
_name_—a _keyword_.
_agent_—an _agent_. Default is the _calling agent_.
*Description*:
{register} associates _name_ with _agent_.
*Exceptional Situations:*
If _name_ is already associated with an _agent_ an _error_ of _type_
{simple-error} is signaled."
(check-type name keyword)
(check-type agent agent)
(with-write-lock (*registry*/lock)
(if #1=(gethash name *registry*)
(error "~a is already registered." name)
(setf #1# agent))))
(defun unregister (name)
"*Arguments and Values:*
_name_—a _keyword_.
*Description*:
{unregister} removes the registered _name_, associated with an
_agent_.
*Exceptional Situations:*
If the _name_ is not associated with an _agent_ an _error_ of _type_
{simple-error} is signaled."
(check-type name keyword)
(with-write-lock (*registry*/lock)
(or (remhash name *registry*)
(error "Not registered: ~a" name))))
(defun registered ()
"*Description*:
{registered} returns a _list_ of names associated with _agents_."
(with-read-lock (*registry*/lock)
(loop for name being the hash-keys of *registry* collect name)))
(defun agent-by-name (name)
"Return agent by NAME if registered, signal error otherwise."
(with-read-lock (*registry*/lock)
(or (gethash name *registry*)
(error "No such agent: ~a" name))))