summaryrefslogtreecommitdiff
path: root/fibers/nameset.scm
blob: 312448dd6891033cd5c9d9656a55445c4328dd6e (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
;; Fibers: cooperative, event-driven user-space threads.

;;;; Copyright (C) 2016 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;;;

(define-module (fibers nameset)
  #:use-module (ice-9 atomic)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-26)
  #:export ((make-nameset/public . make-nameset)
            nameset-add!
            nameset-ref
            nameset-fold))

(define-syntax struct
  (lambda (stx)
    (define (id-append ctx . ids)
      (datum->syntax ctx (apply symbol-append (map syntax->datum ids))))
    (syntax-case stx ()
      ((struct tag (field ...))
       (with-syntax ((make-tag (id-append #'tag #'make- #'tag))
                     (tag? (id-append #'tag #'tag #'?))
                     ((tag-field ...) (map (cut id-append #'tag #'tag #'- <>)
                                           #'(field ...))))
         #'(define-record-type tag
             (make-tag field ...)
             tag?
             (field tag-field)
             ...))))))

(struct nameset (names counter))

(define (make-nameset/public)
  "Create a fresh nameset, a weak collection of objects named by
incrementing integers."
  (make-nameset (make-weak-value-hash-table) (make-atomic-box 0)))

(define (atomic-box-fetch-and-inc! box)
  (let lp ((cur (atomic-box-ref box)))
    (let* ((next (1+ cur))
           (cur* (atomic-box-compare-and-swap! box cur next)))
      (if (eqv? cur cur*)
          cur
          (lp cur*)))))

(define (nameset-add! ns obj)
  "Add @var{obj} to the nameset @var{ns}, and return the fresh
name that was created."
  (let ((name (atomic-box-fetch-and-inc! (nameset-counter ns))))
    (hashv-set! (nameset-names ns) name obj)
    name))

(define (nameset-ref ns name)
  "Return the object named @var{name} in nameset @var{ns}, or
@code{#f} if no such object has that name (perhaps because the object
was reclaimed by the garbage collector)."
  (hashv-ref (nameset-names ns) name))

(define* (nameset-fold f ns seed)
  "Fold @var{f} over the objects contained in the nameset @var{ns}.
This will call @var{f} as @code{(@var{f} @var{name} @var{obj}
@var{seed})}, for each @var{name} and @var{obj} in the nameset,
passing the result as @var{seed} to the next call and ultimately
returning the final @var{seed} value."
  (hash-fold f seed (nameset-names ns)))