-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
430 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "lldb", | ||
"request": "launch", | ||
"name": "Debug unit tests in library 'disrustor'", | ||
"cargo": { | ||
"args": [ | ||
"test", | ||
"--no-run", | ||
"--lib", | ||
"--package=disrustor" | ||
], | ||
"filter": { | ||
"name": "disrustor", | ||
"kind": "lib" | ||
} | ||
}, | ||
"args": [], | ||
"cwd": "${workspaceFolder}" | ||
}, | ||
{ | ||
"type": "lldb", | ||
"request": "launch", | ||
"name": "Debug example 'multi_producer'", | ||
"cargo": { | ||
"args": [ | ||
"build", | ||
"--example=multi_producer", | ||
"--package=disrustor" | ||
], | ||
"filter": { | ||
"name": "multi_producer", | ||
"kind": "example" | ||
} | ||
}, | ||
"args": [], | ||
"cwd": "${workspaceFolder}" | ||
}, | ||
{ | ||
"type": "lldb", | ||
"request": "launch", | ||
"name": "Debug unit tests in example 'multi_producer'", | ||
"cargo": { | ||
"args": [ | ||
"test", | ||
"--no-run", | ||
"--example=multi_producer", | ||
"--package=disrustor" | ||
], | ||
"filter": { | ||
"name": "multi_producer", | ||
"kind": "example" | ||
} | ||
}, | ||
"args": [], | ||
"cwd": "${workspaceFolder}" | ||
}, | ||
{ | ||
"type": "lldb", | ||
"request": "launch", | ||
"name": "Debug example 'single_producer'", | ||
"cargo": { | ||
"args": [ | ||
"build", | ||
"--example=single_producer", | ||
"--package=disrustor" | ||
], | ||
"filter": { | ||
"name": "single_producer", | ||
"kind": "example" | ||
} | ||
}, | ||
"args": [], | ||
"cwd": "${workspaceFolder}" | ||
}, | ||
{ | ||
"type": "lldb", | ||
"request": "launch", | ||
"name": "Debug unit tests in example 'single_producer'", | ||
"cargo": { | ||
"args": [ | ||
"test", | ||
"--no-run", | ||
"--example=single_producer", | ||
"--package=disrustor" | ||
], | ||
"filter": { | ||
"name": "single_producer", | ||
"kind": "example" | ||
} | ||
}, | ||
"args": [], | ||
"cwd": "${workspaceFolder}" | ||
}, | ||
{ | ||
"type": "lldb", | ||
"request": "launch", | ||
"name": "Debug benchmark 'my_benchmark'", | ||
"cargo": { | ||
"args": [ | ||
"test", | ||
"--no-run", | ||
"--bench=my_benchmark", | ||
"--package=disrustor" | ||
], | ||
"filter": { | ||
"name": "my_benchmark", | ||
"kind": "bench" | ||
} | ||
}, | ||
"args": [], | ||
"cwd": "${workspaceFolder}" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use disrustor::{ | ||
internal::{BlockingWaitStrategy, SpinLoopWaitStrategy}, | ||
*, | ||
}; | ||
use log::*; | ||
|
||
const MAX: i64 = 200i64; | ||
|
||
fn follow_sequence<W: WaitStrategy + 'static>() { | ||
let (executor, producer) = DisrustorBuilder::with_ring_buffer(128) | ||
.with_wait_strategy::<W>() | ||
.with_multi_producer() | ||
.with_barrier(|b| { | ||
b.handle_events_mut(|data, sequence, _| { | ||
let val = *data; | ||
if val as i64 != sequence { | ||
panic!( | ||
"concurrency problem detected (p1), expected {}, but got {}", | ||
sequence, val | ||
); | ||
} | ||
debug!("updating sequence {} from {} to {}", sequence, val, val * 2); | ||
*data = val * 2; | ||
}); | ||
}) | ||
.with_barrier(|b| { | ||
b.handle_events(|data, sequence, _| { | ||
let val = *data; | ||
if val as i64 != sequence * 2 { | ||
panic!( | ||
"concurrency problem detected (p2), expected {}, but got {}", | ||
sequence * 2, | ||
val | ||
); | ||
} | ||
}); | ||
}) | ||
.build(); | ||
|
||
let handle = executor.spawn(); | ||
let producer = std::sync::Arc::new(producer); | ||
let producer1 = producer.clone(); | ||
let producer2 = producer.clone(); | ||
|
||
let p1 = std::thread::spawn(move || { | ||
for i in 1..=MAX / 10 { | ||
let range = ((i - 1) * 20)..=((i - 1) * 20 + 19); | ||
let items: Vec<_> = range.collect(); | ||
producer1.write(items, |d, seq, _| { | ||
*d = seq as u32; | ||
}); | ||
} | ||
}); | ||
let p2 = std::thread::spawn(move || { | ||
for i in 1..=MAX / 10 { | ||
let range = ((i - 1) * 20)..=((i - 1) * 20 + 19); | ||
let items: Vec<_> = range.collect(); | ||
producer2.write(items, |d, seq, _| { | ||
*d = seq as u32; | ||
}); | ||
} | ||
}); | ||
|
||
p1.join().unwrap(); | ||
p2.join().unwrap(); | ||
if let Ok(producer) = std::sync::Arc::try_unwrap(producer) { | ||
producer.drain(); | ||
} | ||
|
||
handle.join(); | ||
} | ||
|
||
fn main() { | ||
fern::Dispatch::new() | ||
.format(|out, message, record| { | ||
out.finish(format_args!( | ||
"{}[{}][{:?}][{}] {}", | ||
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"), | ||
record.target(), | ||
std::thread::current().id(), | ||
record.level(), | ||
message | ||
)) | ||
}) | ||
.level(log::LevelFilter::Debug) | ||
.chain(std::io::stdout()) | ||
.chain(fern::log_file("output.log").unwrap()) | ||
.apply() | ||
.unwrap(); | ||
|
||
info!("running blocking wait strategy"); | ||
follow_sequence::<BlockingWaitStrategy>(); | ||
|
||
info!("running spinning wait strategy"); | ||
follow_sequence::<SpinLoopWaitStrategy>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.