wayland: use AtomicUsize for SerialCounter

This commit is contained in:
Victor Berger 2018-10-07 22:36:44 +02:00 committed by Victor Berger
parent 8b66680097
commit 1618c45d4e
1 changed files with 9 additions and 13 deletions

View File

@ -12,7 +12,7 @@
//! destroy the global at all, you don't need to bother keeping the //! destroy the global at all, you don't need to bother keeping the
//! `Global` around. //! `Global` around.
use std::sync::Mutex; use std::sync::atomic::{AtomicUsize, Ordering};
pub mod compositor; pub mod compositor;
pub mod output; pub mod output;
@ -20,14 +20,12 @@ pub mod seat;
pub mod shell; pub mod shell;
pub mod shm; pub mod shm;
lazy_static! {
/// A global SerialCounter for use in your compositor. /// A global SerialCounter for use in your compositor.
/// ///
/// Is is also used internally by some parts of Smithay. /// Is is also used internally by some parts of Smithay.
pub static ref SERIAL_COUNTER: SerialCounter = SerialCounter { pub static SERIAL_COUNTER: SerialCounter = SerialCounter {
serial: Mutex::new(0) serial: AtomicUsize::new(0),
}; };
}
/// A counter for generating serials, for use in the client protocol /// A counter for generating serials, for use in the client protocol
/// ///
@ -39,14 +37,12 @@ lazy_static! {
/// as needed. /// as needed.
pub struct SerialCounter { pub struct SerialCounter {
// TODO: replace with an AtomicU32 when stabilized // TODO: replace with an AtomicU32 when stabilized
serial: Mutex<u32>, serial: AtomicUsize,
} }
impl SerialCounter { impl SerialCounter {
/// Retrieve the next serial from the counter /// Retrieve the next serial from the counter
pub fn next_serial(&self) -> u32 { pub fn next_serial(&self) -> u32 {
let guard = self.serial.lock().unwrap(); self.serial.fetch_add(1, Ordering::AcqRel) as u32
guard.wrapping_add(1);
*guard
} }
} }