Skip to main content

qubit_executor/hook/
task_id.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10use qubit_atomic::atomic::primitive::AtomicU64;
11
12/// Unique identifier assigned to an accepted task.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct TaskId(u64);
15
16impl TaskId {
17    /// Creates a task id from its raw numeric value.
18    ///
19    /// # Parameters
20    ///
21    /// * `value` - Raw task id value.
22    ///
23    /// # Returns
24    ///
25    /// A task id wrapping `value`.
26    #[inline]
27    pub const fn new(value: u64) -> Self {
28        Self(value)
29    }
30
31    /// Returns the raw numeric task id value.
32    ///
33    /// # Returns
34    ///
35    /// The raw task id value.
36    #[inline]
37    pub const fn get(self) -> u64 {
38        self.0
39    }
40}
41
42static NEXT_TASK_ID: AtomicU64 = AtomicU64::new(1);
43
44/// Allocates a fresh task id.
45///
46/// # Returns
47///
48/// A task id unique within this process until the counter wraps.
49#[inline]
50pub(crate) fn next_task_id() -> TaskId {
51    TaskId(NEXT_TASK_ID.fetch_inc())
52}