fluids.c 18 KB

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