|
VLink 2.0.0
A high-performance communication middleware
|
Platform-agnostic system utility functions. 更多...
函数 | |
| VLINK_EXPORT std::string | get_app_path () noexcept |
| Returns the absolute path of the running executable. | |
| VLINK_EXPORT std::string | get_app_dir () noexcept |
| Returns the directory containing the running executable. | |
| VLINK_EXPORT std::string | get_app_name () noexcept |
| Returns the file name of the running executable (without directory prefix). | |
| VLINK_EXPORT std::string | get_host_name () noexcept |
| Returns the host name of the current machine. | |
| VLINK_EXPORT int32_t | get_pid () noexcept |
| Returns the process ID of the current process. | |
| VLINK_EXPORT std::string | get_pid_str () noexcept |
| Returns the process ID of the current process as a decimal string. | |
| VLINK_EXPORT std::string | get_tmp_dir () noexcept |
| Returns the platform-specific temporary directory path. | |
| VLINK_EXPORT std::string | get_env (const std::string &key, const std::string &default_value="") noexcept |
| Reads the value of an environment variable. | |
| VLINK_EXPORT bool | set_env (const std::string &key, const std::string &value, bool force=true) noexcept |
| Sets or updates an environment variable. | |
| VLINK_EXPORT bool | unset_env (const std::string &key) noexcept |
| Removes an environment variable. | |
| VLINK_EXPORT std::vector< std::string > | get_all_ipv4_address (bool filter_available=false) noexcept |
| Returns all IPv4 addresses assigned to local network interfaces. | |
| VLINK_EXPORT std::vector< std::string > | get_all_ipv6_address (bool filter_available=false) noexcept |
| Returns all IPv6 addresses assigned to local network interfaces. | |
| VLINK_EXPORT std::string | get_interface_name_by_ipv4 (const std::string &ipv4) noexcept |
| Returns the network interface name that owns a given IPv4 address. | |
| VLINK_EXPORT std::string | get_interface_name_by_ipv6 (const std::string &ipv6) noexcept |
| Returns the network interface name that owns a given IPv6 address. | |
| VLINK_EXPORT std::vector< std::string > | get_dds_default_address (bool filter_available=false, int max_count=5) noexcept |
| Returns suitable IPv4 addresses for use as DDS participant unicast locators. | |
| VLINK_EXPORT bool | check_singleton (const std::string &program_name="") noexcept |
| Checks that only one instance of the process is running (singleton guard). | |
| VLINK_EXPORT bool | wait_for_device (const std::string &path, int timeout_ms, int poll_ms=50) noexcept |
| Blocks until a file-system path appears or the timeout elapses. | |
| VLINK_EXPORT void | yield_cpu () noexcept |
| Emits a CPU pause/yield hint to reduce bus contention in busy-wait loops. | |
| VLINK_EXPORT void | set_console_utf8_output () noexcept |
| Configures the Windows console for UTF-8 output. | |
| VLINK_EXPORT bool | set_thread_name (const std::string &name, std::thread *thread=nullptr) noexcept |
| Sets the OS-level name of a thread for debugging tools (e.g., gdb, perf). | |
| VLINK_EXPORT bool | set_thread_priority (int priority_level, int policy=-1, std::thread *thread=nullptr) noexcept |
| Sets the scheduling policy and priority of a thread. | |
| VLINK_EXPORT bool | set_thread_stick (uint32_t core_mask, std::thread *thread=nullptr) noexcept |
| Pins a thread to a set of CPU cores specified by a bitmask. | |
| VLINK_EXPORT uint64_t | get_native_thread_id () noexcept |
| Returns the native OS thread identifier of the calling thread. | |
| VLINK_EXPORT void | register_terminate_signal (std::function< void(int)> &&callback, bool is_async=false, bool pass_through=false) noexcept |
| Registers a callback for graceful termination signals (SIGTERM, SIGINT, etc.). | |
| VLINK_EXPORT void | register_crash_signal (std::function< void(int)> &&callback) noexcept |
| Registers a callback for crash signals (SIGSEGV, SIGABRT, SIGFPE, SIGBUS, etc.). | |
| VLINK_EXPORT void | start_detect_keyboard (std::function< void(const std::string &key)> &&callback=nullptr, int poll_ms=20) noexcept |
| Starts a background thread that detects keyboard input. | |
| VLINK_EXPORT void | stop_detect_keyboard () noexcept |
Stops the keyboard detection thread started by start_detect_keyboard(). | |
| VLINK_EXPORT std::pair< int, int > | get_terminal_size () noexcept |
| Returns the current terminal window dimensions. | |
| VLINK_EXPORT double | get_cpu_usage () noexcept |
| Returns the current CPU usage of the process as a percentage. | |
| VLINK_EXPORT double | get_memory_usage () noexcept |
| Returns the resident set size (RSS) of the process as a percentage of total RAM. | |
| VLINK_EXPORT bool | is_process_running (const std::string &process_name) noexcept |
| Checks whether a process with the given name is currently running. | |
| VLINK_EXPORT int32_t | get_timezone_diff () noexcept |
| Returns the local timezone offset from UTC in seconds. | |
| VLINK_EXPORT std::string | get_machine_id () noexcept |
| Returns a unique identifier for the current machine. | |
| VLINK_EXPORT void | try_release_sys_memory () noexcept |
| Hints to the OS that any unreferenced cached memory pages can be released. | |
Platform-agnostic system utility functions.
|
nodiscardnoexcept |
Checks that only one instance of the process is running (singleton guard).
Uses a lock file or named semaphore to ensure mutual exclusion. Returns false if another instance is already running.
| program_name | Program name used for the lock. Defaults to the executable name. |
true if this is the only running instance.
|
nodiscardnoexcept |
Returns all IPv4 addresses assigned to local network interfaces.
| filter_available | If true, only return addresses on interfaces that are UP. Default: false. |
|
nodiscardnoexcept |
Returns all IPv6 addresses assigned to local network interfaces.
| filter_available | If true, only return addresses on interfaces that are UP. Default: false. |
|
nodiscardnoexcept |
Returns the directory containing the running executable.
|
nodiscardnoexcept |
Returns the file name of the running executable (without directory prefix).
|
nodiscardnoexcept |
Returns the absolute path of the running executable.
|
nodiscardnoexcept |
Returns the current CPU usage of the process as a percentage.
Computes (user_time + kernel_time) / elapsed_time * 100. The value is a snapshot since the last call and may return 0 on the first invocation.
|
nodiscardnoexcept |
Returns suitable IPv4 addresses for use as DDS participant unicast locators.
Filters out loopback and link-local addresses, preferring routable unicast addresses.
| filter_available | If true, only return addresses on UP interfaces. Default: false. |
| max_count | Maximum number of addresses to return. Default: 5. |
|
nodiscardnoexcept |
Reads the value of an environment variable.
| key | Name of the environment variable. |
| default_value | Value returned if the variable is not set. Default: empty string. |
default_value if not found.
|
nodiscardnoexcept |
Returns the host name of the current machine.
gethostname(), or empty string on failure.
|
nodiscardnoexcept |
Returns the network interface name that owns a given IPv4 address.
| ipv4 | Dotted-decimal IPv4 address to look up. |
"eth0"), or empty string if not found.
|
nodiscardnoexcept |
Returns the network interface name that owns a given IPv6 address.
| ipv6 | IPv6 address string to look up. |
|
nodiscardnoexcept |
Returns a unique identifier for the current machine.
On Linux, reads /etc/machine-id. On other platforms a platform-specific identifier is used.
|
nodiscardnoexcept |
Returns the resident set size (RSS) of the process as a percentage of total RAM.
|
nodiscardnoexcept |
Returns the native OS thread identifier of the calling thread.
Returns pthread_self() on POSIX or GetCurrentThreadId() on Windows. The value can be used with profiling tools or for thread identification in logs.
|
nodiscardnoexcept |
Returns the process ID of the current process.
|
nodiscardnoexcept |
Returns the process ID of the current process as a decimal string.
|
nodiscardnoexcept |
Returns the current terminal window dimensions.
{columns, rows}. Returns {0, 0} on failure or if not a tty.
|
nodiscardnoexcept |
Returns the local timezone offset from UTC in seconds.
For example, UTC+8 returns 28800, UTC-5 returns -18000.
|
nodiscardnoexcept |
Returns the platform-specific temporary directory path.
Returns /tmp on Linux/macOS, TEMP% on Windows, or the equivalent on other platforms.
|
nodiscardnoexcept |
Checks whether a process with the given name is currently running.
| process_name | Executable name to search for (without path). |
true if at least one process with that name is running.
|
noexcept |
Registers a callback for crash signals (SIGSEGV, SIGABRT, SIGFPE, SIGBUS, etc.).
Useful for dumping logs or state before an unrecoverable crash. The callback should be async-signal-safe or very short.
| callback | Callback invoked with the signal number when a crash signal fires. |
|
noexcept |
Registers a callback for graceful termination signals (SIGTERM, SIGINT, etc.).
On POSIX, installs a sigaction handler for SIGTERM and SIGINT. The callback receives the signal number as its argument.
| callback | Callback invoked when a termination signal arrives. |
| is_async | If true, the callback runs asynchronously in a dedicated thread. Default: false (synchronous in the signal context). |
| pass_through | If true, re-raise the signal after the callback returns so that the default OS behaviour (core dump, etc.) also occurs. Default: false. |
|
noexcept |
Configures the Windows console for UTF-8 output.
Calls SetConsoleOutputCP(CP_UTF8) on Windows. No-op on other platforms.
|
noexcept |
Sets or updates an environment variable.
| key | Name of the environment variable. |
| value | New value. |
| force | If true, overwrite an existing variable. Default: true. |
true on success.
|
noexcept |
Sets the OS-level name of a thread for debugging tools (e.g., gdb, perf).
| name | Thread name string (max 15 characters on Linux due to pthread_setname_np). |
| thread | Thread to rename, or nullptr for the calling thread. Default: nullptr. |
true on success.
|
noexcept |
Sets the scheduling policy and priority of a thread.
On Linux, wraps pthread_setschedparam. Requires appropriate CAP_SYS_NICE or RLIMIT_RTPRIO permissions for real-time policies.
| priority_level | Scheduling priority (policy-dependent range). |
| policy | Scheduling policy (e.g., SCHED_FIFO, SCHED_RR, SCHED_OTHER). Pass -1 to keep the current policy. Default: -1. |
| thread | Thread to configure, or nullptr for the calling thread. Default: nullptr. |
true on success.
|
noexcept |
Pins a thread to a set of CPU cores specified by a bitmask.
Wraps pthread_setaffinity_np on Linux. Bit i of core_mask corresponds to core i. For example, core_mask == 0b0101 pins to cores 0 and 2.
| core_mask | Bitmask of CPU cores (bit 0 = core 0). |
| thread | Thread to pin, or nullptr for the calling thread. Default: nullptr. |
true on success.
|
noexcept |
Starts a background thread that detects keyboard input.
Polls stdin every poll_ms milliseconds for a key press. When a key is detected, callback is invoked with the key name as a string (e.g., "enter", "q"). Stop the detector with stop_detect_keyboard().
| callback | Callback invoked with the key name, or nullptr to ignore. Default: nullptr. |
| poll_ms | Polling interval in milliseconds. Default: 20. |
|
noexcept |
|
noexcept |
Hints to the OS that any unreferenced cached memory pages can be released.
On Linux calls malloc_trim(0) to return freed memory from the glibc heap to the OS. On other platforms this is a no-op.
|
noexcept |
Removes an environment variable.
| key | Name of the variable to unset. |
true on success.
|
noexcept |
Blocks until a file-system path appears or the timeout elapses.
Polls the path every poll_ms milliseconds. Useful for waiting for a device node (e.g., /dev/video0) to become available at startup.
| path | File-system path to poll. |
| timeout_ms | Maximum wait time in milliseconds. |
| poll_ms | Polling interval in milliseconds. Default: 50. |
true if the path appeared within the timeout; false on timeout.
|
inlinenoexcept |
Emits a CPU pause/yield hint to reduce bus contention in busy-wait loops.
Details
Issues the most efficient idle instruction for the target:
PAUSE YIELD .word 0x0100000f)std::this_thread::yield() This function is always inlined and has zero call overhead.