зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1816835 - Update AudioIPC macOS branch to 916f65cc. r=cubeb-reviewers,glandium,supply-chain-reviewers,padenot
Differential Revision: https://phabricator.services.mozilla.com/D169877
This commit is contained in:
Родитель
57ebc1d81a
Коммит
f8e7c09e02
|
@ -60,9 +60,9 @@ git = "https://github.com/jfkthame/mapped_hyph.git"
|
|||
rev = "c7651a0cffff41996ad13c44f689bd9cd2192c01"
|
||||
replace-with = "vendored-sources"
|
||||
|
||||
[source."git+https://github.com/kinetiknz/audioipc-2?rev=73c8a02da8f2ff022723307bfafa3a58a61448da"]
|
||||
[source."git+https://github.com/kinetiknz/audioipc-2?rev=916f65cc92f6f2484183ff4681b0e9a2bfd60fe7"]
|
||||
git = "https://github.com/kinetiknz/audioipc-2"
|
||||
rev = "73c8a02da8f2ff022723307bfafa3a58a61448da"
|
||||
rev = "916f65cc92f6f2484183ff4681b0e9a2bfd60fe7"
|
||||
replace-with = "vendored-sources"
|
||||
|
||||
[source."git+https://github.com/kinetiknz/mio-named-pipes?rev=21c26326f5f45f415c49eac4ba5bc41a2f961321"]
|
||||
|
|
|
@ -327,7 +327,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "audioipc2"
|
||||
version = "0.5.0"
|
||||
source = "git+https://github.com/kinetiknz/audioipc-2?rev=73c8a02da8f2ff022723307bfafa3a58a61448da#73c8a02da8f2ff022723307bfafa3a58a61448da"
|
||||
source = "git+https://github.com/kinetiknz/audioipc-2?rev=916f65cc92f6f2484183ff4681b0e9a2bfd60fe7#916f65cc92f6f2484183ff4681b0e9a2bfd60fe7"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"ashmem",
|
||||
|
@ -336,7 +336,7 @@ dependencies = [
|
|||
"byteorder",
|
||||
"bytes 1.4.0",
|
||||
"cc",
|
||||
"crossbeam-channel",
|
||||
"crossbeam-queue 0.3.8",
|
||||
"cubeb",
|
||||
"error-chain",
|
||||
"iovec",
|
||||
|
@ -355,7 +355,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "audioipc2-client"
|
||||
version = "0.5.0"
|
||||
source = "git+https://github.com/kinetiknz/audioipc-2?rev=73c8a02da8f2ff022723307bfafa3a58a61448da#73c8a02da8f2ff022723307bfafa3a58a61448da"
|
||||
source = "git+https://github.com/kinetiknz/audioipc-2?rev=916f65cc92f6f2484183ff4681b0e9a2bfd60fe7#916f65cc92f6f2484183ff4681b0e9a2bfd60fe7"
|
||||
dependencies = [
|
||||
"audio_thread_priority",
|
||||
"audioipc2",
|
||||
|
@ -366,7 +366,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "audioipc2-server"
|
||||
version = "0.5.0"
|
||||
source = "git+https://github.com/kinetiknz/audioipc-2?rev=73c8a02da8f2ff022723307bfafa3a58a61448da#73c8a02da8f2ff022723307bfafa3a58a61448da"
|
||||
source = "git+https://github.com/kinetiknz/audioipc-2?rev=916f65cc92f6f2484183ff4681b0e9a2bfd60fe7#916f65cc92f6f2484183ff4681b0e9a2bfd60fe7"
|
||||
dependencies = [
|
||||
"audio_thread_priority",
|
||||
"audioipc2",
|
||||
|
@ -1075,6 +1075,16 @@ dependencies = [
|
|||
"crossbeam-utils 0.6.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-queue"
|
||||
version = "0.3.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d1cfb3ea8a53f37c40dea2c7bedcbd88bdfae54f5e2175d6ecaff1c988353add"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"crossbeam-utils 0.8.14",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.6.6"
|
||||
|
@ -5571,7 +5581,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "f0c32ffea4827978e9aa392d2f743d973c1dfa3730a2ed3f22ce1e6984da848c"
|
||||
dependencies = [
|
||||
"crossbeam-deque 0.7.4",
|
||||
"crossbeam-queue",
|
||||
"crossbeam-queue 0.1.2",
|
||||
"crossbeam-utils 0.6.6",
|
||||
"futures 0.1.31",
|
||||
"lazy_static",
|
||||
|
|
|
@ -105,6 +105,8 @@ TOLERATED_DUPES = {
|
|||
"bytes": 2,
|
||||
"crossbeam-deque": 2,
|
||||
"crossbeam-epoch": 2,
|
||||
# tokio 0.1 uses an older version, allow both during transition.
|
||||
"crossbeam-queue": 2,
|
||||
"crossbeam-utils": 3,
|
||||
"futures": 2,
|
||||
"memoffset": 2,
|
||||
|
|
|
@ -420,6 +420,11 @@ who = "Mike Hommey <mh+mozilla@glandium.org>"
|
|||
criteria = "safe-to-deploy"
|
||||
delta = "0.9.10 -> 0.9.13"
|
||||
|
||||
[[audits.crossbeam-queue]]
|
||||
who = "Matthew Gregan <kinetik@flim.org>"
|
||||
criteria = "safe-to-deploy"
|
||||
version = "0.3.8"
|
||||
|
||||
[[audits.crossbeam-utils]]
|
||||
who = "Mike Hommey <mh+mozilla@glandium.org>"
|
||||
criteria = "safe-to-deploy"
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"files":{"Cargo.toml":"9a41bc278a815fdcbe8d07c1d32f5cedb17dec3375993351b942567b4a644f9e","cbindgen.toml":"fb6abe1671497f432a06e40b1db7ed7cd2cceecbd9a2382193ad7534e8855e34","src/context.rs":"45d640800e8b287ca0242d867618bf33396fb3a9c270b7fb58adca76e37c72cc","src/lib.rs":"c4a6797734489280f6b97dd72c9e51a7bd7be4104592eece3929e29d45cbca4a","src/send_recv.rs":"859abe75b521eb4297c84b30423814b5b87f3c7741ad16fe72189212e123e1ac","src/stream.rs":"46068bb0da752c9dee489f3620d4588967921000e234110ee066397595700ae0"},"package":null}
|
||||
{"files":{"Cargo.toml":"9a41bc278a815fdcbe8d07c1d32f5cedb17dec3375993351b942567b4a644f9e","cbindgen.toml":"fb6abe1671497f432a06e40b1db7ed7cd2cceecbd9a2382193ad7534e8855e34","src/context.rs":"93dc43233603409dbb0c5de20c4e838c88bb0711df65cd5afc882f5b5e39f659","src/lib.rs":"c4a6797734489280f6b97dd72c9e51a7bd7be4104592eece3929e29d45cbca4a","src/send_recv.rs":"859abe75b521eb4297c84b30423814b5b87f3c7741ad16fe72189212e123e1ac","src/stream.rs":"3101f4052e35c1600adafedc67aed92d7d5531ff0ff4845de0a5ea3cedef872d"},"package":null}
|
|
@ -56,8 +56,8 @@ impl ClientContext {
|
|||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn rpc(&self) -> Result<rpccore::Proxy<ServerMessage, ClientMessage>> {
|
||||
self.rpc.try_clone().map_err(|_| Error::default())
|
||||
pub fn rpc(&self) -> rpccore::Proxy<ServerMessage, ClientMessage> {
|
||||
self.rpc.clone()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
|
@ -81,7 +81,7 @@ fn promote_thread(rpc: &rpccore::Proxy<ServerMessage, ClientMessage>) {
|
|||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
fn promote_thread(_rpc: &rpccore::Proxy<ServerMessage, ClientMessage>) {
|
||||
match promote_current_thread_to_real_time(256, 48000) {
|
||||
match promote_current_thread_to_real_time(0, 48000) {
|
||||
Ok(_) => {
|
||||
info!("Audio thread promoted to real-time.");
|
||||
}
|
||||
|
@ -186,7 +186,7 @@ impl ContextOps for ClientContext {
|
|||
.handle()
|
||||
.bind_client::<CubebClient>(server_connection)
|
||||
.map_err(|_| Error::default())?;
|
||||
let rpc2 = rpc.try_clone().map_err(|_| Error::default())?;
|
||||
let rpc2 = rpc.clone();
|
||||
|
||||
// Don't let errors bubble from here. Later calls against this context
|
||||
// will return errors the caller expects to handle.
|
||||
|
@ -225,18 +225,18 @@ impl ContextOps for ClientContext {
|
|||
|
||||
fn max_channel_count(&mut self) -> Result<u32> {
|
||||
assert_not_in_callback();
|
||||
send_recv!(self.rpc()?, ContextGetMaxChannelCount => ContextMaxChannelCount())
|
||||
send_recv!(self.rpc(), ContextGetMaxChannelCount => ContextMaxChannelCount())
|
||||
}
|
||||
|
||||
fn min_latency(&mut self, params: StreamParams) -> Result<u32> {
|
||||
assert_not_in_callback();
|
||||
let params = messages::StreamParams::from(params.as_ref());
|
||||
send_recv!(self.rpc()?, ContextGetMinLatency(params) => ContextMinLatency())
|
||||
send_recv!(self.rpc(), ContextGetMinLatency(params) => ContextMinLatency())
|
||||
}
|
||||
|
||||
fn preferred_sample_rate(&mut self) -> Result<u32> {
|
||||
assert_not_in_callback();
|
||||
send_recv!(self.rpc()?, ContextGetPreferredSampleRate => ContextPreferredSampleRate())
|
||||
send_recv!(self.rpc(), ContextGetPreferredSampleRate => ContextPreferredSampleRate())
|
||||
}
|
||||
|
||||
fn enumerate_devices(
|
||||
|
@ -246,7 +246,7 @@ impl ContextOps for ClientContext {
|
|||
) -> Result<()> {
|
||||
assert_not_in_callback();
|
||||
let v: Vec<ffi::cubeb_device_info> = send_recv!(
|
||||
self.rpc()?, ContextGetDeviceEnumeration(devtype.bits()) => ContextEnumeratedDevices())?
|
||||
self.rpc(), ContextGetDeviceEnumeration(devtype.bits()) => ContextEnumeratedDevices())?
|
||||
.into_iter()
|
||||
.map(|i| i.into())
|
||||
.collect();
|
||||
|
@ -329,7 +329,7 @@ impl ContextOps for ClientContext {
|
|||
assert_not_in_callback();
|
||||
|
||||
if !self.device_collection_rpc {
|
||||
let mut fd = send_recv!(self.rpc()?,
|
||||
let mut fd = send_recv!(self.rpc(),
|
||||
ContextSetupDeviceCollectionCallback =>
|
||||
ContextSetupDeviceCollectionCallback())?;
|
||||
|
||||
|
@ -358,7 +358,7 @@ impl ContextOps for ClientContext {
|
|||
}
|
||||
|
||||
let enable = collection_changed_callback.is_some();
|
||||
send_recv!(self.rpc()?,
|
||||
send_recv!(self.rpc(),
|
||||
ContextRegisterDeviceCollectionChanged(devtype.bits(), enable) =>
|
||||
ContextRegisteredDeviceCollectionChanged)
|
||||
}
|
||||
|
@ -367,9 +367,7 @@ impl ContextOps for ClientContext {
|
|||
impl Drop for ClientContext {
|
||||
fn drop(&mut self) {
|
||||
debug!("ClientContext dropped...");
|
||||
let _ = self
|
||||
.rpc()
|
||||
.and_then(|rpc| send_recv!(rpc, ClientDisconnect => ClientDisconnected));
|
||||
let _ = send_recv!(self.rpc(), ClientDisconnect => ClientDisconnected);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -156,7 +156,7 @@ impl<'ctx> ClientStream<'ctx> {
|
|||
) -> Result<Stream> {
|
||||
assert_not_in_callback();
|
||||
|
||||
let rpc = ctx.rpc()?;
|
||||
let rpc = ctx.rpc();
|
||||
let create_params = StreamCreateParams {
|
||||
input_stream_params: init_params.input_stream_params,
|
||||
output_stream_params: init_params.output_stream_params,
|
||||
|
@ -238,10 +238,7 @@ impl<'ctx> ClientStream<'ctx> {
|
|||
impl Drop for ClientStream<'_> {
|
||||
fn drop(&mut self) {
|
||||
debug!("ClientStream drop");
|
||||
let _ = self
|
||||
.context
|
||||
.rpc()
|
||||
.and_then(|rpc| send_recv!(rpc, StreamDestroy(self.token) => StreamDestroyed));
|
||||
let _ = send_recv!(self.context.rpc(), StreamDestroy(self.token) => StreamDestroyed);
|
||||
debug!("ClientStream drop - stream destroyed");
|
||||
// Wait for CallbackServer to shutdown. The remote server drops the RPC
|
||||
// connection during StreamDestroy, which will cause CallbackServer to drop
|
||||
|
@ -256,49 +253,49 @@ impl Drop for ClientStream<'_> {
|
|||
impl StreamOps for ClientStream<'_> {
|
||||
fn start(&mut self) -> Result<()> {
|
||||
assert_not_in_callback();
|
||||
let rpc = self.context.rpc()?;
|
||||
let rpc = self.context.rpc();
|
||||
send_recv!(rpc, StreamStart(self.token) => StreamStarted)
|
||||
}
|
||||
|
||||
fn stop(&mut self) -> Result<()> {
|
||||
assert_not_in_callback();
|
||||
let rpc = self.context.rpc()?;
|
||||
let rpc = self.context.rpc();
|
||||
send_recv!(rpc, StreamStop(self.token) => StreamStopped)
|
||||
}
|
||||
|
||||
fn position(&mut self) -> Result<u64> {
|
||||
assert_not_in_callback();
|
||||
let rpc = self.context.rpc()?;
|
||||
let rpc = self.context.rpc();
|
||||
send_recv!(rpc, StreamGetPosition(self.token) => StreamPosition())
|
||||
}
|
||||
|
||||
fn latency(&mut self) -> Result<u32> {
|
||||
assert_not_in_callback();
|
||||
let rpc = self.context.rpc()?;
|
||||
let rpc = self.context.rpc();
|
||||
send_recv!(rpc, StreamGetLatency(self.token) => StreamLatency())
|
||||
}
|
||||
|
||||
fn input_latency(&mut self) -> Result<u32> {
|
||||
assert_not_in_callback();
|
||||
let rpc = self.context.rpc()?;
|
||||
let rpc = self.context.rpc();
|
||||
send_recv!(rpc, StreamGetInputLatency(self.token) => StreamInputLatency())
|
||||
}
|
||||
|
||||
fn set_volume(&mut self, volume: f32) -> Result<()> {
|
||||
assert_not_in_callback();
|
||||
let rpc = self.context.rpc()?;
|
||||
let rpc = self.context.rpc();
|
||||
send_recv!(rpc, StreamSetVolume(self.token, volume) => StreamVolumeSet)
|
||||
}
|
||||
|
||||
fn set_name(&mut self, name: &CStr) -> Result<()> {
|
||||
assert_not_in_callback();
|
||||
let rpc = self.context.rpc()?;
|
||||
let rpc = self.context.rpc();
|
||||
send_recv!(rpc, StreamSetName(self.token, name.to_owned()) => StreamNameSet)
|
||||
}
|
||||
|
||||
fn current_device(&mut self) -> Result<&DeviceRef> {
|
||||
assert_not_in_callback();
|
||||
let rpc = self.context.rpc()?;
|
||||
let rpc = self.context.rpc();
|
||||
match send_recv!(rpc, StreamGetCurrentDevice(self.token) => StreamCurrentDevice()) {
|
||||
Ok(d) => Ok(unsafe { DeviceRef::from_ptr(Box::into_raw(Box::new(d.into()))) }),
|
||||
Err(e) => Err(e),
|
||||
|
@ -322,7 +319,7 @@ impl StreamOps for ClientStream<'_> {
|
|||
device_changed_callback: ffi::cubeb_device_changed_callback,
|
||||
) -> Result<()> {
|
||||
assert_not_in_callback();
|
||||
let rpc = self.context.rpc()?;
|
||||
let rpc = self.context.rpc();
|
||||
let enable = device_changed_callback.is_some();
|
||||
*self.device_change_cb.lock().unwrap() = device_changed_callback;
|
||||
send_recv!(rpc, StreamRegisterDeviceChangeCallback(self.token, enable) => StreamRegisterDeviceChangeCallback)
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"files":{"Cargo.toml":"4b00cd5b0ef67640c29a5b4c27ff0eb2bda60ea9bfaa0321625e2eb48714efa8","cbindgen.toml":"fb6abe1671497f432a06e40b1db7ed7cd2cceecbd9a2382193ad7534e8855e34","src/lib.rs":"d9cc7ca311cceb70acbc63b2190d6205094152e582faaad1b4a6061019f5803f","src/server.rs":"6118664d1d2f8917bb3b1582a441ead599d35fe246c5a69213a5baecfcbd94bd"},"package":null}
|
||||
{"files":{"Cargo.toml":"4b00cd5b0ef67640c29a5b4c27ff0eb2bda60ea9bfaa0321625e2eb48714efa8","cbindgen.toml":"fb6abe1671497f432a06e40b1db7ed7cd2cceecbd9a2382193ad7534e8855e34","src/lib.rs":"06aff4fd1326aeabb16b01f81a6f3c59c1717ebe96285a063724830cdf30303a","src/server.rs":"ea839fe4607ba6b70a1ef5dfb2305eb668b148820c5590b87192609fbe3c9edd"},"package":null}
|
|
@ -98,7 +98,7 @@ fn init_threads(
|
|||
None,
|
||||
move || {
|
||||
trace!("Starting {} thread", callback_name);
|
||||
if let Err(e) = promote_current_thread_to_real_time(256, 48000) {
|
||||
if let Err(e) = promote_current_thread_to_real_time(0, 48000) {
|
||||
debug!(
|
||||
"Failed to promote {} thread to real-time: {:?}",
|
||||
callback_name, e
|
||||
|
|
|
@ -647,7 +647,7 @@ impl CubebServer {
|
|||
#[cfg(target_os = "linux")]
|
||||
ServerMessage::PromoteThreadToRealTime(thread_info) => {
|
||||
let info = RtPriorityThreadInfo::deserialize(thread_info);
|
||||
match promote_thread_to_real_time(info, 256, 48000) {
|
||||
match promote_thread_to_real_time(info, 0, 48000) {
|
||||
Ok(_) => {
|
||||
info!("Promotion of content process thread to real-time OK");
|
||||
}
|
||||
|
@ -741,8 +741,8 @@ impl CubebServer {
|
|||
input_frame_size,
|
||||
output_frame_size,
|
||||
shm,
|
||||
state_callback_rpc: rpc.try_clone()?,
|
||||
device_change_callback_rpc: rpc.try_clone()?,
|
||||
state_callback_rpc: rpc.clone(),
|
||||
device_change_callback_rpc: rpc.clone(),
|
||||
data_callback_rpc: rpc,
|
||||
connected: AtomicBool::new(false),
|
||||
});
|
||||
|
@ -802,7 +802,7 @@ impl CubebServer {
|
|||
let min_latency = round_up_pow2(5 * rate / 1000);
|
||||
// Note: maximum latency is restricted by the SharedMem size.
|
||||
let max_latency = rate;
|
||||
let latency = params.latency_frames.max(min_latency).min(max_latency);
|
||||
let latency = params.latency_frames.clamp(min_latency, max_latency);
|
||||
trace!(
|
||||
"stream rate={} latency requested={} calculated={}",
|
||||
rate,
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"files":{"Cargo.toml":"11a215a008476e21b91c45d2afb342ed7f84cb63e2a48ebc6e4cfa2b31149c94","benches/serialization.rs":"d56855d868dab6aa22c8b03a61084535351b76c94b68d8b1d20764e352fe473f","build.rs":"65df9a97c6cdaa3faf72581f04ac289197b0b1797d69d22c1796e957ff1089e2","src/codec.rs":"4e029396765db803201249e90bcf724eb56deed3b2e455822d6673f40550a3e1","src/errors.rs":"67a4a994d0724397657581cde153bdfc05ce86e7efc467f23fafc8f64df80fa4","src/ipccore.rs":"be36c9d927a4ea35869163f2a30be77366acfffadb7f67f550b3b9c53b23a201","src/lib.rs":"9b107cb52081eeea3fa742d30361db70f7138baa423dfe21d37dcf5087afc338","src/messages.rs":"452362da2cace9a0f2e3134c190ecb6a9997f8be4036cde06643e17c6c238240","src/rpccore.rs":"9b7199c0e31941c399934c4442e2169fb85c6067810c37879d1c9614ce00bdf5","src/shm.rs":"1d88f19606899e3e477865d6b84bbe3e272f51618a1c2d57b6dab03a4787cde3","src/sys/mod.rs":"e6fa1d260abf093e1f7b50185195e2d3aee0eb8c9774c6f253953b5896d838f3","src/sys/unix/cmsg.rs":"8a27a20383c333c5d033e58a546a530e26b964942a4615793d1ca078c65efb75","src/sys/unix/cmsghdr.c":"d7344b3dc15cdce410c68669b848bb81f7fe36362cd3699668cb613fa05180f8","src/sys/unix/mod.rs":"59835f0d5509940078b1820a54f49fc5514adeb3e45e7d21e3ab917431da2e74","src/sys/unix/msg.rs":"c0103cc058aeb890ab7aa023fcd6d3b9a0135d6b28fdecdec446650957210508","src/sys/windows/mod.rs":"7b1288e42b3ce34c7004b9fe3eeb6d9822c55e2688d3c2a40e55db46a2ca5d76"},"package":null}
|
||||
{"files":{"Cargo.toml":"6d3b3004351e3313ef74472ab6b3f96d59a785b3d873698ca4caf67eb3d47aab","benches/serialization.rs":"d56855d868dab6aa22c8b03a61084535351b76c94b68d8b1d20764e352fe473f","build.rs":"65df9a97c6cdaa3faf72581f04ac289197b0b1797d69d22c1796e957ff1089e2","src/codec.rs":"38408b512d935cd7889a03b25dd14b36083ec4e6d2fcabd636182cf45e3d50bc","src/errors.rs":"67a4a994d0724397657581cde153bdfc05ce86e7efc467f23fafc8f64df80fa4","src/ipccore.rs":"a8fc9e361884ac641b0c6cb4ec812ba5a8c0e625c6a5521b3250926372f0db50","src/lib.rs":"9b107cb52081eeea3fa742d30361db70f7138baa423dfe21d37dcf5087afc338","src/messages.rs":"452362da2cace9a0f2e3134c190ecb6a9997f8be4036cde06643e17c6c238240","src/rpccore.rs":"025b6614f1c42b96b0a8e74fd7881032d338c66e0d67ec0af70f910a9e30ebe1","src/shm.rs":"1d88f19606899e3e477865d6b84bbe3e272f51618a1c2d57b6dab03a4787cde3","src/sys/mod.rs":"e6fa1d260abf093e1f7b50185195e2d3aee0eb8c9774c6f253953b5896d838f3","src/sys/unix/cmsg.rs":"22349b3df39b51b9c414da363313c92d41b02a623753ffcca6f59613e8f79eb2","src/sys/unix/cmsghdr.c":"d7344b3dc15cdce410c68669b848bb81f7fe36362cd3699668cb613fa05180f8","src/sys/unix/mod.rs":"59835f0d5509940078b1820a54f49fc5514adeb3e45e7d21e3ab917431da2e74","src/sys/unix/msg.rs":"0e297d73bae9414184f85c2209cca0a3fde6d999a3f1d3f42faa3f56b6d57233","src/sys/windows/mod.rs":"3441a3212c6d44443a5975621d9594b0c841e5a7f113aa1b108a080330df2b77"},"package":null}
|
|
@ -28,7 +28,7 @@ harness = false
|
|||
bincode = "1.3"
|
||||
byteorder = "1"
|
||||
bytes = "1"
|
||||
crossbeam-channel = "0.5"
|
||||
crossbeam-queue = "0.3"
|
||||
cubeb = "0.10"
|
||||
log = "0.4"
|
||||
scopeguard = "1.1.0"
|
||||
|
|
|
@ -5,6 +5,11 @@
|
|||
|
||||
//! `Encoder`s and `Decoder`s from items to/from `BytesMut` buffers.
|
||||
|
||||
// The assert in LengthDelimitedCodec::decode triggers this clippy warning but
|
||||
// requires upgrading the workspace to Rust 2021 to resolve.
|
||||
// This should be fixed in Rust 1.68, after which the following `allow` can be deleted.
|
||||
#![allow(clippy::uninlined_format_args)]
|
||||
|
||||
use bincode::{self, Options};
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use bytes::{Buf, BufMut, BytesMut};
|
||||
|
|
|
@ -7,12 +7,13 @@ use std::io::{self, Result};
|
|||
use std::sync::{mpsc, Arc};
|
||||
use std::thread;
|
||||
|
||||
use crossbeam_channel::{self, Receiver, Sender};
|
||||
use mio::{event::Event, Events, Interest, Poll, Registry, Token, Waker};
|
||||
use slab::Slab;
|
||||
|
||||
use crate::messages::AssociateHandleForMessage;
|
||||
use crate::rpccore::{make_client, make_server, Client, Handler, Proxy, Server};
|
||||
use crate::rpccore::{
|
||||
make_client, make_server, Client, Handler, Proxy, RequestQueue, RequestQueueSender, Server,
|
||||
};
|
||||
use crate::{
|
||||
codec::Codec,
|
||||
codec::LengthDelimitedCodec,
|
||||
|
@ -54,7 +55,7 @@ enum Request {
|
|||
#[derive(Clone, Debug)]
|
||||
pub struct EventLoopHandle {
|
||||
waker: Arc<Waker>,
|
||||
requests_tx: Sender<Request>,
|
||||
requests: RequestQueueSender<Request>,
|
||||
}
|
||||
|
||||
impl EventLoopHandle {
|
||||
|
@ -101,8 +102,8 @@ impl EventLoopHandle {
|
|||
) -> Result<Token> {
|
||||
assert_not_in_event_loop_thread();
|
||||
let (tx, rx) = mpsc::channel();
|
||||
self.requests_tx
|
||||
.send(Request::AddConnection(connection, driver, tx))
|
||||
self.requests
|
||||
.push(Request::AddConnection(connection, driver, tx))
|
||||
.map_err(|_| {
|
||||
debug!("EventLoopHandle::add_connection send failed");
|
||||
io::ErrorKind::ConnectionAborted
|
||||
|
@ -116,7 +117,7 @@ impl EventLoopHandle {
|
|||
|
||||
// Signal EventLoop to shutdown. Causes EventLoop::poll to return Ok(false).
|
||||
fn shutdown(&self) -> Result<()> {
|
||||
self.requests_tx.send(Request::Shutdown).map_err(|_| {
|
||||
self.requests.push(Request::Shutdown).map_err(|_| {
|
||||
debug!("EventLoopHandle::shutdown send failed");
|
||||
io::ErrorKind::ConnectionAborted
|
||||
})?;
|
||||
|
@ -125,9 +126,8 @@ impl EventLoopHandle {
|
|||
|
||||
// Signal EventLoop to wake connection specified by `token` for processing.
|
||||
pub(crate) fn wake_connection(&self, token: Token) {
|
||||
match self.requests_tx.send(Request::WakeConnection(token)) {
|
||||
Ok(_) => self.waker.wake().expect("wake failed"),
|
||||
Err(e) => debug!("EventLoopHandle::wake_connection failed: {:?}", e),
|
||||
if self.requests.push(Request::WakeConnection(token)).is_ok() {
|
||||
self.waker.wake().expect("wake failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -141,8 +141,7 @@ struct EventLoop {
|
|||
waker: Arc<Waker>,
|
||||
name: String,
|
||||
connections: Slab<Connection>,
|
||||
requests_rx: Receiver<Request>,
|
||||
requests_tx: Sender<Request>,
|
||||
requests: Arc<RequestQueue<Request>>,
|
||||
}
|
||||
|
||||
const EVENT_LOOP_INITIAL_CLIENTS: usize = 64; // Initial client allocation, exceeding this will cause the connection slab to grow.
|
||||
|
@ -152,15 +151,13 @@ impl EventLoop {
|
|||
fn new(name: String) -> Result<EventLoop> {
|
||||
let poll = Poll::new()?;
|
||||
let waker = Arc::new(Waker::new(poll.registry(), WAKE_TOKEN)?);
|
||||
let (tx, rx) = crossbeam_channel::bounded(EVENT_LOOP_INITIAL_CLIENTS);
|
||||
let eventloop = EventLoop {
|
||||
poll,
|
||||
events: Events::with_capacity(EVENT_LOOP_EVENTS_PER_ITERATION),
|
||||
waker,
|
||||
name,
|
||||
connections: Slab::with_capacity(EVENT_LOOP_INITIAL_CLIENTS),
|
||||
requests_rx: rx,
|
||||
requests_tx: tx,
|
||||
requests: Arc::new(RequestQueue::new(EVENT_LOOP_INITIAL_CLIENTS)),
|
||||
};
|
||||
|
||||
Ok(eventloop)
|
||||
|
@ -170,7 +167,7 @@ impl EventLoop {
|
|||
fn handle(&mut self) -> EventLoopHandle {
|
||||
EventLoopHandle {
|
||||
waker: self.waker.clone(),
|
||||
requests_tx: self.requests_tx.clone(),
|
||||
requests: self.requests.new_sender(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -247,7 +244,7 @@ impl EventLoop {
|
|||
}
|
||||
|
||||
// If the waker was signalled there may be pending requests to process.
|
||||
while let Ok(req) = self.requests_rx.try_recv() {
|
||||
while let Some(req) = self.requests.pop() {
|
||||
match req {
|
||||
Request::AddConnection(pipe, driver, tx) => {
|
||||
debug!("{}: EventLoop: handling add_connection", self.name);
|
||||
|
@ -889,9 +886,9 @@ mod test {
|
|||
drop(server);
|
||||
drop(client);
|
||||
|
||||
client_proxy
|
||||
.try_clone()
|
||||
.expect_err("cloning a closed proxy");
|
||||
let clone = client_proxy.clone();
|
||||
let response = clone.call(TestServerMessage::TestRequest);
|
||||
response.expect_err("sending to a dropped ClientHandler");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -3,17 +3,109 @@
|
|||
// This program is made available under an ISC-style license. See the
|
||||
// accompanying file LICENSE for details
|
||||
|
||||
use crossbeam_queue::ArrayQueue;
|
||||
use mio::Token;
|
||||
use std::cell::UnsafeCell;
|
||||
use std::collections::VecDeque;
|
||||
use std::io::{self, Error, ErrorKind, Result};
|
||||
use std::marker::PhantomPinned;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crossbeam_channel::{self, Receiver, Sender};
|
||||
use mio::Token;
|
||||
use slab::Slab;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
use crate::ipccore::EventLoopHandle;
|
||||
|
||||
// This provides a safe-ish method for a thread to allocate
|
||||
// stack storage space for a result, then pass a (wrapped)
|
||||
// pointer to that location to another thread via
|
||||
// a CompletionWriter to eventually store a result into.
|
||||
struct Completion<T> {
|
||||
item: UnsafeCell<Option<T>>,
|
||||
writer: AtomicBool,
|
||||
_pin: PhantomPinned, // disable rustc's no-alias
|
||||
}
|
||||
|
||||
impl<T> Completion<T> {
|
||||
fn new() -> Self {
|
||||
Completion {
|
||||
item: UnsafeCell::new(None),
|
||||
writer: AtomicBool::new(false),
|
||||
_pin: PhantomPinned,
|
||||
}
|
||||
}
|
||||
|
||||
// Wait until the writer completes, then return the result.
|
||||
// This is intended to be a single-use function, once the writer
|
||||
// has completed any further attempts to wait will return None.
|
||||
fn wait(&self) -> Option<T> {
|
||||
// Wait for the writer to complete or be dropped.
|
||||
while self.writer.load(Ordering::Acquire) {
|
||||
std::thread::park();
|
||||
}
|
||||
unsafe { (*self.item.get()).take() }
|
||||
}
|
||||
|
||||
// Create a writer for the other thread to store the
|
||||
// expected result into.
|
||||
fn writer(&self) -> CompletionWriter<T> {
|
||||
assert!(!self.writer.load(Ordering::Relaxed));
|
||||
self.writer.store(true, Ordering::Release);
|
||||
CompletionWriter {
|
||||
ptr: self as *const _ as *mut _,
|
||||
waiter: std::thread::current(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for Completion<T> {
|
||||
fn drop(&mut self) {
|
||||
// Wait for the outstanding writer to complete before
|
||||
// dropping, since the CompletionWriter references
|
||||
// memory owned by this object.
|
||||
while self.writer.load(Ordering::Acquire) {
|
||||
std::thread::park();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CompletionWriter<T> {
|
||||
ptr: *mut Completion<T>, // Points to a Completion on another thread's stack
|
||||
waiter: std::thread::Thread, // Identifies thread waiting for completion
|
||||
}
|
||||
|
||||
impl<T> CompletionWriter<T> {
|
||||
fn set(self, value: T) {
|
||||
// Store the result into the Completion's memory.
|
||||
// Since `set` consumes `self`, rely on `Drop` to
|
||||
// mark the writer as done and wake the Completion's
|
||||
// thread.
|
||||
unsafe {
|
||||
assert!((*self.ptr).writer.load(Ordering::Relaxed));
|
||||
*(*self.ptr).item.get() = Some(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for CompletionWriter<T> {
|
||||
fn drop(&mut self) {
|
||||
// Mark writer as complete - if `set` was not called,
|
||||
// the waiter will receive `None`.
|
||||
unsafe {
|
||||
(*self.ptr).writer.store(false, Ordering::Release);
|
||||
}
|
||||
// Wake the Completion's thread.
|
||||
self.waiter.unpark();
|
||||
}
|
||||
}
|
||||
|
||||
// Safety: CompletionWriter holds a pointer to a Completion
|
||||
// residing on another thread's stack. The Completion always
|
||||
// waits for an outstanding writer if present, and CompletionWriter
|
||||
// releases the waiter and wakes the Completion's thread on drop,
|
||||
// so this pointer will always be live for the duration of a
|
||||
// CompletionWriter.
|
||||
unsafe impl<T> Send for CompletionWriter<T> {}
|
||||
|
||||
// RPC message handler. Implemented by ClientHandler (for Client)
|
||||
// and ServerHandler (for Server).
|
||||
pub(crate) trait Handler {
|
||||
|
@ -45,62 +137,36 @@ pub trait Server {
|
|||
}
|
||||
|
||||
// RPC Client Proxy implementation.
|
||||
type ProxyKey = usize;
|
||||
type ProxyRequest<Request> = (ProxyKey, Request);
|
||||
type ProxyRequest<Request, Response> = (Request, CompletionWriter<Response>);
|
||||
|
||||
// RPC Proxy that may be `clone`d for use by multiple owners/threads.
|
||||
// A Proxy `call` arranges for the supplied request to be transmitted
|
||||
// to the associated Server via RPC and blocks awaiting the response
|
||||
// via `response_rx`.
|
||||
// A Proxy is associated with the ClientHandler via `handler_tx` to send requests,
|
||||
// `response_rx` to receive responses, and uses `key` to identify the Proxy with
|
||||
// the sending side of `response_rx` ClientHandler.
|
||||
// Each Proxy is registered with the ClientHandler on initialization via the
|
||||
// ProxyManager and unregistered when dropped.
|
||||
// via the associated `Completion`.
|
||||
// A ClientHandler normally lives until the last Proxy is dropped, but if the ClientHandler
|
||||
// encounters an internal error, `handler_tx` and `response_tx` will be closed.
|
||||
// encounters an internal error, `requests` will fail to upgrade, allowing
|
||||
// the proxy to report an error.
|
||||
#[derive(Debug)]
|
||||
pub struct Proxy<Request, Response> {
|
||||
handle: Option<(EventLoopHandle, Token)>,
|
||||
key: ProxyKey,
|
||||
response_rx: Receiver<Response>,
|
||||
handler_tx: ManuallyDrop<Sender<ProxyRequest<Request>>>,
|
||||
proxy_mgr: Arc<ProxyManager<Response>>,
|
||||
requests: ManuallyDrop<RequestQueueSender<ProxyRequest<Request, Response>>>,
|
||||
}
|
||||
|
||||
impl<Request, Response> Proxy<Request, Response> {
|
||||
fn new(
|
||||
handler_tx: Sender<ProxyRequest<Request>>,
|
||||
proxy_mgr: Arc<ProxyManager<Response>>,
|
||||
) -> Result<Self> {
|
||||
let (tx, rx) = crossbeam_channel::bounded(1);
|
||||
Ok(Self {
|
||||
fn new(requests: RequestQueueSender<ProxyRequest<Request, Response>>) -> Self {
|
||||
Self {
|
||||
handle: None,
|
||||
key: proxy_mgr.register_proxy(tx)?,
|
||||
response_rx: rx,
|
||||
handler_tx: ManuallyDrop::new(handler_tx),
|
||||
proxy_mgr,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn try_clone(&self) -> Result<Self> {
|
||||
let mut clone = Self::new((*self.handler_tx).clone(), self.proxy_mgr.clone())?;
|
||||
let (handle, token) = self
|
||||
.handle
|
||||
.as_ref()
|
||||
.expect("proxy not connected to event loop");
|
||||
clone.connect_event_loop(handle.clone(), *token);
|
||||
Ok(clone)
|
||||
requests: ManuallyDrop::new(requests),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(&self, request: Request) -> Result<Response> {
|
||||
match self.handler_tx.send((self.key, request)) {
|
||||
Ok(_) => self.wake_connection(),
|
||||
Err(_) => return Err(Error::new(ErrorKind::Other, "proxy send error")),
|
||||
}
|
||||
match self.response_rx.recv() {
|
||||
Ok(resp) => Ok(resp),
|
||||
Err(_) => Err(Error::new(ErrorKind::Other, "proxy recv error")),
|
||||
let response = Completion::new();
|
||||
self.requests.push((request, response.writer()))?;
|
||||
self.wake_connection();
|
||||
match response.wait() {
|
||||
Some(resp) => Ok(resp),
|
||||
None => Err(Error::new(ErrorKind::Other, "proxy recv error")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,16 +183,29 @@ impl<Request, Response> Proxy<Request, Response> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<Request, Response> Clone for Proxy<Request, Response> {
|
||||
fn clone(&self) -> Self {
|
||||
let mut clone = Self::new((*self.requests).clone());
|
||||
let (handle, token) = self
|
||||
.handle
|
||||
.as_ref()
|
||||
.expect("proxy not connected to event loop");
|
||||
clone.connect_event_loop(handle.clone(), *token);
|
||||
clone
|
||||
}
|
||||
}
|
||||
|
||||
impl<Request, Response> Drop for Proxy<Request, Response> {
|
||||
fn drop(&mut self) {
|
||||
trace!("Proxy drop, waking EventLoop");
|
||||
let _ = self.proxy_mgr.unregister_proxy(self.key);
|
||||
// Must drop `handler_tx` before waking the connection, otherwise
|
||||
// the wake may be processed before Sender is closed.
|
||||
// Must drop `requests` before waking the connection, otherwise
|
||||
// the wake may be processed before the (last) weak reference is
|
||||
// dropped.
|
||||
let last_proxy = self.requests.live_proxies();
|
||||
unsafe {
|
||||
ManuallyDrop::drop(&mut self.handler_tx);
|
||||
ManuallyDrop::drop(&mut self.requests);
|
||||
}
|
||||
if self.handle.is_some() {
|
||||
if last_proxy == 1 && self.handle.is_some() {
|
||||
self.wake_connection()
|
||||
}
|
||||
}
|
||||
|
@ -134,79 +213,6 @@ impl<Request, Response> Drop for Proxy<Request, Response> {
|
|||
|
||||
const RPC_CLIENT_INITIAL_PROXIES: usize = 32; // Initial proxy pre-allocation per client.
|
||||
|
||||
// Manage the Sender side of a ClientHandler's Proxies. Each Proxy registers itself with
|
||||
// the manager on initialization.
|
||||
#[derive(Debug)]
|
||||
struct ProxyManager<Response> {
|
||||
proxies: Mutex<Option<Slab<Sender<Response>>>>,
|
||||
}
|
||||
|
||||
impl<Response> ProxyManager<Response> {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
proxies: Mutex::new(Some(Slab::with_capacity(RPC_CLIENT_INITIAL_PROXIES))),
|
||||
}
|
||||
}
|
||||
|
||||
// Register a Proxy's response Sender, returning a unique ID identifying
|
||||
// the Proxy to the ClientHandler.
|
||||
fn register_proxy(&self, tx: Sender<Response>) -> Result<ProxyKey> {
|
||||
let mut proxies = self.proxies.lock().unwrap();
|
||||
match &mut *proxies {
|
||||
Some(proxies) => {
|
||||
let entry = proxies.vacant_entry();
|
||||
let key = entry.key();
|
||||
entry.insert(tx);
|
||||
Ok(key)
|
||||
}
|
||||
None => Err(Error::new(
|
||||
ErrorKind::Other,
|
||||
"register: proxy manager disconnected",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn unregister_proxy(&self, key: ProxyKey) -> Result<()> {
|
||||
let mut proxies = self.proxies.lock().unwrap();
|
||||
match &mut *proxies {
|
||||
Some(proxies) => match proxies.try_remove(key) {
|
||||
Some(_) => Ok(()),
|
||||
None => Err(Error::new(
|
||||
ErrorKind::Other,
|
||||
"unregister: unknown proxy key",
|
||||
)),
|
||||
},
|
||||
None => Err(Error::new(
|
||||
ErrorKind::Other,
|
||||
"unregister: proxy manager disconnected",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
// Deliver ClientHandler's Response to the Proxy associated with `key`.
|
||||
fn deliver(&self, key: ProxyKey, resp: Response) -> Result<()> {
|
||||
let proxies = self.proxies.lock().unwrap();
|
||||
match &*proxies {
|
||||
Some(proxies) => match proxies.get(key) {
|
||||
Some(proxy) => {
|
||||
drop(proxy.send(resp));
|
||||
Ok(())
|
||||
}
|
||||
None => Err(Error::new(ErrorKind::Other, "deliver: unknown proxy key")),
|
||||
},
|
||||
None => Err(Error::new(
|
||||
ErrorKind::Other,
|
||||
"unregister: proxy manager disconnected",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
// ClientHandler disconnected, close Proxy Senders to unblock any waiters.
|
||||
fn disconnect_handler(&self) {
|
||||
*self.proxies.lock().unwrap() = None;
|
||||
}
|
||||
}
|
||||
|
||||
// Client-specific Handler implementation.
|
||||
// The IPC EventLoop Driver calls this to execute client-specific
|
||||
// RPC handling. Serialized messages sent via a Proxy are queued
|
||||
|
@ -215,26 +221,19 @@ impl<Response> ProxyManager<Response> {
|
|||
// trigger response completion by sending the response via a channel
|
||||
// connected to a ProxyResponse.
|
||||
pub(crate) struct ClientHandler<C: Client> {
|
||||
messages: Receiver<ProxyRequest<C::ServerMessage>>,
|
||||
// Proxies hold an Arc<ProxyManager> to register on initialization.
|
||||
// When ClientHandler drops, any Proxies blocked on a response will
|
||||
// error due to their Sender closing.
|
||||
proxies: Arc<ProxyManager<C::ClientMessage>>,
|
||||
in_flight: VecDeque<ProxyKey>,
|
||||
in_flight: VecDeque<CompletionWriter<C::ClientMessage>>,
|
||||
requests: Arc<RequestQueue<ProxyRequest<C::ServerMessage, C::ClientMessage>>>,
|
||||
}
|
||||
|
||||
impl<C: Client> ClientHandler<C> {
|
||||
fn new(rx: Receiver<ProxyRequest<C::ServerMessage>>) -> ClientHandler<C> {
|
||||
fn new(
|
||||
requests: Arc<RequestQueue<ProxyRequest<C::ServerMessage, C::ClientMessage>>>,
|
||||
) -> ClientHandler<C> {
|
||||
ClientHandler::<C> {
|
||||
messages: rx,
|
||||
proxies: Arc::new(ProxyManager::new()),
|
||||
in_flight: VecDeque::with_capacity(RPC_CLIENT_INITIAL_PROXIES),
|
||||
requests,
|
||||
}
|
||||
}
|
||||
|
||||
fn proxy_manager(&self) -> &Arc<ProxyManager<<C as Client>::ClientMessage>> {
|
||||
&self.proxies
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Client> Handler for ClientHandler<C> {
|
||||
|
@ -243,9 +242,8 @@ impl<C: Client> Handler for ClientHandler<C> {
|
|||
|
||||
fn consume(&mut self, response: Self::In) -> Result<()> {
|
||||
trace!("ClientHandler::consume");
|
||||
// `proxy` identifies the waiting Proxy expecting `response`.
|
||||
if let Some(proxy) = self.in_flight.pop_front() {
|
||||
self.proxies.deliver(proxy, response)?;
|
||||
if let Some(response_writer) = self.in_flight.pop_front() {
|
||||
response_writer.set(response);
|
||||
} else {
|
||||
return Err(Error::new(ErrorKind::Other, "request/response mismatch"));
|
||||
}
|
||||
|
@ -256,40 +254,101 @@ impl<C: Client> Handler for ClientHandler<C> {
|
|||
fn produce(&mut self) -> Result<Option<Self::Out>> {
|
||||
trace!("ClientHandler::produce");
|
||||
|
||||
// If the weak count is zero, no proxies are attached and
|
||||
// no further proxies can be attached since every proxy
|
||||
// after the initial one is cloned from an existing instance.
|
||||
self.requests.check_live_proxies()?;
|
||||
// Try to get a new message
|
||||
match self.messages.try_recv() {
|
||||
Ok((proxy, request)) => {
|
||||
match self.requests.pop() {
|
||||
Some((request, response_writer)) => {
|
||||
trace!(" --> received request");
|
||||
self.in_flight.push_back(proxy);
|
||||
self.in_flight.push_back(response_writer);
|
||||
Ok(Some(request))
|
||||
}
|
||||
Err(crossbeam_channel::TryRecvError::Empty) => {
|
||||
None => {
|
||||
trace!(" --> no request");
|
||||
Ok(None)
|
||||
}
|
||||
Err(e) => {
|
||||
trace!(" --> client disconnected");
|
||||
Err(io::Error::new(io::ErrorKind::ConnectionAborted, e))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Client> Drop for ClientHandler<C> {
|
||||
fn drop(&mut self) {
|
||||
self.proxies.disconnect_handler();
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct RequestQueue<T> {
|
||||
queue: ArrayQueue<T>,
|
||||
}
|
||||
|
||||
impl<T> RequestQueue<T> {
|
||||
pub(crate) fn new(size: usize) -> Self {
|
||||
RequestQueue {
|
||||
queue: ArrayQueue::new(size),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn pop(&self) -> Option<T> {
|
||||
self.queue.pop()
|
||||
}
|
||||
|
||||
pub(crate) fn new_sender(self: &Arc<Self>) -> RequestQueueSender<T> {
|
||||
RequestQueueSender {
|
||||
inner: Arc::downgrade(self),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn check_live_proxies(self: &Arc<Self>) -> Result<()> {
|
||||
if Arc::weak_count(self) == 0 {
|
||||
return Err(io::ErrorKind::ConnectionAborted.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct RequestQueueSender<T> {
|
||||
inner: Weak<RequestQueue<T>>,
|
||||
}
|
||||
|
||||
impl<T> RequestQueueSender<T> {
|
||||
pub(crate) fn push(&self, request: T) -> Result<()> {
|
||||
if let Some(consumer) = self.inner.upgrade() {
|
||||
if consumer.queue.push(request).is_err() {
|
||||
debug!("Proxy[{:p}]: call failed - CH::requests full", self);
|
||||
return Err(io::ErrorKind::ConnectionAborted.into());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
debug!("Proxy[{:p}]: call failed - CH::requests dropped", self);
|
||||
Err(Error::new(ErrorKind::Other, "proxy send error"))
|
||||
}
|
||||
|
||||
pub(crate) fn live_proxies(&self) -> usize {
|
||||
Weak::weak_count(&self.inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for RequestQueueSender<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
inner: self.inner.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::fmt::Debug for RequestQueueSender<T> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("RequestQueueProducer")
|
||||
.field("inner", &self.inner.as_ptr())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub(crate) fn make_client<C: Client>(
|
||||
) -> Result<(ClientHandler<C>, Proxy<C::ServerMessage, C::ClientMessage>)> {
|
||||
let (tx, rx) = crossbeam_channel::bounded(RPC_CLIENT_INITIAL_PROXIES);
|
||||
let requests = Arc::new(RequestQueue::new(RPC_CLIENT_INITIAL_PROXIES));
|
||||
let proxy_req = requests.new_sender();
|
||||
let handler = ClientHandler::new(requests);
|
||||
|
||||
let handler = ClientHandler::new(rx);
|
||||
let proxy_mgr = handler.proxy_manager().clone();
|
||||
|
||||
Ok((handler, Proxy::new(tx, proxy_mgr)?))
|
||||
Ok((handler, Proxy::new(proxy_req)))
|
||||
}
|
||||
|
||||
// Server-specific Handler implementation.
|
||||
|
@ -338,3 +397,74 @@ pub(crate) fn make_server<S: Server>(server: S) -> ServerHandler<S> {
|
|||
in_flight: VecDeque::with_capacity(RPC_SERVER_INITIAL_CLIENTS),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let queue = Arc::new(RequestQueue::new(1));
|
||||
let producer = queue.new_sender();
|
||||
assert!(queue.pop().is_none());
|
||||
producer.push(1).unwrap();
|
||||
assert!(queue.pop().is_some());
|
||||
assert!(queue.pop().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queue_dropped() {
|
||||
let queue = Arc::new(RequestQueue::new(1));
|
||||
let producer = queue.new_sender();
|
||||
drop(queue);
|
||||
assert!(producer.push(1).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queue_full() {
|
||||
let queue = Arc::new(RequestQueue::new(1));
|
||||
let producer = queue.new_sender();
|
||||
producer.push(1).unwrap();
|
||||
assert!(producer.push(2).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queue_producer_clone() {
|
||||
let queue = Arc::new(RequestQueue::new(1));
|
||||
let producer = queue.new_sender();
|
||||
let producer2 = producer.clone();
|
||||
producer.push(1).unwrap();
|
||||
assert!(producer2.push(2).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queue_producer_drop() {
|
||||
let queue = Arc::new(RequestQueue::new(1));
|
||||
let producer = queue.new_sender();
|
||||
let producer2 = producer.clone();
|
||||
drop(producer);
|
||||
assert!(producer2.push(2).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queue_producer_weak() {
|
||||
let queue = Arc::new(RequestQueue::new(1));
|
||||
let producer = queue.new_sender();
|
||||
let producer2 = producer.clone();
|
||||
drop(queue);
|
||||
assert!(producer2.push(2).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queue_producer_shutdown() {
|
||||
let queue = Arc::new(RequestQueue::new(1));
|
||||
let producer = queue.new_sender();
|
||||
let producer2 = producer.clone();
|
||||
producer.push(1).unwrap();
|
||||
assert!(Arc::weak_count(&queue) == 2);
|
||||
drop(producer);
|
||||
assert!(Arc::weak_count(&queue) == 1);
|
||||
drop(producer2);
|
||||
assert!(Arc::weak_count(&queue) == 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,6 +71,7 @@ pub fn decode_handles(buf: &mut BytesMut) -> arrayvec::ArrayVec<RawFd, HANDLE_QU
|
|||
}
|
||||
|
||||
let cmsg: &cmsghdr = unsafe { &*(buf.as_ptr() as *const _) };
|
||||
#[allow(clippy::unnecessary_cast)] // `cmsg_len` type is platform-dependent.
|
||||
let cmsg_len = cmsg.cmsg_len as usize;
|
||||
|
||||
match (cmsg.cmsg_level, cmsg.cmsg_type) {
|
||||
|
|
|
@ -51,6 +51,7 @@ pub(crate) fn recv_msg_with_flags(
|
|||
|
||||
let n = cvt_r(|| unsafe { libc::recvmsg(socket, &mut msghdr as *mut _, flags) })?;
|
||||
|
||||
#[allow(clippy::unnecessary_cast)] // `msg_controllen` type is platform-dependent.
|
||||
let controllen = msghdr.msg_controllen as usize;
|
||||
Ok((n, controllen, msghdr.msg_flags))
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ static PIPE_ID: AtomicUsize = AtomicUsize::new(0);
|
|||
fn get_pipe_name() -> String {
|
||||
let pid = std::process::id();
|
||||
let pipe_id = PIPE_ID.fetch_add(1, Ordering::Relaxed);
|
||||
format!("\\\\.\\pipe\\LOCAL\\cubeb-pipe-{}-{}", pid, pipe_id)
|
||||
format!("\\\\.\\pipe\\LOCAL\\cubeb-pipe-{pid}-{pipe_id}")
|
||||
}
|
||||
|
||||
impl Pipe {
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
{"files":{"CHANGELOG.md":"b725a3d6c6503de9db1edc44ee1c947455afe6f9785a68cbaeb803007ac497b7","Cargo.toml":"ac712a130ff21901162c8300c482e90a8286a5f5e50759cf9c15cd2db66918cc","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","LICENSE-THIRD-PARTY":"abb1905b1c2b7db73902e6c328d332d82f3797d7fab61e6fc1c2fc366b132014","README.md":"34a8eb8f4d66515ed50d3775788fd61ef7cfb1e7b0aac3bdb7f7a543827bca1e","src/array_queue.rs":"0006d01beb428d7ef0e836f0fac74a550c3b86719315af4ae5b67d7f3f84322e","src/err.rs":"ac5d5b8a30d9d140347b077c5d9d44fca205cfc413a5d0cd4547d7623d7aca3e","src/lib.rs":"759be18e31442614133ab28bedef37ce1a7f536d955e9cb198a56941cfaf2dbf","src/seg_queue.rs":"3ea1ac7038252b8aeb9b5fb3d3432f83cb26327f876061885ab8144e272ba0cf","tests/array_queue.rs":"e4b3f294f1a62db916035d2ea5db3142c45d98aef2bed5da9074fb7e0f1d82d7","tests/seg_queue.rs":"9d2c4da321a457219c212c6333c8753bcf2516b30cc862c6a527e8f1225733e0"},"package":"7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b"}
|
|
@ -0,0 +1,11 @@
|
|||
# Version 0.1.2
|
||||
|
||||
- Update `crossbeam-utils` to `0.6.5`.
|
||||
|
||||
# Version 0.1.1
|
||||
|
||||
- Update `crossbeam-utils` to `0.6.4`.
|
||||
|
||||
# Version 0.1.0
|
||||
|
||||
- Initial version with `ArrayQueue` and `SegQueue`.
|
|
@ -0,0 +1,28 @@
|
|||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g. crates.io) dependencies
|
||||
#
|
||||
# If you believe there's an error in this file please file an
|
||||
# issue against the rust-lang/cargo repository. If you're
|
||||
# editing this file be aware that the upstream Cargo.toml
|
||||
# will likely look very different (and much more reasonable)
|
||||
|
||||
[package]
|
||||
name = "crossbeam-queue"
|
||||
version = "0.1.2"
|
||||
authors = ["The Crossbeam Project Developers"]
|
||||
description = "Concurrent queues"
|
||||
homepage = "https://github.com/crossbeam-rs/crossbeam/tree/master/crossbeam-utils"
|
||||
documentation = "https://docs.rs/crossbeam-queue"
|
||||
readme = "README.md"
|
||||
keywords = ["queue", "mpmc", "lock-free", "producer", "consumer"]
|
||||
categories = ["concurrency", "data-structures"]
|
||||
license = "MIT/Apache-2.0"
|
||||
repository = "https://github.com/crossbeam-rs/crossbeam"
|
||||
[dependencies.crossbeam-utils]
|
||||
version = "0.6.5"
|
||||
[dev-dependencies.rand]
|
||||
version = "0.6"
|
|
@ -0,0 +1,201 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
|
@ -0,0 +1,23 @@
|
|||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,66 @@
|
|||
# Crossbeam Queue
|
||||
|
||||
[![Build Status](https://travis-ci.org/crossbeam-rs/crossbeam.svg?branch=master)](
|
||||
https://travis-ci.org/crossbeam-rs/crossbeam)
|
||||
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](
|
||||
https://github.com/crossbeam-rs/crossbeam-queue/tree/master/src)
|
||||
[![Cargo](https://img.shields.io/crates/v/crossbeam-queue.svg)](
|
||||
https://crates.io/crates/crossbeam-queue)
|
||||
[![Documentation](https://docs.rs/crossbeam-queue/badge.svg)](
|
||||
https://docs.rs/crossbeam-queue)
|
||||
[![Rust 1.26+](https://img.shields.io/badge/rust-1.26+-lightgray.svg)](
|
||||
https://www.rust-lang.org)
|
||||
|
||||
This crate provides concurrent queues that can be shared among threads:
|
||||
|
||||
* [`ArrayQueue`], a bounded MPMC queue that allocates a fixed-capacity buffer on construction.
|
||||
* [`SegQueue`], an unbounded MPMC queue that allocates small buffers, segments, on demand.
|
||||
|
||||
[`ArrayQueue`]: https://docs.rs/crossbeam-queue/*/crossbeam_queue/struct.ArrayQueue.html
|
||||
[`SegQueue`]: https://docs.rs/crossbeam-queue/*/crossbeam_queue/struct.SegQueue.html
|
||||
|
||||
## Usage
|
||||
|
||||
Add this to your `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
crossbeam-queue = "0.1"
|
||||
```
|
||||
|
||||
Next, add this to your crate:
|
||||
|
||||
```rust
|
||||
extern crate crossbeam_queue;
|
||||
```
|
||||
|
||||
## Compatibility
|
||||
|
||||
The minimum supported Rust version is 1.26.
|
||||
|
||||
## License
|
||||
|
||||
Licensed under either of
|
||||
|
||||
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
||||
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||
|
||||
at your option.
|
||||
|
||||
#### Contribution
|
||||
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
||||
dual licensed as above, without any additional terms or conditions.
|
||||
|
||||
#### Third party software
|
||||
|
||||
This product includes copies and modifications of software developed by third parties:
|
||||
|
||||
* [src/array_queue.rs](src/array_queue.rs) is based on
|
||||
[Bounded MPMC queue](http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue)
|
||||
by Dmitry Vyukov, licensed under the Simplified BSD License and the Apache License, Version 2.0.
|
||||
|
||||
See the source code files for more details.
|
||||
|
||||
Copies of third party licenses can be found in [LICENSE-THIRD-PARTY](LICENSE-THIRD-PARTY).
|
|
@ -0,0 +1,425 @@
|
|||
//! The implementation is based on Dmitry Vyukov's bounded MPMC queue.
|
||||
//!
|
||||
//! Source:
|
||||
//! - http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue
|
||||
//!
|
||||
//! Copyright & License:
|
||||
//! - Copyright (c) 2010-2011 Dmitry Vyukov
|
||||
//! - Simplified BSD License and Apache License, Version 2.0
|
||||
//! - http://www.1024cores.net/home/code-license
|
||||
|
||||
use std::cell::UnsafeCell;
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem;
|
||||
use std::ptr;
|
||||
use std::sync::atomic::{self, AtomicUsize, Ordering};
|
||||
|
||||
use crossbeam_utils::{Backoff, CachePadded};
|
||||
|
||||
use err::{PopError, PushError};
|
||||
|
||||
/// A slot in a queue.
|
||||
struct Slot<T> {
|
||||
/// The current stamp.
|
||||
///
|
||||
/// If the stamp equals the tail, this node will be next written to. If it equals the head,
|
||||
/// this node will be next read from.
|
||||
stamp: AtomicUsize,
|
||||
|
||||
/// The value in this slot.
|
||||
value: UnsafeCell<T>,
|
||||
}
|
||||
|
||||
/// A bounded multi-producer multi-consumer queue.
|
||||
///
|
||||
/// This queue allocates a fixed-capacity buffer on construction, which is used to store pushed
|
||||
/// elements. The queue cannot hold more elements that the buffer allows. Attempting to push an
|
||||
/// element into a full queue will fail. Having a buffer allocated upfront makes this queue a bit
|
||||
/// faster than [`SegQueue`].
|
||||
///
|
||||
/// [`SegQueue`]: struct.SegQueue.html
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PushError};
|
||||
///
|
||||
/// let q = ArrayQueue::new(2);
|
||||
///
|
||||
/// assert_eq!(q.push('a'), Ok(()));
|
||||
/// assert_eq!(q.push('b'), Ok(()));
|
||||
/// assert_eq!(q.push('c'), Err(PushError('c')));
|
||||
/// assert_eq!(q.pop(), Ok('a'));
|
||||
/// ```
|
||||
pub struct ArrayQueue<T> {
|
||||
/// The head of the queue.
|
||||
///
|
||||
/// This value is a "stamp" consisting of an index into the buffer and a lap, but packed into a
|
||||
/// single `usize`. The lower bits represent the index, while the upper bits represent the lap.
|
||||
///
|
||||
/// Elements are popped from the head of the queue.
|
||||
head: CachePadded<AtomicUsize>,
|
||||
|
||||
/// The tail of the queue.
|
||||
///
|
||||
/// This value is a "stamp" consisting of an index into the buffer and a lap, but packed into a
|
||||
/// single `usize`. The lower bits represent the index, while the upper bits represent the lap.
|
||||
///
|
||||
/// Elements are pushed into the tail of the queue.
|
||||
tail: CachePadded<AtomicUsize>,
|
||||
|
||||
/// The buffer holding slots.
|
||||
buffer: *mut Slot<T>,
|
||||
|
||||
/// The queue capacity.
|
||||
cap: usize,
|
||||
|
||||
/// A stamp with the value of `{ lap: 1, index: 0 }`.
|
||||
one_lap: usize,
|
||||
|
||||
/// Indicates that dropping an `ArrayQueue<T>` may drop elements of type `T`.
|
||||
_marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
unsafe impl<T: Send> Sync for ArrayQueue<T> {}
|
||||
unsafe impl<T: Send> Send for ArrayQueue<T> {}
|
||||
|
||||
impl<T> ArrayQueue<T> {
|
||||
/// Creates a new bounded queue with the given capacity.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the capacity is zero.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::ArrayQueue;
|
||||
///
|
||||
/// let q = ArrayQueue::<i32>::new(100);
|
||||
/// ```
|
||||
pub fn new(cap: usize) -> ArrayQueue<T> {
|
||||
assert!(cap > 0, "capacity must be non-zero");
|
||||
|
||||
// Head is initialized to `{ lap: 0, index: 0 }`.
|
||||
// Tail is initialized to `{ lap: 0, index: 0 }`.
|
||||
let head = 0;
|
||||
let tail = 0;
|
||||
|
||||
// Allocate a buffer of `cap` slots.
|
||||
let buffer = {
|
||||
let mut v = Vec::<Slot<T>>::with_capacity(cap);
|
||||
let ptr = v.as_mut_ptr();
|
||||
mem::forget(v);
|
||||
ptr
|
||||
};
|
||||
|
||||
// Initialize stamps in the slots.
|
||||
for i in 0..cap {
|
||||
unsafe {
|
||||
// Set the stamp to `{ lap: 0, index: i }`.
|
||||
let slot = buffer.add(i);
|
||||
ptr::write(&mut (*slot).stamp, AtomicUsize::new(i));
|
||||
}
|
||||
}
|
||||
|
||||
// One lap is the smallest power of two greater than `cap`.
|
||||
let one_lap = (cap + 1).next_power_of_two();
|
||||
|
||||
ArrayQueue {
|
||||
buffer,
|
||||
cap,
|
||||
one_lap,
|
||||
head: CachePadded::new(AtomicUsize::new(head)),
|
||||
tail: CachePadded::new(AtomicUsize::new(tail)),
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to push an element into the queue.
|
||||
///
|
||||
/// If the queue is full, the element is returned back as an error.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PushError};
|
||||
///
|
||||
/// let q = ArrayQueue::new(1);
|
||||
///
|
||||
/// assert_eq!(q.push(10), Ok(()));
|
||||
/// assert_eq!(q.push(20), Err(PushError(20)));
|
||||
/// ```
|
||||
pub fn push(&self, value: T) -> Result<(), PushError<T>> {
|
||||
let backoff = Backoff::new();
|
||||
let mut tail = self.tail.load(Ordering::Relaxed);
|
||||
|
||||
loop {
|
||||
// Deconstruct the tail.
|
||||
let index = tail & (self.one_lap - 1);
|
||||
let lap = tail & !(self.one_lap - 1);
|
||||
|
||||
// Inspect the corresponding slot.
|
||||
let slot = unsafe { &*self.buffer.add(index) };
|
||||
let stamp = slot.stamp.load(Ordering::Acquire);
|
||||
|
||||
// If the tail and the stamp match, we may attempt to push.
|
||||
if tail == stamp {
|
||||
let new_tail = if index + 1 < self.cap {
|
||||
// Same lap, incremented index.
|
||||
// Set to `{ lap: lap, index: index + 1 }`.
|
||||
tail + 1
|
||||
} else {
|
||||
// One lap forward, index wraps around to zero.
|
||||
// Set to `{ lap: lap.wrapping_add(1), index: 0 }`.
|
||||
lap.wrapping_add(self.one_lap)
|
||||
};
|
||||
|
||||
// Try moving the tail.
|
||||
match self
|
||||
.tail
|
||||
.compare_exchange_weak(tail, new_tail, Ordering::SeqCst, Ordering::Relaxed)
|
||||
{
|
||||
Ok(_) => {
|
||||
// Write the value into the slot and update the stamp.
|
||||
unsafe { slot.value.get().write(value); }
|
||||
slot.stamp.store(tail + 1, Ordering::Release);
|
||||
return Ok(());
|
||||
}
|
||||
Err(t) => {
|
||||
tail = t;
|
||||
backoff.spin();
|
||||
}
|
||||
}
|
||||
} else if stamp.wrapping_add(self.one_lap) == tail + 1 {
|
||||
atomic::fence(Ordering::SeqCst);
|
||||
let head = self.head.load(Ordering::Relaxed);
|
||||
|
||||
// If the head lags one lap behind the tail as well...
|
||||
if head.wrapping_add(self.one_lap) == tail {
|
||||
// ...then the queue is full.
|
||||
return Err(PushError(value));
|
||||
}
|
||||
|
||||
backoff.spin();
|
||||
tail = self.tail.load(Ordering::Relaxed);
|
||||
} else {
|
||||
// Snooze because we need to wait for the stamp to get updated.
|
||||
backoff.snooze();
|
||||
tail = self.tail.load(Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to pop an element from the queue.
|
||||
///
|
||||
/// If the queue is empty, an error is returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PopError};
|
||||
///
|
||||
/// let q = ArrayQueue::new(1);
|
||||
/// assert_eq!(q.push(10), Ok(()));
|
||||
///
|
||||
/// assert_eq!(q.pop(), Ok(10));
|
||||
/// assert_eq!(q.pop(), Err(PopError));
|
||||
/// ```
|
||||
pub fn pop(&self) -> Result<T, PopError> {
|
||||
let backoff = Backoff::new();
|
||||
let mut head = self.head.load(Ordering::Relaxed);
|
||||
|
||||
loop {
|
||||
// Deconstruct the head.
|
||||
let index = head & (self.one_lap - 1);
|
||||
let lap = head & !(self.one_lap - 1);
|
||||
|
||||
// Inspect the corresponding slot.
|
||||
let slot = unsafe { &*self.buffer.add(index) };
|
||||
let stamp = slot.stamp.load(Ordering::Acquire);
|
||||
|
||||
// If the the stamp is ahead of the head by 1, we may attempt to pop.
|
||||
if head + 1 == stamp {
|
||||
let new = if index + 1 < self.cap {
|
||||
// Same lap, incremented index.
|
||||
// Set to `{ lap: lap, index: index + 1 }`.
|
||||
head + 1
|
||||
} else {
|
||||
// One lap forward, index wraps around to zero.
|
||||
// Set to `{ lap: lap.wrapping_add(1), index: 0 }`.
|
||||
lap.wrapping_add(self.one_lap)
|
||||
};
|
||||
|
||||
// Try moving the head.
|
||||
match self
|
||||
.head
|
||||
.compare_exchange_weak(head, new, Ordering::SeqCst, Ordering::Relaxed)
|
||||
{
|
||||
Ok(_) => {
|
||||
// Read the value from the slot and update the stamp.
|
||||
let msg = unsafe { slot.value.get().read() };
|
||||
slot.stamp.store(head.wrapping_add(self.one_lap), Ordering::Release);
|
||||
return Ok(msg);
|
||||
}
|
||||
Err(h) => {
|
||||
head = h;
|
||||
backoff.spin();
|
||||
}
|
||||
}
|
||||
} else if stamp == head {
|
||||
atomic::fence(Ordering::SeqCst);
|
||||
let tail = self.tail.load(Ordering::Relaxed);
|
||||
|
||||
// If the tail equals the head, that means the channel is empty.
|
||||
if tail == head {
|
||||
return Err(PopError);
|
||||
}
|
||||
|
||||
backoff.spin();
|
||||
head = self.head.load(Ordering::Relaxed);
|
||||
} else {
|
||||
// Snooze because we need to wait for the stamp to get updated.
|
||||
backoff.snooze();
|
||||
head = self.head.load(Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the capacity of the queue.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PopError};
|
||||
///
|
||||
/// let q = ArrayQueue::<i32>::new(100);
|
||||
///
|
||||
/// assert_eq!(q.capacity(), 100);
|
||||
/// ```
|
||||
pub fn capacity(&self) -> usize {
|
||||
self.cap
|
||||
}
|
||||
|
||||
/// Returns `true` if the queue is empty.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PopError};
|
||||
///
|
||||
/// let q = ArrayQueue::new(100);
|
||||
///
|
||||
/// assert!(q.is_empty());
|
||||
/// q.push(1).unwrap();
|
||||
/// assert!(!q.is_empty());
|
||||
/// ```
|
||||
pub fn is_empty(&self) -> bool {
|
||||
let head = self.head.load(Ordering::SeqCst);
|
||||
let tail = self.tail.load(Ordering::SeqCst);
|
||||
|
||||
// Is the tail lagging one lap behind head?
|
||||
// Is the tail equal to the head?
|
||||
//
|
||||
// Note: If the head changes just before we load the tail, that means there was a moment
|
||||
// when the channel was not empty, so it is safe to just return `false`.
|
||||
tail == head
|
||||
}
|
||||
|
||||
/// Returns `true` if the queue is full.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PopError};
|
||||
///
|
||||
/// let q = ArrayQueue::new(1);
|
||||
///
|
||||
/// assert!(!q.is_full());
|
||||
/// q.push(1).unwrap();
|
||||
/// assert!(q.is_full());
|
||||
/// ```
|
||||
pub fn is_full(&self) -> bool {
|
||||
let tail = self.tail.load(Ordering::SeqCst);
|
||||
let head = self.head.load(Ordering::SeqCst);
|
||||
|
||||
// Is the head lagging one lap behind tail?
|
||||
//
|
||||
// Note: If the tail changes just before we load the head, that means there was a moment
|
||||
// when the queue was not full, so it is safe to just return `false`.
|
||||
head.wrapping_add(self.one_lap) == tail
|
||||
}
|
||||
|
||||
/// Returns the number of elements in the queue.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PopError};
|
||||
///
|
||||
/// let q = ArrayQueue::new(100);
|
||||
/// assert_eq!(q.len(), 0);
|
||||
///
|
||||
/// q.push(10).unwrap();
|
||||
/// assert_eq!(q.len(), 1);
|
||||
///
|
||||
/// q.push(20).unwrap();
|
||||
/// assert_eq!(q.len(), 2);
|
||||
/// ```
|
||||
pub fn len(&self) -> usize {
|
||||
loop {
|
||||
// Load the tail, then load the head.
|
||||
let tail = self.tail.load(Ordering::SeqCst);
|
||||
let head = self.head.load(Ordering::SeqCst);
|
||||
|
||||
// If the tail didn't change, we've got consistent values to work with.
|
||||
if self.tail.load(Ordering::SeqCst) == tail {
|
||||
let hix = head & (self.one_lap - 1);
|
||||
let tix = tail & (self.one_lap - 1);
|
||||
|
||||
return if hix < tix {
|
||||
tix - hix
|
||||
} else if hix > tix {
|
||||
self.cap - hix + tix
|
||||
} else if tail == head {
|
||||
0
|
||||
} else {
|
||||
self.cap
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for ArrayQueue<T> {
|
||||
fn drop(&mut self) {
|
||||
// Get the index of the head.
|
||||
let hix = self.head.load(Ordering::Relaxed) & (self.one_lap - 1);
|
||||
|
||||
// Loop over all slots that hold a message and drop them.
|
||||
for i in 0..self.len() {
|
||||
// Compute the index of the next slot holding a message.
|
||||
let index = if hix + i < self.cap {
|
||||
hix + i
|
||||
} else {
|
||||
hix + i - self.cap
|
||||
};
|
||||
|
||||
unsafe {
|
||||
self.buffer.add(index).drop_in_place();
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, deallocate the buffer, but don't run any destructors.
|
||||
unsafe {
|
||||
Vec::from_raw_parts(self.buffer, 0, self.cap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for ArrayQueue<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.pad("ArrayQueue { .. }")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
//! Concurrent queues.
|
||||
//!
|
||||
//! This crate provides concurrent queues that can be shared among threads:
|
||||
//!
|
||||
//! * [`ArrayQueue`], a bounded MPMC queue that allocates a fixed-capacity buffer on construction.
|
||||
//! * [`SegQueue`], an unbounded MPMC queue that allocates small buffers, segments, on demand.
|
||||
//!
|
||||
//! [`ArrayQueue`]: struct.ArrayQueue.html
|
||||
//! [`SegQueue`]: struct.SegQueue.html
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![warn(missing_debug_implementations)]
|
||||
|
||||
extern crate crossbeam_utils;
|
||||
|
||||
mod array_queue;
|
||||
mod err;
|
||||
mod seg_queue;
|
||||
|
||||
pub use self::array_queue::ArrayQueue;
|
||||
pub use self::seg_queue::SegQueue;
|
||||
pub use self::err::{PopError, PushError};
|
|
@ -0,0 +1,481 @@
|
|||
use std::cell::UnsafeCell;
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::{self, ManuallyDrop};
|
||||
use std::ptr;
|
||||
use std::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
||||
|
||||
use crossbeam_utils::{Backoff, CachePadded};
|
||||
|
||||
use err::PopError;
|
||||
|
||||
// Bits indicating the state of a slot:
|
||||
// * If a value has been written into the slot, `WRITE` is set.
|
||||
// * If a value has been read from the slot, `READ` is set.
|
||||
// * If the block is being destroyed, `DESTROY` is set.
|
||||
const WRITE: usize = 1;
|
||||
const READ: usize = 2;
|
||||
const DESTROY: usize = 4;
|
||||
|
||||
// Each block covers one "lap" of indices.
|
||||
const LAP: usize = 32;
|
||||
// The maximum number of values a block can hold.
|
||||
const BLOCK_CAP: usize = LAP - 1;
|
||||
// How many lower bits are reserved for metadata.
|
||||
const SHIFT: usize = 1;
|
||||
// Indicates that the block is not the last one.
|
||||
const HAS_NEXT: usize = 1;
|
||||
|
||||
/// A slot in a block.
|
||||
struct Slot<T> {
|
||||
/// The value.
|
||||
value: UnsafeCell<ManuallyDrop<T>>,
|
||||
|
||||
/// The state of the slot.
|
||||
state: AtomicUsize,
|
||||
}
|
||||
|
||||
impl<T> Slot<T> {
|
||||
/// Waits until a value is written into the slot.
|
||||
fn wait_write(&self) {
|
||||
let backoff = Backoff::new();
|
||||
while self.state.load(Ordering::Acquire) & WRITE == 0 {
|
||||
backoff.snooze();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A block in a linked list.
|
||||
///
|
||||
/// Each block in the list can hold up to `BLOCK_CAP` values.
|
||||
struct Block<T> {
|
||||
/// The next block in the linked list.
|
||||
next: AtomicPtr<Block<T>>,
|
||||
|
||||
/// Slots for values.
|
||||
slots: [Slot<T>; BLOCK_CAP],
|
||||
}
|
||||
|
||||
impl<T> Block<T> {
|
||||
/// Creates an empty block that starts at `start_index`.
|
||||
fn new() -> Block<T> {
|
||||
unsafe { mem::zeroed() }
|
||||
}
|
||||
|
||||
/// Waits until the next pointer is set.
|
||||
fn wait_next(&self) -> *mut Block<T> {
|
||||
let backoff = Backoff::new();
|
||||
loop {
|
||||
let next = self.next.load(Ordering::Acquire);
|
||||
if !next.is_null() {
|
||||
return next;
|
||||
}
|
||||
backoff.snooze();
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the `DESTROY` bit in slots starting from `start` and destroys the block.
|
||||
unsafe fn destroy(this: *mut Block<T>, start: usize) {
|
||||
// It is not necessary to set the `DESTROY` bit in the last slot because that slot has
|
||||
// begun destruction of the block.
|
||||
for i in start..BLOCK_CAP - 1 {
|
||||
let slot = (*this).slots.get_unchecked(i);
|
||||
|
||||
// Mark the `DESTROY` bit if a thread is still using the slot.
|
||||
if slot.state.load(Ordering::Acquire) & READ == 0
|
||||
&& slot.state.fetch_or(DESTROY, Ordering::AcqRel) & READ == 0
|
||||
{
|
||||
// If a thread is still using the slot, it will continue destruction of the block.
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// No thread is using the block, now it is safe to destroy it.
|
||||
drop(Box::from_raw(this));
|
||||
}
|
||||
}
|
||||
|
||||
/// A position in a queue.
|
||||
struct Position<T> {
|
||||
/// The index in the queue.
|
||||
index: AtomicUsize,
|
||||
|
||||
/// The block in the linked list.
|
||||
block: AtomicPtr<Block<T>>,
|
||||
}
|
||||
|
||||
/// An unbounded multi-producer multi-consumer queue.
|
||||
///
|
||||
/// This queue is implemented as a linked list of segments, where each segment is a small buffer
|
||||
/// that can hold a handful of elements. There is no limit to how many elements can be in the queue
|
||||
/// at a time. However, since segments need to be dynamically allocated as elements get pushed,
|
||||
/// this queue is somewhat slower than [`ArrayQueue`].
|
||||
///
|
||||
/// [`ArrayQueue`]: struct.ArrayQueue.html
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{PopError, SegQueue};
|
||||
///
|
||||
/// let q = SegQueue::new();
|
||||
///
|
||||
/// q.push('a');
|
||||
/// q.push('b');
|
||||
///
|
||||
/// assert_eq!(q.pop(), Ok('a'));
|
||||
/// assert_eq!(q.pop(), Ok('b'));
|
||||
/// assert_eq!(q.pop(), Err(PopError));
|
||||
/// ```
|
||||
pub struct SegQueue<T> {
|
||||
/// The head of the queue.
|
||||
head: CachePadded<Position<T>>,
|
||||
|
||||
/// The tail of the queue.
|
||||
tail: CachePadded<Position<T>>,
|
||||
|
||||
/// Indicates that dropping a `SegQueue<T>` may drop values of type `T`.
|
||||
_marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
unsafe impl<T: Send> Send for SegQueue<T> {}
|
||||
unsafe impl<T: Send> Sync for SegQueue<T> {}
|
||||
|
||||
impl<T> SegQueue<T> {
|
||||
/// Creates a new unbounded queue.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::SegQueue;
|
||||
///
|
||||
/// let q = SegQueue::<i32>::new();
|
||||
/// ```
|
||||
pub fn new() -> SegQueue<T> {
|
||||
SegQueue {
|
||||
head: CachePadded::new(Position {
|
||||
block: AtomicPtr::new(ptr::null_mut()),
|
||||
index: AtomicUsize::new(0),
|
||||
}),
|
||||
tail: CachePadded::new(Position {
|
||||
block: AtomicPtr::new(ptr::null_mut()),
|
||||
index: AtomicUsize::new(0),
|
||||
}),
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Pushes an element into the queue.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::SegQueue;
|
||||
///
|
||||
/// let q = SegQueue::new();
|
||||
///
|
||||
/// q.push(10);
|
||||
/// q.push(20);
|
||||
/// ```
|
||||
pub fn push(&self, value: T) {
|
||||
let backoff = Backoff::new();
|
||||
let mut tail = self.tail.index.load(Ordering::Acquire);
|
||||
let mut block = self.tail.block.load(Ordering::Acquire);
|
||||
let mut next_block = None;
|
||||
|
||||
loop {
|
||||
// Calculate the offset of the index into the block.
|
||||
let offset = (tail >> SHIFT) % LAP;
|
||||
|
||||
// If we reached the end of the block, wait until the next one is installed.
|
||||
if offset == BLOCK_CAP {
|
||||
backoff.snooze();
|
||||
tail = self.tail.index.load(Ordering::Acquire);
|
||||
block = self.tail.block.load(Ordering::Acquire);
|
||||
continue;
|
||||
}
|
||||
|
||||
// If we're going to have to install the next block, allocate it in advance in order to
|
||||
// make the wait for other threads as short as possible.
|
||||
if offset + 1 == BLOCK_CAP && next_block.is_none() {
|
||||
next_block = Some(Box::new(Block::<T>::new()));
|
||||
}
|
||||
|
||||
// If this is the first push operation, we need to allocate the first block.
|
||||
if block.is_null() {
|
||||
let new = Box::into_raw(Box::new(Block::<T>::new()));
|
||||
|
||||
if self.tail.block.compare_and_swap(block, new, Ordering::Release) == block {
|
||||
self.head.block.store(new, Ordering::Release);
|
||||
block = new;
|
||||
} else {
|
||||
next_block = unsafe { Some(Box::from_raw(new)) };
|
||||
tail = self.tail.index.load(Ordering::Acquire);
|
||||
block = self.tail.block.load(Ordering::Acquire);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let new_tail = tail + (1 << SHIFT);
|
||||
|
||||
// Try advancing the tail forward.
|
||||
match self.tail.index
|
||||
.compare_exchange_weak(
|
||||
tail,
|
||||
new_tail,
|
||||
Ordering::SeqCst,
|
||||
Ordering::Acquire,
|
||||
)
|
||||
{
|
||||
Ok(_) => unsafe {
|
||||
// If we've reached the end of the block, install the next one.
|
||||
if offset + 1 == BLOCK_CAP {
|
||||
let next_block = Box::into_raw(next_block.unwrap());
|
||||
let next_index = new_tail.wrapping_add(1 << SHIFT);
|
||||
|
||||
self.tail.block.store(next_block, Ordering::Release);
|
||||
self.tail.index.store(next_index, Ordering::Release);
|
||||
(*block).next.store(next_block, Ordering::Release);
|
||||
}
|
||||
|
||||
// Write the value into the slot.
|
||||
let slot = (*block).slots.get_unchecked(offset);
|
||||
slot.value.get().write(ManuallyDrop::new(value));
|
||||
slot.state.fetch_or(WRITE, Ordering::Release);
|
||||
|
||||
return;
|
||||
}
|
||||
Err(t) => {
|
||||
tail = t;
|
||||
block = self.tail.block.load(Ordering::Acquire);
|
||||
backoff.spin();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Pops an element from the queue.
|
||||
///
|
||||
/// If the queue is empty, an error is returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{PopError, SegQueue};
|
||||
///
|
||||
/// let q = SegQueue::new();
|
||||
///
|
||||
/// q.push(10);
|
||||
/// assert_eq!(q.pop(), Ok(10));
|
||||
/// assert_eq!(q.pop(), Err(PopError));
|
||||
/// ```
|
||||
pub fn pop(&self) -> Result<T, PopError> {
|
||||
let backoff = Backoff::new();
|
||||
let mut head = self.head.index.load(Ordering::Acquire);
|
||||
let mut block = self.head.block.load(Ordering::Acquire);
|
||||
|
||||
loop {
|
||||
// Calculate the offset of the index into the block.
|
||||
let offset = (head >> SHIFT) % LAP;
|
||||
|
||||
// If we reached the end of the block, wait until the next one is installed.
|
||||
if offset == BLOCK_CAP {
|
||||
backoff.snooze();
|
||||
head = self.head.index.load(Ordering::Acquire);
|
||||
block = self.head.block.load(Ordering::Acquire);
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut new_head = head + (1 << SHIFT);
|
||||
|
||||
if new_head & HAS_NEXT == 0 {
|
||||
atomic::fence(Ordering::SeqCst);
|
||||
let tail = self.tail.index.load(Ordering::Relaxed);
|
||||
|
||||
// If the tail equals the head, that means the queue is empty.
|
||||
if head >> SHIFT == tail >> SHIFT {
|
||||
return Err(PopError);
|
||||
}
|
||||
|
||||
// If head and tail are not in the same block, set `HAS_NEXT` in head.
|
||||
if (head >> SHIFT) / LAP != (tail >> SHIFT) / LAP {
|
||||
new_head |= HAS_NEXT;
|
||||
}
|
||||
}
|
||||
|
||||
// The block can be null here only if the first push operation is in progress. In that
|
||||
// case, just wait until it gets initialized.
|
||||
if block.is_null() {
|
||||
backoff.snooze();
|
||||
head = self.head.index.load(Ordering::Acquire);
|
||||
block = self.head.block.load(Ordering::Acquire);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Try moving the head index forward.
|
||||
match self.head.index
|
||||
.compare_exchange_weak(
|
||||
head,
|
||||
new_head,
|
||||
Ordering::SeqCst,
|
||||
Ordering::Acquire,
|
||||
)
|
||||
{
|
||||
Ok(_) => unsafe {
|
||||
// If we've reached the end of the block, move to the next one.
|
||||
if offset + 1 == BLOCK_CAP {
|
||||
let next = (*block).wait_next();
|
||||
let mut next_index = (new_head & !HAS_NEXT).wrapping_add(1 << SHIFT);
|
||||
if !(*next).next.load(Ordering::Relaxed).is_null() {
|
||||
next_index |= HAS_NEXT;
|
||||
}
|
||||
|
||||
self.head.block.store(next, Ordering::Release);
|
||||
self.head.index.store(next_index, Ordering::Release);
|
||||
}
|
||||
|
||||
// Read the value.
|
||||
let slot = (*block).slots.get_unchecked(offset);
|
||||
slot.wait_write();
|
||||
let m = slot.value.get().read();
|
||||
let value = ManuallyDrop::into_inner(m);
|
||||
|
||||
// Destroy the block if we've reached the end, or if another thread wanted to
|
||||
// destroy but couldn't because we were busy reading from the slot.
|
||||
if offset + 1 == BLOCK_CAP {
|
||||
Block::destroy(block, 0);
|
||||
} else if slot.state.fetch_or(READ, Ordering::AcqRel) & DESTROY != 0 {
|
||||
Block::destroy(block, offset + 1);
|
||||
}
|
||||
|
||||
return Ok(value);
|
||||
}
|
||||
Err(h) => {
|
||||
head = h;
|
||||
block = self.head.block.load(Ordering::Acquire);
|
||||
backoff.spin();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the queue is empty.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::SegQueue;
|
||||
///
|
||||
/// let q = SegQueue::new();
|
||||
///
|
||||
/// assert!(q.is_empty());
|
||||
/// q.push(1);
|
||||
/// assert!(!q.is_empty());
|
||||
/// ```
|
||||
pub fn is_empty(&self) -> bool {
|
||||
let head = self.head.index.load(Ordering::SeqCst);
|
||||
let tail = self.tail.index.load(Ordering::SeqCst);
|
||||
head >> SHIFT == tail >> SHIFT
|
||||
}
|
||||
|
||||
/// Returns the number of elements in the queue.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{SegQueue, PopError};
|
||||
///
|
||||
/// let q = SegQueue::new();
|
||||
/// assert_eq!(q.len(), 0);
|
||||
///
|
||||
/// q.push(10);
|
||||
/// assert_eq!(q.len(), 1);
|
||||
///
|
||||
/// q.push(20);
|
||||
/// assert_eq!(q.len(), 2);
|
||||
/// ```
|
||||
pub fn len(&self) -> usize {
|
||||
loop {
|
||||
// Load the tail index, then load the head index.
|
||||
let mut tail = self.tail.index.load(Ordering::SeqCst);
|
||||
let mut head = self.head.index.load(Ordering::SeqCst);
|
||||
|
||||
// If the tail index didn't change, we've got consistent indices to work with.
|
||||
if self.tail.index.load(Ordering::SeqCst) == tail {
|
||||
// Erase the lower bits.
|
||||
tail &= !((1 << SHIFT) - 1);
|
||||
head &= !((1 << SHIFT) - 1);
|
||||
|
||||
// Rotate indices so that head falls into the first block.
|
||||
let lap = (head >> SHIFT) / LAP;
|
||||
tail = tail.wrapping_sub((lap * LAP) << SHIFT);
|
||||
head = head.wrapping_sub((lap * LAP) << SHIFT);
|
||||
|
||||
// Remove the lower bits.
|
||||
tail >>= SHIFT;
|
||||
head >>= SHIFT;
|
||||
|
||||
// Fix up indices if they fall onto block ends.
|
||||
if head == BLOCK_CAP {
|
||||
head = 0;
|
||||
tail -= LAP;
|
||||
}
|
||||
if tail == BLOCK_CAP {
|
||||
tail += 1;
|
||||
}
|
||||
|
||||
// Return the difference minus the number of blocks between tail and head.
|
||||
return tail - head - tail / LAP;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for SegQueue<T> {
|
||||
fn drop(&mut self) {
|
||||
let mut head = self.head.index.load(Ordering::Relaxed);
|
||||
let mut tail = self.tail.index.load(Ordering::Relaxed);
|
||||
let mut block = self.head.block.load(Ordering::Relaxed);
|
||||
|
||||
// Erase the lower bits.
|
||||
head &= !((1 << SHIFT) - 1);
|
||||
tail &= !((1 << SHIFT) - 1);
|
||||
|
||||
unsafe {
|
||||
// Drop all values between `head` and `tail` and deallocate the heap-allocated blocks.
|
||||
while head != tail {
|
||||
let offset = (head >> SHIFT) % LAP;
|
||||
|
||||
if offset < BLOCK_CAP {
|
||||
// Drop the value in the slot.
|
||||
let slot = (*block).slots.get_unchecked(offset);
|
||||
ManuallyDrop::drop(&mut *(*slot).value.get());
|
||||
} else {
|
||||
// Deallocate the block and move to the next one.
|
||||
let next = (*block).next.load(Ordering::Relaxed);
|
||||
drop(Box::from_raw(block));
|
||||
block = next;
|
||||
}
|
||||
|
||||
head = head.wrapping_add(1 << SHIFT);
|
||||
}
|
||||
|
||||
// Deallocate the last remaining block.
|
||||
if !block.is_null() {
|
||||
drop(Box::from_raw(block));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for SegQueue<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.pad("SegQueue { .. }")
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for SegQueue<T> {
|
||||
fn default() -> SegQueue<T> {
|
||||
SegQueue::new()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,249 @@
|
|||
extern crate crossbeam_queue;
|
||||
extern crate crossbeam_utils;
|
||||
extern crate rand;
|
||||
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use crossbeam_queue::ArrayQueue;
|
||||
use crossbeam_utils::thread::scope;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
#[test]
|
||||
fn smoke() {
|
||||
let q = ArrayQueue::new(1);
|
||||
|
||||
q.push(7).unwrap();
|
||||
assert_eq!(q.pop(), Ok(7));
|
||||
|
||||
q.push(8).unwrap();
|
||||
assert_eq!(q.pop(), Ok(8));
|
||||
assert!(q.pop().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capacity() {
|
||||
for i in 1..10 {
|
||||
let q = ArrayQueue::<i32>::new(i);
|
||||
assert_eq!(q.capacity(), i);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "capacity must be non-zero")]
|
||||
fn zero_capacity() {
|
||||
let _ = ArrayQueue::<i32>::new(0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn len_empty_full() {
|
||||
let q = ArrayQueue::new(2);
|
||||
|
||||
assert_eq!(q.len(), 0);
|
||||
assert_eq!(q.is_empty(), true);
|
||||
assert_eq!(q.is_full(), false);
|
||||
|
||||
q.push(()).unwrap();
|
||||
|
||||
assert_eq!(q.len(), 1);
|
||||
assert_eq!(q.is_empty(), false);
|
||||
assert_eq!(q.is_full(), false);
|
||||
|
||||
q.push(()).unwrap();
|
||||
|
||||
assert_eq!(q.len(), 2);
|
||||
assert_eq!(q.is_empty(), false);
|
||||
assert_eq!(q.is_full(), true);
|
||||
|
||||
q.pop().unwrap();
|
||||
|
||||
assert_eq!(q.len(), 1);
|
||||
assert_eq!(q.is_empty(), false);
|
||||
assert_eq!(q.is_full(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn len() {
|
||||
const COUNT: usize = 25_000;
|
||||
const CAP: usize = 1000;
|
||||
|
||||
let q = ArrayQueue::new(CAP);
|
||||
assert_eq!(q.len(), 0);
|
||||
|
||||
for _ in 0..CAP / 10 {
|
||||
for i in 0..50 {
|
||||
q.push(i).unwrap();
|
||||
assert_eq!(q.len(), i + 1);
|
||||
}
|
||||
|
||||
for i in 0..50 {
|
||||
q.pop().unwrap();
|
||||
assert_eq!(q.len(), 50 - i - 1);
|
||||
}
|
||||
}
|
||||
assert_eq!(q.len(), 0);
|
||||
|
||||
for i in 0..CAP {
|
||||
q.push(i).unwrap();
|
||||
assert_eq!(q.len(), i + 1);
|
||||
}
|
||||
|
||||
for _ in 0..CAP {
|
||||
q.pop().unwrap();
|
||||
}
|
||||
assert_eq!(q.len(), 0);
|
||||
|
||||
scope(|scope| {
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
loop {
|
||||
if let Ok(x) = q.pop() {
|
||||
assert_eq!(x, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
let len = q.len();
|
||||
assert!(len <= CAP);
|
||||
}
|
||||
});
|
||||
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
while q.push(i).is_err() {}
|
||||
let len = q.len();
|
||||
assert!(len <= CAP);
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
assert_eq!(q.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spsc() {
|
||||
const COUNT: usize = 100_000;
|
||||
|
||||
let q = ArrayQueue::new(3);
|
||||
|
||||
scope(|scope| {
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
loop {
|
||||
if let Ok(x) = q.pop() {
|
||||
assert_eq!(x, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(q.pop().is_err());
|
||||
});
|
||||
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
while q.push(i).is_err() {}
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mpmc() {
|
||||
const COUNT: usize = 25_000;
|
||||
const THREADS: usize = 4;
|
||||
|
||||
let q = ArrayQueue::<usize>::new(3);
|
||||
let v = (0..COUNT).map(|_| AtomicUsize::new(0)).collect::<Vec<_>>();
|
||||
|
||||
scope(|scope| {
|
||||
for _ in 0..THREADS {
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..COUNT {
|
||||
let n = loop {
|
||||
if let Ok(x) = q.pop() {
|
||||
break x;
|
||||
}
|
||||
};
|
||||
v[n].fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
});
|
||||
}
|
||||
for _ in 0..THREADS {
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
while q.push(i).is_err() {}
|
||||
}
|
||||
});
|
||||
}
|
||||
}).unwrap();
|
||||
|
||||
for c in v {
|
||||
assert_eq!(c.load(Ordering::SeqCst), THREADS);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drops() {
|
||||
const RUNS: usize = 100;
|
||||
|
||||
static DROPS: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct DropCounter;
|
||||
|
||||
impl Drop for DropCounter {
|
||||
fn drop(&mut self) {
|
||||
DROPS.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
let mut rng = thread_rng();
|
||||
|
||||
for _ in 0..RUNS {
|
||||
let steps = rng.gen_range(0, 10_000);
|
||||
let additional = rng.gen_range(0, 50);
|
||||
|
||||
DROPS.store(0, Ordering::SeqCst);
|
||||
let q = ArrayQueue::new(50);
|
||||
|
||||
scope(|scope| {
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..steps {
|
||||
while q.pop().is_err() {}
|
||||
}
|
||||
});
|
||||
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..steps {
|
||||
while q.push(DropCounter).is_err() {
|
||||
DROPS.fetch_sub(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
|
||||
for _ in 0..additional {
|
||||
q.push(DropCounter).unwrap();
|
||||
}
|
||||
|
||||
assert_eq!(DROPS.load(Ordering::SeqCst), steps);
|
||||
drop(q);
|
||||
assert_eq!(DROPS.load(Ordering::SeqCst), steps + additional);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linearizable() {
|
||||
const COUNT: usize = 25_000;
|
||||
const THREADS: usize = 4;
|
||||
|
||||
let q = ArrayQueue::new(THREADS);
|
||||
|
||||
scope(|scope| {
|
||||
for _ in 0..THREADS {
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..COUNT {
|
||||
while q.push(0).is_err() {}
|
||||
q.pop().unwrap();
|
||||
}
|
||||
});
|
||||
}
|
||||
}).unwrap();
|
||||
}
|
|
@ -0,0 +1,164 @@
|
|||
extern crate crossbeam_queue;
|
||||
extern crate crossbeam_utils;
|
||||
extern crate rand;
|
||||
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use crossbeam_queue::SegQueue;
|
||||
use crossbeam_utils::thread::scope;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
#[test]
|
||||
fn smoke() {
|
||||
let q = SegQueue::new();
|
||||
q.push(7);
|
||||
assert_eq!(q.pop(), Ok(7));
|
||||
|
||||
q.push(8);
|
||||
assert_eq!(q.pop(), Ok(8));
|
||||
assert!(q.pop().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn len_empty_full() {
|
||||
let q = SegQueue::new();
|
||||
|
||||
assert_eq!(q.len(), 0);
|
||||
assert_eq!(q.is_empty(), true);
|
||||
|
||||
q.push(());
|
||||
|
||||
assert_eq!(q.len(), 1);
|
||||
assert_eq!(q.is_empty(), false);
|
||||
|
||||
q.pop().unwrap();
|
||||
|
||||
assert_eq!(q.len(), 0);
|
||||
assert_eq!(q.is_empty(), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn len() {
|
||||
let q = SegQueue::new();
|
||||
|
||||
assert_eq!(q.len(), 0);
|
||||
|
||||
for i in 0..50 {
|
||||
q.push(i);
|
||||
assert_eq!(q.len(), i + 1);
|
||||
}
|
||||
|
||||
for i in 0..50 {
|
||||
q.pop().unwrap();
|
||||
assert_eq!(q.len(), 50 - i - 1);
|
||||
}
|
||||
|
||||
assert_eq!(q.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spsc() {
|
||||
const COUNT: usize = 100_000;
|
||||
|
||||
let q = SegQueue::new();
|
||||
|
||||
scope(|scope| {
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
loop {
|
||||
if let Ok(x) = q.pop() {
|
||||
assert_eq!(x, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(q.pop().is_err());
|
||||
});
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
q.push(i);
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mpmc() {
|
||||
const COUNT: usize = 25_000;
|
||||
const THREADS: usize = 4;
|
||||
|
||||
let q = SegQueue::<usize>::new();
|
||||
let v = (0..COUNT).map(|_| AtomicUsize::new(0)).collect::<Vec<_>>();
|
||||
|
||||
scope(|scope| {
|
||||
for _ in 0..THREADS {
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..COUNT {
|
||||
let n = loop {
|
||||
if let Ok(x) = q.pop() {
|
||||
break x;
|
||||
}
|
||||
};
|
||||
v[n].fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
});
|
||||
}
|
||||
for _ in 0..THREADS {
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
q.push(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
}).unwrap();
|
||||
|
||||
for c in v {
|
||||
assert_eq!(c.load(Ordering::SeqCst), THREADS);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drops() {
|
||||
static DROPS: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct DropCounter;
|
||||
|
||||
impl Drop for DropCounter {
|
||||
fn drop(&mut self) {
|
||||
DROPS.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
let mut rng = thread_rng();
|
||||
|
||||
for _ in 0..100 {
|
||||
let steps = rng.gen_range(0, 10_000);
|
||||
let additional = rng.gen_range(0, 1000);
|
||||
|
||||
DROPS.store(0, Ordering::SeqCst);
|
||||
let q = SegQueue::new();
|
||||
|
||||
scope(|scope| {
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..steps {
|
||||
while q.pop().is_err() {}
|
||||
}
|
||||
});
|
||||
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..steps {
|
||||
q.push(DropCounter);
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
|
||||
for _ in 0..additional {
|
||||
q.push(DropCounter);
|
||||
}
|
||||
|
||||
assert_eq!(DROPS.load(Ordering::SeqCst), steps);
|
||||
drop(q);
|
||||
assert_eq!(DROPS.load(Ordering::SeqCst), steps + additional);
|
||||
}
|
||||
}
|
|
@ -1 +1 @@
|
|||
{"files":{"CHANGELOG.md":"b725a3d6c6503de9db1edc44ee1c947455afe6f9785a68cbaeb803007ac497b7","Cargo.toml":"ac712a130ff21901162c8300c482e90a8286a5f5e50759cf9c15cd2db66918cc","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","LICENSE-THIRD-PARTY":"abb1905b1c2b7db73902e6c328d332d82f3797d7fab61e6fc1c2fc366b132014","README.md":"34a8eb8f4d66515ed50d3775788fd61ef7cfb1e7b0aac3bdb7f7a543827bca1e","src/array_queue.rs":"0006d01beb428d7ef0e836f0fac74a550c3b86719315af4ae5b67d7f3f84322e","src/err.rs":"ac5d5b8a30d9d140347b077c5d9d44fca205cfc413a5d0cd4547d7623d7aca3e","src/lib.rs":"759be18e31442614133ab28bedef37ce1a7f536d955e9cb198a56941cfaf2dbf","src/seg_queue.rs":"3ea1ac7038252b8aeb9b5fb3d3432f83cb26327f876061885ab8144e272ba0cf","tests/array_queue.rs":"e4b3f294f1a62db916035d2ea5db3142c45d98aef2bed5da9074fb7e0f1d82d7","tests/seg_queue.rs":"9d2c4da321a457219c212c6333c8753bcf2516b30cc862c6a527e8f1225733e0"},"package":"7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b"}
|
||||
{"files":{"CHANGELOG.md":"2821f0a64845f2a8046b8c78c40c31f5d67c6b89ab94ea56951aa7a30b27cdab","Cargo.toml":"146684572cc251b02395a6e6112b3a9a3e00ae77faf125f97dc9853eece16123","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"5734ed989dfca1f625b40281ee9f4530f91b2411ec01cb748223e7eb87e201ab","README.md":"43d55c46767a4f4a78f6df4324dafd702723a4986f5b13b18f3400ee56d6c8c6","build-common.rs":"502cb7494549bed6fa10ac7bea36e880eeb60290dc69b679ac5c92b376469562","build.rs":"466741d1347312cc8d7587dc1730c8fe8e8797813f5cea81ac696dd665cf5656","no_atomic.rs":"3314524d2afa0360c947455a6e6566fb54ebf909c99479ca3b7435741fd3293e","src/array_queue.rs":"066305ca224ffff1626d3d2361a315fea6d50fb25ce60bbe31bb06e749f1bb12","src/lib.rs":"effccc001c5168217291e435ae79f4889495cb943a6720d1e73e65b64831e4ed","src/seg_queue.rs":"7cfc4aa1a681192ccec964880771a6e08e945a38ccf8be4730ed89de3dc48c1a","tests/array_queue.rs":"426dd0ff6698bd63108b3a567703ec2e635bce0f337134116e237b11925a7716","tests/seg_queue.rs":"7abb1008638a947440b201e1ad15f273020730715fae1f876407f1b967ae28ff"},"package":"d1cfb3ea8a53f37c40dea2c7bedcbd88bdfae54f5e2175d6ecaff1c988353add"}
|
|
@ -1,3 +1,60 @@
|
|||
# Version 0.3.8
|
||||
|
||||
- Fix build script bug introduced in 0.3.7. (#932)
|
||||
|
||||
# Version 0.3.7
|
||||
|
||||
**Note:** This release has been yanked due to regression fixed in 0.3.8.
|
||||
|
||||
- Improve support for custom targets. (#922)
|
||||
|
||||
# Version 0.3.6
|
||||
|
||||
- Bump the minimum supported Rust version to 1.38. (#877)
|
||||
|
||||
# Version 0.3.5
|
||||
|
||||
- Add `ArrayQueue::force_push`. (#789)
|
||||
|
||||
# Version 0.3.4
|
||||
|
||||
- Implement `IntoIterator` for `ArrayQueue` and `SegQueue`. (#772)
|
||||
|
||||
# Version 0.3.3
|
||||
|
||||
- Fix stacked borrows violation in `ArrayQueue` when `-Zmiri-tag-raw-pointers` is enabled. (#763)
|
||||
|
||||
# Version 0.3.2
|
||||
|
||||
- Support targets that do not have atomic CAS on stable Rust. (#698)
|
||||
|
||||
# Version 0.3.1
|
||||
|
||||
- Make `SegQueue::new` const fn. (#584)
|
||||
- Change license to "MIT OR Apache-2.0".
|
||||
|
||||
# Version 0.3.0
|
||||
|
||||
- Bump the minimum supported Rust version to 1.36.
|
||||
- Remove `PushError` and `PopError`.
|
||||
|
||||
# Version 0.2.3
|
||||
|
||||
- Fix bug in release (yanking 0.2.2)
|
||||
|
||||
# Version 0.2.2
|
||||
|
||||
- Fix unsoundness issues by adopting `MaybeUninit`. (#458)
|
||||
|
||||
# Version 0.2.1
|
||||
|
||||
- Add `no_std` support.
|
||||
|
||||
# Version 0.2.0
|
||||
|
||||
- Bump the minimum required version to 1.28.
|
||||
- Bump `crossbeam-utils` to `0.7`.
|
||||
|
||||
# Version 0.1.2
|
||||
|
||||
- Update `crossbeam-utils` to `0.6.5`.
|
||||
|
|
|
@ -3,26 +3,50 @@
|
|||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g. crates.io) dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you believe there's an error in this file please file an
|
||||
# issue against the rust-lang/cargo repository. If you're
|
||||
# editing this file be aware that the upstream Cargo.toml
|
||||
# will likely look very different (and much more reasonable)
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2018"
|
||||
rust-version = "1.38"
|
||||
name = "crossbeam-queue"
|
||||
version = "0.1.2"
|
||||
authors = ["The Crossbeam Project Developers"]
|
||||
version = "0.3.8"
|
||||
description = "Concurrent queues"
|
||||
homepage = "https://github.com/crossbeam-rs/crossbeam/tree/master/crossbeam-utils"
|
||||
documentation = "https://docs.rs/crossbeam-queue"
|
||||
homepage = "https://github.com/crossbeam-rs/crossbeam/tree/master/crossbeam-queue"
|
||||
readme = "README.md"
|
||||
keywords = ["queue", "mpmc", "lock-free", "producer", "consumer"]
|
||||
categories = ["concurrency", "data-structures"]
|
||||
license = "MIT/Apache-2.0"
|
||||
keywords = [
|
||||
"queue",
|
||||
"mpmc",
|
||||
"lock-free",
|
||||
"producer",
|
||||
"consumer",
|
||||
]
|
||||
categories = [
|
||||
"concurrency",
|
||||
"data-structures",
|
||||
"no-std",
|
||||
]
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/crossbeam-rs/crossbeam"
|
||||
|
||||
[dependencies.cfg-if]
|
||||
version = "1"
|
||||
|
||||
[dependencies.crossbeam-utils]
|
||||
version = "0.6.5"
|
||||
version = "0.8.5"
|
||||
default-features = false
|
||||
|
||||
[dev-dependencies.rand]
|
||||
version = "0.6"
|
||||
version = "0.8"
|
||||
|
||||
[features]
|
||||
alloc = []
|
||||
default = ["std"]
|
||||
nightly = ["crossbeam-utils/nightly"]
|
||||
std = [
|
||||
"alloc",
|
||||
"crossbeam-utils/std",
|
||||
]
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2019 The Crossbeam Project Developers
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
|
|
|
@ -1,21 +1,25 @@
|
|||
# Crossbeam Queue
|
||||
|
||||
[![Build Status](https://travis-ci.org/crossbeam-rs/crossbeam.svg?branch=master)](
|
||||
https://travis-ci.org/crossbeam-rs/crossbeam)
|
||||
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)](
|
||||
https://github.com/crossbeam-rs/crossbeam-queue/tree/master/src)
|
||||
[![Build Status](https://github.com/crossbeam-rs/crossbeam/workflows/CI/badge.svg)](
|
||||
https://github.com/crossbeam-rs/crossbeam/actions)
|
||||
[![License](https://img.shields.io/badge/license-MIT_OR_Apache--2.0-blue.svg)](
|
||||
https://github.com/crossbeam-rs/crossbeam/tree/master/crossbeam-queue#license)
|
||||
[![Cargo](https://img.shields.io/crates/v/crossbeam-queue.svg)](
|
||||
https://crates.io/crates/crossbeam-queue)
|
||||
[![Documentation](https://docs.rs/crossbeam-queue/badge.svg)](
|
||||
https://docs.rs/crossbeam-queue)
|
||||
[![Rust 1.26+](https://img.shields.io/badge/rust-1.26+-lightgray.svg)](
|
||||
[![Rust 1.38+](https://img.shields.io/badge/rust-1.38+-lightgray.svg)](
|
||||
https://www.rust-lang.org)
|
||||
[![chat](https://img.shields.io/discord/569610676205781012.svg?logo=discord)](https://discord.com/invite/JXYwgWZ)
|
||||
|
||||
This crate provides concurrent queues that can be shared among threads:
|
||||
|
||||
* [`ArrayQueue`], a bounded MPMC queue that allocates a fixed-capacity buffer on construction.
|
||||
* [`SegQueue`], an unbounded MPMC queue that allocates small buffers, segments, on demand.
|
||||
|
||||
Everything in this crate can be used in `no_std` environments, provided that `alloc` feature is
|
||||
enabled.
|
||||
|
||||
[`ArrayQueue`]: https://docs.rs/crossbeam-queue/*/crossbeam_queue/struct.ArrayQueue.html
|
||||
[`SegQueue`]: https://docs.rs/crossbeam-queue/*/crossbeam_queue/struct.SegQueue.html
|
||||
|
||||
|
@ -25,18 +29,14 @@ Add this to your `Cargo.toml`:
|
|||
|
||||
```toml
|
||||
[dependencies]
|
||||
crossbeam-queue = "0.1"
|
||||
```
|
||||
|
||||
Next, add this to your crate:
|
||||
|
||||
```rust
|
||||
extern crate crossbeam_queue;
|
||||
crossbeam-queue = "0.3"
|
||||
```
|
||||
|
||||
## Compatibility
|
||||
|
||||
The minimum supported Rust version is 1.26.
|
||||
Crossbeam Queue supports stable Rust releases going back at least six months,
|
||||
and every time the minimum supported Rust version is increased, a new minor
|
||||
version is released. Currently, the minimum supported Rust version is 1.38.
|
||||
|
||||
## License
|
||||
|
||||
|
@ -52,15 +52,3 @@ at your option.
|
|||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
||||
dual licensed as above, without any additional terms or conditions.
|
||||
|
||||
#### Third party software
|
||||
|
||||
This product includes copies and modifications of software developed by third parties:
|
||||
|
||||
* [src/array_queue.rs](src/array_queue.rs) is based on
|
||||
[Bounded MPMC queue](http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue)
|
||||
by Dmitry Vyukov, licensed under the Simplified BSD License and the Apache License, Version 2.0.
|
||||
|
||||
See the source code files for more details.
|
||||
|
||||
Copies of third party licenses can be found in [LICENSE-THIRD-PARTY](LICENSE-THIRD-PARTY).
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
// The target triplets have the form of 'arch-vendor-system'.
|
||||
//
|
||||
// When building for Linux (e.g. the 'system' part is
|
||||
// 'linux-something'), replace the vendor with 'unknown'
|
||||
// so that mapping to rust standard targets happens correctly.
|
||||
fn convert_custom_linux_target(target: String) -> String {
|
||||
let mut parts: Vec<&str> = target.split('-').collect();
|
||||
let system = parts.get(2);
|
||||
if system == Some(&"linux") {
|
||||
parts[1] = "unknown";
|
||||
};
|
||||
parts.join("-")
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
// The rustc-cfg listed below are considered public API, but it is *unstable*
|
||||
// and outside of the normal semver guarantees:
|
||||
//
|
||||
// - `crossbeam_no_atomic_cas`
|
||||
// Assume the target does *not* support atomic CAS operations.
|
||||
// This is usually detected automatically by the build script, but you may
|
||||
// need to enable it manually when building for custom targets or using
|
||||
// non-cargo build systems that don't run the build script.
|
||||
//
|
||||
// With the exceptions mentioned above, the rustc-cfg emitted by the build
|
||||
// script are *not* public API.
|
||||
|
||||
#![warn(rust_2018_idioms)]
|
||||
|
||||
use std::env;
|
||||
|
||||
include!("no_atomic.rs");
|
||||
include!("build-common.rs");
|
||||
|
||||
fn main() {
|
||||
let target = match env::var("TARGET") {
|
||||
Ok(target) => convert_custom_linux_target(target),
|
||||
Err(e) => {
|
||||
println!(
|
||||
"cargo:warning={}: unable to get TARGET environment variable: {}",
|
||||
env!("CARGO_PKG_NAME"),
|
||||
e
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Note that this is `no_`*, not `has_*`. This allows treating as the latest
|
||||
// stable rustc is used when the build script doesn't run. This is useful
|
||||
// for non-cargo build systems that don't run the build script.
|
||||
if NO_ATOMIC_CAS.contains(&&*target) {
|
||||
println!("cargo:rustc-cfg=crossbeam_no_atomic_cas");
|
||||
}
|
||||
|
||||
println!("cargo:rerun-if-changed=no_atomic.rs");
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
// This file is @generated by no_atomic.sh.
|
||||
// It is not intended for manual editing.
|
||||
|
||||
const NO_ATOMIC_CAS: &[&str] = &[
|
||||
"armv4t-none-eabi",
|
||||
"armv5te-none-eabi",
|
||||
"avr-unknown-gnu-atmega328",
|
||||
"bpfeb-unknown-none",
|
||||
"bpfel-unknown-none",
|
||||
"msp430-none-elf",
|
||||
"riscv32i-unknown-none-elf",
|
||||
"riscv32im-unknown-none-elf",
|
||||
"riscv32imc-unknown-none-elf",
|
||||
"thumbv4t-none-eabi",
|
||||
"thumbv5te-none-eabi",
|
||||
"thumbv6m-none-eabi",
|
||||
];
|
||||
|
||||
#[allow(dead_code)] // Only crossbeam-utils uses this.
|
||||
const NO_ATOMIC_64: &[&str] = &[
|
||||
"arm-linux-androideabi",
|
||||
"armebv7r-none-eabi",
|
||||
"armebv7r-none-eabihf",
|
||||
"armv4t-none-eabi",
|
||||
"armv4t-unknown-linux-gnueabi",
|
||||
"armv5te-none-eabi",
|
||||
"armv5te-unknown-linux-gnueabi",
|
||||
"armv5te-unknown-linux-musleabi",
|
||||
"armv5te-unknown-linux-uclibceabi",
|
||||
"armv6k-nintendo-3ds",
|
||||
"armv7r-none-eabi",
|
||||
"armv7r-none-eabihf",
|
||||
"avr-unknown-gnu-atmega328",
|
||||
"hexagon-unknown-linux-musl",
|
||||
"m68k-unknown-linux-gnu",
|
||||
"mips-unknown-linux-gnu",
|
||||
"mips-unknown-linux-musl",
|
||||
"mips-unknown-linux-uclibc",
|
||||
"mipsel-sony-psp",
|
||||
"mipsel-sony-psx",
|
||||
"mipsel-unknown-linux-gnu",
|
||||
"mipsel-unknown-linux-musl",
|
||||
"mipsel-unknown-linux-uclibc",
|
||||
"mipsel-unknown-none",
|
||||
"mipsisa32r6-unknown-linux-gnu",
|
||||
"mipsisa32r6el-unknown-linux-gnu",
|
||||
"msp430-none-elf",
|
||||
"powerpc-unknown-freebsd",
|
||||
"powerpc-unknown-linux-gnu",
|
||||
"powerpc-unknown-linux-gnuspe",
|
||||
"powerpc-unknown-linux-musl",
|
||||
"powerpc-unknown-netbsd",
|
||||
"powerpc-unknown-openbsd",
|
||||
"powerpc-wrs-vxworks",
|
||||
"powerpc-wrs-vxworks-spe",
|
||||
"riscv32gc-unknown-linux-gnu",
|
||||
"riscv32gc-unknown-linux-musl",
|
||||
"riscv32i-unknown-none-elf",
|
||||
"riscv32im-unknown-none-elf",
|
||||
"riscv32imac-unknown-none-elf",
|
||||
"riscv32imac-unknown-xous-elf",
|
||||
"riscv32imc-unknown-none-elf",
|
||||
"thumbv4t-none-eabi",
|
||||
"thumbv5te-none-eabi",
|
||||
"thumbv6m-none-eabi",
|
||||
"thumbv7em-none-eabi",
|
||||
"thumbv7em-none-eabihf",
|
||||
"thumbv7m-none-eabi",
|
||||
"thumbv8m.base-none-eabi",
|
||||
"thumbv8m.main-none-eabi",
|
||||
"thumbv8m.main-none-eabihf",
|
||||
];
|
||||
|
||||
#[allow(dead_code)] // Only crossbeam-utils uses this.
|
||||
const NO_ATOMIC: &[&str] = &[
|
||||
"avr-unknown-gnu-atmega328",
|
||||
"mipsel-sony-psx",
|
||||
"msp430-none-elf",
|
||||
"riscv32i-unknown-none-elf",
|
||||
"riscv32im-unknown-none-elf",
|
||||
"riscv32imc-unknown-none-elf",
|
||||
];
|
|
@ -1,56 +1,50 @@
|
|||
//! The implementation is based on Dmitry Vyukov's bounded MPMC queue.
|
||||
//!
|
||||
//! Source:
|
||||
//! - http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue
|
||||
//!
|
||||
//! Copyright & License:
|
||||
//! - Copyright (c) 2010-2011 Dmitry Vyukov
|
||||
//! - Simplified BSD License and Apache License, Version 2.0
|
||||
//! - http://www.1024cores.net/home/code-license
|
||||
//! - <http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue>
|
||||
|
||||
use std::cell::UnsafeCell;
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem;
|
||||
use std::ptr;
|
||||
use std::sync::atomic::{self, AtomicUsize, Ordering};
|
||||
use alloc::boxed::Box;
|
||||
use core::cell::UnsafeCell;
|
||||
use core::fmt;
|
||||
use core::mem::MaybeUninit;
|
||||
use core::sync::atomic::{self, AtomicUsize, Ordering};
|
||||
|
||||
use crossbeam_utils::{Backoff, CachePadded};
|
||||
|
||||
use err::{PopError, PushError};
|
||||
|
||||
/// A slot in a queue.
|
||||
struct Slot<T> {
|
||||
/// The current stamp.
|
||||
///
|
||||
/// If the stamp equals the tail, this node will be next written to. If it equals the head,
|
||||
/// If the stamp equals the tail, this node will be next written to. If it equals head + 1,
|
||||
/// this node will be next read from.
|
||||
stamp: AtomicUsize,
|
||||
|
||||
/// The value in this slot.
|
||||
value: UnsafeCell<T>,
|
||||
value: UnsafeCell<MaybeUninit<T>>,
|
||||
}
|
||||
|
||||
/// A bounded multi-producer multi-consumer queue.
|
||||
///
|
||||
/// This queue allocates a fixed-capacity buffer on construction, which is used to store pushed
|
||||
/// elements. The queue cannot hold more elements that the buffer allows. Attempting to push an
|
||||
/// element into a full queue will fail. Having a buffer allocated upfront makes this queue a bit
|
||||
/// faster than [`SegQueue`].
|
||||
/// elements. The queue cannot hold more elements than the buffer allows. Attempting to push an
|
||||
/// element into a full queue will fail. Alternatively, [`force_push`] makes it possible for
|
||||
/// this queue to be used as a ring-buffer. Having a buffer allocated upfront makes this queue
|
||||
/// a bit faster than [`SegQueue`].
|
||||
///
|
||||
/// [`SegQueue`]: struct.SegQueue.html
|
||||
/// [`force_push`]: ArrayQueue::force_push
|
||||
/// [`SegQueue`]: super::SegQueue
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PushError};
|
||||
/// use crossbeam_queue::ArrayQueue;
|
||||
///
|
||||
/// let q = ArrayQueue::new(2);
|
||||
///
|
||||
/// assert_eq!(q.push('a'), Ok(()));
|
||||
/// assert_eq!(q.push('b'), Ok(()));
|
||||
/// assert_eq!(q.push('c'), Err(PushError('c')));
|
||||
/// assert_eq!(q.pop(), Ok('a'));
|
||||
/// assert_eq!(q.push('c'), Err('c'));
|
||||
/// assert_eq!(q.pop(), Some('a'));
|
||||
/// ```
|
||||
pub struct ArrayQueue<T> {
|
||||
/// The head of the queue.
|
||||
|
@ -70,16 +64,13 @@ pub struct ArrayQueue<T> {
|
|||
tail: CachePadded<AtomicUsize>,
|
||||
|
||||
/// The buffer holding slots.
|
||||
buffer: *mut Slot<T>,
|
||||
buffer: Box<[Slot<T>]>,
|
||||
|
||||
/// The queue capacity.
|
||||
cap: usize,
|
||||
|
||||
/// A stamp with the value of `{ lap: 1, index: 0 }`.
|
||||
one_lap: usize,
|
||||
|
||||
/// Indicates that dropping an `ArrayQueue<T>` may drop elements of type `T`.
|
||||
_marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
unsafe impl<T: Send> Sync for ArrayQueue<T> {}
|
||||
|
@ -107,22 +98,17 @@ impl<T> ArrayQueue<T> {
|
|||
let head = 0;
|
||||
let tail = 0;
|
||||
|
||||
// Allocate a buffer of `cap` slots.
|
||||
let buffer = {
|
||||
let mut v = Vec::<Slot<T>>::with_capacity(cap);
|
||||
let ptr = v.as_mut_ptr();
|
||||
mem::forget(v);
|
||||
ptr
|
||||
};
|
||||
|
||||
// Initialize stamps in the slots.
|
||||
for i in 0..cap {
|
||||
unsafe {
|
||||
// Allocate a buffer of `cap` slots initialized
|
||||
// with stamps.
|
||||
let buffer: Box<[Slot<T>]> = (0..cap)
|
||||
.map(|i| {
|
||||
// Set the stamp to `{ lap: 0, index: i }`.
|
||||
let slot = buffer.add(i);
|
||||
ptr::write(&mut (*slot).stamp, AtomicUsize::new(i));
|
||||
}
|
||||
}
|
||||
Slot {
|
||||
stamp: AtomicUsize::new(i),
|
||||
value: UnsafeCell::new(MaybeUninit::uninit()),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// One lap is the smallest power of two greater than `cap`.
|
||||
let one_lap = (cap + 1).next_power_of_two();
|
||||
|
@ -133,25 +119,13 @@ impl<T> ArrayQueue<T> {
|
|||
one_lap,
|
||||
head: CachePadded::new(AtomicUsize::new(head)),
|
||||
tail: CachePadded::new(AtomicUsize::new(tail)),
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to push an element into the queue.
|
||||
///
|
||||
/// If the queue is full, the element is returned back as an error.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PushError};
|
||||
///
|
||||
/// let q = ArrayQueue::new(1);
|
||||
///
|
||||
/// assert_eq!(q.push(10), Ok(()));
|
||||
/// assert_eq!(q.push(20), Err(PushError(20)));
|
||||
/// ```
|
||||
pub fn push(&self, value: T) -> Result<(), PushError<T>> {
|
||||
fn push_or_else<F>(&self, mut value: T, f: F) -> Result<(), T>
|
||||
where
|
||||
F: Fn(T, usize, usize, &Slot<T>) -> Result<T, T>,
|
||||
{
|
||||
let backoff = Backoff::new();
|
||||
let mut tail = self.tail.load(Ordering::Relaxed);
|
||||
|
||||
|
@ -160,30 +134,35 @@ impl<T> ArrayQueue<T> {
|
|||
let index = tail & (self.one_lap - 1);
|
||||
let lap = tail & !(self.one_lap - 1);
|
||||
|
||||
let new_tail = if index + 1 < self.cap {
|
||||
// Same lap, incremented index.
|
||||
// Set to `{ lap: lap, index: index + 1 }`.
|
||||
tail + 1
|
||||
} else {
|
||||
// One lap forward, index wraps around to zero.
|
||||
// Set to `{ lap: lap.wrapping_add(1), index: 0 }`.
|
||||
lap.wrapping_add(self.one_lap)
|
||||
};
|
||||
|
||||
// Inspect the corresponding slot.
|
||||
let slot = unsafe { &*self.buffer.add(index) };
|
||||
debug_assert!(index < self.buffer.len());
|
||||
let slot = unsafe { self.buffer.get_unchecked(index) };
|
||||
let stamp = slot.stamp.load(Ordering::Acquire);
|
||||
|
||||
// If the tail and the stamp match, we may attempt to push.
|
||||
if tail == stamp {
|
||||
let new_tail = if index + 1 < self.cap {
|
||||
// Same lap, incremented index.
|
||||
// Set to `{ lap: lap, index: index + 1 }`.
|
||||
tail + 1
|
||||
} else {
|
||||
// One lap forward, index wraps around to zero.
|
||||
// Set to `{ lap: lap.wrapping_add(1), index: 0 }`.
|
||||
lap.wrapping_add(self.one_lap)
|
||||
};
|
||||
|
||||
// Try moving the tail.
|
||||
match self
|
||||
.tail
|
||||
.compare_exchange_weak(tail, new_tail, Ordering::SeqCst, Ordering::Relaxed)
|
||||
{
|
||||
match self.tail.compare_exchange_weak(
|
||||
tail,
|
||||
new_tail,
|
||||
Ordering::SeqCst,
|
||||
Ordering::Relaxed,
|
||||
) {
|
||||
Ok(_) => {
|
||||
// Write the value into the slot and update the stamp.
|
||||
unsafe { slot.value.get().write(value); }
|
||||
unsafe {
|
||||
slot.value.get().write(MaybeUninit::new(value));
|
||||
}
|
||||
slot.stamp.store(tail + 1, Ordering::Release);
|
||||
return Ok(());
|
||||
}
|
||||
|
@ -194,14 +173,7 @@ impl<T> ArrayQueue<T> {
|
|||
}
|
||||
} else if stamp.wrapping_add(self.one_lap) == tail + 1 {
|
||||
atomic::fence(Ordering::SeqCst);
|
||||
let head = self.head.load(Ordering::Relaxed);
|
||||
|
||||
// If the head lags one lap behind the tail as well...
|
||||
if head.wrapping_add(self.one_lap) == tail {
|
||||
// ...then the queue is full.
|
||||
return Err(PushError(value));
|
||||
}
|
||||
|
||||
value = f(value, tail, new_tail, slot)?;
|
||||
backoff.spin();
|
||||
tail = self.tail.load(Ordering::Relaxed);
|
||||
} else {
|
||||
|
@ -212,22 +184,95 @@ impl<T> ArrayQueue<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Attempts to pop an element from the queue.
|
||||
/// Attempts to push an element into the queue.
|
||||
///
|
||||
/// If the queue is empty, an error is returned.
|
||||
/// If the queue is full, the element is returned back as an error.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PopError};
|
||||
/// use crossbeam_queue::ArrayQueue;
|
||||
///
|
||||
/// let q = ArrayQueue::new(1);
|
||||
///
|
||||
/// assert_eq!(q.push(10), Ok(()));
|
||||
/// assert_eq!(q.push(20), Err(20));
|
||||
/// ```
|
||||
pub fn push(&self, value: T) -> Result<(), T> {
|
||||
self.push_or_else(value, |v, tail, _, _| {
|
||||
let head = self.head.load(Ordering::Relaxed);
|
||||
|
||||
// If the head lags one lap behind the tail as well...
|
||||
if head.wrapping_add(self.one_lap) == tail {
|
||||
// ...then the queue is full.
|
||||
Err(v)
|
||||
} else {
|
||||
Ok(v)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Pushes an element into the queue, replacing the oldest element if necessary.
|
||||
///
|
||||
/// If the queue is full, the oldest element is replaced and returned,
|
||||
/// otherwise `None` is returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::ArrayQueue;
|
||||
///
|
||||
/// let q = ArrayQueue::new(2);
|
||||
///
|
||||
/// assert_eq!(q.force_push(10), None);
|
||||
/// assert_eq!(q.force_push(20), None);
|
||||
/// assert_eq!(q.force_push(30), Some(10));
|
||||
/// assert_eq!(q.pop(), Some(20));
|
||||
/// ```
|
||||
pub fn force_push(&self, value: T) -> Option<T> {
|
||||
self.push_or_else(value, |v, tail, new_tail, slot| {
|
||||
let head = tail.wrapping_sub(self.one_lap);
|
||||
let new_head = new_tail.wrapping_sub(self.one_lap);
|
||||
|
||||
// Try moving the head.
|
||||
if self
|
||||
.head
|
||||
.compare_exchange_weak(head, new_head, Ordering::SeqCst, Ordering::Relaxed)
|
||||
.is_ok()
|
||||
{
|
||||
// Move the tail.
|
||||
self.tail.store(new_tail, Ordering::SeqCst);
|
||||
|
||||
// Swap the previous value.
|
||||
let old = unsafe { slot.value.get().replace(MaybeUninit::new(v)).assume_init() };
|
||||
|
||||
// Update the stamp.
|
||||
slot.stamp.store(tail + 1, Ordering::Release);
|
||||
|
||||
Err(old)
|
||||
} else {
|
||||
Ok(v)
|
||||
}
|
||||
})
|
||||
.err()
|
||||
}
|
||||
|
||||
/// Attempts to pop an element from the queue.
|
||||
///
|
||||
/// If the queue is empty, `None` is returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::ArrayQueue;
|
||||
///
|
||||
/// let q = ArrayQueue::new(1);
|
||||
/// assert_eq!(q.push(10), Ok(()));
|
||||
///
|
||||
/// assert_eq!(q.pop(), Ok(10));
|
||||
/// assert_eq!(q.pop(), Err(PopError));
|
||||
/// assert_eq!(q.pop(), Some(10));
|
||||
/// assert!(q.pop().is_none());
|
||||
/// ```
|
||||
pub fn pop(&self) -> Result<T, PopError> {
|
||||
pub fn pop(&self) -> Option<T> {
|
||||
let backoff = Backoff::new();
|
||||
let mut head = self.head.load(Ordering::Relaxed);
|
||||
|
||||
|
@ -237,7 +282,8 @@ impl<T> ArrayQueue<T> {
|
|||
let lap = head & !(self.one_lap - 1);
|
||||
|
||||
// Inspect the corresponding slot.
|
||||
let slot = unsafe { &*self.buffer.add(index) };
|
||||
debug_assert!(index < self.buffer.len());
|
||||
let slot = unsafe { self.buffer.get_unchecked(index) };
|
||||
let stamp = slot.stamp.load(Ordering::Acquire);
|
||||
|
||||
// If the the stamp is ahead of the head by 1, we may attempt to pop.
|
||||
|
@ -253,15 +299,18 @@ impl<T> ArrayQueue<T> {
|
|||
};
|
||||
|
||||
// Try moving the head.
|
||||
match self
|
||||
.head
|
||||
.compare_exchange_weak(head, new, Ordering::SeqCst, Ordering::Relaxed)
|
||||
{
|
||||
match self.head.compare_exchange_weak(
|
||||
head,
|
||||
new,
|
||||
Ordering::SeqCst,
|
||||
Ordering::Relaxed,
|
||||
) {
|
||||
Ok(_) => {
|
||||
// Read the value from the slot and update the stamp.
|
||||
let msg = unsafe { slot.value.get().read() };
|
||||
slot.stamp.store(head.wrapping_add(self.one_lap), Ordering::Release);
|
||||
return Ok(msg);
|
||||
let msg = unsafe { slot.value.get().read().assume_init() };
|
||||
slot.stamp
|
||||
.store(head.wrapping_add(self.one_lap), Ordering::Release);
|
||||
return Some(msg);
|
||||
}
|
||||
Err(h) => {
|
||||
head = h;
|
||||
|
@ -274,7 +323,7 @@ impl<T> ArrayQueue<T> {
|
|||
|
||||
// If the tail equals the head, that means the channel is empty.
|
||||
if tail == head {
|
||||
return Err(PopError);
|
||||
return None;
|
||||
}
|
||||
|
||||
backoff.spin();
|
||||
|
@ -292,7 +341,7 @@ impl<T> ArrayQueue<T> {
|
|||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PopError};
|
||||
/// use crossbeam_queue::ArrayQueue;
|
||||
///
|
||||
/// let q = ArrayQueue::<i32>::new(100);
|
||||
///
|
||||
|
@ -307,7 +356,7 @@ impl<T> ArrayQueue<T> {
|
|||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PopError};
|
||||
/// use crossbeam_queue::ArrayQueue;
|
||||
///
|
||||
/// let q = ArrayQueue::new(100);
|
||||
///
|
||||
|
@ -332,7 +381,7 @@ impl<T> ArrayQueue<T> {
|
|||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PopError};
|
||||
/// use crossbeam_queue::ArrayQueue;
|
||||
///
|
||||
/// let q = ArrayQueue::new(1);
|
||||
///
|
||||
|
@ -356,7 +405,7 @@ impl<T> ArrayQueue<T> {
|
|||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{ArrayQueue, PopError};
|
||||
/// use crossbeam_queue::ArrayQueue;
|
||||
///
|
||||
/// let q = ArrayQueue::new(100);
|
||||
/// assert_eq!(q.len(), 0);
|
||||
|
@ -395,10 +444,24 @@ impl<T> ArrayQueue<T> {
|
|||
impl<T> Drop for ArrayQueue<T> {
|
||||
fn drop(&mut self) {
|
||||
// Get the index of the head.
|
||||
let hix = self.head.load(Ordering::Relaxed) & (self.one_lap - 1);
|
||||
let head = *self.head.get_mut();
|
||||
let tail = *self.tail.get_mut();
|
||||
|
||||
let hix = head & (self.one_lap - 1);
|
||||
let tix = tail & (self.one_lap - 1);
|
||||
|
||||
let len = if hix < tix {
|
||||
tix - hix
|
||||
} else if hix > tix {
|
||||
self.cap - hix + tix
|
||||
} else if tail == head {
|
||||
0
|
||||
} else {
|
||||
self.cap
|
||||
};
|
||||
|
||||
// Loop over all slots that hold a message and drop them.
|
||||
for i in 0..self.len() {
|
||||
for i in 0..len {
|
||||
// Compute the index of the next slot holding a message.
|
||||
let index = if hix + i < self.cap {
|
||||
hix + i
|
||||
|
@ -407,19 +470,67 @@ impl<T> Drop for ArrayQueue<T> {
|
|||
};
|
||||
|
||||
unsafe {
|
||||
self.buffer.add(index).drop_in_place();
|
||||
debug_assert!(index < self.buffer.len());
|
||||
let slot = self.buffer.get_unchecked_mut(index);
|
||||
let value = &mut *slot.value.get();
|
||||
value.as_mut_ptr().drop_in_place();
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, deallocate the buffer, but don't run any destructors.
|
||||
unsafe {
|
||||
Vec::from_raw_parts(self.buffer, 0, self.cap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for ArrayQueue<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.pad("ArrayQueue { .. }")
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoIterator for ArrayQueue<T> {
|
||||
type Item = T;
|
||||
|
||||
type IntoIter = IntoIter<T>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
IntoIter { value: self }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct IntoIter<T> {
|
||||
value: ArrayQueue<T>,
|
||||
}
|
||||
|
||||
impl<T> Iterator for IntoIter<T> {
|
||||
type Item = T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let value = &mut self.value;
|
||||
let head = *value.head.get_mut();
|
||||
if value.head.get_mut() != value.tail.get_mut() {
|
||||
let index = head & (value.one_lap - 1);
|
||||
let lap = head & !(value.one_lap - 1);
|
||||
// SAFETY: We have mutable access to this, so we can read without
|
||||
// worrying about concurrency. Furthermore, we know this is
|
||||
// initialized because it is the value pointed at by `value.head`
|
||||
// and this is a non-empty queue.
|
||||
let val = unsafe {
|
||||
debug_assert!(index < value.buffer.len());
|
||||
let slot = value.buffer.get_unchecked_mut(index);
|
||||
slot.value.get().read().assume_init()
|
||||
};
|
||||
let new = if index + 1 < value.cap {
|
||||
// Same lap, incremented index.
|
||||
// Set to `{ lap: lap, index: index + 1 }`.
|
||||
head + 1
|
||||
} else {
|
||||
// One lap forward, index wraps around to zero.
|
||||
// Set to `{ lap: lap.wrapping_add(1), index: 0 }`.
|
||||
lap.wrapping_add(value.one_lap)
|
||||
};
|
||||
*value.head.get_mut() = new;
|
||||
Option::Some(val)
|
||||
} else {
|
||||
Option::None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,19 +4,31 @@
|
|||
//!
|
||||
//! * [`ArrayQueue`], a bounded MPMC queue that allocates a fixed-capacity buffer on construction.
|
||||
//! * [`SegQueue`], an unbounded MPMC queue that allocates small buffers, segments, on demand.
|
||||
//!
|
||||
//! [`ArrayQueue`]: struct.ArrayQueue.html
|
||||
//! [`SegQueue`]: struct.SegQueue.html
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![warn(missing_debug_implementations)]
|
||||
#![doc(test(
|
||||
no_crate_inject,
|
||||
attr(
|
||||
deny(warnings, rust_2018_idioms),
|
||||
allow(dead_code, unused_assignments, unused_variables)
|
||||
)
|
||||
))]
|
||||
#![warn(
|
||||
missing_docs,
|
||||
missing_debug_implementations,
|
||||
rust_2018_idioms,
|
||||
unreachable_pub
|
||||
)]
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
extern crate crossbeam_utils;
|
||||
#[cfg(not(crossbeam_no_atomic_cas))]
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "alloc")] {
|
||||
extern crate alloc;
|
||||
|
||||
mod array_queue;
|
||||
mod err;
|
||||
mod seg_queue;
|
||||
mod array_queue;
|
||||
mod seg_queue;
|
||||
|
||||
pub use self::array_queue::ArrayQueue;
|
||||
pub use self::seg_queue::SegQueue;
|
||||
pub use self::err::{PopError, PushError};
|
||||
pub use self::array_queue::ArrayQueue;
|
||||
pub use self::seg_queue::SegQueue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
use std::cell::UnsafeCell;
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::{self, ManuallyDrop};
|
||||
use std::ptr;
|
||||
use std::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
||||
use alloc::boxed::Box;
|
||||
use core::cell::UnsafeCell;
|
||||
use core::fmt;
|
||||
use core::marker::PhantomData;
|
||||
use core::mem::MaybeUninit;
|
||||
use core::ptr;
|
||||
use core::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
||||
|
||||
use crossbeam_utils::{Backoff, CachePadded};
|
||||
|
||||
use err::PopError;
|
||||
|
||||
// Bits indicating the state of a slot:
|
||||
// * If a value has been written into the slot, `WRITE` is set.
|
||||
// * If a value has been read from the slot, `READ` is set.
|
||||
|
@ -29,13 +28,18 @@ const HAS_NEXT: usize = 1;
|
|||
/// A slot in a block.
|
||||
struct Slot<T> {
|
||||
/// The value.
|
||||
value: UnsafeCell<ManuallyDrop<T>>,
|
||||
value: UnsafeCell<MaybeUninit<T>>,
|
||||
|
||||
/// The state of the slot.
|
||||
state: AtomicUsize,
|
||||
}
|
||||
|
||||
impl<T> Slot<T> {
|
||||
const UNINIT: Self = Self {
|
||||
value: UnsafeCell::new(MaybeUninit::uninit()),
|
||||
state: AtomicUsize::new(0),
|
||||
};
|
||||
|
||||
/// Waits until a value is written into the slot.
|
||||
fn wait_write(&self) {
|
||||
let backoff = Backoff::new();
|
||||
|
@ -59,7 +63,10 @@ struct Block<T> {
|
|||
impl<T> Block<T> {
|
||||
/// Creates an empty block that starts at `start_index`.
|
||||
fn new() -> Block<T> {
|
||||
unsafe { mem::zeroed() }
|
||||
Self {
|
||||
next: AtomicPtr::new(ptr::null_mut()),
|
||||
slots: [Slot::UNINIT; BLOCK_CAP],
|
||||
}
|
||||
}
|
||||
|
||||
/// Waits until the next pointer is set.
|
||||
|
@ -111,21 +118,21 @@ struct Position<T> {
|
|||
/// at a time. However, since segments need to be dynamically allocated as elements get pushed,
|
||||
/// this queue is somewhat slower than [`ArrayQueue`].
|
||||
///
|
||||
/// [`ArrayQueue`]: struct.ArrayQueue.html
|
||||
/// [`ArrayQueue`]: super::ArrayQueue
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{PopError, SegQueue};
|
||||
/// use crossbeam_queue::SegQueue;
|
||||
///
|
||||
/// let q = SegQueue::new();
|
||||
///
|
||||
/// q.push('a');
|
||||
/// q.push('b');
|
||||
///
|
||||
/// assert_eq!(q.pop(), Ok('a'));
|
||||
/// assert_eq!(q.pop(), Ok('b'));
|
||||
/// assert_eq!(q.pop(), Err(PopError));
|
||||
/// assert_eq!(q.pop(), Some('a'));
|
||||
/// assert_eq!(q.pop(), Some('b'));
|
||||
/// assert!(q.pop().is_none());
|
||||
/// ```
|
||||
pub struct SegQueue<T> {
|
||||
/// The head of the queue.
|
||||
|
@ -151,7 +158,7 @@ impl<T> SegQueue<T> {
|
|||
///
|
||||
/// let q = SegQueue::<i32>::new();
|
||||
/// ```
|
||||
pub fn new() -> SegQueue<T> {
|
||||
pub const fn new() -> SegQueue<T> {
|
||||
SegQueue {
|
||||
head: CachePadded::new(Position {
|
||||
block: AtomicPtr::new(ptr::null_mut()),
|
||||
|
@ -205,7 +212,12 @@ impl<T> SegQueue<T> {
|
|||
if block.is_null() {
|
||||
let new = Box::into_raw(Box::new(Block::<T>::new()));
|
||||
|
||||
if self.tail.block.compare_and_swap(block, new, Ordering::Release) == block {
|
||||
if self
|
||||
.tail
|
||||
.block
|
||||
.compare_exchange(block, new, Ordering::Release, Ordering::Relaxed)
|
||||
.is_ok()
|
||||
{
|
||||
self.head.block.store(new, Ordering::Release);
|
||||
block = new;
|
||||
} else {
|
||||
|
@ -219,14 +231,12 @@ impl<T> SegQueue<T> {
|
|||
let new_tail = tail + (1 << SHIFT);
|
||||
|
||||
// Try advancing the tail forward.
|
||||
match self.tail.index
|
||||
.compare_exchange_weak(
|
||||
tail,
|
||||
new_tail,
|
||||
Ordering::SeqCst,
|
||||
Ordering::Acquire,
|
||||
)
|
||||
{
|
||||
match self.tail.index.compare_exchange_weak(
|
||||
tail,
|
||||
new_tail,
|
||||
Ordering::SeqCst,
|
||||
Ordering::Acquire,
|
||||
) {
|
||||
Ok(_) => unsafe {
|
||||
// If we've reached the end of the block, install the next one.
|
||||
if offset + 1 == BLOCK_CAP {
|
||||
|
@ -240,11 +250,11 @@ impl<T> SegQueue<T> {
|
|||
|
||||
// Write the value into the slot.
|
||||
let slot = (*block).slots.get_unchecked(offset);
|
||||
slot.value.get().write(ManuallyDrop::new(value));
|
||||
slot.value.get().write(MaybeUninit::new(value));
|
||||
slot.state.fetch_or(WRITE, Ordering::Release);
|
||||
|
||||
return;
|
||||
}
|
||||
},
|
||||
Err(t) => {
|
||||
tail = t;
|
||||
block = self.tail.block.load(Ordering::Acquire);
|
||||
|
@ -256,20 +266,20 @@ impl<T> SegQueue<T> {
|
|||
|
||||
/// Pops an element from the queue.
|
||||
///
|
||||
/// If the queue is empty, an error is returned.
|
||||
/// If the queue is empty, `None` is returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{PopError, SegQueue};
|
||||
/// use crossbeam_queue::SegQueue;
|
||||
///
|
||||
/// let q = SegQueue::new();
|
||||
///
|
||||
/// q.push(10);
|
||||
/// assert_eq!(q.pop(), Ok(10));
|
||||
/// assert_eq!(q.pop(), Err(PopError));
|
||||
/// assert_eq!(q.pop(), Some(10));
|
||||
/// assert!(q.pop().is_none());
|
||||
/// ```
|
||||
pub fn pop(&self) -> Result<T, PopError> {
|
||||
pub fn pop(&self) -> Option<T> {
|
||||
let backoff = Backoff::new();
|
||||
let mut head = self.head.index.load(Ordering::Acquire);
|
||||
let mut block = self.head.block.load(Ordering::Acquire);
|
||||
|
@ -294,7 +304,7 @@ impl<T> SegQueue<T> {
|
|||
|
||||
// If the tail equals the head, that means the queue is empty.
|
||||
if head >> SHIFT == tail >> SHIFT {
|
||||
return Err(PopError);
|
||||
return None;
|
||||
}
|
||||
|
||||
// If head and tail are not in the same block, set `HAS_NEXT` in head.
|
||||
|
@ -313,14 +323,12 @@ impl<T> SegQueue<T> {
|
|||
}
|
||||
|
||||
// Try moving the head index forward.
|
||||
match self.head.index
|
||||
.compare_exchange_weak(
|
||||
head,
|
||||
new_head,
|
||||
Ordering::SeqCst,
|
||||
Ordering::Acquire,
|
||||
)
|
||||
{
|
||||
match self.head.index.compare_exchange_weak(
|
||||
head,
|
||||
new_head,
|
||||
Ordering::SeqCst,
|
||||
Ordering::Acquire,
|
||||
) {
|
||||
Ok(_) => unsafe {
|
||||
// If we've reached the end of the block, move to the next one.
|
||||
if offset + 1 == BLOCK_CAP {
|
||||
|
@ -337,8 +345,7 @@ impl<T> SegQueue<T> {
|
|||
// Read the value.
|
||||
let slot = (*block).slots.get_unchecked(offset);
|
||||
slot.wait_write();
|
||||
let m = slot.value.get().read();
|
||||
let value = ManuallyDrop::into_inner(m);
|
||||
let value = slot.value.get().read().assume_init();
|
||||
|
||||
// Destroy the block if we've reached the end, or if another thread wanted to
|
||||
// destroy but couldn't because we were busy reading from the slot.
|
||||
|
@ -348,8 +355,8 @@ impl<T> SegQueue<T> {
|
|||
Block::destroy(block, offset + 1);
|
||||
}
|
||||
|
||||
return Ok(value);
|
||||
}
|
||||
return Some(value);
|
||||
},
|
||||
Err(h) => {
|
||||
head = h;
|
||||
block = self.head.block.load(Ordering::Acquire);
|
||||
|
@ -383,7 +390,7 @@ impl<T> SegQueue<T> {
|
|||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossbeam_queue::{SegQueue, PopError};
|
||||
/// use crossbeam_queue::SegQueue;
|
||||
///
|
||||
/// let q = SegQueue::new();
|
||||
/// assert_eq!(q.len(), 0);
|
||||
|
@ -406,6 +413,14 @@ impl<T> SegQueue<T> {
|
|||
tail &= !((1 << SHIFT) - 1);
|
||||
head &= !((1 << SHIFT) - 1);
|
||||
|
||||
// Fix up indices if they fall onto block ends.
|
||||
if (tail >> SHIFT) & (LAP - 1) == LAP - 1 {
|
||||
tail = tail.wrapping_add(1 << SHIFT);
|
||||
}
|
||||
if (head >> SHIFT) & (LAP - 1) == LAP - 1 {
|
||||
head = head.wrapping_add(1 << SHIFT);
|
||||
}
|
||||
|
||||
// Rotate indices so that head falls into the first block.
|
||||
let lap = (head >> SHIFT) / LAP;
|
||||
tail = tail.wrapping_sub((lap * LAP) << SHIFT);
|
||||
|
@ -415,15 +430,6 @@ impl<T> SegQueue<T> {
|
|||
tail >>= SHIFT;
|
||||
head >>= SHIFT;
|
||||
|
||||
// Fix up indices if they fall onto block ends.
|
||||
if head == BLOCK_CAP {
|
||||
head = 0;
|
||||
tail -= LAP;
|
||||
}
|
||||
if tail == BLOCK_CAP {
|
||||
tail += 1;
|
||||
}
|
||||
|
||||
// Return the difference minus the number of blocks between tail and head.
|
||||
return tail - head - tail / LAP;
|
||||
}
|
||||
|
@ -433,9 +439,9 @@ impl<T> SegQueue<T> {
|
|||
|
||||
impl<T> Drop for SegQueue<T> {
|
||||
fn drop(&mut self) {
|
||||
let mut head = self.head.index.load(Ordering::Relaxed);
|
||||
let mut tail = self.tail.index.load(Ordering::Relaxed);
|
||||
let mut block = self.head.block.load(Ordering::Relaxed);
|
||||
let mut head = *self.head.index.get_mut();
|
||||
let mut tail = *self.tail.index.get_mut();
|
||||
let mut block = *self.head.block.get_mut();
|
||||
|
||||
// Erase the lower bits.
|
||||
head &= !((1 << SHIFT) - 1);
|
||||
|
@ -449,10 +455,11 @@ impl<T> Drop for SegQueue<T> {
|
|||
if offset < BLOCK_CAP {
|
||||
// Drop the value in the slot.
|
||||
let slot = (*block).slots.get_unchecked(offset);
|
||||
ManuallyDrop::drop(&mut *(*slot).value.get());
|
||||
let p = &mut *slot.value.get();
|
||||
p.as_mut_ptr().drop_in_place();
|
||||
} else {
|
||||
// Deallocate the block and move to the next one.
|
||||
let next = (*block).next.load(Ordering::Relaxed);
|
||||
let next = *(*block).next.get_mut();
|
||||
drop(Box::from_raw(block));
|
||||
block = next;
|
||||
}
|
||||
|
@ -469,7 +476,7 @@ impl<T> Drop for SegQueue<T> {
|
|||
}
|
||||
|
||||
impl<T> fmt::Debug for SegQueue<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.pad("SegQueue { .. }")
|
||||
}
|
||||
}
|
||||
|
@ -479,3 +486,62 @@ impl<T> Default for SegQueue<T> {
|
|||
SegQueue::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoIterator for SegQueue<T> {
|
||||
type Item = T;
|
||||
|
||||
type IntoIter = IntoIter<T>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
IntoIter { value: self }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct IntoIter<T> {
|
||||
value: SegQueue<T>,
|
||||
}
|
||||
|
||||
impl<T> Iterator for IntoIter<T> {
|
||||
type Item = T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let value = &mut self.value;
|
||||
let head = *value.head.index.get_mut();
|
||||
let tail = *value.tail.index.get_mut();
|
||||
if head >> SHIFT == tail >> SHIFT {
|
||||
None
|
||||
} else {
|
||||
let block = *value.head.block.get_mut();
|
||||
let offset = (head >> SHIFT) % LAP;
|
||||
|
||||
// SAFETY: We have mutable access to this, so we can read without
|
||||
// worrying about concurrency. Furthermore, we know this is
|
||||
// initialized because it is the value pointed at by `value.head`
|
||||
// and this is a non-empty queue.
|
||||
let item = unsafe {
|
||||
let slot = (*block).slots.get_unchecked(offset);
|
||||
let p = &mut *slot.value.get();
|
||||
p.as_mut_ptr().read()
|
||||
};
|
||||
if offset + 1 == BLOCK_CAP {
|
||||
// Deallocate the block and move to the next one.
|
||||
// SAFETY: The block is initialized because we've been reading
|
||||
// from it this entire time. We can drop it b/c everything has
|
||||
// been read out of it, so nothing is pointing to it anymore.
|
||||
unsafe {
|
||||
let next = *(*block).next.get_mut();
|
||||
drop(Box::from_raw(block));
|
||||
*value.head.block.get_mut() = next;
|
||||
}
|
||||
// The last value in a block is empty, so skip it
|
||||
*value.head.index.get_mut() = head.wrapping_add(2 << SHIFT);
|
||||
// Double-check that we're pointing to the first item in a block.
|
||||
debug_assert_eq!((*value.head.index.get_mut() >> SHIFT) % LAP, 0);
|
||||
} else {
|
||||
*value.head.index.get_mut() = head.wrapping_add(1 << SHIFT);
|
||||
}
|
||||
Some(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
extern crate crossbeam_queue;
|
||||
extern crate crossbeam_utils;
|
||||
extern crate rand;
|
||||
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use crossbeam_queue::ArrayQueue;
|
||||
|
@ -13,11 +9,11 @@ fn smoke() {
|
|||
let q = ArrayQueue::new(1);
|
||||
|
||||
q.push(7).unwrap();
|
||||
assert_eq!(q.pop(), Ok(7));
|
||||
assert_eq!(q.pop(), Some(7));
|
||||
|
||||
q.push(8).unwrap();
|
||||
assert_eq!(q.pop(), Ok(8));
|
||||
assert!(q.pop().is_err());
|
||||
assert_eq!(q.pop(), Some(8));
|
||||
assert!(q.pop().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -39,45 +35,52 @@ fn len_empty_full() {
|
|||
let q = ArrayQueue::new(2);
|
||||
|
||||
assert_eq!(q.len(), 0);
|
||||
assert_eq!(q.is_empty(), true);
|
||||
assert_eq!(q.is_full(), false);
|
||||
assert!(q.is_empty());
|
||||
assert!(!q.is_full());
|
||||
|
||||
q.push(()).unwrap();
|
||||
|
||||
assert_eq!(q.len(), 1);
|
||||
assert_eq!(q.is_empty(), false);
|
||||
assert_eq!(q.is_full(), false);
|
||||
assert!(!q.is_empty());
|
||||
assert!(!q.is_full());
|
||||
|
||||
q.push(()).unwrap();
|
||||
|
||||
assert_eq!(q.len(), 2);
|
||||
assert_eq!(q.is_empty(), false);
|
||||
assert_eq!(q.is_full(), true);
|
||||
assert!(!q.is_empty());
|
||||
assert!(q.is_full());
|
||||
|
||||
q.pop().unwrap();
|
||||
|
||||
assert_eq!(q.len(), 1);
|
||||
assert_eq!(q.is_empty(), false);
|
||||
assert_eq!(q.is_full(), false);
|
||||
assert!(!q.is_empty());
|
||||
assert!(!q.is_full());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn len() {
|
||||
#[cfg(miri)]
|
||||
const COUNT: usize = 30;
|
||||
#[cfg(not(miri))]
|
||||
const COUNT: usize = 25_000;
|
||||
#[cfg(miri)]
|
||||
const CAP: usize = 40;
|
||||
#[cfg(not(miri))]
|
||||
const CAP: usize = 1000;
|
||||
const ITERS: usize = CAP / 20;
|
||||
|
||||
let q = ArrayQueue::new(CAP);
|
||||
assert_eq!(q.len(), 0);
|
||||
|
||||
for _ in 0..CAP / 10 {
|
||||
for i in 0..50 {
|
||||
for i in 0..ITERS {
|
||||
q.push(i).unwrap();
|
||||
assert_eq!(q.len(), i + 1);
|
||||
}
|
||||
|
||||
for i in 0..50 {
|
||||
for i in 0..ITERS {
|
||||
q.pop().unwrap();
|
||||
assert_eq!(q.len(), 50 - i - 1);
|
||||
assert_eq!(q.len(), ITERS - i - 1);
|
||||
}
|
||||
}
|
||||
assert_eq!(q.len(), 0);
|
||||
|
@ -96,7 +99,7 @@ fn len() {
|
|||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
loop {
|
||||
if let Ok(x) = q.pop() {
|
||||
if let Some(x) = q.pop() {
|
||||
assert_eq!(x, i);
|
||||
break;
|
||||
}
|
||||
|
@ -113,12 +116,16 @@ fn len() {
|
|||
assert!(len <= CAP);
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(q.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spsc() {
|
||||
#[cfg(miri)]
|
||||
const COUNT: usize = 50;
|
||||
#[cfg(not(miri))]
|
||||
const COUNT: usize = 100_000;
|
||||
|
||||
let q = ArrayQueue::new(3);
|
||||
|
@ -127,13 +134,13 @@ fn spsc() {
|
|||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
loop {
|
||||
if let Ok(x) = q.pop() {
|
||||
if let Some(x) = q.pop() {
|
||||
assert_eq!(x, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(q.pop().is_err());
|
||||
assert!(q.pop().is_none());
|
||||
});
|
||||
|
||||
scope.spawn(|_| {
|
||||
|
@ -141,11 +148,56 @@ fn spsc() {
|
|||
while q.push(i).is_err() {}
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spsc_ring_buffer() {
|
||||
#[cfg(miri)]
|
||||
const COUNT: usize = 50;
|
||||
#[cfg(not(miri))]
|
||||
const COUNT: usize = 100_000;
|
||||
|
||||
let t = AtomicUsize::new(1);
|
||||
let q = ArrayQueue::<usize>::new(3);
|
||||
let v = (0..COUNT).map(|_| AtomicUsize::new(0)).collect::<Vec<_>>();
|
||||
|
||||
scope(|scope| {
|
||||
scope.spawn(|_| loop {
|
||||
match t.load(Ordering::SeqCst) {
|
||||
0 if q.is_empty() => break,
|
||||
|
||||
_ => {
|
||||
while let Some(n) = q.pop() {
|
||||
v[n].fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
if let Some(n) = q.force_push(i) {
|
||||
v[n].fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
t.fetch_sub(1, Ordering::SeqCst);
|
||||
});
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
for c in v {
|
||||
assert_eq!(c.load(Ordering::SeqCst), 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mpmc() {
|
||||
#[cfg(miri)]
|
||||
const COUNT: usize = 50;
|
||||
#[cfg(not(miri))]
|
||||
const COUNT: usize = 25_000;
|
||||
const THREADS: usize = 4;
|
||||
|
||||
|
@ -157,7 +209,7 @@ fn mpmc() {
|
|||
scope.spawn(|_| {
|
||||
for _ in 0..COUNT {
|
||||
let n = loop {
|
||||
if let Ok(x) = q.pop() {
|
||||
if let Some(x) = q.pop() {
|
||||
break x;
|
||||
}
|
||||
};
|
||||
|
@ -172,7 +224,54 @@ fn mpmc() {
|
|||
}
|
||||
});
|
||||
}
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
for c in v {
|
||||
assert_eq!(c.load(Ordering::SeqCst), THREADS);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mpmc_ring_buffer() {
|
||||
#[cfg(miri)]
|
||||
const COUNT: usize = 50;
|
||||
#[cfg(not(miri))]
|
||||
const COUNT: usize = 25_000;
|
||||
const THREADS: usize = 4;
|
||||
|
||||
let t = AtomicUsize::new(THREADS);
|
||||
let q = ArrayQueue::<usize>::new(3);
|
||||
let v = (0..COUNT).map(|_| AtomicUsize::new(0)).collect::<Vec<_>>();
|
||||
|
||||
scope(|scope| {
|
||||
for _ in 0..THREADS {
|
||||
scope.spawn(|_| loop {
|
||||
match t.load(Ordering::SeqCst) {
|
||||
0 if q.is_empty() => break,
|
||||
|
||||
_ => {
|
||||
while let Some(n) = q.pop() {
|
||||
v[n].fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for _ in 0..THREADS {
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
if let Some(n) = q.force_push(i) {
|
||||
v[n].fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
t.fetch_sub(1, Ordering::SeqCst);
|
||||
});
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
for c in v {
|
||||
assert_eq!(c.load(Ordering::SeqCst), THREADS);
|
||||
|
@ -181,7 +280,9 @@ fn mpmc() {
|
|||
|
||||
#[test]
|
||||
fn drops() {
|
||||
const RUNS: usize = 100;
|
||||
let runs: usize = if cfg!(miri) { 3 } else { 100 };
|
||||
let steps: usize = if cfg!(miri) { 50 } else { 10_000 };
|
||||
let additional: usize = if cfg!(miri) { 10 } else { 50 };
|
||||
|
||||
static DROPS: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
|
@ -196,9 +297,9 @@ fn drops() {
|
|||
|
||||
let mut rng = thread_rng();
|
||||
|
||||
for _ in 0..RUNS {
|
||||
let steps = rng.gen_range(0, 10_000);
|
||||
let additional = rng.gen_range(0, 50);
|
||||
for _ in 0..runs {
|
||||
let steps = rng.gen_range(0..steps);
|
||||
let additional = rng.gen_range(0..additional);
|
||||
|
||||
DROPS.store(0, Ordering::SeqCst);
|
||||
let q = ArrayQueue::new(50);
|
||||
|
@ -206,7 +307,7 @@ fn drops() {
|
|||
scope(|scope| {
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..steps {
|
||||
while q.pop().is_err() {}
|
||||
while q.pop().is_none() {}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -217,7 +318,8 @@ fn drops() {
|
|||
}
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
for _ in 0..additional {
|
||||
q.push(DropCounter).unwrap();
|
||||
|
@ -231,19 +333,42 @@ fn drops() {
|
|||
|
||||
#[test]
|
||||
fn linearizable() {
|
||||
#[cfg(miri)]
|
||||
const COUNT: usize = 100;
|
||||
#[cfg(not(miri))]
|
||||
const COUNT: usize = 25_000;
|
||||
const THREADS: usize = 4;
|
||||
|
||||
let q = ArrayQueue::new(THREADS);
|
||||
|
||||
scope(|scope| {
|
||||
for _ in 0..THREADS {
|
||||
for _ in 0..THREADS / 2 {
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..COUNT {
|
||||
while q.push(0).is_err() {}
|
||||
q.pop().unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..COUNT {
|
||||
if q.force_push(0).is_none() {
|
||||
q.pop().unwrap();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn into_iter() {
|
||||
let q = ArrayQueue::new(100);
|
||||
for i in 0..100 {
|
||||
q.push(i).unwrap();
|
||||
}
|
||||
for (i, j) in q.into_iter().enumerate() {
|
||||
assert_eq!(i, j);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
extern crate crossbeam_queue;
|
||||
extern crate crossbeam_utils;
|
||||
extern crate rand;
|
||||
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use crossbeam_queue::SegQueue;
|
||||
|
@ -12,11 +8,11 @@ use rand::{thread_rng, Rng};
|
|||
fn smoke() {
|
||||
let q = SegQueue::new();
|
||||
q.push(7);
|
||||
assert_eq!(q.pop(), Ok(7));
|
||||
assert_eq!(q.pop(), Some(7));
|
||||
|
||||
q.push(8);
|
||||
assert_eq!(q.pop(), Ok(8));
|
||||
assert!(q.pop().is_err());
|
||||
assert_eq!(q.pop(), Some(8));
|
||||
assert!(q.pop().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -24,17 +20,17 @@ fn len_empty_full() {
|
|||
let q = SegQueue::new();
|
||||
|
||||
assert_eq!(q.len(), 0);
|
||||
assert_eq!(q.is_empty(), true);
|
||||
assert!(q.is_empty());
|
||||
|
||||
q.push(());
|
||||
|
||||
assert_eq!(q.len(), 1);
|
||||
assert_eq!(q.is_empty(), false);
|
||||
assert!(!q.is_empty());
|
||||
|
||||
q.pop().unwrap();
|
||||
|
||||
assert_eq!(q.len(), 0);
|
||||
assert_eq!(q.is_empty(), true);
|
||||
assert!(q.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -58,6 +54,9 @@ fn len() {
|
|||
|
||||
#[test]
|
||||
fn spsc() {
|
||||
#[cfg(miri)]
|
||||
const COUNT: usize = 100;
|
||||
#[cfg(not(miri))]
|
||||
const COUNT: usize = 100_000;
|
||||
|
||||
let q = SegQueue::new();
|
||||
|
@ -66,24 +65,28 @@ fn spsc() {
|
|||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
loop {
|
||||
if let Ok(x) = q.pop() {
|
||||
if let Some(x) = q.pop() {
|
||||
assert_eq!(x, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(q.pop().is_err());
|
||||
assert!(q.pop().is_none());
|
||||
});
|
||||
scope.spawn(|_| {
|
||||
for i in 0..COUNT {
|
||||
q.push(i);
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mpmc() {
|
||||
#[cfg(miri)]
|
||||
const COUNT: usize = 50;
|
||||
#[cfg(not(miri))]
|
||||
const COUNT: usize = 25_000;
|
||||
const THREADS: usize = 4;
|
||||
|
||||
|
@ -95,7 +98,7 @@ fn mpmc() {
|
|||
scope.spawn(|_| {
|
||||
for _ in 0..COUNT {
|
||||
let n = loop {
|
||||
if let Ok(x) = q.pop() {
|
||||
if let Some(x) = q.pop() {
|
||||
break x;
|
||||
}
|
||||
};
|
||||
|
@ -110,7 +113,8 @@ fn mpmc() {
|
|||
}
|
||||
});
|
||||
}
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
for c in v {
|
||||
assert_eq!(c.load(Ordering::SeqCst), THREADS);
|
||||
|
@ -119,6 +123,10 @@ fn mpmc() {
|
|||
|
||||
#[test]
|
||||
fn drops() {
|
||||
let runs: usize = if cfg!(miri) { 5 } else { 100 };
|
||||
let steps: usize = if cfg!(miri) { 50 } else { 10_000 };
|
||||
let additional: usize = if cfg!(miri) { 100 } else { 1_000 };
|
||||
|
||||
static DROPS: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
@ -132,9 +140,9 @@ fn drops() {
|
|||
|
||||
let mut rng = thread_rng();
|
||||
|
||||
for _ in 0..100 {
|
||||
let steps = rng.gen_range(0, 10_000);
|
||||
let additional = rng.gen_range(0, 1000);
|
||||
for _ in 0..runs {
|
||||
let steps = rng.gen_range(0..steps);
|
||||
let additional = rng.gen_range(0..additional);
|
||||
|
||||
DROPS.store(0, Ordering::SeqCst);
|
||||
let q = SegQueue::new();
|
||||
|
@ -142,7 +150,7 @@ fn drops() {
|
|||
scope(|scope| {
|
||||
scope.spawn(|_| {
|
||||
for _ in 0..steps {
|
||||
while q.pop().is_err() {}
|
||||
while q.pop().is_none() {}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -151,7 +159,8 @@ fn drops() {
|
|||
q.push(DropCounter);
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
for _ in 0..additional {
|
||||
q.push(DropCounter);
|
||||
|
@ -162,3 +171,25 @@ fn drops() {
|
|||
assert_eq!(DROPS.load(Ordering::SeqCst), steps + additional);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn into_iter() {
|
||||
let q = SegQueue::new();
|
||||
for i in 0..100 {
|
||||
q.push(i);
|
||||
}
|
||||
for (i, j) in q.into_iter().enumerate() {
|
||||
assert_eq!(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn into_iter_drop() {
|
||||
let q = SegQueue::new();
|
||||
for i in 0..100 {
|
||||
q.push(i);
|
||||
}
|
||||
for (i, j) in q.into_iter().enumerate().take(50) {
|
||||
assert_eq!(i, j);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,8 +25,8 @@ webrender_bindings = { path = "../../../../gfx/webrender_bindings" }
|
|||
cubeb-coreaudio = { git = "https://github.com/mozilla/cubeb-coreaudio-rs", rev = "93b5c01a131f65c83c11aeb317f4583405c5eb79", optional = true }
|
||||
cubeb-pulse = { git = "https://github.com/mozilla/cubeb-pulse-rs", rev="cf48897be5cbe147d051ebbbe1eaf5fd8fb6bbc9", optional = true, features=["pulse-dlopen"] }
|
||||
cubeb-sys = { version = "0.10.3", optional = true, features=["gecko-in-tree"] }
|
||||
audioipc2-client = { git = "https://github.com/kinetiknz/audioipc-2", rev = "73c8a02da8f2ff022723307bfafa3a58a61448da", optional = true } # macos (v2) branch
|
||||
audioipc2-server = { git = "https://github.com/kinetiknz/audioipc-2", rev = "73c8a02da8f2ff022723307bfafa3a58a61448da", optional = true } # macos (v2) branch
|
||||
audioipc2-client = { git = "https://github.com/kinetiknz/audioipc-2", rev = "916f65cc92f6f2484183ff4681b0e9a2bfd60fe7", optional = true } # macos (v2) branch
|
||||
audioipc2-server = { git = "https://github.com/kinetiknz/audioipc-2", rev = "916f65cc92f6f2484183ff4681b0e9a2bfd60fe7", optional = true } # macos (v2) branch
|
||||
audioipc-client = { git = "https://github.com/mozilla/audioipc", rev = "fb7a2b12ced3b43e6a268621989c6191d1ed7e39", optional = true }
|
||||
audioipc-server = { git = "https://github.com/mozilla/audioipc", rev = "fb7a2b12ced3b43e6a268621989c6191d1ed7e39", optional = true }
|
||||
# Force tokio-reactor on an old version to avoid new dependencies of newer versions.
|
||||
|
|
Загрузка…
Ссылка в новой задаче