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