fluids.c 21 KB

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