libguile-concepts.texi 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996-1997, 2000-2005, 2010-2011, 2013-2016
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node General Libguile Concepts
  7. @section General concepts for using libguile
  8. When you want to embed the Guile Scheme interpreter into your program or
  9. library, you need to link it against the @file{libguile} library
  10. (@pxref{Linking Programs With Guile}). Once you have done this, your C
  11. code has access to a number of data types and functions that can be used
  12. to invoke the interpreter, or make new functions that you have written
  13. in C available to be called from Scheme code, among other things.
  14. Scheme is different from C in a number of significant ways, and Guile
  15. tries to make the advantages of Scheme available to C as well. Thus, in
  16. addition to a Scheme interpreter, libguile also offers dynamic types,
  17. garbage collection, continuations, arithmetic on arbitrary sized
  18. numbers, and other things.
  19. The two fundamental concepts are dynamic types and garbage collection.
  20. You need to understand how libguile offers them to C programs in order
  21. to use the rest of libguile. Also, the more general control flow of
  22. Scheme caused by continuations needs to be dealt with.
  23. Running asynchronous signal handlers and multi-threading is known to C
  24. code already, but there are of course a few additional rules when using
  25. them together with libguile.
  26. @menu
  27. * Dynamic Types:: Dynamic Types.
  28. * Garbage Collection:: Garbage Collection.
  29. * Control Flow:: Control Flow.
  30. * Asynchronous Signals:: Asynchronous Signals
  31. * Multi-Threading:: Multi-Threading
  32. @end menu
  33. @node Dynamic Types
  34. @subsection Dynamic Types
  35. Scheme is a dynamically-typed language; this means that the system
  36. cannot, in general, determine the type of a given expression at compile
  37. time. Types only become apparent at run time. Variables do not have
  38. fixed types; a variable may hold a pair at one point, an integer at the
  39. next, and a thousand-element vector later. Instead, values, not
  40. variables, have fixed types.
  41. In order to implement standard Scheme functions like @code{pair?} and
  42. @code{string?} and provide garbage collection, the representation of
  43. every value must contain enough information to accurately determine its
  44. type at run time. Often, Scheme systems also use this information to
  45. determine whether a program has attempted to apply an operation to an
  46. inappropriately typed value (such as taking the @code{car} of a string).
  47. Because variables, pairs, and vectors may hold values of any type,
  48. Scheme implementations use a uniform representation for values --- a
  49. single type large enough to hold either a complete value or a pointer
  50. to a complete value, along with the necessary typing information.
  51. In Guile, this uniform representation of all Scheme values is the C type
  52. @code{SCM}. This is an opaque type and its size is typically equivalent
  53. to that of a pointer to @code{void}. Thus, @code{SCM} values can be
  54. passed around efficiently and they take up reasonably little storage on
  55. their own.
  56. The most important rule is: You never access a @code{SCM} value
  57. directly; you only pass it to functions or macros defined in libguile.
  58. As an obvious example, although a @code{SCM} variable can contain
  59. integers, you can of course not compute the sum of two @code{SCM} values
  60. by adding them with the C @code{+} operator. You must use the libguile
  61. function @code{scm_sum}.
  62. Less obvious and therefore more important to keep in mind is that you
  63. also cannot directly test @code{SCM} values for trueness. In Scheme,
  64. the value @code{#f} is considered false and of course a @code{SCM}
  65. variable can represent that value. But there is no guarantee that the
  66. @code{SCM} representation of @code{#f} looks false to C code as well.
  67. You need to use @code{scm_is_true} or @code{scm_is_false} to test a
  68. @code{SCM} value for trueness or falseness, respectively.
  69. You also can not directly compare two @code{SCM} values to find out
  70. whether they are identical (that is, whether they are @code{eq?} in
  71. Scheme terms). You need to use @code{scm_is_eq} for this.
  72. The one exception is that you can directly assign a @code{SCM} value to
  73. a @code{SCM} variable by using the C @code{=} operator.
  74. The following (contrived) example shows how to do it right. It
  75. implements a function of two arguments (@var{a} and @var{flag}) that
  76. returns @var{a}+1 if @var{flag} is true, else it returns @var{a}
  77. unchanged.
  78. @example
  79. SCM
  80. my_incrementing_function (SCM a, SCM flag)
  81. @{
  82. SCM result;
  83. if (scm_is_true (flag))
  84. result = scm_sum (a, scm_from_int (1));
  85. else
  86. result = a;
  87. return result;
  88. @}
  89. @end example
  90. Often, you need to convert between @code{SCM} values and appropriate C
  91. values. For example, we needed to convert the integer @code{1} to its
  92. @code{SCM} representation in order to add it to @var{a}. Libguile
  93. provides many function to do these conversions, both from C to
  94. @code{SCM} and from @code{SCM} to C.
  95. The conversion functions follow a common naming pattern: those that make
  96. a @code{SCM} value from a C value have names of the form
  97. @code{scm_from_@var{type} (@dots{})} and those that convert a @code{SCM}
  98. value to a C value use the form @code{scm_to_@var{type} (@dots{})}.
  99. However, it is best to avoid converting values when you can. When you
  100. must combine C values and @code{SCM} values in a computation, it is
  101. often better to convert the C values to @code{SCM} values and do the
  102. computation by using libguile functions than to the other way around
  103. (converting @code{SCM} to C and doing the computation some other way).
  104. As a simple example, consider this version of
  105. @code{my_incrementing_function} from above:
  106. @example
  107. SCM
  108. my_other_incrementing_function (SCM a, SCM flag)
  109. @{
  110. int result;
  111. if (scm_is_true (flag))
  112. result = scm_to_int (a) + 1;
  113. else
  114. result = scm_to_int (a);
  115. return scm_from_int (result);
  116. @}
  117. @end example
  118. This version is much less general than the original one: it will only
  119. work for values @var{A} that can fit into a @code{int}. The original
  120. function will work for all values that Guile can represent and that
  121. @code{scm_sum} can understand, including integers bigger than @code{long
  122. long}, floating point numbers, complex numbers, and new numerical types
  123. that have been added to Guile by third-party libraries.
  124. Also, computing with @code{SCM} is not necessarily inefficient. Small
  125. integers will be encoded directly in the @code{SCM} value, for example,
  126. and do not need any additional memory on the heap. See @ref{Data
  127. Representation} to find out the details.
  128. Some special @code{SCM} values are available to C code without needing
  129. to convert them from C values:
  130. @multitable {Scheme value} {C representation}
  131. @item Scheme value @tab C representation
  132. @item @nicode{#f} @tab @nicode{SCM_BOOL_F}
  133. @item @nicode{#t} @tab @nicode{SCM_BOOL_T}
  134. @item @nicode{()} @tab @nicode{SCM_EOL}
  135. @end multitable
  136. In addition to @code{SCM}, Guile also defines the related type
  137. @code{scm_t_bits}. This is an unsigned integral type of sufficient
  138. size to hold all information that is directly contained in a
  139. @code{SCM} value. The @code{scm_t_bits} type is used internally by
  140. Guile to do all the bit twiddling explained in @ref{Data Representation}, but
  141. you will encounter it occasionally in low-level user code as well.
  142. @node Garbage Collection
  143. @subsection Garbage Collection
  144. As explained above, the @code{SCM} type can represent all Scheme values.
  145. Some values fit entirely into a @code{SCM} value (such as small
  146. integers), but other values require additional storage in the heap (such
  147. as strings and vectors). This additional storage is managed
  148. automatically by Guile. You don't need to explicitly deallocate it
  149. when a @code{SCM} value is no longer used.
  150. Two things must be guaranteed so that Guile is able to manage the
  151. storage automatically: it must know about all blocks of memory that have
  152. ever been allocated for Scheme values, and it must know about all Scheme
  153. values that are still being used. Given this knowledge, Guile can
  154. periodically free all blocks that have been allocated but are not used
  155. by any active Scheme values. This activity is called @dfn{garbage
  156. collection}.
  157. Guile's garbage collector will automatically discover references to
  158. @code{SCM} objects that originate in global variables, static data
  159. sections, function arguments or local variables on the C and Scheme
  160. stacks, and values in machine registers. Other references to @code{SCM}
  161. objects, such as those in other random data structures in the C heap
  162. that contain fields of type @code{SCM}, can be made visible to the
  163. garbage collector by calling the functions @code{scm_gc_protect_object} or
  164. @code{scm_permanent_object}. Collectively, these values form the ``root
  165. set'' of garbage collection; any value on the heap that is referenced
  166. directly or indirectly by a member of the root set is preserved, and all
  167. other objects are eligible for reclamation.
  168. In Guile, garbage collection has two logical phases: the @dfn{mark
  169. phase}, in which the collector discovers the set of all live objects,
  170. and the @dfn{sweep phase}, in which the collector reclaims the resources
  171. associated with dead objects. The mark phase pauses the program and
  172. traces all @code{SCM} object references, starting with the root set.
  173. The sweep phase actually runs concurrently with the main program,
  174. incrementally reclaiming memory as needed by allocation.
  175. In the mark phase, the garbage collector traces the Scheme stack and
  176. heap @dfn{precisely}. Because the Scheme stack and heap are managed by
  177. Guile, Guile can know precisely where in those data structures it might
  178. find references to other heap objects. This is not the case,
  179. unfortunately, for pointers on the C stack and static data segment.
  180. Instead of requiring the user to inform Guile about all variables in C
  181. that might point to heap objects, Guile traces the C stack and static
  182. data segment @dfn{conservatively}. That is to say, Guile just treats
  183. every word on the C stack and every C global variable as a potential
  184. reference in to the Scheme heap@footnote{Note that Guile does not scan
  185. the C heap for references, so a reference to a @code{SCM} object from a
  186. memory segment allocated with @code{malloc} will have to use some other
  187. means to keep the @code{SCM} object alive. @xref{Garbage Collection
  188. Functions}.}. Any value that looks like a pointer to a GC-managed
  189. object is treated as such, whether it actually is a reference or not.
  190. Thus, scanning the C stack and static data segment is guaranteed to find
  191. all actual references, but it might also find words that only
  192. accidentally look like references. These ``false positives'' might keep
  193. @code{SCM} objects alive that would otherwise be considered dead. While
  194. this might waste memory, keeping an object around longer than it
  195. strictly needs to is harmless. This is why this technique is called
  196. ``conservative garbage collection''. In practice, the wasted memory
  197. seems to be no problem, as the static C root set is almost always finite
  198. and small, given that the Scheme stack is separate from the C stack.
  199. The stack of every thread is scanned in this way and the registers of
  200. the CPU and all other memory locations where local variables or function
  201. parameters might show up are included in this scan as well.
  202. The consequence of the conservative scanning is that you can just
  203. declare local variables and function parameters of type @code{SCM} and
  204. be sure that the garbage collector will not free the corresponding
  205. objects.
  206. However, a local variable or function parameter is only protected as
  207. long as it is really on the stack (or in some register). As an
  208. optimization, the C compiler might reuse its location for some other
  209. value and the @code{SCM} object would no longer be protected. Normally,
  210. this leads to exactly the right behavior: the compiler will only
  211. overwrite a reference when it is no longer needed and thus the object
  212. becomes unprotected precisely when the reference disappears, just as
  213. wanted.
  214. There are situations, however, where a @code{SCM} object needs to be
  215. around longer than its reference from a local variable or function
  216. parameter. This happens, for example, when you retrieve some pointer
  217. from a foreign object and work with that pointer directly. The
  218. reference to the @code{SCM} foreign object might be dead after the
  219. pointer has been retrieved, but the pointer itself (and the memory
  220. pointed to) is still in use and thus the foreign object must be
  221. protected. The compiler does not know about this connection and might
  222. overwrite the @code{SCM} reference too early.
  223. To get around this problem, you can use @code{scm_remember_upto_here_1}
  224. and its cousins. It will keep the compiler from overwriting the
  225. reference. @xref{Foreign Object Memory Management}.
  226. @node Control Flow
  227. @subsection Control Flow
  228. Scheme has a more general view of program flow than C, both locally and
  229. non-locally.
  230. Controlling the local flow of control involves things like gotos, loops,
  231. calling functions and returning from them. Non-local control flow
  232. refers to situations where the program jumps across one or more levels
  233. of function activations without using the normal call or return
  234. operations.
  235. The primitive means of C for local control flow is the @code{goto}
  236. statement, together with @code{if}. Loops done with @code{for},
  237. @code{while} or @code{do} could in principle be rewritten with just
  238. @code{goto} and @code{if}. In Scheme, the primitive means for local
  239. control flow is the @emph{function call} (together with @code{if}).
  240. Thus, the repetition of some computation in a loop is ultimately
  241. implemented by a function that calls itself, that is, by recursion.
  242. This approach is theoretically very powerful since it is easier to
  243. reason formally about recursion than about gotos. In C, using
  244. recursion exclusively would not be practical, though, since it would eat
  245. up the stack very quickly. In Scheme, however, it is practical:
  246. function calls that appear in a @dfn{tail position} do not use any
  247. additional stack space (@pxref{Tail Calls}).
  248. A function call is in a tail position when it is the last thing the
  249. calling function does. The value returned by the called function is
  250. immediately returned from the calling function. In the following
  251. example, the call to @code{bar-1} is in a tail position, while the
  252. call to @code{bar-2} is not. (The call to @code{1-} in @code{foo-2}
  253. is in a tail position, though.)
  254. @lisp
  255. (define (foo-1 x)
  256. (bar-1 (1- x)))
  257. (define (foo-2 x)
  258. (1- (bar-2 x)))
  259. @end lisp
  260. Thus, when you take care to recurse only in tail positions, the
  261. recursion will only use constant stack space and will be as good as a
  262. loop constructed from gotos.
  263. Scheme offers a few syntactic abstractions (@code{do} and @dfn{named}
  264. @code{let}) that make writing loops slightly easier.
  265. But only Scheme functions can call other functions in a tail position:
  266. C functions can not. This matters when you have, say, two functions
  267. that call each other recursively to form a common loop. The following
  268. (unrealistic) example shows how one might go about determining whether a
  269. non-negative integer @var{n} is even or odd.
  270. @lisp
  271. (define (my-even? n)
  272. (cond ((zero? n) #t)
  273. (else (my-odd? (1- n)))))
  274. (define (my-odd? n)
  275. (cond ((zero? n) #f)
  276. (else (my-even? (1- n)))))
  277. @end lisp
  278. Because the calls to @code{my-even?} and @code{my-odd?} are in tail
  279. positions, these two procedures can be applied to arbitrary large
  280. integers without overflowing the stack. (They will still take a lot
  281. of time, of course.)
  282. However, when one or both of the two procedures would be rewritten in
  283. C, it could no longer call its companion in a tail position (since C
  284. does not have this concept). You might need to take this
  285. consideration into account when deciding which parts of your program
  286. to write in Scheme and which in C.
  287. In addition to calling functions and returning from them, a Scheme
  288. program can also exit non-locally from a function so that the control
  289. flow returns directly to an outer level. This means that some functions
  290. might not return at all.
  291. Even more, it is not only possible to jump to some outer level of
  292. control, a Scheme program can also jump back into the middle of a
  293. function that has already exited. This might cause some functions to
  294. return more than once.
  295. In general, these non-local jumps are done by invoking
  296. @dfn{continuations} that have previously been captured using
  297. @code{call-with-current-continuation}. Guile also offers a slightly
  298. restricted set of functions, @code{catch} and @code{throw}, that can
  299. only be used for non-local exits. This restriction makes them more
  300. efficient. Error reporting (with the function @code{error}) is
  301. implemented by invoking @code{throw}, for example. The functions
  302. @code{catch} and @code{throw} belong to the topic of @dfn{exceptions}.
  303. Since Scheme functions can call C functions and vice versa, C code can
  304. experience the more general control flow of Scheme as well. It is
  305. possible that a C function will not return at all, or will return more
  306. than once. While C does offer @code{setjmp} and @code{longjmp} for
  307. non-local exits, it is still an unusual thing for C code. In
  308. contrast, non-local exits are very common in Scheme, mostly to report
  309. errors.
  310. You need to be prepared for the non-local jumps in the control flow
  311. whenever you use a function from @code{libguile}: it is best to assume
  312. that any @code{libguile} function might signal an error or run a pending
  313. signal handler (which in turn can do arbitrary things).
  314. It is often necessary to take cleanup actions when the control leaves a
  315. function non-locally. Also, when the control returns non-locally, some
  316. setup actions might be called for. For example, the Scheme function
  317. @code{with-output-to-port} needs to modify the global state so that
  318. @code{current-output-port} returns the port passed to
  319. @code{with-output-to-port}. The global output port needs to be reset to
  320. its previous value when @code{with-output-to-port} returns normally or
  321. when it is exited non-locally. Likewise, the port needs to be set again
  322. when control enters non-locally.
  323. Scheme code can use the @code{dynamic-wind} function to arrange for
  324. the setting and resetting of the global state. C code can use the
  325. corresponding @code{scm_internal_dynamic_wind} function, or a
  326. @code{scm_dynwind_begin}/@code{scm_dynwind_end} pair together with
  327. suitable 'dynwind actions' (@pxref{Dynamic Wind}).
  328. Instead of coping with non-local control flow, you can also prevent it
  329. by erecting a @emph{continuation barrier}, @xref{Continuation
  330. Barriers}. The function @code{scm_c_with_continuation_barrier}, for
  331. example, is guaranteed to return exactly once.
  332. @node Asynchronous Signals
  333. @subsection Asynchronous Signals
  334. You can not call libguile functions from handlers for POSIX signals, but
  335. you can register Scheme handlers for POSIX signals such as
  336. @code{SIGINT}. These handlers do not run during the actual signal
  337. delivery. Instead, they are run when the program (more precisely, the
  338. thread that the handler has been registered for) reaches the next
  339. @emph{safe point}.
  340. The libguile functions themselves have many such safe points.
  341. Consequently, you must be prepared for arbitrary actions anytime you
  342. call a libguile function. For example, even @code{scm_cons} can contain
  343. a safe point and when a signal handler is pending for your thread,
  344. calling @code{scm_cons} will run this handler and anything might happen,
  345. including a non-local exit although @code{scm_cons} would not ordinarily
  346. do such a thing on its own.
  347. If you do not want to allow the running of asynchronous signal handlers,
  348. you can block them temporarily with @code{scm_dynwind_block_asyncs}, for
  349. example. @xref{Asyncs}.
  350. Since signal handling in Guile relies on safe points, you need to make
  351. sure that your functions do offer enough of them. Normally, calling
  352. libguile functions in the normal course of action is all that is needed.
  353. But when a thread might spent a long time in a code section that calls
  354. no libguile function, it is good to include explicit safe points. This
  355. can allow the user to interrupt your code with @key{C-c}, for example.
  356. You can do this with the macro @code{SCM_TICK}. This macro is
  357. syntactically a statement. That is, you could use it like this:
  358. @example
  359. while (1)
  360. @{
  361. SCM_TICK;
  362. do_some_work ();
  363. @}
  364. @end example
  365. Frequent execution of a safe point is even more important in multi
  366. threaded programs, @xref{Multi-Threading}.
  367. @node Multi-Threading
  368. @subsection Multi-Threading
  369. Guile can be used in multi-threaded programs just as well as in
  370. single-threaded ones.
  371. Each thread that wants to use functions from libguile must put itself
  372. into @emph{guile mode} and must then follow a few rules. If it doesn't
  373. want to honor these rules in certain situations, a thread can
  374. temporarily leave guile mode (but can no longer use libguile functions
  375. during that time, of course).
  376. Threads enter guile mode by calling @code{scm_with_guile},
  377. @code{scm_boot_guile}, or @code{scm_init_guile}. As explained in the
  378. reference documentation for these functions, Guile will then learn about
  379. the stack bounds of the thread and can protect the @code{SCM} values
  380. that are stored in local variables. When a thread puts itself into
  381. guile mode for the first time, it gets a Scheme representation and is
  382. listed by @code{all-threads}, for example.
  383. Threads in guile mode can block (e.g., do blocking I/O) without causing
  384. any problems@footnote{In Guile 1.8, a thread blocking in guile mode
  385. would prevent garbage collection to occur. Thus, threads had to leave
  386. guile mode whenever they could block. This is no longer needed with
  387. Guile 2.@var{x}.}; temporarily leaving guile mode with
  388. @code{scm_without_guile} before blocking slightly improves GC
  389. performance, though. For some common blocking operations, Guile
  390. provides convenience functions. For example, if you want to lock a
  391. pthread mutex while in guile mode, you might want to use
  392. @code{scm_pthread_mutex_lock} which is just like
  393. @code{pthread_mutex_lock} except that it leaves guile mode while
  394. blocking.
  395. All libguile functions are (intended to be) robust in the face of
  396. multiple threads using them concurrently. This means that there is no
  397. risk of the internal data structures of libguile becoming corrupted in
  398. such a way that the process crashes.
  399. A program might still produce nonsensical results, though. Taking
  400. hashtables as an example, Guile guarantees that you can use them from
  401. multiple threads concurrently and a hashtable will always remain a valid
  402. hashtable and Guile will not crash when you access it. It does not
  403. guarantee, however, that inserting into it concurrently from two threads
  404. will give useful results: only one insertion might actually happen, none
  405. might happen, or the table might in general be modified in a totally
  406. arbitrary manner. (It will still be a valid hashtable, but not the one
  407. that you might have expected.) Guile might also signal an error when it
  408. detects a harmful race condition.
  409. Thus, you need to put in additional synchronizations when multiple
  410. threads want to use a single hashtable, or any other mutable Scheme
  411. object.
  412. When writing C code for use with libguile, you should try to make it
  413. robust as well. An example that converts a list into a vector will help
  414. to illustrate. Here is a correct version:
  415. @example
  416. SCM
  417. my_list_to_vector (SCM list)
  418. @{
  419. SCM vector = scm_make_vector (scm_length (list), SCM_UNDEFINED);
  420. size_t len, i;
  421. len = scm_c_vector_length (vector);
  422. i = 0;
  423. while (i < len && scm_is_pair (list))
  424. @{
  425. scm_c_vector_set_x (vector, i, scm_car (list));
  426. list = scm_cdr (list);
  427. i++;
  428. @}
  429. return vector;
  430. @}
  431. @end example
  432. The first thing to note is that storing into a @code{SCM} location
  433. concurrently from multiple threads is guaranteed to be robust: you don't
  434. know which value wins but it will in any case be a valid @code{SCM}
  435. value.
  436. But there is no guarantee that the list referenced by @var{list} is not
  437. modified in another thread while the loop iterates over it. Thus, while
  438. copying its elements into the vector, the list might get longer or
  439. shorter. For this reason, the loop must check both that it doesn't
  440. overrun the vector and that it doesn't overrun the list. Otherwise,
  441. @code{scm_c_vector_set_x} would raise an error if the index is out of
  442. range, and @code{scm_car} and @code{scm_cdr} would raise an error if the
  443. value is not a pair.
  444. It is safe to use @code{scm_car} and @code{scm_cdr} on the local
  445. variable @var{list} once it is known that the variable contains a pair.
  446. The contents of the pair might change spontaneously, but it will always
  447. stay a valid pair (and a local variable will of course not spontaneously
  448. point to a different Scheme object).
  449. Likewise, a vector such as the one returned by @code{scm_make_vector} is
  450. guaranteed to always stay the same length so that it is safe to only use
  451. scm_c_vector_length once and store the result. (In the example,
  452. @var{vector} is safe anyway since it is a fresh object that no other
  453. thread can possibly know about until it is returned from
  454. @code{my_list_to_vector}.)
  455. Of course the behavior of @code{my_list_to_vector} is suboptimal when
  456. @var{list} does indeed get asynchronously lengthened or shortened in
  457. another thread. But it is robust: it will always return a valid vector.
  458. That vector might be shorter than expected, or its last elements might
  459. be unspecified, but it is a valid vector and if a program wants to rule
  460. out these cases, it must avoid modifying the list asynchronously.
  461. Here is another version that is also correct:
  462. @example
  463. SCM
  464. my_pedantic_list_to_vector (SCM list)
  465. @{
  466. SCM vector = scm_make_vector (scm_length (list), SCM_UNDEFINED);
  467. size_t len, i;
  468. len = scm_c_vector_length (vector);
  469. i = 0;
  470. while (i < len)
  471. @{
  472. scm_c_vector_set_x (vector, i, scm_car (list));
  473. list = scm_cdr (list);
  474. i++;
  475. @}
  476. return vector;
  477. @}
  478. @end example
  479. This version relies on the error-checking behavior of @code{scm_car} and
  480. @code{scm_cdr}. When the list is shortened (that is, when @var{list}
  481. holds a non-pair), @code{scm_car} will throw an error. This might be
  482. preferable to just returning a half-initialized vector.
  483. The API for accessing vectors and arrays of various kinds from C takes a
  484. slightly different approach to thread-robustness. In order to get at
  485. the raw memory that stores the elements of an array, you need to
  486. @emph{reserve} that array as long as you need the raw memory. During
  487. the time an array is reserved, its elements can still spontaneously
  488. change their values, but the memory itself and other things like the
  489. size of the array are guaranteed to stay fixed. Any operation that
  490. would change these parameters of an array that is currently reserved
  491. will signal an error. In order to avoid these errors, a program should
  492. of course put suitable synchronization mechanisms in place. As you can
  493. see, Guile itself is again only concerned about robustness, not about
  494. correctness: without proper synchronization, your program will likely
  495. not be correct, but the worst consequence is an error message.
  496. Real thread-safety often requires that a critical section of code is
  497. executed in a certain restricted manner. A common requirement is that
  498. the code section is not entered a second time when it is already being
  499. executed. Locking a mutex while in that section ensures that no other
  500. thread will start executing it, blocking asyncs ensures that no
  501. asynchronous code enters the section again from the current thread, and
  502. the error checking of Guile mutexes guarantees that an error is
  503. signaled when the current thread accidentally reenters the critical
  504. section via recursive function calls.
  505. Guile provides two mechanisms to support critical sections as outlined
  506. above. You can either use the macros
  507. @code{SCM_CRITICAL_SECTION_START} and @code{SCM_CRITICAL_SECTION_END}
  508. for very simple sections; or use a dynwind context together with a
  509. call to @code{scm_dynwind_critical_section}.
  510. The macros only work reliably for critical sections that are
  511. guaranteed to not cause a non-local exit. They also do not detect an
  512. accidental reentry by the current thread. Thus, you should probably
  513. only use them to delimit critical sections that do not contain calls
  514. to libguile functions or to other external functions that might do
  515. complicated things.
  516. The function @code{scm_dynwind_critical_section}, on the other hand,
  517. will correctly deal with non-local exits because it requires a dynwind
  518. context. Also, by using a separate mutex for each critical section,
  519. it can detect accidental reentries.