weak-set.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /* Copyright (C) 2011, 2012, 2013 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <assert.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/hash.h"
  24. #include "libguile/eval.h"
  25. #include "libguile/ports.h"
  26. #include "libguile/bdw-gc.h"
  27. #include "libguile/validate.h"
  28. #include "libguile/weak-set.h"
  29. /* Weak Sets
  30. This file implements weak sets. One example of a weak set is the
  31. symbol table, where you want all instances of the `foo' symbol to map
  32. to one object. So when you load a file and it wants a symbol with
  33. the characters "foo", you one up in the table, using custom hash and
  34. equality predicates. Only if one is not found will you bother to
  35. cons one up and intern it.
  36. Another use case for weak sets is the set of open ports. Guile needs
  37. to be able to flush them all when the process exits, but the set
  38. shouldn't prevent the GC from collecting the port (and thus closing
  39. it).
  40. Weak sets are implemented using an open-addressed hash table.
  41. Basically this means that there is an array of entries, and the item
  42. is expected to be found the slot corresponding to its hash code,
  43. modulo the length of the array.
  44. Collisions are handled using linear probing with the Robin Hood
  45. technique. See Pedro Celis' paper, "Robin Hood Hashing":
  46. http://www.cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf
  47. The vector of entries is allocated as an "atomic" piece of memory, so
  48. that the GC doesn't trace it. When an item is added to the set, a
  49. disappearing link is registered to its location. If the item is
  50. collected, then that link will be zeroed out.
  51. An entry is not just an item, though; the hash code is also stored in
  52. the entry. We munge hash codes so that they are never 0. In this
  53. way we can detect removed entries (key of zero but nonzero hash
  54. code), and can then reshuffle elements as needed to maintain the
  55. robin hood ordering.
  56. Compared to buckets-and-chains hash tables, open addressing has the
  57. advantage that it is very cache-friendly. It also uses less memory.
  58. Implementation-wise, there are two things to note.
  59. 1. We assume that hash codes are evenly distributed across the
  60. range of unsigned longs. The actual hash code stored in the
  61. entry is left-shifted by 1 bit (losing 1 bit of hash precision),
  62. and then or'd with 1. In this way we ensure that the hash field
  63. of an occupied entry is nonzero. To map to an index, we
  64. right-shift the hash by one, divide by the size, and take the
  65. remainder.
  66. 2. Since the "keys" (the objects in the set) are stored in an
  67. atomic region with disappearing links, they need to be accessed
  68. with the GC alloc lock. `copy_weak_entry' will do that for
  69. you. The hash code itself can be read outside the lock,
  70. though.
  71. */
  72. typedef struct {
  73. unsigned long hash;
  74. scm_t_bits key;
  75. } scm_t_weak_entry;
  76. struct weak_entry_data {
  77. scm_t_weak_entry *in;
  78. scm_t_weak_entry *out;
  79. };
  80. static void*
  81. do_copy_weak_entry (void *data)
  82. {
  83. struct weak_entry_data *e = data;
  84. e->out->hash = e->in->hash;
  85. e->out->key = e->in->key;
  86. return NULL;
  87. }
  88. static void
  89. copy_weak_entry (scm_t_weak_entry *src, scm_t_weak_entry *dst)
  90. {
  91. struct weak_entry_data data;
  92. data.in = src;
  93. data.out = dst;
  94. GC_call_with_alloc_lock (do_copy_weak_entry, &data);
  95. }
  96. typedef struct {
  97. scm_t_weak_entry *entries; /* the data */
  98. scm_i_pthread_mutex_t lock; /* the lock */
  99. unsigned long size; /* total number of slots. */
  100. unsigned long n_items; /* number of items in set */
  101. unsigned long lower; /* when to shrink */
  102. unsigned long upper; /* when to grow */
  103. int size_index; /* index into hashset_size */
  104. int min_size_index; /* minimum size_index */
  105. } scm_t_weak_set;
  106. #define SCM_WEAK_SET_P(x) (SCM_HAS_TYP7 (x, scm_tc7_weak_set))
  107. #define SCM_VALIDATE_WEAK_SET(pos, arg) \
  108. SCM_MAKE_VALIDATE_MSG (pos, arg, WEAK_SET_P, "weak-set")
  109. #define SCM_WEAK_SET(x) ((scm_t_weak_set *) SCM_CELL_WORD_1 (x))
  110. static unsigned long
  111. hash_to_index (unsigned long hash, unsigned long size)
  112. {
  113. return (hash >> 1) % size;
  114. }
  115. static unsigned long
  116. entry_distance (unsigned long hash, unsigned long k, unsigned long size)
  117. {
  118. unsigned long origin = hash_to_index (hash, size);
  119. if (k >= origin)
  120. return k - origin;
  121. else
  122. /* The other key was displaced and wrapped around. */
  123. return size - origin + k;
  124. }
  125. #ifndef HAVE_GC_MOVE_DISAPPEARING_LINK
  126. static void
  127. GC_move_disappearing_link (void **from, void **to)
  128. {
  129. GC_unregister_disappearing_link (from);
  130. SCM_I_REGISTER_DISAPPEARING_LINK (to, *to);
  131. }
  132. #endif
  133. static void
  134. move_weak_entry (scm_t_weak_entry *from, scm_t_weak_entry *to)
  135. {
  136. if (from->hash)
  137. {
  138. scm_t_weak_entry copy;
  139. copy_weak_entry (from, &copy);
  140. to->hash = copy.hash;
  141. to->key = copy.key;
  142. if (copy.key && SCM_HEAP_OBJECT_P (SCM_PACK (copy.key)))
  143. GC_move_disappearing_link ((void **) &from->key, (void **) &to->key);
  144. }
  145. else
  146. {
  147. to->hash = 0;
  148. to->key = 0;
  149. }
  150. }
  151. static void
  152. rob_from_rich (scm_t_weak_set *set, unsigned long k)
  153. {
  154. unsigned long empty, size;
  155. size = set->size;
  156. /* If we are to free up slot K in the set, we need room to do so. */
  157. assert (set->n_items < size);
  158. empty = k;
  159. do
  160. empty = (empty + 1) % size;
  161. /* Here we access key outside the lock. Is this a problem? At first
  162. glance, I wouldn't think so. */
  163. while (set->entries[empty].key);
  164. do
  165. {
  166. unsigned long last = empty ? (empty - 1) : (size - 1);
  167. move_weak_entry (&set->entries[last], &set->entries[empty]);
  168. empty = last;
  169. }
  170. while (empty != k);
  171. /* Just for sanity. */
  172. set->entries[empty].hash = 0;
  173. set->entries[empty].key = 0;
  174. }
  175. static void
  176. give_to_poor (scm_t_weak_set *set, unsigned long k)
  177. {
  178. /* Slot K was just freed up; possibly shuffle others down. */
  179. unsigned long size = set->size;
  180. while (1)
  181. {
  182. unsigned long next = (k + 1) % size;
  183. unsigned long hash;
  184. scm_t_weak_entry copy;
  185. hash = set->entries[next].hash;
  186. if (!hash || hash_to_index (hash, size) == next)
  187. break;
  188. copy_weak_entry (&set->entries[next], &copy);
  189. if (!copy.key)
  190. /* Lost weak reference. */
  191. {
  192. give_to_poor (set, next);
  193. set->n_items--;
  194. continue;
  195. }
  196. move_weak_entry (&set->entries[next], &set->entries[k]);
  197. k = next;
  198. }
  199. /* We have shuffled down any entries that should be shuffled down; now
  200. free the end. */
  201. set->entries[k].hash = 0;
  202. set->entries[k].key = 0;
  203. }
  204. /* Growing or shrinking is triggered when the load factor
  205. *
  206. * L = N / S (N: number of items in set, S: bucket vector length)
  207. *
  208. * passes an upper limit of 0.9 or a lower limit of 0.2.
  209. *
  210. * The implementation stores the upper and lower number of items which
  211. * trigger a resize in the hashset object.
  212. *
  213. * Possible hash set sizes (primes) are stored in the array
  214. * hashset_size.
  215. */
  216. static unsigned long hashset_size[] = {
  217. 31, 61, 113, 223, 443, 883, 1759, 3517, 7027, 14051, 28099, 56197, 112363,
  218. 224717, 449419, 898823, 1797641, 3595271, 7190537, 14381041, 28762081,
  219. 57524111, 115048217, 230096423
  220. };
  221. #define HASHSET_SIZE_N (sizeof(hashset_size)/sizeof(unsigned long))
  222. static int
  223. compute_size_index (scm_t_weak_set *set)
  224. {
  225. int i = set->size_index;
  226. if (set->n_items < set->lower)
  227. {
  228. /* rehashing is not triggered when i <= min_size */
  229. do
  230. --i;
  231. while (i > set->min_size_index
  232. && set->n_items < hashset_size[i] / 5);
  233. }
  234. else if (set->n_items > set->upper)
  235. {
  236. ++i;
  237. if (i >= HASHSET_SIZE_N)
  238. /* The biggest size currently is 230096423, which for a 32-bit
  239. machine will occupy 1.5GB of memory at a load of 80%. There
  240. is probably something better to do here, but if you have a
  241. weak map of that size, you are hosed in any case. */
  242. abort ();
  243. }
  244. return i;
  245. }
  246. static int
  247. is_acceptable_size_index (scm_t_weak_set *set, int size_index)
  248. {
  249. int computed = compute_size_index (set);
  250. if (size_index == computed)
  251. /* We were going to grow or shrink, and allocating the new vector
  252. didn't change the target size. */
  253. return 1;
  254. if (size_index == computed + 1)
  255. {
  256. /* We were going to enlarge the set, but allocating the new
  257. vector finalized some objects, making an enlargement
  258. unnecessary. It might still be a good idea to use the larger
  259. set, though. (This branch also gets hit if, while allocating
  260. the vector, some other thread was actively removing items from
  261. the set. That is less likely, though.) */
  262. unsigned long new_lower = hashset_size[size_index] / 5;
  263. return set->size > new_lower;
  264. }
  265. if (size_index == computed - 1)
  266. {
  267. /* We were going to shrink the set, but when we dropped the lock
  268. to allocate the new vector, some other thread added elements to
  269. the set. */
  270. return 0;
  271. }
  272. /* The computed size differs from our newly allocated size by more
  273. than one size index -- recalculate. */
  274. return 0;
  275. }
  276. static void
  277. resize_set (scm_t_weak_set *set)
  278. {
  279. scm_t_weak_entry *old_entries, *new_entries;
  280. int new_size_index;
  281. unsigned long old_size, new_size, old_k;
  282. do
  283. {
  284. new_size_index = compute_size_index (set);
  285. if (new_size_index == set->size_index)
  286. return;
  287. new_size = hashset_size[new_size_index];
  288. new_entries = scm_gc_malloc_pointerless (new_size * sizeof(scm_t_weak_entry),
  289. "weak set");
  290. }
  291. while (!is_acceptable_size_index (set, new_size_index));
  292. old_entries = set->entries;
  293. old_size = set->size;
  294. memset (new_entries, 0, new_size * sizeof(scm_t_weak_entry));
  295. set->size_index = new_size_index;
  296. set->size = new_size;
  297. if (new_size_index <= set->min_size_index)
  298. set->lower = 0;
  299. else
  300. set->lower = new_size / 5;
  301. set->upper = 9 * new_size / 10;
  302. set->n_items = 0;
  303. set->entries = new_entries;
  304. for (old_k = 0; old_k < old_size; old_k++)
  305. {
  306. scm_t_weak_entry copy;
  307. unsigned long new_k, distance;
  308. if (!old_entries[old_k].hash)
  309. continue;
  310. copy_weak_entry (&old_entries[old_k], &copy);
  311. if (!copy.key)
  312. continue;
  313. new_k = hash_to_index (copy.hash, new_size);
  314. for (distance = 0; ; distance++, new_k = (new_k + 1) % new_size)
  315. {
  316. unsigned long other_hash = new_entries[new_k].hash;
  317. if (!other_hash)
  318. /* Found an empty entry. */
  319. break;
  320. /* Displace the entry if our distance is less, otherwise keep
  321. looking. */
  322. if (entry_distance (other_hash, new_k, new_size) < distance)
  323. {
  324. rob_from_rich (set, new_k);
  325. break;
  326. }
  327. }
  328. set->n_items++;
  329. new_entries[new_k].hash = copy.hash;
  330. new_entries[new_k].key = copy.key;
  331. if (SCM_HEAP_OBJECT_P (SCM_PACK (copy.key)))
  332. SCM_I_REGISTER_DISAPPEARING_LINK ((void **) &new_entries[new_k].key,
  333. (void *) new_entries[new_k].key);
  334. }
  335. }
  336. /* Run from a finalizer via do_vacuum_weak_set, this function runs over
  337. the whole table, removing lost weak references, reshuffling the set
  338. as it goes. It might resize the set if it reaps enough entries. */
  339. static void
  340. vacuum_weak_set (scm_t_weak_set *set)
  341. {
  342. scm_t_weak_entry *entries = set->entries;
  343. unsigned long size = set->size;
  344. unsigned long k;
  345. for (k = 0; k < size; k++)
  346. {
  347. unsigned long hash = entries[k].hash;
  348. if (hash)
  349. {
  350. scm_t_weak_entry copy;
  351. copy_weak_entry (&entries[k], &copy);
  352. if (!copy.key)
  353. /* Lost weak reference; reshuffle. */
  354. {
  355. give_to_poor (set, k);
  356. set->n_items--;
  357. }
  358. }
  359. }
  360. if (set->n_items < set->lower)
  361. resize_set (set);
  362. }
  363. static SCM
  364. weak_set_lookup (scm_t_weak_set *set, unsigned long hash,
  365. scm_t_set_predicate_fn pred, void *closure,
  366. SCM dflt)
  367. {
  368. unsigned long k, distance, size;
  369. scm_t_weak_entry *entries;
  370. size = set->size;
  371. entries = set->entries;
  372. hash = (hash << 1) | 0x1;
  373. k = hash_to_index (hash, size);
  374. for (distance = 0; distance < size; distance++, k = (k + 1) % size)
  375. {
  376. unsigned long other_hash;
  377. retry:
  378. other_hash = entries[k].hash;
  379. if (!other_hash)
  380. /* Not found. */
  381. return dflt;
  382. if (hash == other_hash)
  383. {
  384. scm_t_weak_entry copy;
  385. copy_weak_entry (&entries[k], &copy);
  386. if (!copy.key)
  387. /* Lost weak reference; reshuffle. */
  388. {
  389. give_to_poor (set, k);
  390. set->n_items--;
  391. goto retry;
  392. }
  393. if (pred (SCM_PACK (copy.key), closure))
  394. /* Found. */
  395. return SCM_PACK (copy.key);
  396. }
  397. /* If the entry's distance is less, our key is not in the set. */
  398. if (entry_distance (other_hash, k, size) < distance)
  399. return dflt;
  400. }
  401. /* If we got here, then we were unfortunate enough to loop through the
  402. whole set. Shouldn't happen, but hey. */
  403. return dflt;
  404. }
  405. static SCM
  406. weak_set_add_x (scm_t_weak_set *set, unsigned long hash,
  407. scm_t_set_predicate_fn pred, void *closure,
  408. SCM obj)
  409. {
  410. unsigned long k, distance, size;
  411. scm_t_weak_entry *entries;
  412. size = set->size;
  413. entries = set->entries;
  414. hash = (hash << 1) | 0x1;
  415. k = hash_to_index (hash, size);
  416. for (distance = 0; ; distance++, k = (k + 1) % size)
  417. {
  418. unsigned long other_hash;
  419. retry:
  420. other_hash = entries[k].hash;
  421. if (!other_hash)
  422. /* Found an empty entry. */
  423. break;
  424. if (other_hash == hash)
  425. {
  426. scm_t_weak_entry copy;
  427. copy_weak_entry (&entries[k], &copy);
  428. if (!copy.key)
  429. /* Lost weak reference; reshuffle. */
  430. {
  431. give_to_poor (set, k);
  432. set->n_items--;
  433. goto retry;
  434. }
  435. if (pred (SCM_PACK (copy.key), closure))
  436. /* Found an entry with this key. */
  437. return SCM_PACK (copy.key);
  438. }
  439. if (set->n_items > set->upper)
  440. /* Full set, time to resize. */
  441. {
  442. resize_set (set);
  443. return weak_set_add_x (set, hash >> 1, pred, closure, obj);
  444. }
  445. /* Displace the entry if our distance is less, otherwise keep
  446. looking. */
  447. if (entry_distance (other_hash, k, size) < distance)
  448. {
  449. rob_from_rich (set, k);
  450. break;
  451. }
  452. }
  453. set->n_items++;
  454. entries[k].hash = hash;
  455. entries[k].key = SCM_UNPACK (obj);
  456. if (SCM_HEAP_OBJECT_P (obj))
  457. SCM_I_REGISTER_DISAPPEARING_LINK ((void **) &entries[k].key,
  458. (void *) SCM2PTR (obj));
  459. return obj;
  460. }
  461. static void
  462. weak_set_remove_x (scm_t_weak_set *set, unsigned long hash,
  463. scm_t_set_predicate_fn pred, void *closure)
  464. {
  465. unsigned long k, distance, size;
  466. scm_t_weak_entry *entries;
  467. size = set->size;
  468. entries = set->entries;
  469. hash = (hash << 1) | 0x1;
  470. k = hash_to_index (hash, size);
  471. for (distance = 0; distance < size; distance++, k = (k + 1) % size)
  472. {
  473. unsigned long other_hash;
  474. retry:
  475. other_hash = entries[k].hash;
  476. if (!other_hash)
  477. /* Not found. */
  478. return;
  479. if (other_hash == hash)
  480. {
  481. scm_t_weak_entry copy;
  482. copy_weak_entry (&entries[k], &copy);
  483. if (!copy.key)
  484. /* Lost weak reference; reshuffle. */
  485. {
  486. give_to_poor (set, k);
  487. set->n_items--;
  488. goto retry;
  489. }
  490. if (pred (SCM_PACK (copy.key), closure))
  491. /* Found an entry with this key. */
  492. {
  493. entries[k].hash = 0;
  494. entries[k].key = 0;
  495. if (SCM_HEAP_OBJECT_P (SCM_PACK (copy.key)))
  496. GC_unregister_disappearing_link ((void **) &entries[k].key);
  497. if (--set->n_items < set->lower)
  498. resize_set (set);
  499. else
  500. give_to_poor (set, k);
  501. return;
  502. }
  503. }
  504. /* If the entry's distance is less, our key is not in the set. */
  505. if (entry_distance (other_hash, k, size) < distance)
  506. return;
  507. }
  508. }
  509. static SCM
  510. make_weak_set (unsigned long k)
  511. {
  512. scm_t_weak_set *set;
  513. int i = 0, n = k ? k : 31;
  514. while (i + 1 < HASHSET_SIZE_N && n > hashset_size[i])
  515. ++i;
  516. n = hashset_size[i];
  517. set = scm_gc_malloc (sizeof (*set), "weak-set");
  518. set->entries = scm_gc_malloc_pointerless (n * sizeof(scm_t_weak_entry),
  519. "weak-set");
  520. memset (set->entries, 0, n * sizeof(scm_t_weak_entry));
  521. set->n_items = 0;
  522. set->size = n;
  523. set->lower = 0;
  524. set->upper = 9 * n / 10;
  525. set->size_index = i;
  526. set->min_size_index = i;
  527. scm_i_pthread_mutex_init (&set->lock, NULL);
  528. return scm_cell (scm_tc7_weak_set, (scm_t_bits)set);
  529. }
  530. void
  531. scm_i_weak_set_print (SCM exp, SCM port, scm_print_state *pstate)
  532. {
  533. scm_puts_unlocked ("#<", port);
  534. scm_puts_unlocked ("weak-set ", port);
  535. scm_uintprint (SCM_WEAK_SET (exp)->n_items, 10, port);
  536. scm_putc_unlocked ('/', port);
  537. scm_uintprint (SCM_WEAK_SET (exp)->size, 10, port);
  538. scm_puts_unlocked (">", port);
  539. }
  540. static void
  541. do_vacuum_weak_set (SCM set)
  542. {
  543. scm_t_weak_set *s;
  544. s = SCM_WEAK_SET (set);
  545. /* We should always be able to grab this lock, because we are run from
  546. a finalizer, which runs in another thread (or an async, which is
  547. mostly equivalent). */
  548. scm_i_pthread_mutex_lock (&s->lock);
  549. vacuum_weak_set (s);
  550. scm_i_pthread_mutex_unlock (&s->lock);
  551. }
  552. SCM
  553. scm_c_make_weak_set (unsigned long k)
  554. {
  555. SCM ret;
  556. ret = make_weak_set (k);
  557. scm_i_register_weak_gc_callback (ret, do_vacuum_weak_set);
  558. return ret;
  559. }
  560. SCM
  561. scm_weak_set_p (SCM obj)
  562. {
  563. return scm_from_bool (SCM_WEAK_SET_P (obj));
  564. }
  565. SCM
  566. scm_weak_set_clear_x (SCM set)
  567. {
  568. scm_t_weak_set *s = SCM_WEAK_SET (set);
  569. scm_i_pthread_mutex_lock (&s->lock);
  570. memset (s->entries, 0, sizeof (scm_t_weak_entry) * s->size);
  571. s->n_items = 0;
  572. scm_i_pthread_mutex_unlock (&s->lock);
  573. return SCM_UNSPECIFIED;
  574. }
  575. SCM
  576. scm_c_weak_set_lookup (SCM set, unsigned long raw_hash,
  577. scm_t_set_predicate_fn pred,
  578. void *closure, SCM dflt)
  579. {
  580. SCM ret;
  581. scm_t_weak_set *s = SCM_WEAK_SET (set);
  582. scm_i_pthread_mutex_lock (&s->lock);
  583. ret = weak_set_lookup (s, raw_hash, pred, closure, dflt);
  584. scm_i_pthread_mutex_unlock (&s->lock);
  585. return ret;
  586. }
  587. SCM
  588. scm_c_weak_set_add_x (SCM set, unsigned long raw_hash,
  589. scm_t_set_predicate_fn pred,
  590. void *closure, SCM obj)
  591. {
  592. SCM ret;
  593. scm_t_weak_set *s = SCM_WEAK_SET (set);
  594. scm_i_pthread_mutex_lock (&s->lock);
  595. ret = weak_set_add_x (s, raw_hash, pred, closure, obj);
  596. scm_i_pthread_mutex_unlock (&s->lock);
  597. return ret;
  598. }
  599. void
  600. scm_c_weak_set_remove_x (SCM set, unsigned long raw_hash,
  601. scm_t_set_predicate_fn pred,
  602. void *closure)
  603. {
  604. scm_t_weak_set *s = SCM_WEAK_SET (set);
  605. scm_i_pthread_mutex_lock (&s->lock);
  606. weak_set_remove_x (s, raw_hash, pred, closure);
  607. scm_i_pthread_mutex_unlock (&s->lock);
  608. }
  609. static int
  610. eq_predicate (SCM x, void *closure)
  611. {
  612. return scm_is_eq (x, SCM_PACK_POINTER (closure));
  613. }
  614. SCM
  615. scm_weak_set_add_x (SCM set, SCM obj)
  616. {
  617. return scm_c_weak_set_add_x (set, scm_ihashq (obj, -1),
  618. eq_predicate, SCM_UNPACK_POINTER (obj), obj);
  619. }
  620. SCM
  621. scm_weak_set_remove_x (SCM set, SCM obj)
  622. {
  623. scm_c_weak_set_remove_x (set, scm_ihashq (obj, -1),
  624. eq_predicate, SCM_UNPACK_POINTER (obj));
  625. return SCM_UNSPECIFIED;
  626. }
  627. SCM
  628. scm_c_weak_set_fold (scm_t_set_fold_fn proc, void *closure,
  629. SCM init, SCM set)
  630. {
  631. scm_t_weak_set *s;
  632. scm_t_weak_entry *entries;
  633. unsigned long k, size;
  634. s = SCM_WEAK_SET (set);
  635. scm_i_pthread_mutex_lock (&s->lock);
  636. size = s->size;
  637. entries = s->entries;
  638. for (k = 0; k < size; k++)
  639. {
  640. if (entries[k].hash)
  641. {
  642. scm_t_weak_entry copy;
  643. copy_weak_entry (&entries[k], &copy);
  644. if (copy.key)
  645. {
  646. /* Release set lock while we call the function. */
  647. scm_i_pthread_mutex_unlock (&s->lock);
  648. init = proc (closure, SCM_PACK (copy.key), init);
  649. scm_i_pthread_mutex_lock (&s->lock);
  650. }
  651. }
  652. }
  653. scm_i_pthread_mutex_unlock (&s->lock);
  654. return init;
  655. }
  656. static SCM
  657. fold_trampoline (void *closure, SCM item, SCM init)
  658. {
  659. return scm_call_2 (SCM_PACK_POINTER (closure), item, init);
  660. }
  661. SCM
  662. scm_weak_set_fold (SCM proc, SCM init, SCM set)
  663. {
  664. return scm_c_weak_set_fold (fold_trampoline, SCM_UNPACK_POINTER (proc), init, set);
  665. }
  666. static SCM
  667. for_each_trampoline (void *closure, SCM item, SCM seed)
  668. {
  669. scm_call_1 (SCM_PACK_POINTER (closure), item);
  670. return seed;
  671. }
  672. SCM
  673. scm_weak_set_for_each (SCM proc, SCM set)
  674. {
  675. scm_c_weak_set_fold (for_each_trampoline, SCM_UNPACK_POINTER (proc), SCM_BOOL_F, set);
  676. return SCM_UNSPECIFIED;
  677. }
  678. static SCM
  679. map_trampoline (void *closure, SCM item, SCM seed)
  680. {
  681. return scm_cons (scm_call_1 (SCM_PACK_POINTER (closure), item), seed);
  682. }
  683. SCM
  684. scm_weak_set_map_to_list (SCM proc, SCM set)
  685. {
  686. return scm_c_weak_set_fold (map_trampoline, SCM_UNPACK_POINTER (proc), SCM_EOL, set);
  687. }
  688. void
  689. scm_init_weak_set ()
  690. {
  691. #include "libguile/weak-set.x"
  692. }
  693. /*
  694. Local Variables:
  695. c-file-style: "gnu"
  696. End:
  697. */