Skip to main content

flow_id_rs/
lib.rs

1// Copyright 2026 The Fuchsia Authors
2//
3// Use of this source code is governed by a MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT
6
7#![no_std]
8
9use core::sync::atomic::{AtomicU64, Ordering};
10
11// Decrease the likelihood of collisions with zero-based userspace flow ID
12// generators by starting in the second half of the flow ID space.
13const FIRST_KERNEL_FLOW_ID: u64 = 1 << 63;
14
15static FLOW_ID_GENERATOR: AtomicU64 = AtomicU64::new(FIRST_KERNEL_FLOW_ID);
16
17/// Generates globally unique 64-bit flow IDs for tracing.
18pub fn generate() -> u64 {
19    FLOW_ID_GENERATOR.fetch_add(1, Ordering::Relaxed)
20}
21
22/// Generates globally unique 64-bit flow IDs for tracing (C-exported).
23#[unsafe(no_mangle)]
24pub extern "C" fn flow_id_generate() -> u64 {
25    generate()
26}