Skip to content

Commit

Permalink
lib: Make objects Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Drakulix committed Nov 21, 2024
1 parent 634cf78 commit d137d3a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ impl<T> Drop for PtrDrop<T> {

#[derive(Clone)]
pub(crate) struct Ptr<T>(Arc<PtrDrop<T>>);
// SAFETY: The types used with Ptr in this crate are all Send (namely gbm_device, gbm_surface and gbm_bo).
// SAFETY: The types used with Ptr in this crate are all Send and Sync (namely gbm_device, gbm_surface and gbm_bo).
// Reference counting is implemented with the thread-safe atomic `Arc`-wrapper.
// The type is private and can thus not be used unsoundly by other crates.
unsafe impl<T> Send for Ptr<T> {}
unsafe impl<T> Sync for Ptr<T> {}

impl<T> Ptr<T> {
fn new<F: FnOnce(*mut T) + Send + 'static>(ptr: *mut T, destructor: F) -> Ptr<T> {
Expand Down Expand Up @@ -183,27 +185,46 @@ impl<T> fmt::Pointer for WeakPtr<T> {
}

unsafe impl<T> Send for WeakPtr<T> where Ptr<T>: Send {}
unsafe impl<T> Sync for WeakPtr<T> where Ptr<T>: Sync {}

#[cfg(test)]
mod test {
use std::os::unix::io::OwnedFd;

fn is_send<T: Send>() {}
fn is_sync<T: Sync>() {}

#[test]
fn device_is_send() {
is_send::<super::Device<std::fs::File>>();
is_send::<super::Device<OwnedFd>>();
}

#[test]
fn device_is_sync() {
is_sync::<super::Device<std::fs::File>>();
is_sync::<super::Device<OwnedFd>>();
}

#[test]
fn surface_is_send() {
is_send::<super::Surface<std::fs::File>>();
is_send::<super::Surface<OwnedFd>>();
}

#[test]
fn surface_is_sync() {
is_sync::<super::Surface<std::fs::File>>();
is_sync::<super::Surface<OwnedFd>>();
}

#[test]
fn unmapped_bo_is_send() {
is_send::<super::BufferObject<()>>();
}

#[test]
fn unmapped_bo_is_sync() {
is_sync::<super::BufferObject<()>>();
}
}

0 comments on commit d137d3a

Please sign in to comment.