fluids.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /* Copyright (C) 1996,1997,2000,2001, 2004, 2006, 2007, 2008, 2009, 2010,
  2. * 2011, 2012, 2013, 2017 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. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/atomics-internal.h"
  26. #include "libguile/cache-internal.h"
  27. #include "libguile/print.h"
  28. #include "libguile/dynwind.h"
  29. #include "libguile/fluids.h"
  30. #include "libguile/alist.h"
  31. #include "libguile/eval.h"
  32. #include "libguile/ports.h"
  33. #include "libguile/deprecation.h"
  34. #include "libguile/validate.h"
  35. #include "libguile/bdw-gc.h"
  36. /* A dynamic state associates fluids with values. There are two
  37. representations of a dynamic state in Guile: the active
  38. representation that is part of each thread, and a frozen
  39. representation that can live in Scheme land as a value.
  40. The active dynamic state has two parts: a locals cache, and a values
  41. table. The locals cache stores fluid values that have been recently
  42. referenced or set. If a value isn't in the locals cache, Guile then
  43. looks for it in the values table, which is a weak-key hash table.
  44. Otherwise Guile falls back to the default value of the fluid. In any
  45. case, the value is recorded in the locals cache. Likewise setting a
  46. fluid's value simply inserts that association into the locals cache.
  47. The locals cache is not large, so adding an entry to it might evict
  48. some other entry. In that case the entry gets flushed to the values
  49. table.
  50. The values table begins as being inherited from the parent dynamic
  51. state, and represents a capture of the fluid values at a point in
  52. time. A dynamic state records when its values table might be
  53. referenced by other dynamic states. If it is aliased, then any
  54. update to that table has to start by making a fresh local copy to
  55. work on.
  56. There are two interesting constraints on dynamic states, besides
  57. speed. One is that they should hold onto their fluid-value
  58. associations weakly: they shouldn't keep fluids alive indefinitely,
  59. and if a fluid goes away, its value should become collectible too.
  60. This is why the values table is a weak table; it makes access
  61. somewhat slower, but this is mitigated by the cache. The cache
  62. itself holds onto fluids and values strongly, but if there are more
  63. than 8 fluids in use by a dynamic state, this won't be a problem.
  64. The other interesting constraint is memory usage: you don't want a
  65. program with M fluids and N dynamic states to consume N*M memory.
  66. Guile associates each thread with a dynamic state, which itself isn't
  67. that bad as there are relatively few threads in a program. The
  68. problem comes in with "fibers", lightweight user-space threads that
  69. can be allocated in the millions. Here you want new fibers to
  70. inherit the dynamic state from the fiber that created them, but you
  71. really need to limit memory overheads. For reference, in late 2016,
  72. non-dynamic-state memory overhead per fiber in one user-space library
  73. is around 500 bytes, in a simple "all fibers try to send a message on
  74. one channel" test case.
  75. For this reason the frozen representation of dynamic states is the
  76. probably-shared values table at the end of a list of fluid-value
  77. pairs, representing entries from the locals cache that differ from
  78. the values table. This keeps per-dynamic-state memory usage in
  79. check. A family of fibers that uses the same 3 or 4 fluids probably
  80. won't ever have to allocate a new values table. Ideally the values
  81. table could share more state, as in an immutable weak array-mapped
  82. hash trie or something, but we don't have such a data structure. */
  83. #define FLUID_F_THREAD_LOCAL 0x100
  84. #define SCM_I_FLUID_THREAD_LOCAL_P(x) \
  85. (SCM_CELL_WORD_0 (x) & FLUID_F_THREAD_LOCAL)
  86. static inline int
  87. is_dynamic_state (SCM x)
  88. {
  89. return SCM_HAS_TYP7 (x, scm_tc7_dynamic_state);
  90. }
  91. static inline SCM
  92. get_dynamic_state (SCM dynamic_state)
  93. {
  94. return SCM_CELL_OBJECT_1 (dynamic_state);
  95. }
  96. /* Precondition: It's OK to throw away any unflushed data in the current
  97. cache. */
  98. static inline void
  99. restore_dynamic_state (SCM saved, scm_t_dynamic_state *state)
  100. {
  101. int slot;
  102. for (slot = SCM_CACHE_SIZE - 1; slot >= 0; slot--)
  103. {
  104. struct scm_cache_entry *entry = &state->cache.entries[slot];
  105. if (scm_is_pair (saved))
  106. {
  107. entry->key = SCM_UNPACK (SCM_CAAR (saved));
  108. entry->value = SCM_UNPACK (SCM_CDAR (saved));
  109. saved = scm_cdr (saved);
  110. }
  111. else
  112. entry->key = entry->value = 0;
  113. }
  114. state->values = saved;
  115. state->has_aliased_values = 1;
  116. }
  117. static inline SCM
  118. save_dynamic_state (scm_t_dynamic_state *state)
  119. {
  120. int slot;
  121. SCM saved = state->values;
  122. for (slot = 0; slot < SCM_CACHE_SIZE; slot++)
  123. {
  124. struct scm_cache_entry *entry = &state->cache.entries[slot];
  125. SCM key = SCM_PACK (entry->key);
  126. SCM value = SCM_PACK (entry->value);
  127. if (!entry->key)
  128. continue;
  129. if (SCM_I_FLUID_THREAD_LOCAL_P (key))
  130. {
  131. /* Because we don't include unflushed thread-local fluids in
  132. the result, we need to flush them to the table so that
  133. restore_dynamic_state can just throw away the current
  134. cache. */
  135. scm_hashq_set_x (state->thread_local_values, key, value);
  136. }
  137. else if (!scm_is_eq (scm_weak_table_refq (state->values, key,
  138. SCM_UNDEFINED),
  139. value))
  140. {
  141. if (state->has_aliased_values)
  142. saved = scm_acons (key, value, saved);
  143. else
  144. scm_weak_table_putq_x (state->values, key, value);
  145. }
  146. }
  147. state->has_aliased_values = 1;
  148. return saved;
  149. }
  150. static SCM
  151. saved_dynamic_state_ref (SCM saved, SCM fluid, SCM dflt)
  152. {
  153. for (; scm_is_pair (saved); saved = SCM_CDR (saved))
  154. if (scm_is_eq (SCM_CAAR (saved), fluid))
  155. return SCM_CDAR (saved);
  156. return scm_weak_table_refq (saved, fluid, dflt);
  157. }
  158. static SCM
  159. add_entry (void *data, SCM k, SCM v, SCM result)
  160. {
  161. scm_weak_table_putq_x (result, k, v);
  162. return result;
  163. }
  164. static SCM
  165. copy_value_table (SCM tab)
  166. {
  167. SCM ret = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  168. return scm_c_weak_table_fold (add_entry, NULL, ret, tab);
  169. }
  170. void
  171. scm_i_fluid_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  172. {
  173. if (SCM_I_FLUID_THREAD_LOCAL_P (exp))
  174. scm_puts ("#<thread-local-fluid ", port);
  175. else
  176. scm_puts ("#<fluid ", port);
  177. scm_intprint (SCM_UNPACK (exp), 16, port);
  178. scm_putc ('>', port);
  179. }
  180. void
  181. scm_i_dynamic_state_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  182. {
  183. scm_puts ("#<dynamic-state ", port);
  184. scm_intprint (SCM_UNPACK (exp), 16, port);
  185. scm_putc ('>', port);
  186. }
  187. #define SCM_I_FLUID_DEFAULT(x) (SCM_CELL_OBJECT_1 (x))
  188. static SCM
  189. new_fluid (SCM init, scm_t_bits flags)
  190. {
  191. return scm_cell (scm_tc7_fluid | flags, SCM_UNPACK (init));
  192. }
  193. SCM
  194. scm_make_fluid (void)
  195. {
  196. return new_fluid (SCM_BOOL_F, 0);
  197. }
  198. SCM_DEFINE (scm_make_fluid_with_default, "make-fluid", 0, 1, 0,
  199. (SCM dflt),
  200. "Return a newly created fluid, whose initial value is @var{dflt},\n"
  201. "or @code{#f} if @var{dflt} is not given.\n"
  202. "Fluids are objects that can hold one\n"
  203. "value per dynamic state. That is, modifications to this value are\n"
  204. "only visible to code that executes with the same dynamic state as\n"
  205. "the modifying code. When a new dynamic state is constructed, it\n"
  206. "inherits the values from its parent. Because each thread normally executes\n"
  207. "with its own dynamic state, you can use fluids for thread local storage.")
  208. #define FUNC_NAME s_scm_make_fluid_with_default
  209. {
  210. return new_fluid (SCM_UNBNDP (dflt) ? SCM_BOOL_F : dflt, 0);
  211. }
  212. #undef FUNC_NAME
  213. SCM_DEFINE (scm_make_unbound_fluid, "make-unbound-fluid", 0, 0, 0,
  214. (),
  215. "Make a fluid that is initially unbound.")
  216. #define FUNC_NAME s_scm_make_unbound_fluid
  217. {
  218. return new_fluid (SCM_UNDEFINED, 0);
  219. }
  220. #undef FUNC_NAME
  221. SCM_DEFINE (scm_make_thread_local_fluid, "make-thread-local-fluid", 0, 1, 0,
  222. (SCM dflt),
  223. "Return a newly created fluid, whose initial value is @var{dflt},\n"
  224. "or @code{#f} if @var{dflt} is not given. Unlike fluids made\n"
  225. "with @code{make-fluid}, thread local fluids are not captured\n"
  226. "by @code{make-dynamic-state}. Similarly, a newly spawned\n"
  227. "child thread does not inherit thread-local fluid values from\n"
  228. "the parent thread.")
  229. #define FUNC_NAME s_scm_make_thread_local_fluid
  230. {
  231. return new_fluid (SCM_UNBNDP (dflt) ? SCM_BOOL_F : dflt,
  232. FLUID_F_THREAD_LOCAL);
  233. }
  234. #undef FUNC_NAME
  235. SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0,
  236. (SCM obj),
  237. "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n"
  238. "@code{#f}.")
  239. #define FUNC_NAME s_scm_fluid_p
  240. {
  241. return scm_from_bool (SCM_FLUID_P (obj));
  242. }
  243. #undef FUNC_NAME
  244. SCM_DEFINE (scm_fluid_thread_local_p, "fluid-thread-local?", 1, 0, 0,
  245. (SCM fluid),
  246. "Return @code{#t} if the fluid @var{fluid} is is thread local,\n"
  247. "or @code{#f} otherwise.")
  248. #define FUNC_NAME s_scm_fluid_thread_local_p
  249. {
  250. SCM_VALIDATE_FLUID (1, fluid);
  251. return scm_from_bool (SCM_I_FLUID_THREAD_LOCAL_P (fluid));
  252. }
  253. #undef FUNC_NAME
  254. int
  255. scm_is_fluid (SCM obj)
  256. {
  257. return SCM_FLUID_P (obj);
  258. }
  259. static void
  260. fluid_set_x (scm_t_dynamic_state *dynamic_state, SCM fluid, SCM value)
  261. {
  262. struct scm_cache_entry *entry;
  263. struct scm_cache_entry evicted = { 0, 0 };
  264. entry = scm_cache_lookup (&dynamic_state->cache, fluid);
  265. if (scm_is_eq (SCM_PACK (entry->key), fluid))
  266. {
  267. entry->value = SCM_UNPACK (value);
  268. return;
  269. }
  270. scm_cache_insert (&dynamic_state->cache, fluid, value, &evicted);
  271. if (evicted.key != 0)
  272. {
  273. fluid = SCM_PACK (evicted.key);
  274. value = SCM_PACK (evicted.value);
  275. if (SCM_I_FLUID_THREAD_LOCAL_P (fluid))
  276. {
  277. scm_hashq_set_x (dynamic_state->thread_local_values, fluid, value);
  278. return;
  279. }
  280. if (dynamic_state->has_aliased_values)
  281. {
  282. if (scm_is_eq (scm_weak_table_refq (dynamic_state->values,
  283. fluid, SCM_UNDEFINED),
  284. value))
  285. return;
  286. dynamic_state->values = copy_value_table (dynamic_state->values);
  287. dynamic_state->has_aliased_values = 0;
  288. }
  289. scm_weak_table_putq_x (dynamic_state->values, fluid, value);
  290. }
  291. }
  292. /* Return value can be SCM_UNDEFINED; caller checks. */
  293. static SCM
  294. fluid_ref (scm_t_dynamic_state *dynamic_state, SCM fluid)
  295. {
  296. SCM val;
  297. struct scm_cache_entry *entry;
  298. entry = scm_cache_lookup (&dynamic_state->cache, fluid);
  299. if (scm_is_eq (SCM_PACK (entry->key), fluid))
  300. return SCM_PACK (entry->value);
  301. if (SCM_I_FLUID_THREAD_LOCAL_P (fluid))
  302. val = scm_hashq_ref (dynamic_state->thread_local_values, fluid,
  303. SCM_I_FLUID_DEFAULT (fluid));
  304. else
  305. val = scm_weak_table_refq (dynamic_state->values, fluid,
  306. SCM_I_FLUID_DEFAULT (fluid));
  307. /* Cache this lookup. */
  308. fluid_set_x (dynamic_state, fluid, val);
  309. return val;
  310. }
  311. SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0,
  312. (SCM fluid),
  313. "Return the value associated with @var{fluid} in the current\n"
  314. "dynamic root. If @var{fluid} has not been set, then return\n"
  315. "its default value.")
  316. #define FUNC_NAME s_scm_fluid_ref
  317. {
  318. SCM ret;
  319. SCM_VALIDATE_FLUID (1, fluid);
  320. ret = fluid_ref (SCM_I_CURRENT_THREAD->dynamic_state, fluid);
  321. if (SCM_UNBNDP (ret))
  322. scm_misc_error ("fluid-ref", "unbound fluid: ~S", scm_list_1 (fluid));
  323. return ret;
  324. }
  325. #undef FUNC_NAME
  326. SCM_DEFINE (scm_fluid_ref_star, "fluid-ref*", 2, 0, 0,
  327. (SCM fluid, SCM depth),
  328. "Return the @var{depth}th oldest value associated with\n"
  329. "@var{fluid} in the current thread. If @var{depth} equals\n"
  330. "or exceeds the number of values that have been assigned to\n"
  331. "@var{fluid}, return the default value of the fluid.")
  332. #define FUNC_NAME s_scm_fluid_ref_star
  333. {
  334. SCM ret;
  335. size_t c_depth;
  336. SCM_VALIDATE_FLUID (1, fluid);
  337. c_depth = SCM_NUM2SIZE (2, depth);
  338. /* Because this function is called to look up the current exception
  339. handler and this can happen in an out-of-memory situation, we avoid
  340. cache flushes to the weak table which might cause allocation of a
  341. disappearing link. */
  342. if (c_depth == 0)
  343. {
  344. scm_t_dynamic_state *dynamic_state = SCM_I_CURRENT_THREAD->dynamic_state;
  345. struct scm_cache_entry *entry;
  346. entry = scm_cache_lookup (&dynamic_state->cache, fluid);
  347. if (scm_is_eq (SCM_PACK (entry->key), fluid))
  348. ret = SCM_PACK (entry->value);
  349. else
  350. {
  351. if (SCM_I_FLUID_THREAD_LOCAL_P (fluid))
  352. ret = scm_hashq_ref (dynamic_state->thread_local_values, fluid,
  353. SCM_UNDEFINED);
  354. else
  355. ret = scm_weak_table_refq (dynamic_state->values, fluid,
  356. SCM_UNDEFINED);
  357. if (SCM_UNBNDP (ret))
  358. ret = SCM_I_FLUID_DEFAULT (fluid);
  359. /* Don't cache the lookup. */
  360. }
  361. }
  362. else
  363. ret = scm_dynstack_find_old_fluid_value (&SCM_I_CURRENT_THREAD->dynstack,
  364. fluid, c_depth - 1,
  365. SCM_I_FLUID_DEFAULT (fluid));
  366. if (SCM_UNBNDP (ret))
  367. scm_misc_error ("fluid-ref*", "unbound fluid: ~S", scm_list_1 (fluid));
  368. return ret;
  369. }
  370. #undef FUNC_NAME
  371. SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0,
  372. (SCM fluid, SCM value),
  373. "Set the value associated with @var{fluid} in the current dynamic root.")
  374. #define FUNC_NAME s_scm_fluid_set_x
  375. {
  376. SCM_VALIDATE_FLUID (1, fluid);
  377. fluid_set_x (SCM_I_CURRENT_THREAD->dynamic_state, fluid, value);
  378. return SCM_UNSPECIFIED;
  379. }
  380. #undef FUNC_NAME
  381. SCM_DEFINE (scm_fluid_unset_x, "fluid-unset!", 1, 0, 0,
  382. (SCM fluid),
  383. "Unset the value associated with @var{fluid}.")
  384. #define FUNC_NAME s_scm_fluid_unset_x
  385. {
  386. /* FIXME: really unset the default value, too? The current test
  387. suite demands it, but I would prefer not to. */
  388. SCM_VALIDATE_FLUID (1, fluid);
  389. SCM_SET_CELL_OBJECT_1 (fluid, SCM_UNDEFINED);
  390. fluid_set_x (SCM_I_CURRENT_THREAD->dynamic_state, fluid, SCM_UNDEFINED);
  391. return SCM_UNSPECIFIED;
  392. }
  393. #undef FUNC_NAME
  394. SCM_DEFINE (scm_fluid_bound_p, "fluid-bound?", 1, 0, 0,
  395. (SCM fluid),
  396. "Return @code{#t} iff @var{fluid} is bound to a value.\n"
  397. "Throw an error if @var{fluid} is not a fluid.")
  398. #define FUNC_NAME s_scm_fluid_bound_p
  399. {
  400. SCM val;
  401. SCM_VALIDATE_FLUID (1, fluid);
  402. val = fluid_ref (SCM_I_CURRENT_THREAD->dynamic_state, fluid);
  403. return scm_from_bool (! (SCM_UNBNDP (val)));
  404. }
  405. #undef FUNC_NAME
  406. static SCM
  407. apply_thunk (void *thunk)
  408. {
  409. return scm_call_0 (SCM_PACK (thunk));
  410. }
  411. void
  412. scm_swap_fluid (SCM fluid, SCM value_box, scm_t_dynamic_state *dynstate)
  413. {
  414. SCM val = fluid_ref (dynstate, fluid);
  415. fluid_set_x (dynstate, fluid, SCM_VARIABLE_REF (value_box));
  416. SCM_VARIABLE_SET (value_box, val);
  417. }
  418. SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0,
  419. (SCM fluids, SCM values, SCM thunk),
  420. "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n"
  421. "@var{fluids} must be a list of fluids and @var{values} must be the same\n"
  422. "number of their values to be applied. Each substitution is done\n"
  423. "one after another. @var{thunk} must be a procedure with no argument.")
  424. #define FUNC_NAME s_scm_with_fluids
  425. {
  426. return scm_c_with_fluids (fluids, values,
  427. apply_thunk, (void *) SCM_UNPACK (thunk));
  428. }
  429. #undef FUNC_NAME
  430. SCM
  431. scm_c_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata)
  432. #define FUNC_NAME "scm_c_with_fluids"
  433. {
  434. SCM ans;
  435. long flen, vlen, i;
  436. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  437. SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen);
  438. SCM_VALIDATE_LIST_COPYLEN (2, values, vlen);
  439. if (flen != vlen)
  440. scm_out_of_range (s_scm_with_fluids, values);
  441. for (i = 0; i < flen; i++)
  442. {
  443. scm_dynstack_push_fluid (&thread->dynstack,
  444. SCM_CAR (fluids), SCM_CAR (values),
  445. thread->dynamic_state);
  446. fluids = SCM_CDR (fluids);
  447. values = SCM_CDR (values);
  448. }
  449. ans = cproc (cdata);
  450. for (i = 0; i < flen; i++)
  451. scm_dynstack_unwind_fluid (&thread->dynstack, thread->dynamic_state);
  452. return ans;
  453. }
  454. #undef FUNC_NAME
  455. SCM
  456. scm_with_fluid (SCM fluid, SCM value, SCM thunk)
  457. {
  458. return scm_c_with_fluid (fluid, value,
  459. apply_thunk, (void *) SCM_UNPACK (thunk));
  460. }
  461. SCM
  462. scm_c_with_fluid (SCM fluid, SCM value, SCM (*cproc) (), void *cdata)
  463. #define FUNC_NAME "scm_c_with_fluid"
  464. {
  465. SCM ans;
  466. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  467. scm_dynstack_push_fluid (&thread->dynstack, fluid, value,
  468. thread->dynamic_state);
  469. ans = cproc (cdata);
  470. scm_dynstack_unwind_fluid (&thread->dynstack, thread->dynamic_state);
  471. return ans;
  472. }
  473. #undef FUNC_NAME
  474. static void
  475. swap_fluid (SCM data)
  476. {
  477. scm_t_dynamic_state *dynstate = SCM_I_CURRENT_THREAD->dynamic_state;
  478. SCM f = SCM_CAR (data);
  479. SCM t = fluid_ref (dynstate, f);
  480. fluid_set_x (dynstate, f, SCM_CDR (data));
  481. SCM_SETCDR (data, t);
  482. }
  483. void
  484. scm_dynwind_fluid (SCM fluid, SCM value)
  485. {
  486. SCM data = scm_cons (fluid, value);
  487. scm_dynwind_rewind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY);
  488. scm_dynwind_unwind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY);
  489. }
  490. SCM
  491. scm_i_make_initial_dynamic_state (void)
  492. {
  493. return scm_cell (scm_tc7_dynamic_state,
  494. SCM_UNPACK (scm_c_make_weak_table
  495. (0, SCM_WEAK_TABLE_KIND_KEY)));
  496. }
  497. SCM_DEFINE (scm_dynamic_state_p, "dynamic-state?", 1, 0, 0,
  498. (SCM obj),
  499. "Return @code{#t} if @var{obj} is a dynamic state object;\n"
  500. "return @code{#f} otherwise")
  501. #define FUNC_NAME s_scm_dynamic_state_p
  502. {
  503. return scm_from_bool (is_dynamic_state (obj));
  504. }
  505. #undef FUNC_NAME
  506. int
  507. scm_is_dynamic_state (SCM obj)
  508. {
  509. return is_dynamic_state (obj);
  510. }
  511. SCM_DEFINE (scm_current_dynamic_state, "current-dynamic-state", 0, 0, 0,
  512. (),
  513. "Return a snapshot of the current fluid-value associations\n"
  514. "as a fresh dynamic state object.")
  515. #define FUNC_NAME s_scm_current_dynamic_state
  516. {
  517. struct scm_dynamic_state *state = SCM_I_CURRENT_THREAD->dynamic_state;
  518. return scm_cell (scm_tc7_dynamic_state,
  519. SCM_UNPACK (save_dynamic_state (state)));
  520. }
  521. #undef FUNC_NAME
  522. SCM_DEFINE (scm_set_current_dynamic_state, "set-current-dynamic-state", 1,0,0,
  523. (SCM state),
  524. "Set the current dynamic state object to @var{state}\n"
  525. "and return the previous current dynamic state object.")
  526. #define FUNC_NAME s_scm_set_current_dynamic_state
  527. {
  528. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  529. SCM old = scm_current_dynamic_state ();
  530. SCM_ASSERT (is_dynamic_state (state), state, SCM_ARG1, FUNC_NAME);
  531. restore_dynamic_state (get_dynamic_state (state), t->dynamic_state);
  532. return old;
  533. }
  534. #undef FUNC_NAME
  535. SCM
  536. scm_dynamic_state_ref (SCM state, SCM fluid, SCM dflt)
  537. {
  538. SCM_ASSERT (is_dynamic_state (state), state, SCM_ARG1,
  539. "dynamic-state-ref");
  540. return saved_dynamic_state_ref (get_dynamic_state (state), fluid, dflt);
  541. }
  542. static void
  543. swap_dynamic_state (SCM loc)
  544. {
  545. SCM_SETCAR (loc, scm_set_current_dynamic_state (SCM_CAR (loc)));
  546. }
  547. void
  548. scm_dynwind_current_dynamic_state (SCM state)
  549. {
  550. SCM loc = scm_cons (state, SCM_EOL);
  551. SCM_ASSERT (is_dynamic_state (state), state, SCM_ARG1, NULL);
  552. scm_dynwind_rewind_handler_with_scm (swap_dynamic_state, loc,
  553. SCM_F_WIND_EXPLICITLY);
  554. scm_dynwind_unwind_handler_with_scm (swap_dynamic_state, loc,
  555. SCM_F_WIND_EXPLICITLY);
  556. }
  557. void *
  558. scm_c_with_dynamic_state (SCM state, void *(*func)(void *), void *data)
  559. {
  560. void *result;
  561. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  562. scm_dynwind_current_dynamic_state (state);
  563. result = func (data);
  564. scm_dynwind_end ();
  565. return result;
  566. }
  567. SCM_DEFINE (scm_with_dynamic_state, "with-dynamic-state", 2, 0, 0,
  568. (SCM state, SCM proc),
  569. "Call @var{proc} while @var{state} is the current dynamic\n"
  570. "state object.")
  571. #define FUNC_NAME s_scm_with_dynamic_state
  572. {
  573. SCM result;
  574. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  575. scm_dynwind_current_dynamic_state (state);
  576. result = scm_call_0 (proc);
  577. scm_dynwind_end ();
  578. return result;
  579. }
  580. #undef FUNC_NAME
  581. void
  582. scm_init_fluids ()
  583. {
  584. #include "libguile/fluids.x"
  585. }
  586. /*
  587. Local Variables:
  588. c-file-style: "gnu"
  589. End:
  590. */