chaokunyang commented on code in PR #2810:
URL: https://github.com/apache/fory/pull/2810#discussion_r2452208004
##########
rust/fory-core/src/util.rs:
##########
@@ -129,3 +131,32 @@ pub fn get_ext_actual_type_id(type_id: u32,
register_by_name: bool) -> u32 {
TypeId::EXT as u32
}
}
+
+pub struct Spinlock<T> {
+ data: UnsafeCell<T>,
+ flag: AtomicBool,
+}
+
+unsafe impl<T: Send + Sync> Send for Spinlock<T> {}
+unsafe impl<T: Send + Sync> Sync for Spinlock<T> {}
+
+impl<T> Spinlock<T> {
+ pub fn new(data: T) -> Self {
+ Spinlock {
+ data: UnsafeCell::new(data),
+ flag: AtomicBool::new(false),
+ }
+ }
+
+ pub fn lock(&self) -> &mut T {
Review Comment:
do we need to return a guard, so the unlock can be invoked always:
```rust
pub struct SpinlockGuard<'a, T> {
lock: &'a Spinlock<T>,
}
impl<'a, T> Drop for SpinlockGuard<'a, T> {
fn drop(&mut self) {
self.lock.unlock();
}
}
impl<'a, T> std::ops::Deref for SpinlockGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { &*self.lock.data.get() }
}
}
impl<'a, T> std::ops::DerefMut for SpinlockGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *self.lock.data.get() }
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]