finalizers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* Copyright 2012-2014,2018-2020,2022
  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 <assert.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <full-write.h>
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include "async.h"
  25. #include "bdw-gc.h"
  26. #include "gc.h"
  27. #include "gsubr.h"
  28. #include "init.h"
  29. #include "threads.h"
  30. #include "finalizers.h"
  31. static int automatic_finalization_p = 1;
  32. static size_t finalization_count;
  33. static SCM run_finalizers_subr;
  34. void
  35. scm_i_set_finalizer (void *obj, scm_t_finalizer_proc proc, void *data)
  36. {
  37. GC_finalization_proc prev;
  38. void *prev_data;
  39. GC_REGISTER_FINALIZER_NO_ORDER (obj, proc, data, &prev, &prev_data);
  40. }
  41. struct scm_t_chained_finalizer
  42. {
  43. int resuscitating_p;
  44. scm_t_finalizer_proc proc;
  45. void *data;
  46. scm_t_finalizer_proc prev;
  47. void *prev_data;
  48. };
  49. static void
  50. chained_finalizer (void *obj, void *data)
  51. {
  52. struct scm_t_chained_finalizer *chained_data = data;
  53. if (chained_data->resuscitating_p)
  54. {
  55. if (chained_data->prev)
  56. scm_i_set_finalizer (obj, chained_data->prev, chained_data->prev_data);
  57. chained_data->proc (obj, chained_data->data);
  58. }
  59. else
  60. {
  61. chained_data->proc (obj, chained_data->data);
  62. if (chained_data->prev)
  63. chained_data->prev (obj, chained_data->prev_data);
  64. }
  65. }
  66. void
  67. scm_i_add_resuscitator (void *obj, scm_t_finalizer_proc proc, void *data)
  68. {
  69. struct scm_t_chained_finalizer *chained_data;
  70. chained_data = scm_gc_malloc (sizeof (*chained_data), "chained finalizer");
  71. chained_data->resuscitating_p = 1;
  72. chained_data->proc = proc;
  73. chained_data->data = data;
  74. GC_REGISTER_FINALIZER_NO_ORDER (obj, chained_finalizer, chained_data,
  75. &chained_data->prev,
  76. &chained_data->prev_data);
  77. }
  78. static void
  79. shuffle_resuscitators_to_front (struct scm_t_chained_finalizer *cd)
  80. {
  81. while (cd->prev == chained_finalizer)
  82. {
  83. struct scm_t_chained_finalizer *prev = cd->prev_data;
  84. scm_t_finalizer_proc proc = cd->proc;
  85. void *data = cd->data;
  86. if (!prev->resuscitating_p)
  87. break;
  88. cd->resuscitating_p = 1;
  89. cd->proc = prev->proc;
  90. cd->data = prev->data;
  91. prev->resuscitating_p = 0;
  92. prev->proc = proc;
  93. prev->data = data;
  94. cd = prev;
  95. }
  96. }
  97. void
  98. scm_i_add_finalizer (void *obj, scm_t_finalizer_proc proc, void *data)
  99. {
  100. struct scm_t_chained_finalizer *chained_data;
  101. chained_data = scm_gc_malloc (sizeof (*chained_data), "chained finalizer");
  102. chained_data->resuscitating_p = 0;
  103. chained_data->proc = proc;
  104. chained_data->data = data;
  105. GC_REGISTER_FINALIZER_NO_ORDER (obj, chained_finalizer, chained_data,
  106. &chained_data->prev,
  107. &chained_data->prev_data);
  108. shuffle_resuscitators_to_front (chained_data);
  109. }
  110. static SCM
  111. run_finalizers_async_thunk (void)
  112. {
  113. scm_run_finalizers ();
  114. return SCM_UNSPECIFIED;
  115. }
  116. /* The function queue_finalizer_async is run by the GC when there are
  117. * objects to finalize. It will enqueue an asynchronous call to
  118. * GC_invoke_finalizers() at the next SCM_TICK in this thread.
  119. */
  120. static void
  121. queue_finalizer_async (void)
  122. {
  123. scm_thread *t = SCM_I_CURRENT_THREAD;
  124. /* Could be that the current thread is is NULL when we're allocating
  125. in threads.c:guilify_self_1. In that case, rely on the
  126. GC_invoke_finalizers call there after the thread spins up. */
  127. if (!t) return;
  128. scm_system_async_mark_for_thread (run_finalizers_subr, t->handle);
  129. }
  130. #if SCM_USE_PTHREAD_THREADS
  131. static int finalization_pipe[2] = { -1, -1 };
  132. static scm_i_pthread_mutex_t finalization_thread_lock =
  133. SCM_I_PTHREAD_MUTEX_INITIALIZER;
  134. static pthread_t finalization_thread;
  135. static int finalization_thread_is_running = 0;
  136. static void
  137. notify_finalizers_to_run (void)
  138. {
  139. char byte = 0;
  140. full_write (finalization_pipe[1], &byte, 1);
  141. }
  142. static void
  143. notify_about_to_fork (void)
  144. {
  145. char byte = 1;
  146. full_write (finalization_pipe[1], &byte, 1);
  147. }
  148. static void
  149. reset_finalization_pipe (void)
  150. {
  151. close (finalization_pipe[0]);
  152. close (finalization_pipe[1]);
  153. finalization_pipe[0] = -1;
  154. finalization_pipe[1] = -1;
  155. }
  156. struct finalization_pipe_data
  157. {
  158. char byte;
  159. ssize_t n;
  160. int err;
  161. };
  162. static void*
  163. read_finalization_pipe_data (void *data)
  164. {
  165. struct finalization_pipe_data *fdata = data;
  166. fdata->n = read (finalization_pipe[0], &fdata->byte, 1);
  167. fdata->err = errno;
  168. return NULL;
  169. }
  170. static void*
  171. finalization_thread_proc (void *unused)
  172. {
  173. while (1)
  174. {
  175. struct finalization_pipe_data data;
  176. scm_without_guile (read_finalization_pipe_data, &data);
  177. if (data.n == 0)
  178. /* The other end of the pipe was closed, so exit. */
  179. return NULL;
  180. else if (data.n < 0)
  181. {
  182. if (data.err != EINTR)
  183. {
  184. errno = data.err;
  185. perror ("error in finalization thread");
  186. return NULL;
  187. }
  188. }
  189. else
  190. {
  191. switch (data.byte)
  192. {
  193. case 0:
  194. scm_run_finalizers ();
  195. break;
  196. case 1:
  197. return NULL;
  198. default:
  199. abort ();
  200. }
  201. }
  202. }
  203. }
  204. static void*
  205. run_finalization_thread (void *arg)
  206. {
  207. return scm_with_guile (finalization_thread_proc, arg);
  208. }
  209. static void
  210. start_finalization_thread (void)
  211. {
  212. scm_i_pthread_mutex_lock (&finalization_thread_lock);
  213. if (!finalization_thread_is_running)
  214. {
  215. assert (finalization_pipe[0] == -1);
  216. /* Use the raw pthread API and scm_with_guile, because we don't want
  217. to block on any lock that scm_spawn_thread might want to take,
  218. and we don't want to inherit the dynamic state (fluids) of the
  219. caller. */
  220. if (pipe2 (finalization_pipe, O_CLOEXEC) != 0)
  221. perror ("error creating finalization pipe");
  222. else if (pthread_create (&finalization_thread, NULL,
  223. run_finalization_thread, NULL))
  224. {
  225. reset_finalization_pipe ();
  226. perror ("error creating finalization thread");
  227. }
  228. else
  229. {
  230. GC_set_finalizer_notifier (notify_finalizers_to_run);
  231. finalization_thread_is_running = 1;
  232. }
  233. }
  234. scm_i_pthread_mutex_unlock (&finalization_thread_lock);
  235. }
  236. static void
  237. stop_finalization_thread (void)
  238. {
  239. scm_i_pthread_mutex_lock (&finalization_thread_lock);
  240. if (finalization_thread_is_running)
  241. {
  242. notify_about_to_fork ();
  243. if (pthread_join (finalization_thread, NULL))
  244. perror ("joining finalization thread");
  245. reset_finalization_pipe ();
  246. finalization_thread_is_running = 0;
  247. }
  248. scm_i_pthread_mutex_unlock (&finalization_thread_lock);
  249. }
  250. static void
  251. spawn_finalizer_thread (void)
  252. {
  253. start_finalization_thread ();
  254. }
  255. #endif /* SCM_USE_PTHREAD_THREADS */
  256. void
  257. scm_i_finalizer_pre_fork (void)
  258. {
  259. #if SCM_USE_PTHREAD_THREADS
  260. if (automatic_finalization_p)
  261. {
  262. stop_finalization_thread ();
  263. GC_set_finalizer_notifier (spawn_finalizer_thread);
  264. }
  265. #endif
  266. }
  267. static void
  268. async_gc_finalizer (void *ptr, void *data)
  269. {
  270. void **obj = ptr;
  271. void (*callback) (void) = obj[0];
  272. callback ();
  273. scm_i_set_finalizer (ptr, async_gc_finalizer, data);
  274. }
  275. /* Arrange to call CALLBACK asynchronously after each GC. The callback
  276. will be invoked from a finalizer, which may be from an async or from
  277. another thread.
  278. As an implementation detail, the way this works is that we allocate a
  279. fresh object and put the callback in the object. We know that this
  280. object should get collected the next time GC is run, so we attach a
  281. finalizer to it to trigger the callback.
  282. Once the callback runs, we re-attach a finalizer to that fresh object
  283. to prepare for the next GC, and the process repeats indefinitely.
  284. We could use the scm_after_gc_hook, but using a finalizer has the
  285. advantage of potentially running in another thread, decreasing pause
  286. time.
  287. Note that libgc currently has a heuristic that adding 500 finalizable
  288. objects will cause GC to collect rather than expand the heap,
  289. drastically reducing performance on workloads that actually need to
  290. expand the heap. Therefore scm_i_register_async_gc_callback is
  291. inappropriate for using on unbounded numbers of callbacks. */
  292. void
  293. scm_i_register_async_gc_callback (void (*callback) (void))
  294. {
  295. void **obj = GC_MALLOC_ATOMIC (sizeof (void*));
  296. obj[0] = (void*)callback;
  297. scm_i_set_finalizer (obj, async_gc_finalizer, NULL);
  298. }
  299. int
  300. scm_set_automatic_finalization_enabled (int enabled_p)
  301. {
  302. int was_enabled_p = automatic_finalization_p;
  303. if (enabled_p == was_enabled_p)
  304. return was_enabled_p;
  305. if (!scm_initialized_p)
  306. {
  307. automatic_finalization_p = enabled_p;
  308. return was_enabled_p;
  309. }
  310. if (enabled_p)
  311. {
  312. #if SCM_USE_PTHREAD_THREADS
  313. GC_set_finalizer_notifier (spawn_finalizer_thread);
  314. #else
  315. GC_set_finalizer_notifier (queue_finalizer_async);
  316. #endif
  317. }
  318. else
  319. {
  320. GC_set_finalizer_notifier (0);
  321. #if SCM_USE_PTHREAD_THREADS
  322. stop_finalization_thread ();
  323. #endif
  324. }
  325. automatic_finalization_p = enabled_p;
  326. return was_enabled_p;
  327. }
  328. int
  329. scm_run_finalizers (void)
  330. {
  331. int finalized = GC_invoke_finalizers ();
  332. finalization_count += finalized;
  333. return finalized;
  334. }
  335. void
  336. scm_init_finalizers (void)
  337. {
  338. /* When the async is to run, the cdr of the pair gets set to the
  339. asyncs queue of the current thread. */
  340. run_finalizers_subr = scm_c_make_gsubr ("%run-finalizers", 0, 0, 0,
  341. run_finalizers_async_thunk);
  342. if (automatic_finalization_p)
  343. GC_set_finalizer_notifier (queue_finalizer_async);
  344. }
  345. void
  346. scm_init_finalizer_thread (void)
  347. {
  348. #if SCM_USE_PTHREAD_THREADS
  349. if (automatic_finalization_p)
  350. GC_set_finalizer_notifier (spawn_finalizer_thread);
  351. #endif
  352. }