Giter Site home page Giter Site logo

Comments (4)

Darksonn avatar Darksonn commented on June 26, 2024

I recommend that you use a type like this one:

use loom::cell::UnsafeCell as LoomCell;
use std::cell::UnsafeCell as StdCell;
use std::mem::MaybeUninit;

struct CellSlice<T> {
    inner: LoomCell<Box<StdCell<[MaybeUninit<T>]>>>,
}

impl<T> CellSlice<T> {
    fn with<F, R>(&self, f: F) -> R
    where
        F: FnOnce(*const T, usize) -> R,
    {
        self.inner.with(|ptr| {
            let ptr = unsafe { StdCell::raw_get(&**ptr) };
            let len = unsafe { (*ptr).len() };
            f(ptr.cast(), len)
        })
    }

    fn with_mut<F, R>(&self, f: F) -> R
    where
        F: FnOnce(*mut T, usize) -> R,
    {
        self.inner.with_mut(|ptr| {
            let ptr = unsafe { StdCell::raw_get(&**ptr) };
            let len = unsafe { (*ptr).len() };
            f(ptr.cast(), len)
        })
    }
}

from loom.

DzenanJupic avatar DzenanJupic commented on June 26, 2024

Wouldn't that lead to a test failure in case someone is reading the already initialized elements, while someone else is writing a new element? [playground]

CellSlice
use loom::cell::UnsafeCell as LoomCell;
use std::cell::UnsafeCell as StdCell;
use std::mem::{self, MaybeUninit};

struct CellSlice<T> {
    inner: LoomCell<Box<StdCell<[MaybeUninit<T>]>>>,
}

#[allow(dead_code)]
impl<T> CellSlice<T> {
    fn new() -> Self {
        let boxed: Box<StdCell<[MaybeUninit<T>]>> = unsafe {
            let boxed = mem::ManuallyDrop::new(Vec::<T>::new().into_boxed_slice());
            std::ptr::read(&boxed as *const _ as *const _)
        };

        Self {
            inner: LoomCell::new(boxed),
        }
    }

    fn with<F, R>(&self, f: F) -> R
        where
            F: FnOnce(*const T, usize) -> R,
    {
        self.inner.with(|ptr| {
            let ptr = unsafe { StdCell::raw_get(&**ptr) };
            let len = unsafe { (*ptr).len() };
            f(ptr.cast(), len)
        })
    }

    fn with_mut<F, R>(&self, f: F) -> R
        where
            F: FnOnce(*mut T, usize) -> R,
    {
        self.inner.with_mut(|ptr| {
            let ptr = unsafe { StdCell::raw_get(&**ptr) };
            let len = unsafe { (*ptr).len() };
            f(ptr.cast(), len)
        })
    }
}
#[test]
fn concurrent_read_and_write() {
    loom::model(|| {
        let cell_slice = std::sync::Arc::new(CellSlice::<String>::new());

        let read = {
            let cell_slice = cell_slice.clone();
            loom::thread::spawn(move || {
                let _ = cell_slice.with(|_, _| {});
            })
        };
        let write = {
            let cell_slice = cell_slice.clone();
            loom::thread::spawn(move || {
                let _ = cell_slice.with_mut(|_, _| {});
            })
        };

        read.join().unwrap();
        write.join().unwrap();
    });
}

from loom.

Darksonn avatar Darksonn commented on June 26, 2024

True, it would not work with per-field concurrency checking. To do that, perhaps you could use a struct like this:

struct CellSlice<T> {
    fields: Box<StdCell<[MaybeUninit<T>]>>,
    loom_check: Box<[LoomCell<()>]>,
}

Then, when accessing field i, you do so inside a with or with_mut call on the ith cell in loom_check.

from loom.

DzenanJupic avatar DzenanJupic commented on June 26, 2024

That's perfect, thanks. Even allows to keep using the 'normal' UnsafeCell interface. Should have thought about that earlier.

from loom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.