Skip to content
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

libsync: Add safer abstraction for SPSC queue. #15995

Merged
merged 1 commit into from
Aug 1, 2014

Conversation

Ryman
Copy link
Contributor

@Ryman Ryman commented Jul 26, 2014

The current spsc implementation doesn't enforce single-producer
single-consumer usage and also allows unsafe memory use through
peek & pop.

For safer usage, new now returns a pair of owned objects which
only allow consumer or producer behaviors through an Arc.
Through restricting the mutability of the receiver to mut the
peek and pop behavior becomes safe again, with the compiler
complaining about usage which could lead to problems.

To fix code broken from this, update:
Queue::new(x) -> unsafe { Queue::unchecked_new(x) }

[breaking-change]

For an example of broken behavior, check the added test which uses the unchecked constructor.

@emberian
Copy link
Member

r? @alexcrichton

@alexcrichton
Copy link
Member

In general the concurrent queue types are not currently meant for general consumption, they would require extensive cleanup and scrutiny for that to be the case. Incremental improvements are always welcome, however!

queue: spsc::Queue<Message<T>>, // internal queue for all message
// internal queue for all message
producer: spsc::Producer<Message<T>>,
consumer: spsc::Consumer<Message<T>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This packet should probably contain just a Queue instead of both halves.

@Ryman
Copy link
Contributor Author

Ryman commented Jul 31, 2014

@alexcrichton Reverted Packet to use the unsafe constructor. Updated docstrings to match feedback.

let producer = Producer { inner: arc };

(consumer, producer)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is currently not much precedent for a constructor of the form T::new() -> (U, V) where the return type is completely different from T. I would recommend taking the same approach as channels and have a top-level queue() or pair() function returning the tuple.

Our current naming conventions for a function such as this would mean that the "unchecked" modifier in the constructor below is out of place, and the function would be best named new s well (if this one was renamed).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in favor of renaming to top-level queue() and removing to the unchecked, so it's unsafe fn new....

If that's all ok I'll go ahead with a final rebase?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

The current spsc implementation doesn't enforce single-producer
single-consumer usage and also allows unsafe memory use through
peek & pop.

For safer usage, `spsc_queue::queue` now returns a pair of owned objects which
only allow consumer or producer behaviours through an `Arc`.
Through restricting the mutability of the receiver to `mut` the
peek and pop behaviour becomes safe again, with the compiler
complaining about usage which could lead to problems.

To fix code broken from this, update:
Queue::new(x) -> unsafe { Queue::new(x) }

[breaking-change]
@Ryman
Copy link
Contributor Author

Ryman commented Aug 1, 2014

@alexcrichton r?

bors added a commit that referenced this pull request Aug 1, 2014
The current spsc implementation doesn't enforce single-producer
single-consumer usage and also allows unsafe memory use through
peek & pop.

For safer usage, `new` now returns a pair of owned objects which
only allow consumer or producer behaviors through an `Arc`.
Through restricting the mutability of the receiver to `mut` the
peek and pop behavior becomes safe again, with the compiler
complaining about usage which could lead to problems.

To fix code broken from this, update:
Queue::new(x) -> unsafe { Queue::unchecked_new(x) }

[breaking-change]

For an example of broken behavior, check the added test which uses the unchecked constructor.
@bors bors closed this Aug 1, 2014
@bors bors merged commit 7b817b6 into rust-lang:master Aug 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants