finalizers.c 11 KB

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