Add add_nonblocking
, capacity
and is_full
for streams
#790
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Having a non-blocking add is useful when adding from an asynchronous callback from C code (e.g. an event handler or timeout in
Lablgtk
) because having the fiber yield would result in anEffect.Unhandled
exception.The simplest solution is to make the stream unbounded by creating it with the max int as capacity, but that has the disadvantage of letting the stream take up a potentially unbounded amount of memory.
Another solution, when using a single domain, is to use the
is_full
(orcapacity
) added by this commit, as one can do e.g.(if Stream.is_full stream then ignore (Stream.take stream)); Stream.add stream item
.In a context with multiple domain, this may not work as the lock is released between the
take
and theadd
, and another domain might add an element between these two operations. This is whyadd_nonblocking
was also added: defining it from within the module allows to do both operations without releasing the lock in between.I'm not too sure about
Sync.put_nonblocking
, and it might be reasonable to just raise anInvalid_argument
exception whenadd_nonblocking
is called on a stream of capacity 0.