Add native self-hosted instance connection to fluxer_desktop
Trimmed monorepo checkout (fluxer_desktop + packages/voice_engine_v2 + tools/ci) with a "Connect to a Different Server" menu item and popout that lets the desktop app switch to any self-hosted Fluxer instance, plus fixes for well-known discovery on single-domain self-hosted deployments and a false-positive ERR_ABORTED on same-origin client redirects during the switch. Defaults to chat.fluxr.chat and uses an isolated userData directory from the official build.
This commit is contained in:
+749
@@ -0,0 +1,749 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "alloca"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e5a7d05ea6aea7e9e64d25b9156ba2fee3fdd659e34e41063cd2fc7cd020d7f4"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anes"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.20.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649"
|
||||
|
||||
[[package]]
|
||||
name = "cast"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.63"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "556e016178bb5662a08681bbe0f00f8e17631781a4dfc8c45e466e4b185ec27f"
|
||||
dependencies = [
|
||||
"find-msvc-tools",
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"ciborium-ll",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-io"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-ll"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"half",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
|
||||
|
||||
[[package]]
|
||||
name = "criterion"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "950046b2aa2492f9a536f5f4f9a3de7b9e2476e575e05bd6c333371add4d98f3"
|
||||
dependencies = [
|
||||
"alloca",
|
||||
"anes",
|
||||
"cast",
|
||||
"ciborium",
|
||||
"clap",
|
||||
"criterion-plot",
|
||||
"itertools",
|
||||
"num-traits",
|
||||
"oorandom",
|
||||
"page_size",
|
||||
"plotters",
|
||||
"rayon",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tinytemplate",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "criterion-plot"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8d80a2f4f5b554395e47b5d8305bc3d27813bacb73493eb1001e8f76dae29ea"
|
||||
dependencies = [
|
||||
"cast",
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
|
||||
dependencies = [
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-epoch"
|
||||
version = "0.9.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "crunchy"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e"
|
||||
|
||||
[[package]]
|
||||
name = "find-msvc-tools"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
|
||||
|
||||
[[package]]
|
||||
name = "fluxer_rt_thread"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"criterion",
|
||||
"libc",
|
||||
"windows",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-core"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"
|
||||
|
||||
[[package]]
|
||||
name = "futures-task"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393"
|
||||
|
||||
[[package]]
|
||||
name = "futures-util"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"pin-project-lite",
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "half"
|
||||
version = "2.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crunchy",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2025f20d7a4fa7785846e7b63d10a76d3f1cee98ee5cb79ea59703f95e42162"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"futures-util",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.186"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b947ae49db0d222b1dbc6b113ce7248a3fc3a6ca21b696717bfc000ba4484d8"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||
|
||||
[[package]]
|
||||
name = "oorandom"
|
||||
version = "11.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
|
||||
|
||||
[[package]]
|
||||
name = "page_size"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "30d5b2194ed13191c1999ae0704b7839fb18384fa22e49b57eeaa97d79ce40da"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
|
||||
|
||||
[[package]]
|
||||
name = "plotters"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
"plotters-backend",
|
||||
"plotters-svg",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "plotters-backend"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
|
||||
|
||||
[[package]]
|
||||
name = "plotters-svg"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
|
||||
dependencies = [
|
||||
"plotters-backend",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon"
|
||||
version = "1.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
|
||||
dependencies = [
|
||||
"either",
|
||||
"rayon-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon-core"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
|
||||
dependencies = [
|
||||
"crossbeam-deque",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.150"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinytemplate"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
|
||||
dependencies = [
|
||||
"same-file",
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.123"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a254a4b10c19a76f09a27640e7ffbf9bc30bf67e16a3bf28aaefa4920fe81563"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
"rustversion",
|
||||
"wasm-bindgen-macro",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.123"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24a40fc75b0ec6f3746ceb10d36f53a93dcd68a93b11b6445983945d79eba0dc"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.123"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "908f34bd9b9ce3d4caf07b72dfab63d61504d156856c6bd3cd87fa350cf3985b"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.123"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7acbf7616c27b194bbb550bf77ed0c2c3e5b7fd1260a93082b95fb7f47959b92"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e0871acf327f283dc6da28a1696cdc64fb355ba9f935d052021fa77f35cce69"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows"
|
||||
version = "0.62.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580"
|
||||
dependencies = [
|
||||
"windows-collections",
|
||||
"windows-core",
|
||||
"windows-future",
|
||||
"windows-numerics",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-collections"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610"
|
||||
dependencies = [
|
||||
"windows-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.62.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
|
||||
dependencies = [
|
||||
"windows-implement",
|
||||
"windows-interface",
|
||||
"windows-link",
|
||||
"windows-result",
|
||||
"windows-strings",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-future"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb"
|
||||
dependencies = [
|
||||
"windows-core",
|
||||
"windows-link",
|
||||
"windows-threading",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-implement"
|
||||
version = "0.60.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-interface"
|
||||
version = "0.59.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-numerics"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26"
|
||||
dependencies = [
|
||||
"windows-core",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-result"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-strings"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-threading"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3b065d4f0e55f82fae73202e189638116a87c55ab6b8e6c2721e13dd9d854ad1"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b631b19d36a892ab55420c92dbc83ccd79274f25be714855d3074aa71cab639"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
|
||||
@@ -0,0 +1,32 @@
|
||||
[package]
|
||||
name = "fluxer_rt_thread"
|
||||
version = "0.0.0"
|
||||
edition = "2024"
|
||||
license = "AGPL-3.0-or-later"
|
||||
publish = false
|
||||
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
|
||||
[lib]
|
||||
crate-type = ["rlib"]
|
||||
|
||||
[dependencies]
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
libc = "0.2"
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
windows = {version = "0.62.2", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_System_Threading",
|
||||
"Win32_Media",
|
||||
"Win32_Media_Multimedia",
|
||||
]}
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.8"
|
||||
|
||||
[[bench]]
|
||||
name = "tick"
|
||||
harness = false
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"measured_at": "85e057a273fd",
|
||||
"host": "darwin-arm64-apple-silicon",
|
||||
"regression_budget_percent": 5.0,
|
||||
"criterion_args": {
|
||||
"warm_up_time_sec": 2,
|
||||
"measurement_time_sec": 5
|
||||
},
|
||||
"benches": {
|
||||
"tick_driver/audio_21_33ms": {
|
||||
"median_ns": 21415000.0,
|
||||
"low_ns": 21395000.0,
|
||||
"high_ns": 21441000.0,
|
||||
"note": "Measures real-time sleep precision against AUDIO_TICK_PERIOD_NS=21333333; baseline reflects clock+scheduler delay on this host, not pure CPU work."
|
||||
},
|
||||
"system_clock_now_ns": {
|
||||
"median_ns": 22.931,
|
||||
"low_ns": 22.763,
|
||||
"high_ns": 23.099,
|
||||
"budget_percent_override": 15.0,
|
||||
"note": "23ns op; clock_gettime variance dominates over 5%. Wider budget reflects the floor of measurement noise."
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
use std::hint::black_box;
|
||||
use std::sync::Arc;
|
||||
|
||||
use criterion::{Criterion, criterion_group, criterion_main};
|
||||
use fluxer_rt_thread::{MonotonicClock, SystemMonotonicClock, TickDriver};
|
||||
|
||||
const AUDIO_TICK_PERIOD_NS: u64 = 21_333_333;
|
||||
|
||||
fn bench_audio_tick(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("tick_driver");
|
||||
group.sample_size(20);
|
||||
group.bench_function("audio_21_33ms", |b| {
|
||||
b.iter_custom(|iters| {
|
||||
let clock = Arc::new(SystemMonotonicClock::new());
|
||||
let mut driver = TickDriver::new(clock.clone(), AUDIO_TICK_PERIOD_NS).expect("driver");
|
||||
let _ = driver.wait_until_next_tick().expect("warmup");
|
||||
let start = std::time::Instant::now();
|
||||
for _ in 0..iters {
|
||||
let info = driver.wait_until_next_tick().expect("tick");
|
||||
black_box(info);
|
||||
}
|
||||
start.elapsed()
|
||||
})
|
||||
});
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_now_ns(c: &mut Criterion) {
|
||||
let clock = SystemMonotonicClock::new();
|
||||
c.bench_function("system_clock_now_ns", |b| {
|
||||
b.iter(|| {
|
||||
let v = MonotonicClock::now_ns(&clock);
|
||||
black_box(v)
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_audio_tick, bench_now_ns);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,12 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
#![deny(clippy::too_many_lines)]
|
||||
|
||||
pub mod priority;
|
||||
pub mod tick;
|
||||
|
||||
pub use priority::{PriorityProfile, RealtimePriorityGuard, RtError, RtOutcome};
|
||||
pub use tick::{MonotonicClock, SystemMonotonicClock, TickDriver, TickInfo};
|
||||
|
||||
pub const NS_PER_MS: u64 = 1_000_000;
|
||||
pub const NS_PER_SEC: u64 = 1_000_000_000;
|
||||
@@ -0,0 +1,399 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
use core::cell::Cell;
|
||||
use core::fmt;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum PriorityProfile {
|
||||
Audio,
|
||||
Video,
|
||||
Network,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum RtError {
|
||||
AlreadyAcquired,
|
||||
PlatformDenied(i32),
|
||||
Unsupported,
|
||||
}
|
||||
|
||||
impl fmt::Display for RtError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
RtError::AlreadyAcquired => {
|
||||
write!(f, "RealtimePriorityGuard already acquired on this thread")
|
||||
}
|
||||
RtError::PlatformDenied(errno) => {
|
||||
write!(f, "platform denied RT priority (errno={errno})")
|
||||
}
|
||||
RtError::Unsupported => write!(f, "RT priority not supported on this platform/profile"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for RtError {}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum RtOutcome {
|
||||
Acquired,
|
||||
PartialFallback,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static GUARD_ACTIVE: Cell<bool> = const { Cell::new(false) };
|
||||
}
|
||||
|
||||
pub struct RealtimePriorityGuard {
|
||||
inner: PlatformGuard,
|
||||
outcome: RtOutcome,
|
||||
}
|
||||
|
||||
impl RealtimePriorityGuard {
|
||||
pub fn acquire(profile: PriorityProfile) -> Result<Self, RtError> {
|
||||
let already = GUARD_ACTIVE.with(|c| c.replace(true));
|
||||
if already {
|
||||
GUARD_ACTIVE.with(|c| c.set(true));
|
||||
return Err(RtError::AlreadyAcquired);
|
||||
}
|
||||
let acquired = PlatformGuard::acquire(profile);
|
||||
match acquired {
|
||||
Ok((inner, outcome)) => {
|
||||
assert!(matches!(
|
||||
outcome,
|
||||
RtOutcome::Acquired | RtOutcome::PartialFallback
|
||||
));
|
||||
Ok(Self { inner, outcome })
|
||||
}
|
||||
Err(e) => {
|
||||
GUARD_ACTIVE.with(|c| c.set(false));
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn outcome(&self) -> &RtOutcome {
|
||||
assert!(GUARD_ACTIVE.with(Cell::get));
|
||||
&self.outcome
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RealtimePriorityGuard {
|
||||
fn drop(&mut self) {
|
||||
self.inner.restore();
|
||||
GUARD_ACTIVE.with(|c| c.set(false));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
mod platform {
|
||||
use super::{PriorityProfile, RtError, RtOutcome};
|
||||
use libc::qos_class_t::{
|
||||
QOS_CLASS_DEFAULT, QOS_CLASS_USER_INITIATED, QOS_CLASS_USER_INTERACTIVE, QOS_CLASS_UTILITY,
|
||||
};
|
||||
|
||||
pub struct PlatformGuard {
|
||||
prior_qos: libc::qos_class_t,
|
||||
prior_relative: i32,
|
||||
}
|
||||
|
||||
impl PlatformGuard {
|
||||
pub fn acquire(profile: PriorityProfile) -> Result<(Self, RtOutcome), RtError> {
|
||||
assert!(matches!(
|
||||
profile,
|
||||
PriorityProfile::Audio | PriorityProfile::Video | PriorityProfile::Network
|
||||
));
|
||||
let mut prior_qos: libc::qos_class_t = QOS_CLASS_DEFAULT;
|
||||
let mut prior_relative: i32 = 0;
|
||||
let read_rc = unsafe {
|
||||
libc::pthread_get_qos_class_np(
|
||||
libc::pthread_self(),
|
||||
&mut prior_qos,
|
||||
&mut prior_relative,
|
||||
)
|
||||
};
|
||||
if read_rc != 0 {
|
||||
return Err(RtError::PlatformDenied(read_rc));
|
||||
}
|
||||
let target = map_profile(profile);
|
||||
let set_rc = unsafe { libc::pthread_set_qos_class_self_np(target, 0) };
|
||||
if set_rc != 0 {
|
||||
return Err(RtError::PlatformDenied(set_rc));
|
||||
}
|
||||
Ok((
|
||||
Self {
|
||||
prior_qos,
|
||||
prior_relative,
|
||||
},
|
||||
RtOutcome::Acquired,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn restore(&mut self) {
|
||||
unsafe {
|
||||
let _ = libc::pthread_set_qos_class_self_np(self.prior_qos, self.prior_relative);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn map_profile(profile: PriorityProfile) -> libc::qos_class_t {
|
||||
match profile {
|
||||
PriorityProfile::Audio => QOS_CLASS_USER_INTERACTIVE,
|
||||
PriorityProfile::Video => QOS_CLASS_USER_INITIATED,
|
||||
PriorityProfile::Network => QOS_CLASS_UTILITY,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
mod platform {
|
||||
use super::{PriorityProfile, RtError, RtOutcome};
|
||||
|
||||
pub struct PlatformGuard {
|
||||
prior_policy: i32,
|
||||
prior_param: libc::sched_param,
|
||||
used_fallback: bool,
|
||||
prior_nice: Option<i32>,
|
||||
}
|
||||
|
||||
impl PlatformGuard {
|
||||
pub fn acquire(profile: PriorityProfile) -> Result<(Self, RtOutcome), RtError> {
|
||||
assert!(matches!(
|
||||
profile,
|
||||
PriorityProfile::Audio | PriorityProfile::Video | PriorityProfile::Network
|
||||
));
|
||||
let mut prior_policy: i32 = 0;
|
||||
let mut prior_param: libc::sched_param = unsafe { core::mem::zeroed() };
|
||||
let read_rc = unsafe {
|
||||
libc::pthread_getschedparam(
|
||||
libc::pthread_self(),
|
||||
&mut prior_policy,
|
||||
&mut prior_param,
|
||||
)
|
||||
};
|
||||
if read_rc != 0 {
|
||||
return Err(RtError::PlatformDenied(read_rc));
|
||||
}
|
||||
let (policy, sched_priority) = map_profile(profile);
|
||||
assert!(sched_priority >= 0);
|
||||
let mut param: libc::sched_param = unsafe { core::mem::zeroed() };
|
||||
param.sched_priority = sched_priority;
|
||||
let set_rc =
|
||||
unsafe { libc::pthread_setschedparam(libc::pthread_self(), policy, ¶m) };
|
||||
if set_rc == 0 {
|
||||
return Ok((
|
||||
Self {
|
||||
prior_policy,
|
||||
prior_param,
|
||||
used_fallback: false,
|
||||
prior_nice: None,
|
||||
},
|
||||
RtOutcome::Acquired,
|
||||
));
|
||||
}
|
||||
if set_rc != libc::EPERM {
|
||||
return Err(RtError::PlatformDenied(set_rc));
|
||||
}
|
||||
let fallback_nice = match profile {
|
||||
PriorityProfile::Audio => -19,
|
||||
PriorityProfile::Video => -10,
|
||||
PriorityProfile::Network => -5,
|
||||
};
|
||||
let prior_nice = unsafe {
|
||||
*libc::__errno_location() = 0;
|
||||
let cur = libc::getpriority(libc::PRIO_PROCESS, 0);
|
||||
let saved_errno = *libc::__errno_location();
|
||||
if cur == -1 && saved_errno != 0 {
|
||||
return Err(RtError::PlatformDenied(saved_errno));
|
||||
}
|
||||
cur
|
||||
};
|
||||
let _ = unsafe { libc::setpriority(libc::PRIO_PROCESS, 0, fallback_nice) };
|
||||
Ok((
|
||||
Self {
|
||||
prior_policy,
|
||||
prior_param,
|
||||
used_fallback: true,
|
||||
prior_nice: Some(prior_nice),
|
||||
},
|
||||
RtOutcome::PartialFallback,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn restore(&mut self) {
|
||||
if self.used_fallback {
|
||||
if let Some(prior_nice) = self.prior_nice {
|
||||
unsafe { libc::setpriority(libc::PRIO_PROCESS, 0, prior_nice) };
|
||||
}
|
||||
} else {
|
||||
unsafe {
|
||||
let _ = libc::pthread_setschedparam(
|
||||
libc::pthread_self(),
|
||||
self.prior_policy,
|
||||
&self.prior_param,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn map_profile(profile: PriorityProfile) -> (i32, i32) {
|
||||
let policy = libc::SCHED_FIFO;
|
||||
let max = unsafe { libc::sched_get_priority_max(policy) };
|
||||
let min = unsafe { libc::sched_get_priority_min(policy) };
|
||||
assert!(max >= min);
|
||||
let span = (max - min).max(1);
|
||||
let prio = match profile {
|
||||
PriorityProfile::Audio => max,
|
||||
PriorityProfile::Video => min + (span * 2) / 3,
|
||||
PriorityProfile::Network => min + span / 3,
|
||||
};
|
||||
(policy, prio)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod platform {
|
||||
use super::{PriorityProfile, RtError, RtOutcome};
|
||||
use windows::Win32::Foundation::HANDLE;
|
||||
use windows::Win32::System::Threading::{
|
||||
AvRevertMmThreadCharacteristics, AvSetMmThreadCharacteristicsW,
|
||||
};
|
||||
use windows::core::PCWSTR;
|
||||
|
||||
pub struct PlatformGuard {
|
||||
handle: HANDLE,
|
||||
}
|
||||
|
||||
impl PlatformGuard {
|
||||
pub fn acquire(profile: PriorityProfile) -> Result<(Self, RtOutcome), RtError> {
|
||||
assert!(matches!(
|
||||
profile,
|
||||
PriorityProfile::Audio | PriorityProfile::Video | PriorityProfile::Network
|
||||
));
|
||||
let task_name: &[u16] = match profile {
|
||||
PriorityProfile::Audio => &AUDIO_W,
|
||||
PriorityProfile::Video => &CAPTURE_W,
|
||||
PriorityProfile::Network => &PLAYBACK_W,
|
||||
};
|
||||
let mut task_index: u32 = 0;
|
||||
let handle = unsafe {
|
||||
AvSetMmThreadCharacteristicsW(PCWSTR(task_name.as_ptr()), &mut task_index)
|
||||
};
|
||||
match handle {
|
||||
Ok(h) if !h.is_invalid() => Ok((Self { handle: h }, RtOutcome::Acquired)),
|
||||
Ok(_) => Err(RtError::PlatformDenied(0)),
|
||||
Err(e) => Err(RtError::PlatformDenied(e.code().0)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn restore(&mut self) {
|
||||
if !self.handle.is_invalid() {
|
||||
unsafe {
|
||||
let _ = AvRevertMmThreadCharacteristics(self.handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const AUDIO_W: [u16; 6] = [
|
||||
b'A' as u16,
|
||||
b'u' as u16,
|
||||
b'd' as u16,
|
||||
b'i' as u16,
|
||||
b'o' as u16,
|
||||
0,
|
||||
];
|
||||
const CAPTURE_W: [u16; 8] = [
|
||||
b'C' as u16,
|
||||
b'a' as u16,
|
||||
b'p' as u16,
|
||||
b't' as u16,
|
||||
b'u' as u16,
|
||||
b'r' as u16,
|
||||
b'e' as u16,
|
||||
0,
|
||||
];
|
||||
const PLAYBACK_W: [u16; 9] = [
|
||||
b'P' as u16,
|
||||
b'l' as u16,
|
||||
b'a' as u16,
|
||||
b'y' as u16,
|
||||
b'b' as u16,
|
||||
b'a' as u16,
|
||||
b'c' as u16,
|
||||
b'k' as u16,
|
||||
0,
|
||||
];
|
||||
}
|
||||
|
||||
#[cfg(not(any(unix, target_os = "windows")))]
|
||||
mod platform {
|
||||
use super::{PriorityProfile, RtError, RtOutcome};
|
||||
|
||||
pub struct PlatformGuard {}
|
||||
|
||||
impl PlatformGuard {
|
||||
pub fn acquire(_profile: PriorityProfile) -> Result<(Self, RtOutcome), RtError> {
|
||||
Err(RtError::Unsupported)
|
||||
}
|
||||
|
||||
pub fn restore(&mut self) {}
|
||||
}
|
||||
}
|
||||
|
||||
use platform::PlatformGuard;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn acquire_and_release_audio_does_not_panic() {
|
||||
let guard = RealtimePriorityGuard::acquire(PriorityProfile::Audio);
|
||||
match guard {
|
||||
Ok(g) => {
|
||||
assert!(matches!(
|
||||
*g.outcome(),
|
||||
RtOutcome::Acquired | RtOutcome::PartialFallback
|
||||
));
|
||||
drop(g);
|
||||
}
|
||||
Err(RtError::PlatformDenied(_)) => {}
|
||||
Err(other) => panic!("unexpected error: {other:?}"),
|
||||
}
|
||||
let again = RealtimePriorityGuard::acquire(PriorityProfile::Audio);
|
||||
if let Ok(g) = again {
|
||||
drop(g);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn double_acquire_on_same_thread_errors() {
|
||||
let first = RealtimePriorityGuard::acquire(PriorityProfile::Audio);
|
||||
if let Ok(first_guard) = first {
|
||||
let second = RealtimePriorityGuard::acquire(PriorityProfile::Audio);
|
||||
assert_eq!(second.err(), Some(RtError::AlreadyAcquired));
|
||||
drop(first_guard);
|
||||
let third = RealtimePriorityGuard::acquire(PriorityProfile::Network);
|
||||
if let Ok(t) = third {
|
||||
drop(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_profiles_round_trip() {
|
||||
for profile in [
|
||||
PriorityProfile::Audio,
|
||||
PriorityProfile::Video,
|
||||
PriorityProfile::Network,
|
||||
] {
|
||||
let g = RealtimePriorityGuard::acquire(profile);
|
||||
match g {
|
||||
Ok(g) => drop(g),
|
||||
Err(RtError::PlatformDenied(_)) => {}
|
||||
Err(other) => panic!("unexpected error for {profile:?}: {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
use core::fmt;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crate::NS_PER_MS;
|
||||
|
||||
pub trait MonotonicClock: Send + Sync {
|
||||
fn now_ns(&self) -> u64;
|
||||
}
|
||||
|
||||
pub struct SystemMonotonicClock {
|
||||
origin: Instant,
|
||||
last_observed: AtomicU64,
|
||||
}
|
||||
|
||||
impl SystemMonotonicClock {
|
||||
pub fn new() -> Self {
|
||||
let me = Self {
|
||||
origin: Instant::now(),
|
||||
last_observed: AtomicU64::new(0),
|
||||
};
|
||||
assert_eq!(me.last_observed.load(Ordering::Acquire), 0);
|
||||
assert!(me.origin.elapsed().as_nanos() < u64::MAX as u128);
|
||||
me
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SystemMonotonicClock {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl MonotonicClock for SystemMonotonicClock {
|
||||
fn now_ns(&self) -> u64 {
|
||||
let elapsed = self.origin.elapsed();
|
||||
let ns = elapsed.as_nanos();
|
||||
assert!(ns <= u64::MAX as u128);
|
||||
let ns_u64 = ns as u64;
|
||||
let prior = self.last_observed.fetch_max(ns_u64, Ordering::AcqRel);
|
||||
let observed = ns_u64.max(prior);
|
||||
assert!(observed >= prior);
|
||||
observed
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct TickInfo {
|
||||
pub tick_index: u64,
|
||||
pub scheduled_ns: u64,
|
||||
pub actual_ns: u64,
|
||||
pub lag_ns: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum TickError {
|
||||
ZeroPeriod,
|
||||
PeriodTooLarge,
|
||||
NonMonotonicClock { prior_ns: u64, observed_ns: u64 },
|
||||
}
|
||||
|
||||
impl fmt::Display for TickError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
TickError::ZeroPeriod => write!(f, "tick period must be non-zero"),
|
||||
TickError::PeriodTooLarge => write!(f, "tick period exceeds u64 budget"),
|
||||
TickError::NonMonotonicClock {
|
||||
prior_ns,
|
||||
observed_ns,
|
||||
} => write!(
|
||||
f,
|
||||
"MonotonicClock returned non-monotonic reading: prior={prior_ns} observed={observed_ns}",
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for TickError {}
|
||||
|
||||
pub const TICK_PERIOD_NS_MAX: u64 = 60 * crate::NS_PER_SEC;
|
||||
|
||||
const SLEEP_SAFETY_MARGIN_NS: u64 = NS_PER_MS;
|
||||
|
||||
const SPIN_THRESHOLD_NS: u64 = 150_000;
|
||||
|
||||
pub struct TickDriver<C: MonotonicClock> {
|
||||
clock: Arc<C>,
|
||||
period_ns: u64,
|
||||
start_ns: u64,
|
||||
next_tick_index: u64,
|
||||
last_scheduled_ns: u64,
|
||||
last_actual_ns: u64,
|
||||
}
|
||||
|
||||
impl<C: MonotonicClock> TickDriver<C> {
|
||||
pub fn new(clock: Arc<C>, period_ns: u64) -> Result<Self, TickError> {
|
||||
if period_ns == 0 {
|
||||
return Err(TickError::ZeroPeriod);
|
||||
}
|
||||
if period_ns > TICK_PERIOD_NS_MAX {
|
||||
return Err(TickError::PeriodTooLarge);
|
||||
}
|
||||
let start_ns = clock.now_ns();
|
||||
assert!(period_ns > 0);
|
||||
assert!(period_ns <= TICK_PERIOD_NS_MAX);
|
||||
Ok(Self {
|
||||
clock,
|
||||
period_ns,
|
||||
start_ns,
|
||||
next_tick_index: 0,
|
||||
last_scheduled_ns: 0,
|
||||
last_actual_ns: start_ns,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn period_ns(&self) -> u64 {
|
||||
assert!(self.period_ns > 0);
|
||||
assert!(self.period_ns <= TICK_PERIOD_NS_MAX);
|
||||
self.period_ns
|
||||
}
|
||||
|
||||
pub fn start_ns(&self) -> u64 {
|
||||
assert!(self.start_ns <= self.clock.now_ns().saturating_add(self.period_ns));
|
||||
self.start_ns
|
||||
}
|
||||
|
||||
pub fn wait_until_next_tick(&mut self) -> Result<TickInfo, TickError> {
|
||||
assert!(self.period_ns > 0);
|
||||
let mut prior_actual = self.last_actual_ns;
|
||||
let mut tick_index = self.next_tick_index;
|
||||
let scheduled_ns = self
|
||||
.start_ns
|
||||
.saturating_add(tick_index.saturating_mul(self.period_ns));
|
||||
assert!(scheduled_ns >= self.last_scheduled_ns || tick_index == 0);
|
||||
loop {
|
||||
let now = self.clock.now_ns();
|
||||
if now < prior_actual {
|
||||
return Err(TickError::NonMonotonicClock {
|
||||
prior_ns: prior_actual,
|
||||
observed_ns: now,
|
||||
});
|
||||
}
|
||||
prior_actual = now;
|
||||
if now >= scheduled_ns {
|
||||
break;
|
||||
}
|
||||
let remaining = scheduled_ns - now;
|
||||
if remaining > SLEEP_SAFETY_MARGIN_NS {
|
||||
thread::park_timeout(Duration::from_nanos(remaining - SLEEP_SAFETY_MARGIN_NS));
|
||||
} else if remaining > SPIN_THRESHOLD_NS {
|
||||
thread::park_timeout(Duration::from_nanos(remaining - SPIN_THRESHOLD_NS));
|
||||
} else {
|
||||
core::hint::spin_loop();
|
||||
}
|
||||
}
|
||||
let actual_ns = self.clock.now_ns();
|
||||
if actual_ns < prior_actual {
|
||||
return Err(TickError::NonMonotonicClock {
|
||||
prior_ns: prior_actual,
|
||||
observed_ns: actual_ns,
|
||||
});
|
||||
}
|
||||
let lag_ns = (actual_ns as i128 - scheduled_ns as i128) as i64;
|
||||
let info = TickInfo {
|
||||
tick_index,
|
||||
scheduled_ns,
|
||||
actual_ns,
|
||||
lag_ns,
|
||||
};
|
||||
if actual_ns > scheduled_ns.saturating_add(self.period_ns) {
|
||||
let overshoot = (actual_ns - scheduled_ns) / self.period_ns;
|
||||
tick_index = tick_index.saturating_add(overshoot);
|
||||
}
|
||||
self.next_tick_index = tick_index.saturating_add(1);
|
||||
self.last_scheduled_ns = scheduled_ns;
|
||||
self.last_actual_ns = actual_ns;
|
||||
assert!(self.next_tick_index > tick_index);
|
||||
assert_eq!(
|
||||
info.lag_ns,
|
||||
info.actual_ns as i64 - info.scheduled_ns as i64
|
||||
);
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
pub fn next_tick_index(&self) -> u64 {
|
||||
assert!(self.next_tick_index >= 1 || self.last_actual_ns == self.start_ns);
|
||||
self.next_tick_index
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ScriptedClock {
|
||||
readings: Mutex<Vec<u64>>,
|
||||
cursor: AtomicU64,
|
||||
}
|
||||
|
||||
impl ScriptedClock {
|
||||
pub fn new(readings: Vec<u64>) -> Self {
|
||||
assert!(!readings.is_empty());
|
||||
Self {
|
||||
readings: Mutex::new(readings),
|
||||
cursor: AtomicU64::new(0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MonotonicClock for ScriptedClock {
|
||||
fn now_ns(&self) -> u64 {
|
||||
let readings = self.readings.lock().expect("readings lock poisoned");
|
||||
assert!(!readings.is_empty());
|
||||
let idx = self.cursor.fetch_add(1, Ordering::AcqRel) as usize;
|
||||
let n = readings.len();
|
||||
let chosen = if idx < n {
|
||||
readings[idx]
|
||||
} else {
|
||||
*readings.last().unwrap()
|
||||
};
|
||||
assert!(n > 0);
|
||||
chosen
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
fn ci_mode() -> bool {
|
||||
std::env::var_os("CI").is_some()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_period_rejected() {
|
||||
let clock = Arc::new(SystemMonotonicClock::new());
|
||||
let err = TickDriver::new(clock, 0).err();
|
||||
assert_eq!(err, Some(TickError::ZeroPeriod));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn period_too_large_rejected() {
|
||||
let clock = Arc::new(SystemMonotonicClock::new());
|
||||
let err = TickDriver::new(clock, TICK_PERIOD_NS_MAX + 1).err();
|
||||
assert_eq!(err, Some(TickError::PeriodTooLarge));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn system_clock_is_monotonic_over_many_samples() {
|
||||
let clock = SystemMonotonicClock::new();
|
||||
let mut prev = clock.now_ns();
|
||||
let samples = 10_000;
|
||||
for _ in 0..samples {
|
||||
let now = clock.now_ns();
|
||||
assert!(now >= prev, "clock went backwards: prev={prev} now={now}");
|
||||
prev = now;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_driver_meets_jitter_budget_at_1ms_period() {
|
||||
let clock = Arc::new(SystemMonotonicClock::new());
|
||||
let period_ns = NS_PER_MS;
|
||||
let mut driver = TickDriver::new(clock, period_ns).expect("driver");
|
||||
let n: u64 = 100;
|
||||
let mut total_error_ns: i128 = 0;
|
||||
let mut max_error_ns: i64 = 0;
|
||||
for _ in 0..n {
|
||||
let info = driver.wait_until_next_tick().expect("tick ok");
|
||||
assert!(info.lag_ns >= -(period_ns as i64));
|
||||
let err = info.lag_ns.abs();
|
||||
total_error_ns += err as i128;
|
||||
if err > max_error_ns {
|
||||
max_error_ns = err;
|
||||
}
|
||||
}
|
||||
let mean_error_ns = (total_error_ns / n as i128) as i64;
|
||||
let max_budget_ns: i64 = if ci_mode() {
|
||||
25 * NS_PER_MS as i64
|
||||
} else {
|
||||
NS_PER_MS as i64
|
||||
};
|
||||
let mean_budget_ns: i64 = if ci_mode() {
|
||||
5 * NS_PER_MS as i64
|
||||
} else {
|
||||
200_000
|
||||
};
|
||||
assert!(
|
||||
mean_error_ns < mean_budget_ns,
|
||||
"mean error {mean_error_ns} ns exceeded budget {mean_budget_ns} ns (ci={})",
|
||||
ci_mode(),
|
||||
);
|
||||
assert!(
|
||||
max_error_ns < max_budget_ns,
|
||||
"max error {max_error_ns} ns exceeded budget {max_budget_ns} ns (ci={})",
|
||||
ci_mode(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lag_reported_when_caller_sleeps_over_a_tick_boundary() {
|
||||
let clock = Arc::new(SystemMonotonicClock::new());
|
||||
let period_ns = 5 * NS_PER_MS;
|
||||
let mut driver = TickDriver::new(clock, period_ns).expect("driver");
|
||||
let _first = driver.wait_until_next_tick().expect("first tick");
|
||||
thread::sleep(Duration::from_millis(20));
|
||||
let info = driver.wait_until_next_tick().expect("after sleep");
|
||||
assert!(
|
||||
info.lag_ns > 0,
|
||||
"expected positive lag, got {}",
|
||||
info.lag_ns
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_index_is_monotonic() {
|
||||
let clock = Arc::new(SystemMonotonicClock::new());
|
||||
let mut driver = TickDriver::new(clock, NS_PER_MS).expect("driver");
|
||||
let mut prior_index = None::<u64>;
|
||||
let mut prior_scheduled = None::<u64>;
|
||||
for _ in 0..16 {
|
||||
let info = driver.wait_until_next_tick().expect("tick");
|
||||
if let Some(prev) = prior_index {
|
||||
assert!(
|
||||
info.tick_index > prev,
|
||||
"tick_index not monotonic: prev={prev} cur={}",
|
||||
info.tick_index
|
||||
);
|
||||
}
|
||||
if let Some(prev) = prior_scheduled {
|
||||
assert!(info.scheduled_ns > prev, "scheduled_ns not monotonic");
|
||||
}
|
||||
prior_index = Some(info.tick_index);
|
||||
prior_scheduled = Some(info.scheduled_ns);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_monotonic_clock_is_detected() {
|
||||
let clock = Arc::new(ScriptedClock::new(vec![
|
||||
0, 10_000_000, 5_000_000, 5_000_000,
|
||||
]));
|
||||
let mut driver = TickDriver::new(clock, NS_PER_MS).expect("driver");
|
||||
let err = driver.wait_until_next_tick().err();
|
||||
assert!(
|
||||
matches!(err, Some(TickError::NonMonotonicClock { .. })),
|
||||
"got {err:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_info_lag_invariant_holds() {
|
||||
let clock = Arc::new(SystemMonotonicClock::new());
|
||||
let mut driver = TickDriver::new(clock, NS_PER_MS).expect("driver");
|
||||
for _ in 0..5 {
|
||||
let info = driver.wait_until_next_tick().expect("tick");
|
||||
let derived = info.actual_ns as i64 - info.scheduled_ns as i64;
|
||||
assert_eq!(info.lag_ns, derived);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user