fluids.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /* Copyright (C) 1996,1997,2000,2001, 2004, 2006, 2007, 2008 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 <stdio.h>
  22. #include <string.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/print.h"
  25. #include "libguile/smob.h"
  26. #include "libguile/dynwind.h"
  27. #include "libguile/fluids.h"
  28. #include "libguile/alist.h"
  29. #include "libguile/eval.h"
  30. #include "libguile/ports.h"
  31. #include "libguile/deprecation.h"
  32. #include "libguile/lang.h"
  33. #include "libguile/validate.h"
  34. #define FLUID_GROW 20
  35. /* A lot of the complexity below stems from the desire to reuse fluid
  36. slots. Normally, fluids should be pretty global and long-lived
  37. things, so that reusing their slots should not be overly critical,
  38. but it is the right thing to do nevertheless. The code therefore
  39. puts the burdon on allocating and collection fluids and keeps
  40. accessing fluids lock free. This is achieved by manipulating the
  41. global state of the fluid machinery mostly in single threaded
  42. sections.
  43. Reusing a fluid slot means that it must be reset to #f in all
  44. dynamic states. We do this by maintaining a weak list of all
  45. dynamic states, which is used after a GC to do the resetting.
  46. Also, the fluid vectors in the dynamic states need to grow from
  47. time to time when more fluids are created. We do this in a single
  48. threaded section so that threads do not need to lock when accessing
  49. a fluid in the normal way.
  50. */
  51. static scm_i_pthread_mutex_t fluid_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  52. /* Protected by fluid_admin_mutex, but also accessed during GC. See
  53. next_fluid_num for a discussion of this.
  54. */
  55. static size_t allocated_fluids_len = 0;
  56. static size_t allocated_fluids_num = 0;
  57. static char *allocated_fluids = NULL;
  58. static scm_t_bits tc16_fluid;
  59. #define IS_FLUID(x) SCM_SMOB_PREDICATE(tc16_fluid, (x))
  60. #define FLUID_NUM(x) ((size_t)SCM_SMOB_DATA(x))
  61. #define FLUID_NEXT(x) SCM_SMOB_OBJECT_2(x)
  62. #define FLUID_NEXT_LOC(x) SCM_SMOB_OBJECT_2_LOC(x)
  63. #define SET_FLUID_NEXT(x,y) SCM_SET_SMOB_OBJECT_2((x), (y))
  64. static scm_t_bits tc16_dynamic_state;
  65. #define IS_DYNAMIC_STATE(x) SCM_SMOB_PREDICATE(tc16_dynamic_state, (x))
  66. #define DYNAMIC_STATE_FLUIDS(x) SCM_SMOB_OBJECT(x)
  67. #define SET_DYNAMIC_STATE_FLUIDS(x, y) SCM_SET_SMOB_OBJECT((x), (y))
  68. #define DYNAMIC_STATE_NEXT(x) SCM_SMOB_OBJECT_2(x)
  69. #define DYNAMIC_STATE_NEXT_LOC(x) SCM_SMOB_OBJECT_2_LOC(x)
  70. #define SET_DYNAMIC_STATE_NEXT(x, y) SCM_SET_SMOB_OBJECT_2((x), (y))
  71. /* Weak lists of all dynamic states and all fluids.
  72. */
  73. static SCM all_dynamic_states = SCM_EOL;
  74. static SCM all_fluids = SCM_EOL;
  75. /* Make sure that all states have the right size. This must be called
  76. while fluid_admin_mutex is held.
  77. */
  78. static void
  79. resize_all_states ()
  80. {
  81. SCM new_vectors, state;
  82. /* Replacing the vector of a dynamic state must be done atomically:
  83. the old values must be copied into the new vector and the new
  84. vector must be installed without someone modifying the old vector
  85. concurrently. Since accessing a fluid should be lock-free, we
  86. need to put all threads to sleep when replacing a vector.
  87. However, when being single threaded, it is best not to do much.
  88. Therefore, we allocate the new vectors before going single
  89. threaded.
  90. */
  91. new_vectors = SCM_EOL;
  92. for (state = all_dynamic_states; !scm_is_null (state);
  93. state = DYNAMIC_STATE_NEXT (state))
  94. new_vectors = scm_cons (scm_c_make_vector (allocated_fluids_len,
  95. SCM_BOOL_F),
  96. new_vectors);
  97. scm_i_thread_put_to_sleep ();
  98. for (state = all_dynamic_states; !scm_is_null (state);
  99. state = DYNAMIC_STATE_NEXT (state))
  100. {
  101. SCM old_fluids = DYNAMIC_STATE_FLUIDS (state);
  102. SCM new_fluids = SCM_CAR (new_vectors);
  103. size_t i, old_len = SCM_SIMPLE_VECTOR_LENGTH (old_fluids);
  104. for (i = 0; i < old_len; i++)
  105. SCM_SIMPLE_VECTOR_SET (new_fluids, i,
  106. SCM_SIMPLE_VECTOR_REF (old_fluids, i));
  107. SET_DYNAMIC_STATE_FLUIDS (state, new_fluids);
  108. new_vectors = SCM_CDR (new_vectors);
  109. }
  110. scm_i_thread_wake_up ();
  111. }
  112. /* This is called during GC, that is, while being single threaded.
  113. See next_fluid_num for a discussion why it is safe to access
  114. allocated_fluids here.
  115. */
  116. static void *
  117. scan_dynamic_states_and_fluids (void *dummy1 SCM_UNUSED,
  118. void *dummy2 SCM_UNUSED,
  119. void *dummy3 SCM_UNUSED)
  120. {
  121. SCM *statep, *fluidp;
  122. /* Scan all fluids and deallocate the unmarked ones.
  123. */
  124. fluidp = &all_fluids;
  125. while (!scm_is_null (*fluidp))
  126. {
  127. if (!SCM_GC_MARK_P (*fluidp))
  128. {
  129. allocated_fluids_num -= 1;
  130. allocated_fluids[FLUID_NUM (*fluidp)] = 0;
  131. *fluidp = FLUID_NEXT (*fluidp);
  132. }
  133. else
  134. fluidp = FLUID_NEXT_LOC (*fluidp);
  135. }
  136. /* Scan all dynamic states and remove the unmarked ones. The live
  137. ones are updated for unallocated fluids.
  138. */
  139. statep = &all_dynamic_states;
  140. while (!scm_is_null (*statep))
  141. {
  142. if (!SCM_GC_MARK_P (*statep))
  143. *statep = DYNAMIC_STATE_NEXT (*statep);
  144. else
  145. {
  146. SCM fluids = DYNAMIC_STATE_FLUIDS (*statep);
  147. size_t len, i;
  148. len = SCM_SIMPLE_VECTOR_LENGTH (fluids);
  149. for (i = 0; i < len && i < allocated_fluids_len; i++)
  150. if (allocated_fluids[i] == 0)
  151. SCM_SIMPLE_VECTOR_SET (fluids, i, SCM_BOOL_F);
  152. statep = DYNAMIC_STATE_NEXT_LOC (*statep);
  153. }
  154. }
  155. return NULL;
  156. }
  157. static size_t
  158. fluid_free (SCM fluid)
  159. {
  160. /* The real work is done in scan_dynamic_states_and_fluids. We can
  161. not touch allocated_fluids etc here since a smob free routine can
  162. be run at any time, in any thread.
  163. */
  164. return 0;
  165. }
  166. static int
  167. fluid_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  168. {
  169. scm_puts ("#<fluid ", port);
  170. scm_intprint ((int) FLUID_NUM (exp), 10, port);
  171. scm_putc ('>', port);
  172. return 1;
  173. }
  174. static size_t
  175. next_fluid_num ()
  176. {
  177. size_t n;
  178. scm_dynwind_begin (0);
  179. scm_i_dynwind_pthread_mutex_lock (&fluid_admin_mutex);
  180. if ((allocated_fluids_len > 0) &&
  181. (allocated_fluids_num == allocated_fluids_len))
  182. {
  183. /* All fluid numbers are in use. Run a GC to try to free some
  184. up.
  185. */
  186. scm_gc ();
  187. }
  188. if (allocated_fluids_num < allocated_fluids_len)
  189. {
  190. for (n = 0; n < allocated_fluids_len; n++)
  191. if (allocated_fluids[n] == 0)
  192. break;
  193. }
  194. else
  195. {
  196. /* During the following call, the GC might run and elements of
  197. allocated_fluids might bet set to zero. Also,
  198. allocated_fluids and allocated_fluids_len are used to scan
  199. all dynamic states during GC. Thus we need to make sure that
  200. no GC can run while updating these two variables.
  201. */
  202. char *prev_allocated_fluids;
  203. char *new_allocated_fluids =
  204. scm_malloc (allocated_fluids_len + FLUID_GROW);
  205. /* Copy over old values and initialize rest. GC can not run
  206. during these two operations since there is no safe point in
  207. them.
  208. */
  209. memcpy (new_allocated_fluids, allocated_fluids, allocated_fluids_len);
  210. memset (new_allocated_fluids + allocated_fluids_len, 0, FLUID_GROW);
  211. n = allocated_fluids_len;
  212. prev_allocated_fluids = allocated_fluids;
  213. allocated_fluids = new_allocated_fluids;
  214. allocated_fluids_len += FLUID_GROW;
  215. if (prev_allocated_fluids != NULL)
  216. free (prev_allocated_fluids);
  217. /* Now allocated_fluids and allocated_fluids_len are valid again
  218. and we can allow GCs to occur.
  219. */
  220. resize_all_states ();
  221. }
  222. allocated_fluids_num += 1;
  223. allocated_fluids[n] = 1;
  224. scm_dynwind_end ();
  225. return n;
  226. }
  227. SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0,
  228. (),
  229. "Return a newly created fluid.\n"
  230. "Fluids are objects that can hold one\n"
  231. "value per dynamic state. That is, modifications to this value are\n"
  232. "only visible to code that executes with the same dynamic state as\n"
  233. "the modifying code. When a new dynamic state is constructed, it\n"
  234. "inherits the values from its parent. Because each thread normally executes\n"
  235. "with its own dynamic state, you can use fluids for thread local storage.")
  236. #define FUNC_NAME s_scm_make_fluid
  237. {
  238. SCM fluid;
  239. SCM_NEWSMOB2 (fluid, tc16_fluid,
  240. (scm_t_bits) next_fluid_num (), SCM_UNPACK (SCM_EOL));
  241. /* The GC must not run until the fluid is properly entered into the
  242. list.
  243. */
  244. scm_i_scm_pthread_mutex_lock (&fluid_admin_mutex);
  245. SET_FLUID_NEXT (fluid, all_fluids);
  246. all_fluids = fluid;
  247. scm_i_pthread_mutex_unlock (&fluid_admin_mutex);
  248. return fluid;
  249. }
  250. #undef FUNC_NAME
  251. SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0,
  252. (SCM obj),
  253. "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n"
  254. "@code{#f}.")
  255. #define FUNC_NAME s_scm_fluid_p
  256. {
  257. return scm_from_bool (IS_FLUID (obj));
  258. }
  259. #undef FUNC_NAME
  260. int
  261. scm_is_fluid (SCM obj)
  262. {
  263. return IS_FLUID (obj);
  264. }
  265. size_t
  266. scm_i_fluid_num (SCM fluid)
  267. {
  268. return FLUID_NUM (fluid);
  269. }
  270. SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0,
  271. (SCM fluid),
  272. "Return the value associated with @var{fluid} in the current\n"
  273. "dynamic root. If @var{fluid} has not been set, then return\n"
  274. "@code{#f}.")
  275. #define FUNC_NAME s_scm_fluid_ref
  276. {
  277. SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
  278. SCM_VALIDATE_FLUID (1, fluid);
  279. return SCM_SIMPLE_VECTOR_REF (fluids, FLUID_NUM (fluid));
  280. }
  281. #undef FUNC_NAME
  282. SCM
  283. scm_i_fast_fluid_ref (size_t n)
  284. {
  285. SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
  286. return SCM_SIMPLE_VECTOR_REF (fluids, n);
  287. }
  288. SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0,
  289. (SCM fluid, SCM value),
  290. "Set the value associated with @var{fluid} in the current dynamic root.")
  291. #define FUNC_NAME s_scm_fluid_set_x
  292. {
  293. SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
  294. SCM_VALIDATE_FLUID (1, fluid);
  295. SCM_SIMPLE_VECTOR_SET (fluids, FLUID_NUM (fluid), value);
  296. return SCM_UNSPECIFIED;
  297. }
  298. #undef FUNC_NAME
  299. void
  300. scm_i_fast_fluid_set_x (size_t n, SCM value)
  301. {
  302. SCM fluids = DYNAMIC_STATE_FLUIDS (SCM_I_CURRENT_THREAD->dynamic_state);
  303. SCM_SIMPLE_VECTOR_SET (fluids, n, value);
  304. }
  305. static void
  306. swap_fluids (SCM data)
  307. {
  308. SCM fluids = SCM_CAR (data), vals = SCM_CDR (data);
  309. while (!SCM_NULL_OR_NIL_P (fluids))
  310. {
  311. SCM fl = SCM_CAR (fluids);
  312. SCM old_val = scm_fluid_ref (fl);
  313. scm_fluid_set_x (fl, SCM_CAR (vals));
  314. SCM_SETCAR (vals, old_val);
  315. fluids = SCM_CDR (fluids);
  316. vals = SCM_CDR (vals);
  317. }
  318. }
  319. /* Swap the fluid values in reverse order. This is important when the
  320. same fluid appears multiple times in the fluids list.
  321. */
  322. static void
  323. swap_fluids_reverse_aux (SCM fluids, SCM vals)
  324. {
  325. if (!SCM_NULL_OR_NIL_P (fluids))
  326. {
  327. SCM fl, old_val;
  328. swap_fluids_reverse_aux (SCM_CDR (fluids), SCM_CDR (vals));
  329. fl = SCM_CAR (fluids);
  330. old_val = scm_fluid_ref (fl);
  331. scm_fluid_set_x (fl, SCM_CAR (vals));
  332. SCM_SETCAR (vals, old_val);
  333. }
  334. }
  335. static void
  336. swap_fluids_reverse (SCM data)
  337. {
  338. swap_fluids_reverse_aux (SCM_CAR (data), SCM_CDR (data));
  339. }
  340. static SCM
  341. apply_thunk (void *thunk)
  342. {
  343. return scm_call_0 (SCM_PACK (thunk));
  344. }
  345. SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0,
  346. (SCM fluids, SCM values, SCM thunk),
  347. "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n"
  348. "@var{fluids} must be a list of fluids and @var{values} must be the same\n"
  349. "number of their values to be applied. Each substitution is done\n"
  350. "one after another. @var{thunk} must be a procedure with no argument.")
  351. #define FUNC_NAME s_scm_with_fluids
  352. {
  353. return scm_c_with_fluids (fluids, values,
  354. apply_thunk, (void *) SCM_UNPACK (thunk));
  355. }
  356. #undef FUNC_NAME
  357. SCM
  358. scm_c_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata)
  359. #define FUNC_NAME "scm_c_with_fluids"
  360. {
  361. SCM ans, data;
  362. long flen, vlen;
  363. SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen);
  364. SCM_VALIDATE_LIST_COPYLEN (2, values, vlen);
  365. if (flen != vlen)
  366. scm_out_of_range (s_scm_with_fluids, values);
  367. if (flen == 1)
  368. return scm_c_with_fluid (SCM_CAR (fluids), SCM_CAR (values),
  369. cproc, cdata);
  370. data = scm_cons (fluids, values);
  371. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  372. scm_dynwind_rewind_handler_with_scm (swap_fluids, data,
  373. SCM_F_WIND_EXPLICITLY);
  374. scm_dynwind_unwind_handler_with_scm (swap_fluids_reverse, data,
  375. SCM_F_WIND_EXPLICITLY);
  376. ans = cproc (cdata);
  377. scm_dynwind_end ();
  378. return ans;
  379. }
  380. #undef FUNC_NAME
  381. SCM_DEFINE (scm_with_fluid, "with-fluid*", 3, 0, 0,
  382. (SCM fluid, SCM value, SCM thunk),
  383. "Set @var{fluid} to @var{value} temporarily, and call @var{thunk}.\n"
  384. "@var{thunk} must be a procedure with no argument.")
  385. #define FUNC_NAME s_scm_with_fluid
  386. {
  387. return scm_c_with_fluid (fluid, value,
  388. apply_thunk, (void *) SCM_UNPACK (thunk));
  389. }
  390. #undef FUNC_NAME
  391. SCM
  392. scm_c_with_fluid (SCM fluid, SCM value, SCM (*cproc) (), void *cdata)
  393. #define FUNC_NAME "scm_c_with_fluid"
  394. {
  395. SCM ans;
  396. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  397. scm_dynwind_fluid (fluid, value);
  398. ans = cproc (cdata);
  399. scm_dynwind_end ();
  400. return ans;
  401. }
  402. #undef FUNC_NAME
  403. static void
  404. swap_fluid (SCM data)
  405. {
  406. SCM f = SCM_CAR (data);
  407. SCM t = scm_fluid_ref (f);
  408. scm_fluid_set_x (f, SCM_CDR (data));
  409. SCM_SETCDR (data, t);
  410. }
  411. void
  412. scm_dynwind_fluid (SCM fluid, SCM value)
  413. {
  414. SCM data = scm_cons (fluid, value);
  415. scm_dynwind_rewind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY);
  416. scm_dynwind_unwind_handler_with_scm (swap_fluid, data, SCM_F_WIND_EXPLICITLY);
  417. }
  418. SCM
  419. scm_i_make_initial_dynamic_state ()
  420. {
  421. SCM fluids = scm_c_make_vector (allocated_fluids_len, SCM_BOOL_F);
  422. SCM state;
  423. SCM_NEWSMOB2 (state, tc16_dynamic_state,
  424. SCM_UNPACK (fluids), SCM_UNPACK (SCM_EOL));
  425. all_dynamic_states = state;
  426. return state;
  427. }
  428. SCM_DEFINE (scm_make_dynamic_state, "make-dynamic-state", 0, 1, 0,
  429. (SCM parent),
  430. "Return a copy of the dynamic state object @var{parent}\n"
  431. "or of the current dynamic state when @var{parent} is omitted.")
  432. #define FUNC_NAME s_scm_make_dynamic_state
  433. {
  434. SCM fluids, state;
  435. if (SCM_UNBNDP (parent))
  436. parent = scm_current_dynamic_state ();
  437. scm_assert_smob_type (tc16_dynamic_state, parent);
  438. fluids = scm_vector_copy (DYNAMIC_STATE_FLUIDS (parent));
  439. SCM_NEWSMOB2 (state, tc16_dynamic_state,
  440. SCM_UNPACK (fluids), SCM_UNPACK (SCM_EOL));
  441. /* The GC must not run until the state is properly entered into the
  442. list.
  443. */
  444. scm_i_scm_pthread_mutex_lock (&fluid_admin_mutex);
  445. SET_DYNAMIC_STATE_NEXT (state, all_dynamic_states);
  446. all_dynamic_states = state;
  447. scm_i_pthread_mutex_unlock (&fluid_admin_mutex);
  448. return state;
  449. }
  450. #undef FUNC_NAME
  451. SCM_DEFINE (scm_dynamic_state_p, "dynamic-state?", 1, 0, 0,
  452. (SCM obj),
  453. "Return @code{#t} if @var{obj} is a dynamic state object;\n"
  454. "return @code{#f} otherwise")
  455. #define FUNC_NAME s_scm_dynamic_state_p
  456. {
  457. return scm_from_bool (IS_DYNAMIC_STATE (obj));
  458. }
  459. #undef FUNC_NAME
  460. int
  461. scm_is_dynamic_state (SCM obj)
  462. {
  463. return IS_DYNAMIC_STATE (obj);
  464. }
  465. SCM_DEFINE (scm_current_dynamic_state, "current-dynamic-state", 0, 0, 0,
  466. (),
  467. "Return the current dynamic state object.")
  468. #define FUNC_NAME s_scm_current_dynamic_state
  469. {
  470. return SCM_I_CURRENT_THREAD->dynamic_state;
  471. }
  472. #undef FUNC_NAME
  473. SCM_DEFINE (scm_set_current_dynamic_state, "set-current-dynamic-state", 1,0,0,
  474. (SCM state),
  475. "Set the current dynamic state object to @var{state}\n"
  476. "and return the previous current dynamic state object.")
  477. #define FUNC_NAME s_scm_set_current_dynamic_state
  478. {
  479. scm_i_thread *t = SCM_I_CURRENT_THREAD;
  480. SCM old = t->dynamic_state;
  481. scm_assert_smob_type (tc16_dynamic_state, state);
  482. t->dynamic_state = state;
  483. return old;
  484. }
  485. #undef FUNC_NAME
  486. static void
  487. swap_dynamic_state (SCM loc)
  488. {
  489. SCM_SETCAR (loc, scm_set_current_dynamic_state (SCM_CAR (loc)));
  490. }
  491. void
  492. scm_dynwind_current_dynamic_state (SCM state)
  493. {
  494. SCM loc = scm_cons (state, SCM_EOL);
  495. scm_assert_smob_type (tc16_dynamic_state, state);
  496. scm_dynwind_rewind_handler_with_scm (swap_dynamic_state, loc,
  497. SCM_F_WIND_EXPLICITLY);
  498. scm_dynwind_unwind_handler_with_scm (swap_dynamic_state, loc,
  499. SCM_F_WIND_EXPLICITLY);
  500. }
  501. void *
  502. scm_c_with_dynamic_state (SCM state, void *(*func)(void *), void *data)
  503. {
  504. void *result;
  505. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  506. scm_dynwind_current_dynamic_state (state);
  507. result = func (data);
  508. scm_dynwind_end ();
  509. return result;
  510. }
  511. SCM_DEFINE (scm_with_dynamic_state, "with-dynamic-state", 2, 0, 0,
  512. (SCM state, SCM proc),
  513. "Call @var{proc} while @var{state} is the current dynamic\n"
  514. "state object.")
  515. #define FUNC_NAME s_scm_with_dynamic_state
  516. {
  517. SCM result;
  518. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  519. scm_dynwind_current_dynamic_state (state);
  520. result = scm_call_0 (proc);
  521. scm_dynwind_end ();
  522. return result;
  523. }
  524. #undef FUNC_NAME
  525. void
  526. scm_fluids_prehistory ()
  527. {
  528. tc16_fluid = scm_make_smob_type ("fluid", 0);
  529. scm_set_smob_free (tc16_fluid, fluid_free);
  530. scm_set_smob_print (tc16_fluid, fluid_print);
  531. tc16_dynamic_state = scm_make_smob_type ("dynamic-state", 0);
  532. scm_set_smob_mark (tc16_dynamic_state, scm_markcdr);
  533. scm_c_hook_add (&scm_after_sweep_c_hook, scan_dynamic_states_and_fluids,
  534. 0, 0);
  535. }
  536. void
  537. scm_init_fluids ()
  538. {
  539. #include "libguile/fluids.x"
  540. }
  541. /*
  542. Local Variables:
  543. c-file-style: "gnu"
  544. End:
  545. */