guardians.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* Copyright (C) 1998,1999,2000,2001, 2006, 2008, 2009, 2011,
  2. * 2012, 2013 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. /* This is an implementation of guardians as described in
  20. * R. Kent Dybvig, Carl Bruggeman, and David Eby (1993) "Guardians in
  21. * a Generation-Based Garbage Collector" ACM SIGPLAN Conference on
  22. * Programming Language Design and Implementation, June 1993
  23. * ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/guardians.ps.gz
  24. *
  25. * Original design: Mikael Djurfeldt
  26. * Original implementation: Michael Livshin
  27. * Hacked on since by: everybody
  28. *
  29. * By this point, the semantics are actually quite different from
  30. * those described in the abovementioned paper. The semantic changes
  31. * are there to improve safety and intuitiveness. The interface is
  32. * still (mostly) the one described by the paper, however.
  33. *
  34. * Boiled down again: Marius Vollmer
  35. *
  36. * Now they should again behave like those described in the paper.
  37. * Scheme guardians should be simple and friendly, not like the greedy
  38. * monsters we had...
  39. *
  40. * Rewritten for the Boehm-Demers-Weiser GC by Ludovic Courtès.
  41. */
  42. /* Uncomment the following line to debug guardian finalization. */
  43. /* #define DEBUG_GUARDIANS 1 */
  44. #ifdef HAVE_CONFIG_H
  45. # include <config.h>
  46. #endif
  47. #include "libguile/_scm.h"
  48. #include "libguile/ports.h"
  49. #include "libguile/print.h"
  50. #include "libguile/smob.h"
  51. #include "libguile/validate.h"
  52. #include "libguile/root.h"
  53. #include "libguile/hashtab.h"
  54. #include "libguile/deprecation.h"
  55. #include "libguile/eval.h"
  56. #include "libguile/guardians.h"
  57. #include "libguile/bdw-gc.h"
  58. static scm_t_bits tc16_guardian;
  59. typedef struct t_guardian
  60. {
  61. scm_i_pthread_mutex_t mutex;
  62. unsigned long live;
  63. SCM zombies;
  64. struct t_guardian *next;
  65. } t_guardian;
  66. #define GUARDIAN_P(x) SCM_SMOB_PREDICATE(tc16_guardian, x)
  67. #define GUARDIAN_DATA(x) ((t_guardian *) SCM_SMOB_DATA_1 (x))
  68. static int
  69. guardian_print (SCM guardian, SCM port, scm_print_state *pstate SCM_UNUSED)
  70. {
  71. t_guardian *g = GUARDIAN_DATA (guardian);
  72. scm_puts_unlocked ("#<guardian ", port);
  73. scm_uintprint ((scm_t_bits) g, 16, port);
  74. scm_puts_unlocked (" (reachable: ", port);
  75. scm_display (scm_from_uint (g->live), port);
  76. scm_puts_unlocked (" unreachable: ", port);
  77. scm_display (scm_length (g->zombies), port);
  78. scm_puts_unlocked (")", port);
  79. scm_puts_unlocked (">", port);
  80. return 1;
  81. }
  82. /* Handle finalization of OBJ which is guarded by the guardians listed in
  83. GUARDIAN_LIST. */
  84. static void
  85. finalize_guarded (void *ptr, void *finalizer_data)
  86. {
  87. SCM cell_pool;
  88. SCM obj, guardian_list, proxied_finalizer;
  89. obj = SCM_PACK_POINTER (ptr);
  90. guardian_list = SCM_CDR (SCM_PACK_POINTER (finalizer_data));
  91. proxied_finalizer = SCM_CAR (SCM_PACK_POINTER (finalizer_data));
  92. #ifdef DEBUG_GUARDIANS
  93. printf ("finalizing guarded %p (%u guardians)\n",
  94. ptr, scm_to_uint (scm_length (guardian_list)));
  95. #endif
  96. /* Preallocate a bunch of cells so that we can make sure that no garbage
  97. collection (and, thus, nested calls to `finalize_guarded ()') occurs
  98. while executing the following loop. This is quite inefficient (call to
  99. `scm_length ()') but that shouldn't be a problem in most cases. */
  100. cell_pool = scm_make_list (scm_length (guardian_list), SCM_UNSPECIFIED);
  101. /* Tell each guardian interested in OBJ that OBJ is no longer
  102. reachable. */
  103. for (;
  104. !scm_is_null (guardian_list);
  105. guardian_list = SCM_CDR (guardian_list))
  106. {
  107. SCM zombies;
  108. SCM guardian;
  109. t_guardian *g;
  110. guardian = scm_c_weak_vector_ref (scm_car (guardian_list), 0);
  111. if (scm_is_false (guardian))
  112. {
  113. /* The guardian itself vanished in the meantime. */
  114. #ifdef DEBUG_GUARDIANS
  115. printf (" guardian for %p vanished\n", ptr);
  116. #endif
  117. continue;
  118. }
  119. g = GUARDIAN_DATA (guardian);
  120. scm_i_pthread_mutex_lock (&g->mutex);
  121. if (g->live == 0)
  122. abort ();
  123. /* Get a fresh cell from CELL_POOL. */
  124. zombies = cell_pool;
  125. cell_pool = SCM_CDR (cell_pool);
  126. /* Compute and update G's zombie list. */
  127. SCM_SETCAR (zombies, obj);
  128. SCM_SETCDR (zombies, g->zombies);
  129. g->zombies = zombies;
  130. g->live--;
  131. scm_i_pthread_mutex_unlock (&g->mutex);
  132. }
  133. if (scm_is_true (proxied_finalizer))
  134. {
  135. /* Re-register the finalizer that was in place before we installed this
  136. one. */
  137. GC_finalization_proc finalizer, prev_finalizer;
  138. void *finalizer_data, *prev_finalizer_data;
  139. finalizer = (GC_finalization_proc) SCM_UNPACK_POINTER (SCM_CAR (proxied_finalizer));
  140. finalizer_data = SCM_UNPACK_POINTER (SCM_CDR (proxied_finalizer));
  141. if (finalizer == NULL)
  142. abort ();
  143. GC_REGISTER_FINALIZER_NO_ORDER (ptr, finalizer, finalizer_data,
  144. &prev_finalizer, &prev_finalizer_data);
  145. #ifdef DEBUG_GUARDIANS
  146. printf (" reinstalled proxied finalizer %p for %p\n", finalizer, ptr);
  147. #endif
  148. }
  149. #ifdef DEBUG_GUARDIANS
  150. printf ("end of finalize (%p)\n", ptr);
  151. #endif
  152. }
  153. /* Add OBJ as a guarded object of GUARDIAN. */
  154. static void
  155. scm_i_guard (SCM guardian, SCM obj)
  156. {
  157. t_guardian *g = GUARDIAN_DATA (guardian);
  158. if (SCM_HEAP_OBJECT_P (obj))
  159. {
  160. /* Register a finalizer and pass a pair as the ``client data''
  161. argument. The pair contains in its car `#f' or a pair describing a
  162. ``proxied'' finalizer (see below); its cdr contains a list of
  163. guardians interested in OBJ.
  164. A ``proxied'' finalizer is a finalizer that was registered for OBJ
  165. before OBJ became guarded (e.g., a SMOB `free' function). We are
  166. assuming here that finalizers are only used internally, either at
  167. the very beginning of an object's lifetime (e.g., see `SCM_NEWSMOB')
  168. or by this function. */
  169. GC_finalization_proc prev_finalizer;
  170. void *prev_data;
  171. SCM guardians_for_obj, finalizer_data;
  172. scm_i_pthread_mutex_lock (&g->mutex);
  173. g->live++;
  174. /* Note: GUARDIANS_FOR_OBJ holds weak references to guardians so
  175. that a guardian can be collected before the objects it guards
  176. (see `guardians.test'). */
  177. guardians_for_obj = scm_cons (scm_make_weak_vector (SCM_INUM1, guardian),
  178. SCM_EOL);
  179. finalizer_data = scm_cons (SCM_BOOL_F, guardians_for_obj);
  180. GC_REGISTER_FINALIZER_NO_ORDER (SCM_UNPACK_POINTER (obj), finalize_guarded,
  181. SCM_UNPACK_POINTER (finalizer_data),
  182. &prev_finalizer, &prev_data);
  183. if (prev_finalizer == finalize_guarded)
  184. {
  185. /* OBJ is already guarded by another guardian: add GUARDIAN to its
  186. list of guardians. */
  187. SCM prev_guardian_list, prev_finalizer_data;
  188. if (prev_data == NULL)
  189. abort ();
  190. prev_finalizer_data = SCM_PACK_POINTER (prev_data);
  191. if (!scm_is_pair (prev_finalizer_data))
  192. abort ();
  193. prev_guardian_list = SCM_CDR (prev_finalizer_data);
  194. SCM_SETCDR (guardians_for_obj, prev_guardian_list);
  195. /* Also copy information about proxied finalizers. */
  196. SCM_SETCAR (finalizer_data, SCM_CAR (prev_finalizer_data));
  197. }
  198. else if (prev_finalizer != NULL)
  199. {
  200. /* There was already a finalizer registered for OBJ so we will
  201. ``proxy'' it, i.e., record it so that we can re-register it once
  202. `finalize_guarded ()' has finished. */
  203. SCM proxied_finalizer;
  204. proxied_finalizer = scm_cons (SCM_PACK_POINTER (prev_finalizer),
  205. SCM_PACK_POINTER (prev_data));
  206. SCM_SETCAR (finalizer_data, proxied_finalizer);
  207. }
  208. scm_i_pthread_mutex_unlock (&g->mutex);
  209. }
  210. }
  211. static SCM
  212. scm_i_get_one_zombie (SCM guardian)
  213. {
  214. t_guardian *g = GUARDIAN_DATA (guardian);
  215. SCM res = SCM_BOOL_F;
  216. scm_i_pthread_mutex_lock (&g->mutex);
  217. if (!scm_is_null (g->zombies))
  218. {
  219. /* Note: We return zombies in reverse order. */
  220. res = SCM_CAR (g->zombies);
  221. g->zombies = SCM_CDR (g->zombies);
  222. }
  223. scm_i_pthread_mutex_unlock (&g->mutex);
  224. return res;
  225. }
  226. /* This is the Scheme entry point for each guardian: If OBJ is an
  227. * object, it's added to the guardian's live list. If OBJ is unbound,
  228. * the next available unreachable object (or #f if none) is returned.
  229. *
  230. * If the second optional argument THROW_P is true (the default), then
  231. * an error is raised if GUARDIAN is greedy and OBJ is already greedily
  232. * guarded. If THROW_P is false, #f is returned instead of raising the
  233. * error, and #t is returned if everything is fine.
  234. */
  235. static SCM
  236. guardian_apply (SCM guardian, SCM obj, SCM throw_p)
  237. {
  238. if (!SCM_UNBNDP (obj))
  239. {
  240. scm_i_guard (guardian, obj);
  241. return SCM_UNSPECIFIED;
  242. }
  243. else
  244. return scm_i_get_one_zombie (guardian);
  245. }
  246. SCM_DEFINE (scm_make_guardian, "make-guardian", 0, 0, 0,
  247. (),
  248. "Create a new guardian. A guardian protects a set of objects from\n"
  249. "garbage collection, allowing a program to apply cleanup or other\n"
  250. "actions.\n"
  251. "\n"
  252. "@code{make-guardian} returns a procedure representing the guardian.\n"
  253. "Calling the guardian procedure with an argument adds the argument to\n"
  254. "the guardian's set of protected objects. Calling the guardian\n"
  255. "procedure without an argument returns one of the protected objects\n"
  256. "which are ready for garbage collection, or @code{#f} if no such object\n"
  257. "is available. Objects which are returned in this way are removed from\n"
  258. "the guardian.\n"
  259. "\n"
  260. "You can put a single object into a guardian more than once and you can\n"
  261. "put a single object into more than one guardian. The object will then\n"
  262. "be returned multiple times by the guardian procedures.\n"
  263. "\n"
  264. "An object is eligible to be returned from a guardian when it is no\n"
  265. "longer referenced from outside any guardian.\n"
  266. "\n"
  267. "There is no guarantee about the order in which objects are returned\n"
  268. "from a guardian. If you want to impose an order on finalization\n"
  269. "actions, for example, you can do that by keeping objects alive in some\n"
  270. "global data structure until they are no longer needed for finalizing\n"
  271. "other objects.\n"
  272. "\n"
  273. "Being an element in a weak vector, a key in a hash table with weak\n"
  274. "keys, or a value in a hash table with weak value does not prevent an\n"
  275. "object from being returned by a guardian. But as long as an object\n"
  276. "can be returned from a guardian it will not be removed from such a\n"
  277. "weak vector or hash table. In other words, a weak link does not\n"
  278. "prevent an object from being considered collectable, but being inside\n"
  279. "a guardian prevents a weak link from being broken.\n"
  280. "\n"
  281. "A key in a weak key hash table can be though of as having a strong\n"
  282. "reference to its associated value as long as the key is accessible.\n"
  283. "Consequently, when the key only accessible from within a guardian, the\n"
  284. "reference from the key to the value is also considered to be coming\n"
  285. "from within a guardian. Thus, if there is no other reference to the\n"
  286. "value, it is eligible to be returned from a guardian.\n")
  287. #define FUNC_NAME s_scm_make_guardian
  288. {
  289. t_guardian *g = scm_gc_malloc (sizeof (t_guardian), "guardian");
  290. SCM z;
  291. scm_i_pthread_mutex_init (&g->mutex, NULL);
  292. /* A tconc starts out with one tail pair. */
  293. g->live = 0;
  294. g->zombies = SCM_EOL;
  295. g->next = NULL;
  296. SCM_NEWSMOB (z, tc16_guardian, g);
  297. return z;
  298. }
  299. #undef FUNC_NAME
  300. void
  301. scm_init_guardians ()
  302. {
  303. /* We use unordered finalization `a la Java. */
  304. GC_set_java_finalization (1);
  305. tc16_guardian = scm_make_smob_type ("guardian", 0);
  306. scm_set_smob_print (tc16_guardian, guardian_print);
  307. scm_set_smob_apply (tc16_guardian, guardian_apply, 0, 1, 0);
  308. #include "libguile/guardians.x"
  309. }
  310. /*
  311. Local Variables:
  312. c-file-style: "gnu"
  313. End:
  314. */