finalizers.c 9.9 KB

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