fdes-finalizers.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright 2016,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include "boolean.h"
  19. #include "eval.h"
  20. #include "extensions.h"
  21. #include "gsubr.h"
  22. #include "hashtab.h"
  23. #include "list.h"
  24. #include "numbers.h"
  25. #include "pairs.h"
  26. #include "threads.h"
  27. #include "version.h"
  28. #include "fdes-finalizers.h"
  29. /* Table of fdes finalizers and associated lock. */
  30. static scm_i_pthread_mutex_t fdes_finalizers_lock =
  31. SCM_I_PTHREAD_MUTEX_INITIALIZER;
  32. static SCM fdes_finalizers;
  33. SCM_DEFINE (scm_add_fdes_finalizer_x, "add-fdes-finalizer!", 2, 0, 0,
  34. (SCM fd, SCM finalizer),
  35. "Add a finalizer that will be called when @var{fd} is closed.")
  36. #define FUNC_NAME s_scm_add_fdes_finalizer_x
  37. {
  38. SCM h;
  39. /* Check type. */
  40. scm_to_uint (fd);
  41. scm_i_pthread_mutex_lock (&fdes_finalizers_lock);
  42. h = scm_hashv_create_handle_x (fdes_finalizers, fd, SCM_EOL);
  43. scm_set_cdr_x (h, scm_cons (finalizer, scm_cdr (h)));
  44. scm_i_pthread_mutex_unlock (&fdes_finalizers_lock);
  45. return SCM_UNSPECIFIED;
  46. }
  47. #undef FUNC_NAME
  48. SCM_DEFINE (scm_remove_fdes_finalizer_x, "remove-fdes-finalizer!", 2, 0, 0,
  49. (SCM fd, SCM finalizer),
  50. "Remove a finalizer that was previously added to the file\n"
  51. "descriptor @var{fd}.")
  52. #define FUNC_NAME s_scm_remove_fdes_finalizer_x
  53. {
  54. SCM h;
  55. /* Check type. */
  56. scm_to_uint (fd);
  57. scm_i_pthread_mutex_lock (&fdes_finalizers_lock);
  58. h = scm_hashv_get_handle (fdes_finalizers, fd);
  59. if (scm_is_true (h))
  60. scm_set_cdr_x (h, scm_delq1_x (finalizer, scm_cdr (h)));
  61. scm_i_pthread_mutex_unlock (&fdes_finalizers_lock);
  62. return SCM_UNSPECIFIED;
  63. }
  64. #undef FUNC_NAME
  65. struct fdes_finalizer_data
  66. {
  67. SCM finalizer;
  68. SCM fd;
  69. };
  70. static SCM
  71. do_run_finalizer (void *data)
  72. {
  73. struct fdes_finalizer_data *fdata = data;
  74. return scm_call_1 (fdata->finalizer, fdata->fd);
  75. }
  76. void
  77. scm_run_fdes_finalizers (int fd)
  78. {
  79. SCM finalizers;
  80. struct fdes_finalizer_data data;
  81. data.fd = scm_from_int (fd);
  82. scm_i_pthread_mutex_lock (&fdes_finalizers_lock);
  83. finalizers = scm_hashv_ref (fdes_finalizers, data.fd, SCM_EOL);
  84. if (!scm_is_null (finalizers))
  85. scm_hashv_remove_x (fdes_finalizers, data.fd);
  86. scm_i_pthread_mutex_unlock (&fdes_finalizers_lock);
  87. for (; !scm_is_null (finalizers); finalizers = scm_cdr (finalizers))
  88. {
  89. data.finalizer = scm_car (finalizers);
  90. scm_internal_catch (SCM_BOOL_T, do_run_finalizer, &data,
  91. scm_handle_by_message_noexit, NULL);
  92. }
  93. }
  94. static void
  95. scm_init_fdes_finalizers (void)
  96. {
  97. #include "fdes-finalizers.x"
  98. }
  99. void
  100. scm_register_fdes_finalizers ()
  101. {
  102. fdes_finalizers = scm_c_make_hash_table (0);
  103. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  104. "scm_init_fdes_finalizers",
  105. (scm_t_extension_init_func) scm_init_fdes_finalizers,
  106. NULL);
  107. }