Skip to content

Commit

Permalink
Made SeccSender and SeccReceiver clonable (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimmonsjr authored Aug 17, 2019
1 parent 6282e08 commit 55afa95
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "secc"
version = "0.0.6"
version = "0.0.7"
edition = "2018"
authors = ["Robert Simmons Jr. MSc."]
license = "Apache-2.0"
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ nodes. When send and receive operations happen, nodes are merely moved around lo
physically.

### What's New
* 2019-08-11: 0.0.6
* Improved README and Lib documentation.
* 2019-08-11: 0.0.5
* Migration from Axiom. SECC has been split off from Axiom into its own crate.
* 2019-08-17: 0.0.7
* Removed `create_with_arcs()` function as `create()` is all that is needed now.
* Made SeccSender and SeccReceiver both clonable as they have internal `Arc`s.

[Release Notes for All Versions](https://github.com/rsimmonsjr/secc/blob/master/RELEASE_NOTES.md)

Expand Down
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release Notes

* 2019-08-17: 0.0.7
* Removed `create_with_arcs()` function as `create()` is all that is needed now.
* Made SeccSender and SeccReceiver both clonable as they have internal `Arc`s.
* 2019-08-11: 0.0.6
* Improved README and Lib documentation.
* 2019-08-11: 0.0.1
Expand Down
21 changes: 6 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ pub struct SeccCore<T: Sync + Send> {
}

/// Sender side of the channel.
#[derive(Clone)]
pub struct SeccSender<T: Sync + Send> {
/// The core of the channel.
core: Arc<SeccCore<T>>,
Expand Down Expand Up @@ -329,6 +330,7 @@ unsafe impl<T: Send + Sync> Send for SeccSender<T> {}
unsafe impl<T: Send + Sync> Sync for SeccSender<T> {}

/// Receiver side of the channel.
#[derive(Clone)]
pub struct SeccReceiver<T: Sync + Send> {
/// The core of the channel.
core: Arc<SeccCore<T>>,
Expand Down Expand Up @@ -652,17 +654,6 @@ pub fn create<T: Sync + Send>(capacity: u16, poll_ms: u16) -> (SeccSender<T>, Se
(sender, receiver)
}

/// Creates the sender and receiver sides of this channel, wrapping each in an [`Arc`] and
/// returns them as a tuple. The user can pass both a channel `capacity` and a `poll_ms` which
/// govern how long operations that wait on the channel will poll.
pub fn create_with_arcs<T: Sync + Send>(
capacity: u16,
poll_ms: u16,
) -> (Arc<SeccSender<T>>, Arc<SeccReceiver<T>>) {
let (sender, receiver) = create(capacity, poll_ms);
(Arc::new(sender), Arc::new(receiver))
}

// --------------------- Test Cases ---------------------

#[cfg(test)]
Expand Down Expand Up @@ -1265,7 +1256,7 @@ mod tests {
// receivers on different threads.
let message_count = 200;
let capacity = 32;
let (sender, receiver) = create_with_arcs::<u32>(capacity, 20);
let (sender, receiver) = create::<u32>(capacity, 20);

let rx = thread::spawn(move || {
let mut count = 0;
Expand Down Expand Up @@ -1294,7 +1285,7 @@ mod tests {

// Test that if a user attempts to receive before a message is sent, he will be forced
// to wait for the message.
let (sender, receiver) = create_with_arcs::<u32>(5, 20);
let (sender, receiver) = create::<u32>(5, 20);
let receiver2 = receiver.clone();
let mutex = Arc::new(Mutex::new(false));
let rx_mutex = mutex.clone();
Expand Down Expand Up @@ -1339,7 +1330,7 @@ mod tests {

// Tests that triggering send and receive as close to at the same time as possible does
// not cause any race conditions.
let (sender, receiver) = create_with_arcs::<u32>(5, 20);
let (sender, receiver) = create::<u32>(5, 20);
let receiver2 = receiver.clone();
let pair = Arc::new((Mutex::new((false, false)), Condvar::new()));
let rx_pair = pair.clone();
Expand Down Expand Up @@ -1396,7 +1387,7 @@ mod tests {
// The channel size is intentionally small to force wait conditions.
let message_count = 10000;
let capacity = 10;
let (sender, receiver) = create_with_arcs::<u32>(capacity, 20);
let (sender, receiver) = create::<u32>(capacity, 20);

let debug_if_needed = |core: Arc<SeccCore<u32>>| {
if core.receivable.load(Ordering::Relaxed) > core.capacity {
Expand Down

0 comments on commit 55afa95

Please sign in to comment.