-
Notifications
You must be signed in to change notification settings - Fork 111
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
What to do when loom::AtomicUsize do not implement as_mut_ptr() #298
Comments
I just found that #154 maybe relates to this. |
|
So to achieve the same semantics and effect, for loom, we should free the 2 pointers (assuming we can get both) instead of one? |
No. When loom's atomic type is freed, the value is also freed, so there is only one pointer that needs to be freed. However, you need to call #[repr(transparent)]
#[derive(Clone, Copy, Debug)]
// Note that NoneNull is used here.
pub struct TaskLocalBytesAllocated(Option<NoneNull<AtomicUsize>>);
impl TaskLocalBytesAllocated {
#[inline(always)]
pub(crate) fn sub(&self, val: usize) {
if let Some(bytes_ptr) = self.0 {
let bytes_ref: &AtomicUsize = unsafe { bytes_ptr.as_ref() };
let old_bytes = bytes_ref.fetch_sub(val, Ordering::Release);
if old_bytes == val {
fence(Ordering::Acquire);
// Note that this is a pointer to AtomicUsize.
// Since I don't know the full context of your code,
// I'm assuming here that the object allocated is only AtomicUsize,
// but if you allocate a structure containing AtomicUsize, you need
// to call from_raw with a pointer to that entire structure.
let ptr: *mut AtomicUsize = bytes_ptr.as_ptr();
unsafe { Box::from_raw_in(ptr, System) };
}
}
}
} |
Thanks for your help. Another question:
error messages: Is it becasue the MAX_THREADS limit? So we only allowed to spawn 3 thread in max? [3 + 1 (the original thread) = 4?] |
I think this is due to panic during panic as well as #291. |
I opened PR #297 which should fix that panic-in-panic SIGABRT error :) |
Basically same as #6822. But we reuse the Counter to avoid code duplicate. Copy the content here. Use https://github.com/tokio-rs/loom for concurrency test. Maybe can make the correctness more confident. But I'm not so sure how to write the best test. it requires some code change for previous structure, so may need discuss: remove workspacke hack in task_stats_alloc crate. Otherwise there will be package conflict if you run RUSTFLAGS="--cfg loom" cargo test --test loom. Have not investigate deeply but simple remove just works. Refactor &'static AtomicUsize to be NonNull<AtomicUsize>. This is because the AtomicUsize in loom type do not support .as_mut_ptr (described in What to do when loom::AtomicUsize do not implement as_mut_ptr() tokio-rs/loom#298), so we use NonNull as intermediate workaround, that will add some unsafe code in .add() and .sub(). But seems OK. To test the drop, I have to add a flag var AtomicUsize in .sub() to ensure that the value is dropped. Please provide some suggestions if you have better ideas on how to write test. Approved-By: BugenZhao Approved-By: liurenjie1024 Co-Authored-By: BowenXiao1999 <931759898@qq.com> Co-Authored-By: Bowen <36908971+BowenXiao1999@users.noreply.github.com>
Hi, I was tring to use loom for a special ref counter. Here is the sample code, basically trying to deallocate a AtomicUsize when down to 0.
But I found that loom::AtomicUsize do not have api
.as_mut_ptr()
to get the mutable pointer. What should I do in this case? I read the doc and found that we should implement API for std type if there is API difference. But for this case, it is the loom type that miss the function, how can I implement this? It seems not feasible viawith_mut
.Thanks!
The text was updated successfully, but these errors were encountered: