finalizers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* Copyright 2012-2014,2018-2020
  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. {
  179. if (data.err != EINTR)
  180. {
  181. errno = data.err;
  182. perror ("error in finalization thread");
  183. return NULL;
  184. }
  185. }
  186. else
  187. {
  188. switch (data.byte)
  189. {
  190. case 0:
  191. scm_run_finalizers ();
  192. break;
  193. case 1:
  194. return NULL;
  195. default:
  196. abort ();
  197. }
  198. }
  199. }
  200. }
  201. static void*
  202. run_finalization_thread (void *arg)
  203. {
  204. return scm_with_guile (finalization_thread_proc, arg);
  205. }
  206. static void
  207. start_finalization_thread (void)
  208. {
  209. scm_i_pthread_mutex_lock (&finalization_thread_lock);
  210. if (!finalization_thread_is_running)
  211. {
  212. assert (finalization_pipe[0] == -1);
  213. /* Use the raw pthread API and scm_with_guile, because we don't want
  214. to block on any lock that scm_spawn_thread might want to take,
  215. and we don't want to inherit the dynamic state (fluids) of the
  216. caller. */
  217. if (pipe2 (finalization_pipe, O_CLOEXEC) != 0)
  218. perror ("error creating finalization pipe");
  219. else if (pthread_create (&finalization_thread, NULL,
  220. run_finalization_thread, NULL))
  221. {
  222. reset_finalization_pipe ();
  223. perror ("error creating finalization thread");
  224. }
  225. else
  226. {
  227. GC_set_finalizer_notifier (notify_finalizers_to_run);
  228. finalization_thread_is_running = 1;
  229. }
  230. }
  231. scm_i_pthread_mutex_unlock (&finalization_thread_lock);
  232. }
  233. static void
  234. stop_finalization_thread (void)
  235. {
  236. scm_i_pthread_mutex_lock (&finalization_thread_lock);
  237. if (finalization_thread_is_running)
  238. {
  239. notify_about_to_fork ();
  240. if (pthread_join (finalization_thread, NULL))
  241. perror ("joining finalization thread");
  242. reset_finalization_pipe ();
  243. finalization_thread_is_running = 0;
  244. }
  245. scm_i_pthread_mutex_unlock (&finalization_thread_lock);
  246. }
  247. static void
  248. spawn_finalizer_thread (void)
  249. {
  250. start_finalization_thread ();
  251. }
  252. #endif /* SCM_USE_PTHREAD_THREADS */
  253. void
  254. scm_i_finalizer_pre_fork (void)
  255. {
  256. #if SCM_USE_PTHREAD_THREADS
  257. if (automatic_finalization_p)
  258. {
  259. stop_finalization_thread ();
  260. GC_set_finalizer_notifier (spawn_finalizer_thread);
  261. }
  262. #endif
  263. }
  264. static void
  265. async_gc_finalizer (void *ptr, void *data)
  266. {
  267. void **obj = ptr;
  268. void (*callback) (void) = obj[0];
  269. callback ();
  270. scm_i_set_finalizer (ptr, async_gc_finalizer, data);
  271. }
  272. /* Arrange to call CALLBACK asynchronously after each GC. The callback
  273. will be invoked from a finalizer, which may be from an async or from
  274. another thread.
  275. As an implementation detail, the way this works is that we allocate a
  276. fresh object and put the callback in the object. We know that this
  277. object should get collected the next time GC is run, so we attach a
  278. finalizer to it to trigger the callback.
  279. Once the callback runs, we re-attach a finalizer to that fresh object
  280. to prepare for the next GC, and the process repeats indefinitely.
  281. We could use the scm_after_gc_hook, but using a finalizer has the
  282. advantage of potentially running in another thread, decreasing pause
  283. time.
  284. Note that libgc currently has a heuristic that adding 500 finalizable
  285. objects will cause GC to collect rather than expand the heap,
  286. drastically reducing performance on workloads that actually need to
  287. expand the heap. Therefore scm_i_register_async_gc_callback is
  288. inappropriate for using on unbounded numbers of callbacks. */
  289. void
  290. scm_i_register_async_gc_callback (void (*callback) (void))
  291. {
  292. void **obj = GC_MALLOC_ATOMIC (sizeof (void*));
  293. obj[0] = (void*)callback;
  294. scm_i_set_finalizer (obj, async_gc_finalizer, NULL);
  295. }
  296. int
  297. scm_set_automatic_finalization_enabled (int enabled_p)
  298. {
  299. int was_enabled_p = automatic_finalization_p;
  300. if (enabled_p == was_enabled_p)
  301. return was_enabled_p;
  302. if (!scm_initialized_p)
  303. {
  304. automatic_finalization_p = enabled_p;
  305. return was_enabled_p;
  306. }
  307. if (enabled_p)
  308. {
  309. #if SCM_USE_PTHREAD_THREADS
  310. GC_set_finalizer_notifier (spawn_finalizer_thread);
  311. #else
  312. GC_set_finalizer_notifier (queue_finalizer_async);
  313. #endif
  314. }
  315. else
  316. {
  317. GC_set_finalizer_notifier (0);
  318. #if SCM_USE_PTHREAD_THREADS
  319. stop_finalization_thread ();
  320. #endif
  321. }
  322. automatic_finalization_p = enabled_p;
  323. return was_enabled_p;
  324. }
  325. int
  326. scm_run_finalizers (void)
  327. {
  328. int finalized = GC_invoke_finalizers ();
  329. finalization_count += finalized;
  330. return finalized;
  331. }
  332. void
  333. scm_init_finalizers (void)
  334. {
  335. /* When the async is to run, the cdr of the pair gets set to the
  336. asyncs queue of the current thread. */
  337. run_finalizers_subr = scm_c_make_gsubr ("%run-finalizers", 0, 0, 0,
  338. run_finalizers_async_thunk);
  339. if (automatic_finalization_p)
  340. GC_set_finalizer_notifier (queue_finalizer_async);
  341. }
  342. void
  343. scm_init_finalizer_thread (void)
  344. {
  345. #if SCM_USE_PTHREAD_THREADS
  346. if (automatic_finalization_p)
  347. GC_set_finalizer_notifier (spawn_finalizer_thread);
  348. #endif
  349. }