-
Notifications
You must be signed in to change notification settings - Fork 421
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
keep knowledge of ongoing merges across merge pipelines #5633
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,108 @@ | ||||||
use std::mem::MaybeUninit; | ||||||
use std::ops::Deref; | ||||||
use std::sync::atomic::{AtomicBool, Ordering}; | ||||||
use std::sync::mpsc::{channel, Receiver, Sender}; | ||||||
use std::sync::{Arc, Mutex}; | ||||||
|
||||||
use census::{Inventory, TrackedObject as InventoredObject}; | ||||||
|
||||||
pub type TrackedObject<T> = InventoredObject<RecordUnacknoledgedDrop<T>>; | ||||||
|
||||||
#[derive(Clone)] | ||||||
pub struct Tracker<T> { | ||||||
trinity-1686a marked this conversation as resolved.
Show resolved
Hide resolved
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. This struct needs a comment explaining its point. |
||||||
inner_inventory: Inventory<RecordUnacknoledgedDrop<T>>, | ||||||
unacknoledged_drop_receiver: Arc<Mutex<Receiver<T>>>, | ||||||
return_channel: Sender<T>, | ||||||
} | ||||||
|
||||||
#[derive(Debug)] | ||||||
pub struct RecordUnacknoledgedDrop<T> { | ||||||
// safety: this is always kept initialized except after Self::drop, where we move that | ||||||
// that value away to either send it through the return channel, or drop it manually | ||||||
inner: MaybeUninit<T>, | ||||||
acknoledged: AtomicBool, | ||||||
return_channel: Sender<T>, | ||||||
} | ||||||
|
||||||
impl<T> RecordUnacknoledgedDrop<T> { | ||||||
pub fn acknoledge(&self) { | ||||||
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. typo (acknowledge) 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.
Suggested change
|
||||||
self.acknoledged.store(true, Ordering::Relaxed); | ||||||
} | ||||||
|
||||||
pub fn untracked(value: T) -> Self { | ||||||
let (sender, _receiver) = channel(); | ||||||
RecordUnacknoledgedDrop { | ||||||
inner: MaybeUninit::new(value), | ||||||
acknoledged: true.into(), | ||||||
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. nitpick... (this helps the reader)
Suggested change
|
||||||
return_channel: sender, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
impl<T> Deref for RecordUnacknoledgedDrop<T> { | ||||||
type Target = T; | ||||||
fn deref(&self) -> &T { | ||||||
unsafe { | ||||||
// safety: see struct definition, this operation is valid except after drop. | ||||||
self.inner.assume_init_ref() | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
impl<T> Drop for RecordUnacknoledgedDrop<T> { | ||||||
fn drop(&mut self) { | ||||||
let item = unsafe { | ||||||
// safety: see struct definition. Additionally, we don't touch to self.inner | ||||||
// after this point so there is no risk of making a 2nd copy and cause a | ||||||
// double-free | ||||||
self.inner.assume_init_read() | ||||||
}; | ||||||
if !*self.acknoledged.get_mut() { | ||||||
// if send fails, no one cared about getting that notification, it's fine to | ||||||
// drop item | ||||||
let _ = self.return_channel.send(item); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
impl<T> Default for Tracker<T> { | ||||||
fn default() -> Self { | ||||||
Self::new() | ||||||
} | ||||||
} | ||||||
|
||||||
impl<T> Tracker<T> { | ||||||
pub fn new() -> Self { | ||||||
let (sender, receiver) = channel(); | ||||||
Tracker { | ||||||
inner_inventory: Inventory::new(), | ||||||
unacknoledged_drop_receiver: Arc::new(Mutex::new(receiver)), | ||||||
return_channel: sender, | ||||||
} | ||||||
} | ||||||
|
||||||
pub fn rebuildable_from_the_void(&self) -> bool { | ||||||
Arc::strong_count(&self.unacknoledged_drop_receiver) == 1 && self.inner_inventory.len() == 0 | ||||||
} | ||||||
|
||||||
pub fn list_ongoing(&self) -> Vec<TrackedObject<T>> { | ||||||
self.inner_inventory.list() | ||||||
} | ||||||
|
||||||
pub fn take_dead(&self) -> Vec<T> { | ||||||
let mut res = Vec::new(); | ||||||
let receiver = self.unacknoledged_drop_receiver.lock().unwrap(); | ||||||
while let Ok(dead_entry) = receiver.try_recv() { | ||||||
res.push(dead_entry); | ||||||
} | ||||||
res | ||||||
} | ||||||
|
||||||
pub fn track(&self, value: T) -> TrackedObject<T> { | ||||||
self.inner_inventory.track(RecordUnacknoledgedDrop { | ||||||
inner: MaybeUninit::new(value), | ||||||
acknoledged: false.into(), | ||||||
return_channel: self.return_channel.clone(), | ||||||
}) | ||||||
} | ||||||
} |
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.
typo