mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-28 04:35:33 +00:00
0f1c59e62d
Source-Repo: https://github.com/servo/servo Source-Revision: bb916bb38bc4298477bfc8134b94dc906054bce1 --HG-- rename : servo/components/util/str.rs => servo/components/style/str.rs rename : servo/components/util/tid.rs => servo/components/style/tid.rs rename : servo/tests/unit/util/str.rs => servo/tests/unit/style/str.rs
27 lines
783 B
Rust
27 lines
783 B
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
|
|
|
|
static NEXT_TID: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
thread_local!(static TASK_LOCAL_TID: Rc<RefCell<Option<usize>>> = Rc::new(RefCell::new(None)));
|
|
|
|
/// Every thread gets one, that's unique.
|
|
pub fn tid() -> usize {
|
|
TASK_LOCAL_TID.with(|ref k| {
|
|
let ret =
|
|
match *k.borrow() {
|
|
None => NEXT_TID.fetch_add(1, Ordering::SeqCst),
|
|
Some(x) => x,
|
|
};
|
|
|
|
*k.borrow_mut() = Some(ret);
|
|
|
|
ret
|
|
})
|
|
}
|