concurrency.tm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <TeXmacs|2.1>
  2. <project|scheme-gnunet.tm>
  3. <style|tmmanual>
  4. <\body>
  5. Scheme-GNUnet uses <code*|guile-fibers> for concurrency, but supports
  6. POSIX-style threading as well, using the <code*|(ice-9 threads)> Guile
  7. module. More concretely, this means <code*|spawn-fiber> is used by default
  8. for starting asynchronuous computations and public procedures accept an
  9. optional <scm|#:spawn> argument accepting a procedure like
  10. <code*|spawn-fiber> or <code*|call-with-new-thread>.
  11. \<#2018\>Conditions\<#2019\> can be used for synchronising concurrent
  12. computations, see the documentation of <code*|guile-fibers> for details.
  13. <paragraph|Repeated conditions>Scheme-GNUnet has a variant of fibers
  14. conditions, named \<#2018\>repeated conditions\<#2019\>, in the module
  15. <scm|(gnu gnunet concurrency repeated-condition)>.<space|1em>It is
  16. unfortunately ill-documented.
  17. <section|Waiting for unreachability of objects>
  18. A service module often allows starting a few operations and
  19. cancelling/stopping them, freeing resources (ports, network, memory, CPU
  20. <text-dots>) used by the operation. It can be useful to also
  21. <em|automatically> cancel the operation when it becomes unreachable, for
  22. example in case an exception was raised before the code for cancelling the
  23. operation.
  24. To automatically cancel when the GC discovers the operation object to be
  25. lost, the records and procedures from <scm|(gnu gnunet concurrency
  26. lost-and-found)><index|(gnu gnunet concurrency lost-and-found)> can be
  27. used. But first, some terminology.
  28. <\description>
  29. <item*|lost>An object is <dfn|lost><index|lost> when it is, in GC terms,
  30. unreachable.
  31. <item*|found>A previously lost object is said to be
  32. <dfn|found><index|found> once this module discovers it was lost \V
  33. currently, this does not happen directly after it became unreachable,
  34. this module only discovers lost objects after a GC cycle (see
  35. <scm|after-gc-hook> in (guile)GC Hooks).
  36. By finding an object, it becomes reachable again and can therefore become
  37. lost again. However, it will only be found the first time it became lost.
  38. <item*|lost-and-found>Each found object is put inside their corresponding
  39. <dfn|lost-and-found><index|lost-and-found>, if any.
  40. </description>
  41. <\explain>
  42. <scm|(make-lost-and-found)>
  43. </explain|Make a fresh lost-and-found. It can be tested if an object is a
  44. lost-and-found with the predicate <scm|lost-and-found?>.>
  45. <\explain>
  46. <scm|(collect-lost-and-found-operation <var|lost-and-found>)>
  47. <|explain>
  48. Make a Fibers operation that waits until some objects have become found
  49. in <var|lost-and-found> and returns these objects as a list. In a few
  50. race circumstances, this operation can spuriously return the empty list.
  51. In the current implementation, this operation can not meaningfully be
  52. reused; in some circumstances, performing the returned operation again
  53. (with Fibers' <scm|perform-operation>) after it has previously been
  54. performed can cause the empty list to be returned even if new objects
  55. have been lost, without blocking.
  56. Summarised, don't reuse <scm|collect-lost-and-found-operation>s that have
  57. returned, always make a fresh one!
  58. </explain>
  59. <\explain>
  60. <scm|\<less\>losable\<gtr\>><index|\<less\>losable\<gtr\>>
  61. <|explain>
  62. This record type is the supertype of all objects that can be added to a
  63. lost-and-found. By constructing an object of this type, the object is
  64. automatically added to the lost-and-found that was passed to the
  65. constructor. Alternatively, falsehood can be passed to the constructor if
  66. the object should <em|not> be added to a lost-and-found.
  67. Due to unknown reasons<\footnote>
  68. perhaps a bug in Guile's implementation of RnRS records, or maybe not
  69. </footnote>, subtypes must define the \<#2018\>protocol\<#2019\> (see
  70. (guile)R6RS Records), otherwise the object will not be added to the
  71. lost-and-found.
  72. </explain>
  73. <\example>
  74. The following code constructs a lost-and-found and a few losable objects
  75. and it let the losable objects become lost, after which they are
  76. collected.
  77. <\scm-code>
  78. (import (gnu gnunet concurrency lost-and-found)
  79. \ \ \ \ \ \ \ \ (rnrs records syntactic) (fibers operations))
  80. (define lost-and-found (make-lost-and-found))
  81. (define-record-type (\<less\>foobar\<gtr\> make-foobar foobar?) (parent
  82. \<less\>losable\<gtr\>)
  83. \ \ (fields (immutable foo foobar-foo) (immutable bar foobar-bar))
  84. \ \ (protocol (lambda (%make)
  85. \ \ \ \ \ \ \ \ \ \ \ \ \ \ (lambda (foo bar) ((%make lost-and-found)
  86. foo bar)))))
  87. (begin
  88. \ \ (make-foobar 'foo 'bar) (make-foobar #\\x #\\y) (gc))
  89. (for-each (lambda (x) (pk 'found (foobar-foo x) (foobar-bar x)))
  90. \ \ \ \ \ \ \ \ \ \ (perform-operation
  91. \ \ \ \ \ \ \ \ \ \ \ (collect-lost-and-found-operation
  92. lost-and-found)))
  93. </scm-code>
  94. Output:
  95. <\verbatim-code>
  96. ;;; (found foo bar)
  97. ;;; (foo #\\x #\\y)
  98. </verbatim-code>
  99. </example>
  100. </body>
  101. <\initial>
  102. <\collection>
  103. <associate|page-medium|paper>
  104. <associate|save-aux|false>
  105. </collection>
  106. </initial>