nameset.scm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ;; Fibers: cooperative, event-driven user-space threads.
  2. ;;;; Copyright (C) 2016 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. (define-module (fibers nameset)
  19. #:use-module (ice-9 atomic)
  20. #:use-module (ice-9 match)
  21. #:use-module (srfi srfi-9)
  22. #:use-module (srfi srfi-26)
  23. #:export ((make-nameset/public . make-nameset)
  24. nameset-add!
  25. nameset-ref
  26. nameset-fold))
  27. (define-syntax struct
  28. (lambda (stx)
  29. (define (id-append ctx . ids)
  30. (datum->syntax ctx (apply symbol-append (map syntax->datum ids))))
  31. (syntax-case stx ()
  32. ((struct tag (field ...))
  33. (with-syntax ((make-tag (id-append #'tag #'make- #'tag))
  34. (tag? (id-append #'tag #'tag #'?))
  35. ((tag-field ...) (map (cut id-append #'tag #'tag #'- <>)
  36. #'(field ...))))
  37. #'(define-record-type tag
  38. (make-tag field ...)
  39. tag?
  40. (field tag-field)
  41. ...))))))
  42. (struct nameset (names counter))
  43. (define (make-nameset/public)
  44. "Create a fresh nameset, a weak collection of objects named by
  45. incrementing integers."
  46. (make-nameset (make-weak-value-hash-table) (make-atomic-box 0)))
  47. (define (atomic-box-fetch-and-inc! box)
  48. (let lp ((cur (atomic-box-ref box)))
  49. (let* ((next (1+ cur))
  50. (cur* (atomic-box-compare-and-swap! box cur next)))
  51. (if (eqv? cur cur*)
  52. cur
  53. (lp cur*)))))
  54. (define (nameset-add! ns obj)
  55. "Add @var{obj} to the nameset @var{ns}, and return the fresh
  56. name that was created."
  57. (let ((name (atomic-box-fetch-and-inc! (nameset-counter ns))))
  58. (hashv-set! (nameset-names ns) name obj)
  59. name))
  60. (define (nameset-ref ns name)
  61. "Return the object named @var{name} in nameset @var{ns}, or
  62. @code{#f} if no such object has that name (perhaps because the object
  63. was reclaimed by the garbage collector)."
  64. (hashv-ref (nameset-names ns) name))
  65. (define* (nameset-fold f ns seed)
  66. "Fold @var{f} over the objects contained in the nameset @var{ns}.
  67. This will call @var{f} as @code{(@var{f} @var{name} @var{obj}
  68. @var{seed})}, for each @var{name} and @var{obj} in the nameset,
  69. passing the result as @var{seed} to the next call and ultimately
  70. returning the final @var{seed} value."
  71. (hash-fold f seed (nameset-names ns)))