| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/sync.R |
| 3 |
\name{cv} |
| 4 |
\alias{cv} |
| 5 |
\alias{wait} |
| 6 |
\alias{wait_} |
| 7 |
\alias{until} |
| 8 |
\alias{until_} |
| 9 |
\alias{cv_value} |
| 10 |
\alias{cv_reset} |
| 11 |
\alias{cv_signal} |
| 12 |
\title{Condition Variables} |
| 13 |
\usage{ |
| 14 |
cv() |
| 15 |
|
| 16 |
wait(cv) |
| 17 |
|
| 18 |
wait_(cv) |
| 19 |
|
| 20 |
until(cv, msec) |
| 21 |
|
| 22 |
until_(cv, msec) |
| 23 |
|
| 24 |
cv_value(cv) |
| 25 |
|
| 26 |
cv_reset(cv) |
| 27 |
|
| 28 |
cv_signal(cv) |
| 29 |
} |
| 30 |
\arguments{ |
| 31 |
\item{cv}{a 'conditionVariable' object.} |
| 32 |
|
| 33 |
\item{msec}{maximum time in milliseconds to wait for the condition variable |
| 34 |
to be signalled.} |
| 35 |
} |
| 36 |
\value{ |
| 37 |
For \strong{cv}: a 'conditionVariable' object. |
| 38 |
|
| 39 |
For \strong{wait}: (invisibly) logical TRUE, or else FALSE if a flag has been |
| 40 |
set. |
| 41 |
|
| 42 |
For \strong{until}: (invisibly) logical TRUE if signalled, or else FALSE if the |
| 43 |
timeout was reached. |
| 44 |
|
| 45 |
For \strong{cv_value}: integer value of the condition variable. |
| 46 |
|
| 47 |
For \strong{cv_reset} and \strong{cv_signal}: zero (invisibly). |
| 48 |
} |
| 49 |
\description{ |
| 50 |
\code{cv} creates a new condition variable (protected by a mutex internal to the |
| 51 |
object). |
| 52 |
|
| 53 |
\code{wait} waits on a condition being signalled by completion of an |
| 54 |
asynchronous receive or pipe event. \cr \code{wait_} is a variant that allows |
| 55 |
user interrupts, suitable for interactive use. |
| 56 |
|
| 57 |
\code{until} waits until a future time on a condition being signalled by |
| 58 |
completion of an asynchronous receive or pipe event. \cr \code{until_} is a |
| 59 |
variant that allows user interrupts, suitable for interactive use. |
| 60 |
|
| 61 |
\code{cv_value} inspects the internal value of a condition variable. |
| 62 |
|
| 63 |
\code{cv_reset} resets the internal value and flag of a condition variable. |
| 64 |
|
| 65 |
\code{cv_signal} signals a condition variable. |
| 66 |
} |
| 67 |
\details{ |
| 68 |
Pass the 'conditionVariable' to the asynchronous receive functions |
| 69 |
\code{\link[=recv_aio]{recv_aio()}} or \code{\link[=request]{request()}}. Alternatively, to be notified of a pipe event, |
| 70 |
pass it to \code{\link[=pipe_notify]{pipe_notify()}}. |
| 71 |
|
| 72 |
Completion of the receive or pipe event, which happens asynchronously and |
| 73 |
independently of the main R thread, will signal the condition variable by |
| 74 |
incrementing it by 1. |
| 75 |
|
| 76 |
This will cause the R execution thread waiting on the condition variable |
| 77 |
using \code{wait()} or \code{until()} to wake and continue. |
| 78 |
|
| 79 |
For argument \code{msec}, non-integer values will be coerced to integer. |
| 80 |
Non-numeric input will be ignored and return immediately. |
| 81 |
} |
| 82 |
\section{Condition}{ |
| 83 |
|
| 84 |
|
| 85 |
The condition internal to this 'conditionVariable' maintains a state (value). |
| 86 |
Each signal increments the value by 1. Each time \code{wait()} or \code{until()} |
| 87 |
returns (apart from due to timeout), the value is decremented by 1. |
| 88 |
|
| 89 |
The internal condition may be inspected at any time using \code{cv_value()} and |
| 90 |
reset using \code{cv_reset()}. This affords a high degree of flexibility in |
| 91 |
designing complex concurrent applications. |
| 92 |
} |
| 93 |
|
| 94 |
\section{Flag}{ |
| 95 |
|
| 96 |
|
| 97 |
The condition variable also contains a flag that certain signalling functions |
| 98 |
such as \code{\link[=pipe_notify]{pipe_notify()}} can set. When this flag has been set, all subsequent |
| 99 |
\code{wait()} calls will return logical FALSE instead of TRUE. |
| 100 |
|
| 101 |
Note that the flag is not automatically reset, but may be reset manually |
| 102 |
using \code{cv_reset()}. |
| 103 |
} |
| 104 |
|
| 105 |
\examples{ |
| 106 |
cv <- cv() |
| 107 |
|
| 108 |
\dontrun{ |
| 109 |
wait(cv) # would block until the cv is signalled |
| 110 |
wait_(cv) # would block until the cv is signalled or interrupted |
| 111 |
} |
| 112 |
|
| 113 |
until(cv, 10L) |
| 114 |
until_(cv, 10L) |
| 115 |
|
| 116 |
cv_value(cv) |
| 117 |
|
| 118 |
cv_reset(cv) |
| 119 |
|
| 120 |
cv_value(cv) |
| 121 |
cv_signal(cv) |
| 122 |
cv_value(cv) |
| 123 |
|
| 124 |
} |