Skip to content

Commit

Permalink
Change IdCounter to be backed by an AtomicU64
Browse files Browse the repository at this point in the history
Let's see if anyone complains.
  • Loading branch information
YaLTeR committed Aug 31, 2024
1 parent 40dbf83 commit ad46489
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ pub type IpcOutputMap = HashMap<OutputId, niri_ipc::Output>;
static OUTPUT_ID_COUNTER: IdCounter = IdCounter::new();

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OutputId(u32);
pub struct OutputId(u64);

impl OutputId {
fn next() -> OutputId {
OutputId(OUTPUT_ID_COUNTER.next())
}

pub fn get(self) -> u64 {
u64::from(self.0)
self.0
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/layout/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ pub struct OutputId(String);
static WORKSPACE_ID_COUNTER: IdCounter = IdCounter::new();

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WorkspaceId(u32);
pub struct WorkspaceId(u64);

impl WorkspaceId {
fn next() -> WorkspaceId {
WorkspaceId(WORKSPACE_ID_COUNTER.next())
}

pub fn get(self) -> u64 {
u64::from(self.0)
self.0
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/utils/id.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::atomic::{AtomicU64, Ordering};

/// Counter that returns unique IDs.
///
/// Under the hood it uses a `u32` that will eventually wrap around. When incrementing it once a
/// second, it will wrap around after about 136 years.
pub struct IdCounter {
value: AtomicU32,
value: AtomicU64,
}

impl IdCounter {
pub const fn new() -> Self {
Self {
// Start from 1 to reduce the possibility that some other code that uses these IDs will
// get confused.
value: AtomicU32::new(1),
value: AtomicU64::new(1),
}
}

pub fn next(&self) -> u32 {
pub fn next(&self) -> u64 {
self.value.fetch_add(1, Ordering::Relaxed)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/window/mapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ niri_render_elements! {
static MAPPED_ID_COUNTER: IdCounter = IdCounter::new();

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MappedId(u32);
pub struct MappedId(u64);

impl MappedId {
fn next() -> MappedId {
MappedId(MAPPED_ID_COUNTER.next())
}

pub fn get(self) -> u64 {
u64::from(self.0)
self.0
}
}

Expand Down

0 comments on commit ad46489

Please sign in to comment.