-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add add_nonblocking
, capacity
and is_full
for streams
#790
base: main
Are you sure you want to change the base?
Conversation
Hm. The |
db9eade
to
e359a09
Compare
I removed |
|
Sorry for the late reply. Having I nevertheless believe that My main reason for adding (Though I'm not sure how to express However, I believe that libraries should aim at making the set of operations clearly complete, meaning that expressing missing operations and convincing oneself of their correctness should be easy, and not require knowing anything about the implementation. For example, For streams (of capacity >= 1), I'd expect a complete set of operations to necessarily contain some way of doing arbitrary operations while holding the lock:
Though complete, this interface of course has a few problems (e.g. it is slightly leaky, and allows breaking invariants by storing the Even an improved version of this complete interface felt a bit too big and opinionated of a change for a first contribution, so I decided to just add |
One use of |
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.