fdes-finalizers.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "fdes-finalizers.h"
  28. /* Table of fdes finalizers and associated lock. */
  29. static scm_i_pthread_mutex_t fdes_finalizers_lock =
  30. SCM_I_PTHREAD_MUTEX_INITIALIZER;
  31. static SCM fdes_finalizers;
  32. SCM_DEFINE (scm_add_fdes_finalizer_x, "add-fdes-finalizer!", 2, 0, 0,
  33. (SCM fd, SCM finalizer),
  34. "Add a finalizer that will be called when @var{fd} is closed.")
  35. #define FUNC_NAME s_scm_add_fdes_finalizer_x
  36. {
  37. SCM h;
  38. /* Check type. */
  39. scm_to_uint (fd);
  40. scm_i_pthread_mutex_lock (&fdes_finalizers_lock);
  41. h = scm_hashv_create_handle_x (fdes_finalizers, fd, SCM_EOL);
  42. scm_set_cdr_x (h, scm_cons (finalizer, scm_cdr (h)));
  43. scm_i_pthread_mutex_unlock (&fdes_finalizers_lock);
  44. return SCM_UNSPECIFIED;
  45. }
  46. #undef FUNC_NAME
  47. SCM_DEFINE (scm_remove_fdes_finalizer_x, "remove-fdes-finalizer!", 2, 0, 0,
  48. (SCM fd, SCM finalizer),
  49. "Remove a finalizer that was previously added to the file\n"
  50. "descriptor @var{fd}.")
  51. #define FUNC_NAME s_scm_remove_fdes_finalizer_x
  52. {
  53. SCM h;
  54. /* Check type. */
  55. scm_to_uint (fd);
  56. scm_i_pthread_mutex_lock (&fdes_finalizers_lock);
  57. h = scm_hashv_get_handle (fdes_finalizers, fd);
  58. if (scm_is_true (h))
  59. scm_set_cdr_x (h, scm_delq1_x (finalizer, scm_cdr (h)));
  60. scm_i_pthread_mutex_unlock (&fdes_finalizers_lock);
  61. return SCM_UNSPECIFIED;
  62. }
  63. #undef FUNC_NAME
  64. struct fdes_finalizer_data
  65. {
  66. SCM finalizer;
  67. SCM fd;
  68. };
  69. static SCM
  70. do_run_finalizer (void *data)
  71. {
  72. struct fdes_finalizer_data *fdata = data;
  73. return scm_call_1 (fdata->finalizer, fdata->fd);
  74. }
  75. void
  76. scm_run_fdes_finalizers (int fd)
  77. {
  78. SCM finalizers;
  79. struct fdes_finalizer_data data;
  80. data.fd = scm_from_int (fd);
  81. scm_i_pthread_mutex_lock (&fdes_finalizers_lock);
  82. finalizers = scm_hashv_ref (fdes_finalizers, data.fd, SCM_EOL);
  83. if (!scm_is_null (finalizers))
  84. scm_hashv_remove_x (fdes_finalizers, data.fd);
  85. scm_i_pthread_mutex_unlock (&fdes_finalizers_lock);
  86. for (; !scm_is_null (finalizers); finalizers = scm_cdr (finalizers))
  87. {
  88. data.finalizer = scm_car (finalizers);
  89. scm_internal_catch (SCM_BOOL_T, do_run_finalizer, &data,
  90. scm_handle_by_message_noexit, NULL);
  91. }
  92. }
  93. static void
  94. scm_init_fdes_finalizers (void)
  95. {
  96. #include "fdes-finalizers.x"
  97. }
  98. void
  99. scm_register_fdes_finalizers ()
  100. {
  101. fdes_finalizers = scm_c_make_hash_table (0);
  102. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  103. "scm_init_fdes_finalizers",
  104. (scm_t_extension_init_func) scm_init_fdes_finalizers,
  105. NULL);
  106. }