aboutsummaryrefslogtreecommitdiffstats
path: root/components/util/tid.rs
blob: fdd9a775bcd86640f7d615e5918f258cd7099849 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use std::sync::atomics::{AtomicUint, INIT_ATOMIC_UINT, SeqCst};

static mut next_tid: AtomicUint = INIT_ATOMIC_UINT;

local_data_key!(task_local_tid: uint)

/// Every task gets one, that's unique.
pub fn tid() -> uint {
    let ret =
        match task_local_tid.replace(None) {
            None => unsafe { next_tid.fetch_add(1, SeqCst) },
            Some(x) => x,
        };

    task_local_tid.replace(Some(ret));

    ret
}