-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Send/Sync audit for libcollections #22715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,7 @@ use ptr; | |
use ptr::PtrExt; | ||
use mem; | ||
use mem::size_of; | ||
use marker::{Sized, self}; | ||
use marker::{Send, Sized, Sync, self}; | ||
use raw::Repr; | ||
// Avoid conflicts with *both* the Slice trait (buggy) and the `slice::raw` module. | ||
use raw::Slice as RawSlice; | ||
|
@@ -740,6 +740,9 @@ pub struct Iter<'a, T: 'a> { | |
_marker: marker::PhantomData<&'a T>, | ||
} | ||
|
||
unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {} | ||
unsafe impl<'a, T: Sync> Send for Iter<'a, T> {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wow that is subtle, looks like I should read this again. |
||
|
||
#[unstable(feature = "core")] | ||
impl<'a, T> ops::Index<ops::Range<usize>> for Iter<'a, T> { | ||
type Output = [T]; | ||
|
@@ -830,6 +833,8 @@ pub struct IterMut<'a, T: 'a> { | |
_marker: marker::PhantomData<&'a mut T>, | ||
} | ||
|
||
unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {} | ||
unsafe impl<'a, T: Send> Send for IterMut<'a, T> {} | ||
|
||
#[unstable(feature = "core")] | ||
impl<'a, T> ops::Index<ops::Range<usize>> for IterMut<'a, T> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![allow(unused_mut)] | ||
#![feature(collections)] | ||
|
||
extern crate collections; | ||
|
||
use collections::BinaryHeap; | ||
use collections::{BitSet, BitVec}; | ||
use collections::{BTreeMap, BTreeSet}; | ||
use collections::EnumSet; | ||
use collections::LinkedList; | ||
use collections::Vec; | ||
use collections::VecDeque; | ||
use collections::VecMap; | ||
|
||
use collections::Bound::Included; | ||
use collections::enum_set::CLike; | ||
use std::mem; | ||
|
||
fn is_sync<T>(_: T) where T: Sync {} | ||
fn is_send<T>(_: T) where T: Send {} | ||
|
||
macro_rules! all_sync_send { | ||
($ctor:expr, $($iter:ident),+) => ({ | ||
$( | ||
let mut x = $ctor; | ||
is_sync(x.$iter()); | ||
let mut y = $ctor; | ||
is_send(y.$iter()); | ||
)+ | ||
}) | ||
} | ||
|
||
macro_rules! is_sync_send { | ||
($ctor:expr, $iter:ident($($param:expr),+)) => ({ | ||
let mut x = $ctor; | ||
is_sync(x.$iter($( $param ),+)); | ||
let mut y = $ctor; | ||
is_send(y.$iter($( $param ),+)); | ||
}) | ||
} | ||
|
||
fn main() { | ||
// The iterator "generator" list should exhaust what corresponding | ||
// implementations have where `Sync` and `Send` semantics apply. | ||
all_sync_send!(BinaryHeap::<usize>::new(), iter, drain, into_iter); | ||
|
||
all_sync_send!(BitVec::new(), iter); | ||
|
||
all_sync_send!(BitSet::new(), iter); | ||
is_sync_send!(BitSet::new(), union(&BitSet::new())); | ||
is_sync_send!(BitSet::new(), intersection(&BitSet::new())); | ||
is_sync_send!(BitSet::new(), difference(&BitSet::new())); | ||
is_sync_send!(BitSet::new(), symmetric_difference(&BitSet::new())); | ||
|
||
all_sync_send!(BTreeMap::<usize, usize>::new(), iter, iter_mut, into_iter, keys, values); | ||
is_sync_send!(BTreeMap::<usize, usize>::new(), range(Included(&0), Included(&9))); | ||
is_sync_send!(BTreeMap::<usize, usize>::new(), range_mut(Included(&0), Included(&9))); | ||
|
||
all_sync_send!(BTreeSet::<usize>::new(), iter, into_iter); | ||
is_sync_send!(BTreeSet::<usize>::new(), range(Included(&0), Included(&9))); | ||
is_sync_send!(BTreeSet::<usize>::new(), difference(&BTreeSet::<usize>::new())); | ||
is_sync_send!(BTreeSet::<usize>::new(), symmetric_difference(&BTreeSet::<usize>::new())); | ||
is_sync_send!(BTreeSet::<usize>::new(), intersection(&BTreeSet::<usize>::new())); | ||
is_sync_send!(BTreeSet::<usize>::new(), union(&BTreeSet::<usize>::new())); | ||
|
||
all_sync_send!(LinkedList::<usize>::new(), iter, iter_mut, into_iter); | ||
|
||
#[derive(Copy)] | ||
#[repr(usize)] | ||
#[allow(dead_code)] | ||
enum Foo { A, B, C } | ||
impl CLike for Foo { | ||
fn to_usize(&self) -> usize { | ||
*self as usize | ||
} | ||
|
||
fn from_usize(v: usize) -> Foo { | ||
unsafe { mem::transmute(v) } | ||
} | ||
} | ||
all_sync_send!(EnumSet::<Foo>::new(), iter); | ||
|
||
all_sync_send!(VecDeque::<usize>::new(), iter, iter_mut, drain, into_iter); | ||
|
||
all_sync_send!(VecMap::<usize>::new(), iter, iter_mut, drain, into_iter, keys, values); | ||
|
||
all_sync_send!(Vec::<usize>::new(), into_iter, drain); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see what you mean about
RawItems
now. Could theseunsafe impl
blocks move onto theRawItems
structure itself instead? cc @gankro (you may know more)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So raw items has basically two functions: drain items out of an allocation, and push items into an allocation. No bounds checks. No lifetimes. It's like straight up
*mut [T]
/UncheckedVec
.But critically it doesn't update the source node's
len
field, becauselen
is shared between three "independent" arrays. It also has no lifetimes attached, so you're free to Drop it or its backing allocation. This means that if youSend
a RawItems alone to another thread, you basically have no idea what the state of your node's memory is at that point. It's not even sound toSync
RawItems since it's possible toDrop
the parent node with an outstanding RawItems.It's very much so an internal utility to make what is basically *mut [T] manipulation a bit nicer.