synch.texi 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. @node Higher-level synchronization
  2. @section Higher-level synchronization
  3. This section details the various higher-level thread synchronization
  4. devices that Scheme48 provides.
  5. @subsection Condition variables
  6. @stindex condvars
  7. @dfn{Condition variables} are multiple-assignment cells on which
  8. readers block. Threads may wait on condition variables; when some
  9. other thread assigns a condition variable, all threads waiting on it
  10. are revived. The @code{condvars} structure exports all of these
  11. condition-variable-related names.
  12. In many concurrency systems, condition variables are operated in
  13. conjunction with mutual exclusion locks. On the other hand, in
  14. Scheme48, they are used in conjunction with its optimistic concurrency
  15. devices.
  16. @deffn procedure make-condvar [id] @returns{} condvar
  17. @deffnx procedure condvar? object @returns{} boolean
  18. Condition variable constructor & disjoint type predicate. @var{Id} is
  19. used purely for debugging.
  20. @end deffn
  21. @deffn procedure maybe-commit-and-wait-for-condvar condvar @returns{} boolean
  22. @deffnx procedure maybe-commit-and-set-condvar! condvar value @returns{} boolean
  23. @code{Maybe-commit-and-wait-for-condvar} attempts to commit the current
  24. proposal. If the commit succeeded, the current thread is blocked on
  25. @var{condvar}, and when the current thread is woken up,
  26. @code{maybe-commit-and-wait-for-condvar} returns @code{#t}. If the
  27. commit did not succeed, @code{maybe-commit-and-wait-for-condvar}
  28. immediately returns @code{#f}. @code{Maybe-commit-and-set-condvar!}
  29. attempts to commit the current proposal as well. If it succeeds, it is
  30. noted that @var{condvar} has a value, @var{condvar}'s value is set to
  31. be @var{value}, and all threads waiting on @var{condvar} are woken up.
  32. @strong{Note:} Do not use these in atomic transactions as delimited by
  33. @code{call-ensuring-atomicity} @etc{}; see the note in @ref{Optimistic
  34. concurrency} on this matter for details.
  35. @end deffn
  36. @deffn procedure condvar-has-value? condvar @returns{} boolean
  37. @deffnx procedure condvar-value condvar @returns{} value
  38. @code{Condvar-has-value?} tells whether or not @var{condvar} has been
  39. assigned. If it has been assigned, @code{condvar-value} accesses the
  40. value to which it was assigned.
  41. @end deffn
  42. @deffn procedure set-condvar-has-value?! condvar boolean @returns{} unspecified
  43. @deffnx procedure set-condvar-value! condvar value @returns{} unspecified
  44. @code{Set-condvar-has-value?!} is used to tell whether or not
  45. @var{condvar} is assigned. @code{Set-condvar-value!} sets
  46. @var{condvar}'s value.
  47. @strong{Note:} @code{Set-condvar-has-value?!} should be used only with
  48. a second argument of @code{#f}. @code{Set-condvar-value!} is a very
  49. dangerous routine, and @code{maybe-commit-and-set-condvar!} is what one
  50. should almost always use, except if one wishes to clean up after
  51. unassigning a condition variable.
  52. @end deffn
  53. @subsection Placeholders
  54. @stindex placeholders
  55. @dfn{Placeholders} are similar to condition variables, except that they
  56. may be assigned only once; they are in general a much simpler mechanism
  57. for throw-away temporary synchronization devices. They are provided by
  58. the @code{placeholders} structure.
  59. @deffn procedure make-placeholder [id] @returns{} placeholder
  60. @deffnx procedure placeholder? object @returns{} boolean
  61. Placeholder constructor & disjoint type predicate. @var{Id} is used
  62. only for debugging purposes when printing placeholders.
  63. @end deffn
  64. @deffn procedure placeholder-value placeholder @returns{} value
  65. @deffnx procedure placeholder-set! placeholder value @returns{} unspecified
  66. @code{Placeholder-value} blocks until @var{placeholder} is assigned, at
  67. which point it returns the value assigned. @code{Placeholder-set!}
  68. assigns @var{placeholder}'s value to @var{value}, awakening all threads
  69. waiting for @var{placeholder}. It is an error to assign a placeholder
  70. with @code{placeholder-set!} that has already been assigned.
  71. @end deffn
  72. @subsection Value pipes
  73. @cindex thread communication channels, asynchronous
  74. @cindex asynchronous thread communication channels
  75. @dfn{Value pipes} are asynchronous communication pipes between threads.
  76. The @code{value-pipes} structure exports these value pipe operations.
  77. @deffn procedure make-pipe [size [id]] @returns{} value-pipe
  78. @deffnx procedure pipe? object @returns{} boolean
  79. @code{Make-pipe} is the value pipe constructor. @var{Size} is a limit
  80. on the number of elements the pipe can hold at one time. @var{Id} is
  81. used for debugging purposes only in printing pipes. @code{Pipe?} is
  82. the disjoint type predicate for value pipes.
  83. @end deffn
  84. @deffn procedure empty-pipe? pipe @returns{} boolean
  85. @deffnx procedure empty-pipe! pipe @returns{} unspecified
  86. @code{Empty-pipe?} returns @code{#t} if @var{pipe} has no elements in
  87. it and @code{#f} if not. @code{Empty-pipe!} removes all elements from
  88. @code{pipe}.
  89. @end deffn
  90. @deffn procedure pipe-read! pipe @returns{} value
  91. @deffnx procedure pipe-maybe-read! pipe @returns{} value or @code{#f}
  92. @deffnx procedure pipe-maybe-read?! pipe @returns{} [boolean value]
  93. @code{Pipe-read!} reads a value from @var{pipe}, removing it from the
  94. queue. It blocks if there are no elements available in the queue.
  95. @code{Pipe-maybe-read!} attempts to read & return a single value from
  96. @var{pipe}; if no elements are available in its queue, it instead
  97. returns @code{#f}. @code{Pipe-maybe-read?!} does similarly, but it
  98. returns two values: a boolean, signifying whether or not a value was
  99. read; and the value, or @code{#f} if no value was read.
  100. @code{Pipe-maybe-read?!} is useful when @var{pipe} may contain the
  101. value @code{#f}.
  102. @end deffn
  103. @deffn procedure pipe-write! pipe value @returns{} unspecified
  104. @deffnx procedure pipe-push! pipe value @returns{} unspecified
  105. @deffnx procedure pipe-maybe-write! pipe value @returns{} boolean
  106. @code{Pipe-write!} attempts to add @var{value} to @var{pipe}'s queue.
  107. If @var{pipe}'s maximum size, as passed to @code{make-pipe} when
  108. constructing the pipe, is either @code{#f} or greater than the number
  109. of elements in @var{pipe}'s queue, @code{pipe-write!} will not block;
  110. otherwise it will block until a space has been made available in the
  111. pipe's queue by another thread reading from it. @code{Pipe-push!} does
  112. similarly, but, in the case where the pipe is full, it pushes the first
  113. element to be read out of the pipe. @code{Pipe-maybe-write!} is also
  114. similar to @code{pipe-write!}, but it returns @code{#t} if the pipe was
  115. not full, and it @emph{immediately} returns @code{#f} if the pipe was
  116. full.
  117. @end deffn