From f27cb146691781b916c8e13764a6d3dafde71c15 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Mon, 18 Sep 2023 12:28:19 +0100 Subject: [PATCH] u/limited_unique_id: Add process unique id generator --- src/xrt/auxiliary/util/CMakeLists.txt | 2 + .../auxiliary/util/u_limited_unique_id.cpp | 26 ++++++++++++ src/xrt/auxiliary/util/u_limited_unique_id.h | 40 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 src/xrt/auxiliary/util/u_limited_unique_id.cpp create mode 100644 src/xrt/auxiliary/util/u_limited_unique_id.h diff --git a/src/xrt/auxiliary/util/CMakeLists.txt b/src/xrt/auxiliary/util/CMakeLists.txt index e541ea66c..426cd2300 100644 --- a/src/xrt/auxiliary/util/CMakeLists.txt +++ b/src/xrt/auxiliary/util/CMakeLists.txt @@ -59,6 +59,8 @@ add_library( u_json.c u_json.h u_json.hpp + u_limited_unique_id.cpp + u_limited_unique_id.h u_logging.c u_logging.h u_metrics.c diff --git a/src/xrt/auxiliary/util/u_limited_unique_id.cpp b/src/xrt/auxiliary/util/u_limited_unique_id.cpp new file mode 100644 index 000000000..53f242966 --- /dev/null +++ b/src/xrt/auxiliary/util/u_limited_unique_id.cpp @@ -0,0 +1,26 @@ +// Copyright 2023, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 +/*! + * @file + * @brief A very simple generator to create process unique ids. + * @author Jakob Bornecrantz + * @ingroup aux_util + */ + +#include "u_limited_unique_id.h" + +#include + + +/* + * This code is in C++ and not C because MSVC doesn't implement C atomics yet, + * but to make things even more fun the C++11 standard defines atomic_uint64_t + * as optional, but atomic_uint_fast64_t is listed as required, so here we are. + */ +static std::atomic_uint_fast64_t generator; + +extern "C" xrt_limited_unique_id_t +u_limited_unique_id_get(void) +{ + return xrt_limited_unique_id_t{static_cast(++generator)}; +} diff --git a/src/xrt/auxiliary/util/u_limited_unique_id.h b/src/xrt/auxiliary/util/u_limited_unique_id.h new file mode 100644 index 000000000..fca7c579c --- /dev/null +++ b/src/xrt/auxiliary/util/u_limited_unique_id.h @@ -0,0 +1,40 @@ +// Copyright 2023, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 +/*! + * @file + * @brief A very simple generator to create process unique ids. + * @author Jakob Bornecrantz + * @ingroup aux_util + */ + + +#pragma once + +#include "xrt/xrt_defines.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/*! + * This function returns a unsigned 64 bit value that is guaranteed to be unique + * within the current running process, and not zero. There is of course the + * limit of running out of those ID once all values has been returned, but the + * value is 64 bit so that should not be a practical limit. The value is useful + * when needing to implement caching of a complex object, this lets us not use + * memory addresses as keys which may be reused by underlying alloc + * implementation and could lead to false hits. + * + * The current implementation is naive and is a simple monotonic counter. + * + * @ingroup aux_util + */ +xrt_limited_unique_id_t +u_limited_unique_id_get(void); + + +#ifdef __cplusplus +} +#endif