Concurrency#
This page covers using one connection from several threads or async tasks, holding the condition of a stream, and the threads callbacks run on. In an async client, each thread in these requirements is an async task.
Calls#
concurrency-concurrent-calls [concurrency] [MUST]Concurrent calls on one connection each return their own result.
concurrency-interleaved-calls [concurrency] [MUST]One call writes its request while another call waits for its response. Each call returns its own result, in whichever order the responses arrive.
concurrency-concurrent-stream-adds [streams, concurrency] [MUST]Streams added concurrently are each registered.
concurrency-close-with-a-call-in-flight [concurrency] [MUST]The program closes the connection while a call waits for a response. The waiting call raises the connection error.
concurrency-response-after-close [concurrency] [MUST]A response that arrives after the client sends its
disconnectdoes not reach the call waiting for it. The call raises the connection error.
Conditions#
Each stream has a condition: a lock that a thread holds while it reads the stream’s value and waits for the next one. While a thread holds it, the value the thread reads changes only when it waits, so the thread misses no update. The connection has a condition for its updates too.
concurrency-wait-holding [streams, conditions] [MUST]A thread holding a stream’s condition sees one update per wait, and the value after the wait is the next value. While it holds the condition, a zero timeout returns false.
concurrency-wait-for-update [streams, conditions] [SHOULD]The client offers a wait for the next update message of any stream, under a condition for the connection’s updates. Holding that condition, a thread sees one update per wait.
concurrency-held-value [streams, conditions, concurrency] [MUST]While a thread holds the condition of a stream, the value it reads changes only when it waits. A thread that does not hold the condition reads the latest value. A wait returns for a value that arrived while the holder read the old one.
concurrency-held-stream-ending [streams, conditions, concurrency] [MUST]While a thread holds the condition of a stream, an error that ends the stream reaches it as a value does. The thread reads the old value until it waits. The wait returns, and later reads and waits raise the error.
concurrency-held-stream-ending-before-value [streams, conditions, concurrency] [MUST]While a thread holds the condition of a stream with no value, an error ends the stream. A read on the thread raises the error.
concurrency-held-read-before-value [streams, conditions, concurrency] [MUST]A read of a stream with no value blocks until the first value arrives, on a thread holding the stream’s condition too. The read waits on the condition, which lets it go while it waits.
concurrency-wait-for-unread-value [streams, conditions] [MUST]A wait returns only for a value newer than the last one the thread read, including a value it read without holding the condition.
concurrency-value-read-before-wake [streams, conditions, races] [MUST]A wait does not return for a value the thread read before the update woke the stream’s waiters.
concurrency-call-while-holding [streams, conditions] [MUST]A thread holding the condition of a stream makes a call, which returns.
concurrency-wait-on-condition-object [streams, conditions, concurrency] [MUST]A thread holding a stream’s condition waits on the condition object itself, as the language waits on a condition variable. Other threads can take the condition while it waits. An update wakes the wait, and the thread then holds the condition again.
concurrency-spurious-wake [streams, events, conditions, concurrency] [MUST]A wait with a timeout, woken by a notification of the condition with no update, waits on until the timeout and returns false.
Removing and Closing#
concurrency-remove-while-holding [streams, conditions] [MUST]A thread holding the condition of a stream removes the stream, while updates for the stream arrive. A wait by that thread then raises the stream error for a removed stream.
concurrency-remove-event-while-holding [events, conditions] [MUST]A thread holding the condition of an event removes the event, while the event fires. A wait by that thread then raises the stream error, at once.
concurrency-close-while-holding [streams, conditions] [MUST]A thread holding the condition of a stream, or the condition for the connection’s updates, closes the connection while updates for the stream arrive. A wait on the stream by that thread then raises the connection error, and the stream’s end callbacks are called once.
concurrency-wait-holding-another [streams, conditions, concurrency] [MUST]A thread holding the condition of one stream waits on a second stream. The wait returns on the second stream’s next update, when an update of the first stream arrives before it.
concurrency-wake-waiter-holding-another [streams, conditions, concurrency] [MUST]A close or a failure of the connection wakes two waits: one on a stream, made while holding the condition of a second stream, and one on that second stream. Both waits raise the connection error. The wait on the second stream raises once the task holding its condition lets go of it.
concurrency-wake-update-waiter [streams, conditions, concurrency] [MUST]A close or a failure of the connection wakes a thread waiting for an update while it holds the condition of a stream. The wait returns or raises the connection error, and a read of the stream then raises the connection error.
concurrency-interrupted-wait [streams, events, concurrency] [MUST]Interrupting a thread that waits on a stream or an event, such as with
Thread.interruptin Java, ends the wait with the language’s error for an interrupted thread. The stream or event runs on.
Callbacks and Conditions#
concurrency-remove-callback-while-holding [streams, callbacks, conditions] [MUST]A thread holding the condition of a stream removes a callback of the stream, while updates for the stream arrive. The remove returns, and a later wait returns for the next update.
concurrency-close-while-callback-waits [streams, callbacks, conditions] [MUST]A thread holding the condition of a stream closes the connection, while a callback of another stream waits to take that condition. The stream held can be one the client removed. The close returns, and the connection is closed.
concurrency-add-callback-while-callback-waits [streams, callbacks, conditions] [MUST]A thread holding the condition of a stream adds a callback to another stream, while a callback of that stream waits to take the condition. The add returns.
Callback Threads#
concurrency-callbacks-run-off-the-caller [streams, callbacks, concurrency] [MUST]Callbacks for stream updates run on a thread of the client, and never on a thread that makes calls. The first call of a callback, with the value the stream already has, may run on the thread that adds it.
concurrency-first-call-waits [streams, callbacks] [MUST]The first call of a callback can wait for a later update of its stream. The wait returns, whether the call runs on the thread that adds the callback or on a thread of the client.
concurrency-blocked-callback [streams, callbacks, concurrency] [MUST]Calls return, and reads of a stream return its latest value, while a callback blocks. Waits and other callbacks can stall. The callback is passed the latest value once it returns.
concurrency-callback-one-call-at-a-time [streams, callbacks] [MUST]The calls of a callback never overlap.
concurrency-close-during-first-call [streams, callbacks, concurrency, races] [MUST]Closing the connection returns while an update of a stream waits for the first call of a callback to finish.
concurrency-update-callback-after-close [streams, callbacks, concurrency, races] [MUST]An update callback is not called once the connection is closed, though the update arrived before the close.
Calls From Callbacks#
concurrency-call-from-a-callback [streams, callbacks] [MUST]A callback makes a call, which returns its result.
concurrency-remove-stream-from-callback [streams, callbacks] [MUST]A callback removes the stream it is called for. The remove returns, and a read of the stream then raises the stream error.
concurrency-remove-callback-from-itself [streams, callbacks] [MUST]A callback removes itself, and is not called again.
concurrency-close-from-a-callback [streams, callbacks] [MUST]A callback closes the connection, from its first call or a later one. The end callbacks of the stream are called.
concurrency-close-from-an-end-callback [streams, callbacks] [MUST]An end callback closes the connection, whether the stream was removed or ended with an error. The end callbacks of the other streams are called.