finalizers.c 10 KB

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