You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let prev_day = self.today.swap(now,Ordering::Relaxed);
82
94
83
95
if prev_day != now {
84
96
let new_counter:*mutCounts = Box::into_raw(Box::new(HashMap::new()));
85
97
86
-
// Release to synchronize-with `counter`. We don't need Acquire because we won't read the previous pointer.
87
-
let prev = guard.swap(&self.counter, new_counter,Ordering::Release);
88
-
// SAFETY: The pointer can no longer be accessed now that it has been swapped into `prev`, and `Box::from_raw` is the correct way to drop the pointer.
89
-
unsafe{
90
-
self.collector
91
-
.retire(prev, |ptr, _| drop(Box::from_raw(ptr)));
92
-
}
98
+
// We need this guard to go into our task so it needs to be owned
99
+
let guard_owned = self.collector.enter();
100
+
101
+
// Release to synchronize-with `counter` and Acquire to ensure that we can see the initialization of the previous one so that we can properly access and write it.
102
+
let prev_ptr = guard_owned.swap(&self.counter, new_counter,Ordering::AcqRel);
103
+
104
+
let output = Arc::clone(&self.output);
105
+
106
+
// Allow it to be moved to our task
107
+
let prev_ptr = prev_ptr asusize;
108
+
109
+
let this_collector = Arc::clone(&self.collector);
110
+
111
+
tokio::task::spawn_blocking(move || {
112
+
letmut output = output.lock().unwrap();
113
+
114
+
let prev_ptr = prev_ptr as*mutCounts;
115
+
// SAFETY: Since this pointer hasn't been retired yet, we have access to it until we do retire it.
116
+
let prev = unsafe{&*prev_ptr }.pin();
117
+
118
+
for((from, to, started_from), count)in&prev {
119
+
let count = count.load(Ordering::Relaxed);
120
+
ifletErr(e) = output.write_fmt(format_args!(
121
+
"{prev_day},{from},{to},{started_from},{count}\n"
122
+
)){
123
+
error!("Error writing statistics: {e}");
124
+
}
125
+
}
126
+
127
+
// SAFETY: The pointer can no longer be accessed from a new location since we previously overwrote the atomic pointer, and `Box::from_raw` is the correct way to drop the pointer. This task is also finished with its access to it.
0 commit comments