finalization.scm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ;;; Hoot finalization
  2. ;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Host finalization registry bindings.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot finalization)
  21. (export make-finalization-registry
  22. finalization-registry?
  23. finalization-registry-register!
  24. finalization-registry-unregister!)
  25. (import (hoot eq)
  26. (hoot ffi)
  27. (hoot primitives))
  28. (define-foreign %make-finalization-registry
  29. "finalization" "make_finalization_registry"
  30. (ref extern) -> (ref extern))
  31. (define-foreign %finalization-registry-register
  32. "finalization" "finalization_registry_register"
  33. (ref extern) (ref eq) (ref eq) -> none)
  34. (define-foreign %finalization-registry-register/token
  35. "finalization" "finalization_registry_register_with_token"
  36. (ref extern) (ref eq) (ref eq) (ref eq) -> none)
  37. (define-foreign %finalization-registry-unregister
  38. "finalization" "finalization_registry_unregister"
  39. (ref extern) (ref eq) -> i32)
  40. (define-external-type <finalization-registry>
  41. finalization-registry?
  42. wrap-finalization-registry
  43. unwrap-finalization-registry)
  44. (define (make-finalization-registry cleanup)
  45. (wrap-finalization-registry
  46. (%make-finalization-registry (procedure->external cleanup))))
  47. (define* (finalization-registry-register! registry target held-value
  48. #:optional unregister-token)
  49. (if unregister-token
  50. (%finalization-registry-register/token (unwrap-finalization-registry registry)
  51. target
  52. held-value
  53. unregister-token)
  54. (%finalization-registry-register (unwrap-finalization-registry registry)
  55. target
  56. held-value)))
  57. (define (finalization-registry-unregister! registry unregister-token)
  58. (eq? (%finalization-registry-unregister (unwrap-finalization-registry registry)
  59. unregister-token)
  60. 1)))