summaryrefslogtreecommitdiffstats
path: root/third_party/rust/audio_thread_priority
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/audio_thread_priority
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/audio_thread_priority')
-rw-r--r--third_party/rust/audio_thread_priority/.cargo-checksum.json1
-rw-r--r--third_party/rust/audio_thread_priority/Cargo.toml65
-rw-r--r--third_party/rust/audio_thread_priority/LICENSE374
-rw-r--r--third_party/rust/audio_thread_priority/Makefile8
-rw-r--r--third_party/rust/audio_thread_priority/README.md43
-rw-r--r--third_party/rust/audio_thread_priority/atp_test.cpp30
-rw-r--r--third_party/rust/audio_thread_priority/audio_thread_priority.h153
-rw-r--r--third_party/rust/audio_thread_priority/generate_osx_bindings.sh3
-rw-r--r--third_party/rust/audio_thread_priority/src/lib.rs735
-rw-r--r--third_party/rust/audio_thread_priority/src/mach_sys.rs36
-rw-r--r--third_party/rust/audio_thread_priority/src/rt_linux.rs319
-rw-r--r--third_party/rust/audio_thread_priority/src/rt_mach.rs162
-rw-r--r--third_party/rust/audio_thread_priority/src/rt_win.rs69
13 files changed, 1998 insertions, 0 deletions
diff --git a/third_party/rust/audio_thread_priority/.cargo-checksum.json b/third_party/rust/audio_thread_priority/.cargo-checksum.json
new file mode 100644
index 0000000000..e1a42d3fe7
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"Cargo.toml":"6bceb8b41d1646bd255b39c13dcca5bbcc0984f0f2aca73decb7ee5627443387","LICENSE":"32ee9dbf6196874fc9d406c54a888a6c4cbb9aa4a7f35b46befeaff43a78fe85","Makefile":"0f9a771cfb30c7c4b9961d82fdca4e9e229a955bb2e636474a4101389e18e938","README.md":"c123692b3b50dd621b896a8269814d609cbf1e532b461bf4a77854ddd607eb7a","atp_test.cpp":"8075a040941a65fb9e3f7cbf0535853ca6661c3ac442ec35569b42b24bbec797","audio_thread_priority.h":"f0ecaf1b674f794cde0dc834028e074d4e4675d22ae96acf08b2ae1dceb3474e","generate_osx_bindings.sh":"06e4e03450f788ced18d31fff5660919e6f6ec1119ddace363ffeb82f0518a71","src/lib.rs":"1d5c629d1ee2dab80f7bf422a3add07eaca203f5c1557fdd379361d632e14224","src/mach_sys.rs":"352560fcb9b41d877cff92e5b3b04d6dc68b1f30508ce4b9aed78940120a883e","src/rt_linux.rs":"e77db6d66fb76772dde6c12850ef2c50730cfe00decc0c67211e63f3e8a59c9f","src/rt_mach.rs":"29f8c0397f14cecbac1f76394c2abfe0e05903b54486cf735f9a94a10c168643","src/rt_win.rs":"f2ba097cebf65252c27d9d6dcfbe1fcc041c4b312724bd98e080e114a4de3bb6"},"package":"17fd24082f432a11ad4fde564af75fc9a0beef2f4199e7691b090ff0e065c4d0"} \ No newline at end of file
diff --git a/third_party/rust/audio_thread_priority/Cargo.toml b/third_party/rust/audio_thread_priority/Cargo.toml
new file mode 100644
index 0000000000..30861cdafd
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/Cargo.toml
@@ -0,0 +1,65 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies.
+#
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
+
+[package]
+edition = "2018"
+name = "audio_thread_priority"
+version = "0.31.0"
+authors = ["Paul Adenot <paul@paul.cx>"]
+description = "Bump a thread to real-time priority, for audio work, on Linux, Windows and macOS"
+readme = "README.md"
+license = "MPL-2.0"
+repository = "https://github.com/padenot/audio_thread_priority"
+
+[lib]
+name = "audio_thread_priority"
+crate-type = [
+ "staticlib",
+ "rlib",
+]
+
+[dependencies.cfg-if]
+version = "1.0"
+
+[dependencies.log]
+version = "0.4"
+
+[dependencies.simple_logger]
+version = "0.4"
+optional = true
+
+[dev-dependencies.nix]
+version = "0.26"
+
+[features]
+default = ["with_dbus"]
+terminal-logging = ["simple_logger"]
+with_dbus = ["dbus"]
+
+[target."cfg(target_os = \"linux\")".dependencies.dbus]
+version = "0.6.4"
+optional = true
+
+[target."cfg(target_os = \"linux\")".dependencies.libc]
+version = "0.2"
+
+[target."cfg(target_os = \"macos\")".dependencies.libc]
+version = "0.2"
+
+[target."cfg(target_os = \"macos\")".dependencies.mach]
+version = "0.3"
+
+[target."cfg(target_os = \"windows\")".dependencies.windows-sys]
+version = "0.52"
+features = [
+ "Win32_Foundation",
+ "Win32_System_Threading",
+]
diff --git a/third_party/rust/audio_thread_priority/LICENSE b/third_party/rust/audio_thread_priority/LICENSE
new file mode 100644
index 0000000000..52d135112e
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/LICENSE
@@ -0,0 +1,374 @@
+Mozilla Public License Version 2.0
+==================================
+
+1. Definitions
+--------------
+
+1.1. "Contributor"
+ means each individual or legal entity that creates, contributes to
+ the creation of, or owns Covered Software.
+
+1.2. "Contributor Version"
+ means the combination of the Contributions of others (if any) used
+ by a Contributor and that particular Contributor's Contribution.
+
+1.3. "Contribution"
+ means Covered Software of a particular Contributor.
+
+1.4. "Covered Software"
+ means Source Code Form to which the initial Contributor has attached
+ the notice in Exhibit A, the Executable Form of such Source Code
+ Form, and Modifications of such Source Code Form, in each case
+ including portions thereof.
+
+1.5. "Incompatible With Secondary Licenses"
+ means
+
+ (a) that the initial Contributor has attached the notice described
+ in Exhibit B to the Covered Software; or
+
+ (b) that the Covered Software was made available under the terms of
+ version 1.1 or earlier of the License, but not also under the
+ terms of a Secondary License.
+
+1.6. "Executable Form"
+ means any form of the work other than Source Code Form.
+
+1.7. "Larger Work"
+ means a work that combines Covered Software with other material, in
+ a separate file or files, that is not Covered Software.
+
+1.8. "License"
+ means this document.
+
+1.9. "Licensable"
+ means having the right to grant, to the maximum extent possible,
+ whether at the time of the initial grant or subsequently, any and
+ all of the rights conveyed by this License.
+
+1.10. "Modifications"
+ means any of the following:
+
+ (a) any file in Source Code Form that results from an addition to,
+ deletion from, or modification of the contents of Covered
+ Software; or
+
+ (b) any new file in Source Code Form that contains any Covered
+ Software.
+
+1.11. "Patent Claims" of a Contributor
+ means any patent claim(s), including without limitation, method,
+ process, and apparatus claims, in any patent Licensable by such
+ Contributor that would be infringed, but for the grant of the
+ License, by the making, using, selling, offering for sale, having
+ made, import, or transfer of either its Contributions or its
+ Contributor Version.
+
+1.12. "Secondary License"
+ means either the GNU General Public License, Version 2.0, the GNU
+ Lesser General Public License, Version 2.1, the GNU Affero General
+ Public License, Version 3.0, or any later versions of those
+ licenses.
+
+1.13. "Source Code Form"
+ means the form of the work preferred for making modifications.
+
+1.14. "You" (or "Your")
+ means an individual or a legal entity exercising rights under this
+ License. For legal entities, "You" includes any entity that
+ controls, is controlled by, or is under common control with You. For
+ purposes of this definition, "control" means (a) the power, direct
+ or indirect, to cause the direction or management of such entity,
+ whether by contract or otherwise, or (b) ownership of more than
+ fifty percent (50%) of the outstanding shares or beneficial
+ ownership of such entity.
+
+2. License Grants and Conditions
+--------------------------------
+
+2.1. Grants
+
+Each Contributor hereby grants You a world-wide, royalty-free,
+non-exclusive license:
+
+(a) under intellectual property rights (other than patent or trademark)
+ Licensable by such Contributor to use, reproduce, make available,
+ modify, display, perform, distribute, and otherwise exploit its
+ Contributions, either on an unmodified basis, with Modifications, or
+ as part of a Larger Work; and
+
+(b) under Patent Claims of such Contributor to make, use, sell, offer
+ for sale, have made, import, and otherwise transfer either its
+ Contributions or its Contributor Version.
+
+2.2. Effective Date
+
+The licenses granted in Section 2.1 with respect to any Contribution
+become effective for each Contribution on the date the Contributor first
+distributes such Contribution.
+
+2.3. Limitations on Grant Scope
+
+The licenses granted in this Section 2 are the only rights granted under
+this License. No additional rights or licenses will be implied from the
+distribution or licensing of Covered Software under this License.
+Notwithstanding Section 2.1(b) above, no patent license is granted by a
+Contributor:
+
+(a) for any code that a Contributor has removed from Covered Software;
+ or
+
+(b) for infringements caused by: (i) Your and any other third party's
+ modifications of Covered Software, or (ii) the combination of its
+ Contributions with other software (except as part of its Contributor
+ Version); or
+
+(c) under Patent Claims infringed by Covered Software in the absence of
+ its Contributions.
+
+This License does not grant any rights in the trademarks, service marks,
+or logos of any Contributor (except as may be necessary to comply with
+the notice requirements in Section 3.4).
+
+2.4. Subsequent Licenses
+
+No Contributor makes additional grants as a result of Your choice to
+distribute the Covered Software under a subsequent version of this
+License (see Section 10.2) or under the terms of a Secondary License (if
+permitted under the terms of Section 3.3).
+
+2.5. Representation
+
+Each Contributor represents that the Contributor believes its
+Contributions are its original creation(s) or it has sufficient rights
+to grant the rights to its Contributions conveyed by this License.
+
+2.6. Fair Use
+
+This License is not intended to limit any rights You have under
+applicable copyright doctrines of fair use, fair dealing, or other
+equivalents.
+
+2.7. Conditions
+
+Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
+in Section 2.1.
+
+3. Responsibilities
+-------------------
+
+3.1. Distribution of Source Form
+
+All distribution of Covered Software in Source Code Form, including any
+Modifications that You create or to which You contribute, must be under
+the terms of this License. You must inform recipients that the Source
+Code Form of the Covered Software is governed by the terms of this
+License, and how they can obtain a copy of this License. You may not
+attempt to alter or restrict the recipients' rights in the Source Code
+Form.
+
+3.2. Distribution of Executable Form
+
+If You distribute Covered Software in Executable Form then:
+
+(a) such Covered Software must also be made available in Source Code
+ Form, as described in Section 3.1, and You must inform recipients of
+ the Executable Form how they can obtain a copy of such Source Code
+ Form by reasonable means in a timely manner, at a charge no more
+ than the cost of distribution to the recipient; and
+
+(b) You may distribute such Executable Form under the terms of this
+ License, or sublicense it under different terms, provided that the
+ license for the Executable Form does not attempt to limit or alter
+ the recipients' rights in the Source Code Form under this License.
+
+3.3. Distribution of a Larger Work
+
+You may create and distribute a Larger Work under terms of Your choice,
+provided that You also comply with the requirements of this License for
+the Covered Software. If the Larger Work is a combination of Covered
+Software with a work governed by one or more Secondary Licenses, and the
+Covered Software is not Incompatible With Secondary Licenses, this
+License permits You to additionally distribute such Covered Software
+under the terms of such Secondary License(s), so that the recipient of
+the Larger Work may, at their option, further distribute the Covered
+Software under the terms of either this License or such Secondary
+License(s).
+
+3.4. Notices
+
+You may not remove or alter the substance of any license notices
+(including copyright notices, patent notices, disclaimers of warranty,
+or limitations of liability) contained within the Source Code Form of
+the Covered Software, except that You may alter any license notices to
+the extent required to remedy known factual inaccuracies.
+
+3.5. Application of Additional Terms
+
+You may choose to offer, and to charge a fee for, warranty, support,
+indemnity or liability obligations to one or more recipients of Covered
+Software. However, You may do so only on Your own behalf, and not on
+behalf of any Contributor. You must make it absolutely clear that any
+such warranty, support, indemnity, or liability obligation is offered by
+You alone, and You hereby agree to indemnify every Contributor for any
+liability incurred by such Contributor as a result of warranty, support,
+indemnity or liability terms You offer. You may include additional
+disclaimers of warranty and limitations of liability specific to any
+jurisdiction.
+
+4. Inability to Comply Due to Statute or Regulation
+---------------------------------------------------
+
+If it is impossible for You to comply with any of the terms of this
+License with respect to some or all of the Covered Software due to
+statute, judicial order, or regulation then You must: (a) comply with
+the terms of this License to the maximum extent possible; and (b)
+describe the limitations and the code they affect. Such description must
+be placed in a text file included with all distributions of the Covered
+Software under this License. Except to the extent prohibited by statute
+or regulation, such description must be sufficiently detailed for a
+recipient of ordinary skill to be able to understand it.
+
+5. Termination
+--------------
+
+5.1. The rights granted under this License will terminate automatically
+if You fail to comply with any of its terms. However, if You become
+compliant, then the rights granted under this License from a particular
+Contributor are reinstated (a) provisionally, unless and until such
+Contributor explicitly and finally terminates Your grants, and (b) on an
+ongoing basis, if such Contributor fails to notify You of the
+non-compliance by some reasonable means prior to 60 days after You have
+come back into compliance. Moreover, Your grants from a particular
+Contributor are reinstated on an ongoing basis if such Contributor
+notifies You of the non-compliance by some reasonable means, this is the
+first time You have received notice of non-compliance with this License
+from such Contributor, and You become compliant prior to 30 days after
+Your receipt of the notice.
+
+5.2. If You initiate litigation against any entity by asserting a patent
+infringement claim (excluding declaratory judgment actions,
+counter-claims, and cross-claims) alleging that a Contributor Version
+directly or indirectly infringes any patent, then the rights granted to
+You by any and all Contributors for the Covered Software under Section
+2.1 of this License shall terminate.
+
+5.3. In the event of termination under Sections 5.1 or 5.2 above, all
+end user license agreements (excluding distributors and resellers) which
+have been validly granted by You or Your distributors under this License
+prior to termination shall survive termination.
+
+************************************************************************
+* *
+* 6. Disclaimer of Warranty *
+* ------------------------- *
+* *
+* Covered Software is provided under this License on an "as is" *
+* basis, without warranty of any kind, either expressed, implied, or *
+* statutory, including, without limitation, warranties that the *
+* Covered Software is free of defects, merchantable, fit for a *
+* particular purpose or non-infringing. The entire risk as to the *
+* quality and performance of the Covered Software is with You. *
+* Should any Covered Software prove defective in any respect, You *
+* (not any Contributor) assume the cost of any necessary servicing, *
+* repair, or correction. This disclaimer of warranty constitutes an *
+* essential part of this License. No use of any Covered Software is *
+* authorized under this License except under this disclaimer. *
+* *
+************************************************************************
+
+************************************************************************
+* *
+* 7. Limitation of Liability *
+* -------------------------- *
+* *
+* Under no circumstances and under no legal theory, whether tort *
+* (including negligence), contract, or otherwise, shall any *
+* Contributor, or anyone who distributes Covered Software as *
+* permitted above, be liable to You for any direct, indirect, *
+* special, incidental, or consequential damages of any character *
+* including, without limitation, damages for lost profits, loss of *
+* goodwill, work stoppage, computer failure or malfunction, or any *
+* and all other commercial damages or losses, even if such party *
+* shall have been informed of the possibility of such damages. This *
+* limitation of liability shall not apply to liability for death or *
+* personal injury resulting from such party's negligence to the *
+* extent applicable law prohibits such limitation. Some *
+* jurisdictions do not allow the exclusion or limitation of *
+* incidental or consequential damages, so this exclusion and *
+* limitation may not apply to You. *
+* *
+************************************************************************
+
+8. Litigation
+-------------
+
+Any litigation relating to this License may be brought only in the
+courts of a jurisdiction where the defendant maintains its principal
+place of business and such litigation shall be governed by laws of that
+jurisdiction, without reference to its conflict-of-law provisions.
+Nothing in this Section shall prevent a party's ability to bring
+cross-claims or counter-claims.
+
+9. Miscellaneous
+----------------
+
+This License represents the complete agreement concerning the subject
+matter hereof. If any provision of this License is held to be
+unenforceable, such provision shall be reformed only to the extent
+necessary to make it enforceable. Any law or regulation which provides
+that the language of a contract shall be construed against the drafter
+shall not be used to construe this License against a Contributor.
+
+10. Versions of the License
+---------------------------
+
+10.1. New Versions
+
+Mozilla Foundation is the license steward. Except as provided in Section
+10.3, no one other than the license steward has the right to modify or
+publish new versions of this License. Each version will be given a
+distinguishing version number.
+
+10.2. Effect of New Versions
+
+You may distribute the Covered Software under the terms of the version
+of the License under which You originally received the Covered Software,
+or under the terms of any subsequent version published by the license
+steward.
+
+10.3. Modified Versions
+
+If you create software not governed by this License, and you want to
+create a new license for such software, you may create and use a
+modified version of this License if you rename the license and remove
+any references to the name of the license steward (except to note that
+such modified license differs from this License).
+
+10.4. Distributing Source Code Form that is Incompatible With Secondary
+Licenses
+
+If You choose to distribute Source Code Form that is Incompatible With
+Secondary Licenses under the terms of this version of the License, the
+notice described in Exhibit B of this License must be attached.
+
+Exhibit A - Source Code Form License Notice
+-------------------------------------------
+
+ 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/.
+
+If it is not possible or desirable to put the notice in a particular
+file, then You may include the notice in a location (such as a LICENSE
+file in a relevant directory) where a recipient would be likely to look
+for such a notice.
+
+You may add additional accurate notices of copyright ownership.
+
+Exhibit B - "Incompatible With Secondary Licenses" Notice
+---------------------------------------------------------
+
+ This Source Code Form is "Incompatible With Secondary Licenses", as
+ defined by the Mozilla Public License, v. 2.0.
+
diff --git a/third_party/rust/audio_thread_priority/Makefile b/third_party/rust/audio_thread_priority/Makefile
new file mode 100644
index 0000000000..9a0e1c7af7
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/Makefile
@@ -0,0 +1,8 @@
+all: target/debug/libaudio_thread_priority.a
+ g++ atp_test.cpp target/debug/libaudio_thread_priority.a -I. -lpthread -ldbus-1 -ldl -g -o atp_test
+
+check:
+ @./atp_test && echo "test passed" || echo "test failed"
+
+target/debug/libaudio_thread_priority.a:
+ cargo build
diff --git a/third_party/rust/audio_thread_priority/README.md b/third_party/rust/audio_thread_priority/README.md
new file mode 100644
index 0000000000..42bb3a02b3
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/README.md
@@ -0,0 +1,43 @@
+# audio_thread_priority
+
+[![](https://img.shields.io/crates/v/audio_thread_priority.svg)](https://crates.io/crates/audio_thread_priority)
+[![](https://docs.rs/audio_thread_priority/badge.svg)](https://docs.rs/audio_thread_priority)
+
+
+Synopsis:
+
+```rust
+
+use audio_thread_priority::{promote_current_thread_to_real_time, demote_current_thread_from_real_time};
+
+// ... on a thread that will compute audio and has to be real-time:
+match promote_current_thread_to_real_time(512, 44100) {
+ Ok(h) => {
+ println!("this thread is now bumped to real-time priority.");
+
+ // Do some real-time work...
+
+ match demote_current_thread_from_real_time(h) {
+ Ok(_) => {
+ println!("this thread is now bumped back to normal.")
+ }
+ Err(_) => {
+ println!("Could not bring the thread back to normal priority.")
+ }
+ };
+ }
+ Err(e) => {
+ eprintln!("Error promoting thread to real-time: {}", e);
+ }
+}
+
+```
+
+This library can also be used from C or C++ using the included header and
+compiling the rust code in the application. By default, a `.a` is compiled to
+ease linking.
+
+# License
+
+MPL-2
+
diff --git a/third_party/rust/audio_thread_priority/atp_test.cpp b/third_party/rust/audio_thread_priority/atp_test.cpp
new file mode 100644
index 0000000000..acdd1d22c1
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/atp_test.cpp
@@ -0,0 +1,30 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+#include "audio_thread_priority.h"
+
+int main() {
+#ifdef __linux__
+ atp_thread_info* info = atp_get_current_thread_info();
+ atp_thread_info* info2 = nullptr;
+
+ uint8_t buffer[ATP_THREAD_INFO_SIZE];
+ atp_serialize_thread_info(info, buffer);
+
+ info2 = atp_deserialize_thread_info(buffer);
+
+ int rv = memcmp(info, info2, ATP_THREAD_INFO_SIZE);
+
+ assert(!rv);
+
+ atp_free_thread_info(info);
+ atp_free_thread_info(info2);
+
+ rv = atp_set_real_time_limit(0, 44100);
+ assert(!rv);
+#endif
+
+ return 0;
+}
diff --git a/third_party/rust/audio_thread_priority/audio_thread_priority.h b/third_party/rust/audio_thread_priority/audio_thread_priority.h
new file mode 100644
index 0000000000..b883728b6d
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/audio_thread_priority.h
@@ -0,0 +1,153 @@
+/* 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/. */
+
+#ifndef AUDIO_THREAD_PRIORITY_H
+#define AUDIO_THREAD_PRIORITY_H
+
+#include <stdint.h>
+#include <stdlib.h>
+
+/**
+ * An opaque structure containing information about a thread that was promoted
+ * to real-time priority.
+ */
+struct atp_handle;
+struct atp_thread_info;
+extern size_t ATP_THREAD_INFO_SIZE;
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+
+/**
+ * Promotes the current thread to real-time priority.
+ *
+ * audio_buffer_frames: number of frames per audio buffer. If unknown, passing 0
+ * will choose an appropriate number, conservatively. If variable, either pass 0
+ * or an upper bound.
+ * audio_samplerate_hz: sample-rate for this audio stream, in Hz
+ *
+ * Returns an opaque handle in case of success, NULL otherwise.
+ */
+atp_handle *atp_promote_current_thread_to_real_time(uint32_t audio_buffer_frames,
+ uint32_t audio_samplerate_hz);
+
+
+/**
+ * Demotes the current thread promoted to real-time priority via
+ * `atp_demote_current_thread_from_real_time` to its previous priority.
+ *
+ * Returns 0 in case of success, non-zero otherwise.
+ */
+int32_t atp_demote_current_thread_from_real_time(atp_handle *handle);
+
+/**
+ * Frees an atp_handle. This is useful when it impractical to call
+ *`atp_demote_current_thread_from_real_time` on the right thread. Access to the
+ * handle must be synchronized externaly (or the related thread must have
+ * exited).
+ *
+ * Returns 0 in case of success, non-zero otherwise.
+ */
+int32_t atp_free_handle(atp_handle *handle);
+
+/*
+ * Linux-only API.
+ *
+ * The Linux backend uses DBUS to promote a thread to real-time priority. In
+ * environment where this is not possible (due to sandboxing), this set of
+ * functions allow remoting the call to a process that can make DBUS calls.
+ *
+ * To do so:
+ * - Set the real-time limit from within the process where a
+ * thread will be promoted. This is a `setrlimit` call, that can be done
+ * before the sandbox lockdown.
+ * - Then, gather information on the thread that will be promoted.
+ * - Serialize this info.
+ * - Send over the serialized data via an IPC mechanism
+ * - Deserialize the inf
+ * - Call `atp_promote_thread_to_real_time`
+ */
+
+#ifdef __linux__
+/**
+ * Promotes a thread, possibly in another process, to real-time priority.
+ *
+ * thread_info: info on the thread to promote, gathered with
+ * `atp_get_current_thread_info()`, called on the thread itself.
+ * audio_buffer_frames: number of frames per audio buffer. If unknown, passing 0
+ * will choose an appropriate number, conservatively. If variable, either pass 0
+ * or an upper bound.
+ * audio_samplerate_hz: sample-rate for this audio stream, in Hz
+ *
+ * Returns an opaque handle in case of success, NULL otherwise.
+ *
+ * This call is useful on Linux desktop only, when the process is sandboxed and
+ * cannot promote itself directly.
+ */
+atp_handle *atp_promote_thread_to_real_time(atp_thread_info *thread_info);
+
+/**
+ * Demotes a thread promoted to real-time priority via
+ * `atp_demote_thread_from_real_time` to its previous priority.
+ *
+ * Returns 0 in case of success, non-zero otherwise.
+ *
+ * This call is useful on Linux desktop only, when the process is sandboxed and
+ * cannot promote itself directly.
+ */
+int32_t atp_demote_thread_from_real_time(atp_thread_info* thread_info);
+
+/**
+ * Gather informations from the calling thread, to be able to promote it from
+ * another thread and/or process.
+ *
+ * Returns a non-null pointer to an `atp_thread_info` structure in case of
+ * sucess, to be freed later with `atp_free_thread_info`, and NULL otherwise.
+ *
+ * This call is useful on Linux desktop only, when the process is sandboxed and
+ * cannot promote itself directly.
+ */
+atp_thread_info *atp_get_current_thread_info();
+
+/**
+ * Free an `atp_thread_info` structure.
+ *
+ * Returns 0 in case of success, non-zero in case of error (because thread_info
+ * was NULL).
+ */
+int32_t atp_free_thread_info(atp_thread_info *thread_info);
+
+/**
+ * Serialize an `atp_thread_info` to a byte buffer that is
+ * sizeof(atp_thread_info) long.
+ */
+void atp_serialize_thread_info(atp_thread_info *thread_info, uint8_t *bytes);
+
+/**
+ * Deserialize a byte buffer of sizeof(atp_thread_info) to an `atp_thread_info`
+ * pointer. It can be then freed using atp_free_thread_info.
+ * */
+atp_thread_info* atp_deserialize_thread_info(uint8_t *bytes);
+
+/**
+ * Set real-time limit for the calling process.
+ *
+ * This is useful only on Linux desktop, and allows remoting the rtkit DBUS call
+ * to a process that has access to DBUS. This function has to be called before
+ * attempting to promote threads from another process.
+ *
+ * This sets the real-time computation limit. For actually promoting the thread
+ * to a real-time scheduling class, see `atp_promote_thread_to_real_time`.
+ */
+int32_t atp_set_real_time_limit(uint32_t audio_buffer_frames,
+ uint32_t audio_samplerate_hz);
+
+#endif // __linux__
+
+#ifdef __cplusplus
+} // extern "C"
+#endif // __cplusplus
+
+#endif // AUDIO_THREAD_PRIORITY_H
diff --git a/third_party/rust/audio_thread_priority/generate_osx_bindings.sh b/third_party/rust/audio_thread_priority/generate_osx_bindings.sh
new file mode 100644
index 0000000000..5d55209045
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/generate_osx_bindings.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+bindgen /usr/include/mach/thread_policy.h --no-layout-tests --whitelist-type "(thread_policy_flavor_t|thread_policy_t|thread_extended_policy_data_t|thread_time_constraint_policy_data_t|thread_precedence_policy_data_t|thread_t)" --whitelist-var "(THREAD_TIME_CONSTRAINT_POLICY|THREAD_EXTENDED_POLICY|THREAD_PRECEDENCE_POLICY)" > src/mach_sys.rs
diff --git a/third_party/rust/audio_thread_priority/src/lib.rs b/third_party/rust/audio_thread_priority/src/lib.rs
new file mode 100644
index 0000000000..eaae33d9d1
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/src/lib.rs
@@ -0,0 +1,735 @@
+//! # audio_thread_priority
+//!
+//! Promote the current thread, or another thread (possibly in another process), to real-time
+//! priority, suitable for low-latency audio processing.
+//!
+//! # Example
+//!
+//! ```rust
+//!
+//! use audio_thread_priority::{promote_current_thread_to_real_time, demote_current_thread_from_real_time};
+//!
+//! // ... on a thread that will compute audio and has to be real-time:
+//! match promote_current_thread_to_real_time(512, 44100) {
+//! Ok(h) => {
+//! println!("this thread is now bumped to real-time priority.");
+//!
+//! // Do some real-time work...
+//!
+//! match demote_current_thread_from_real_time(h) {
+//! Ok(_) => {
+//! println!("this thread is now bumped back to normal.")
+//! }
+//! Err(_) => {
+//! println!("Could not bring the thread back to normal priority.")
+//! }
+//! };
+//! }
+//! Err(e) => {
+//! eprintln!("Error promoting thread to real-time: {}", e);
+//! }
+//! }
+//!
+//! ```
+
+#![warn(missing_docs)]
+
+use cfg_if::cfg_if;
+use std::error::Error;
+use std::fmt;
+
+/// The OS-specific issue is available as `inner`
+#[derive(Debug)]
+pub struct AudioThreadPriorityError {
+ message: String,
+ inner: Option<Box<dyn Error + 'static>>,
+}
+
+impl AudioThreadPriorityError {
+ cfg_if! {
+ if #[cfg(all(target_os = "linux", feature = "dbus"))] {
+ fn new_with_inner(message: &str, inner: Box<dyn Error>) -> AudioThreadPriorityError {
+ AudioThreadPriorityError {
+ message: message.into(),
+ inner: Some(inner),
+ }
+ }
+ }
+ }
+ fn new(message: &str) -> AudioThreadPriorityError {
+ AudioThreadPriorityError {
+ message: message.into(),
+ inner: None,
+ }
+ }
+}
+
+impl fmt::Display for AudioThreadPriorityError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ let mut rv = write!(f, "AudioThreadPriorityError: {}", &self.message);
+ if let Some(inner) = &self.inner {
+ rv = write!(f, " ({})", inner);
+ }
+ rv
+ }
+}
+
+impl Error for AudioThreadPriorityError {
+ fn description(&self) -> &str {
+ &self.message
+ }
+
+ fn source(&self) -> Option<&(dyn Error + 'static)> {
+ self.inner.as_ref().map(|e| e.as_ref())
+ }
+}
+
+cfg_if! {
+ if #[cfg(target_os = "macos")] {
+ mod rt_mach;
+#[allow(unused, non_camel_case_types, non_snake_case, non_upper_case_globals)]
+ mod mach_sys;
+ extern crate mach;
+ extern crate libc;
+ use rt_mach::promote_current_thread_to_real_time_internal;
+ use rt_mach::demote_current_thread_from_real_time_internal;
+ use rt_mach::RtPriorityHandleInternal;
+ } else if #[cfg(target_os = "windows")] {
+ mod rt_win;
+ use rt_win::promote_current_thread_to_real_time_internal;
+ use rt_win::demote_current_thread_from_real_time_internal;
+ use rt_win::RtPriorityHandleInternal;
+ } else if #[cfg(all(target_os = "linux", feature = "dbus"))] {
+ mod rt_linux;
+ extern crate dbus;
+ extern crate libc;
+ use rt_linux::promote_current_thread_to_real_time_internal;
+ use rt_linux::demote_current_thread_from_real_time_internal;
+ use rt_linux::set_real_time_hard_limit_internal as set_real_time_hard_limit;
+ use rt_linux::get_current_thread_info_internal;
+ use rt_linux::promote_thread_to_real_time_internal;
+ use rt_linux::demote_thread_from_real_time_internal;
+ use rt_linux::RtPriorityThreadInfoInternal;
+ use rt_linux::RtPriorityHandleInternal;
+ #[no_mangle]
+ /// Size of a RtPriorityThreadInfo or atp_thread_info struct, for use in FFI.
+ pub static ATP_THREAD_INFO_SIZE: usize = std::mem::size_of::<RtPriorityThreadInfo>();
+ } else {
+ // blanket implementations for Android, Linux Desktop without dbus and others
+ pub struct RtPriorityHandleInternal {}
+ #[derive(Clone, Copy, PartialEq)]
+ pub struct RtPriorityThreadInfoInternal {
+ _dummy: u8
+ }
+
+ cfg_if! {
+ if #[cfg(not(target_os = "linux"))] {
+ pub type RtPriorityThreadInfo = RtPriorityThreadInfoInternal;
+ }
+ }
+
+ impl RtPriorityThreadInfo {
+ pub fn serialize(&self) -> [u8; 1] {
+ [0]
+ }
+ pub fn deserialize(_: [u8; 1]) -> Self {
+ RtPriorityThreadInfo{_dummy: 0}
+ }
+ }
+ pub fn promote_current_thread_to_real_time_internal(_: u32, audio_samplerate_hz: u32) -> Result<RtPriorityHandle, AudioThreadPriorityError> {
+ if audio_samplerate_hz == 0 {
+ return Err(AudioThreadPriorityError{message: "sample rate is zero".to_string(), inner: None});
+ }
+ // no-op
+ Ok(RtPriorityHandle{})
+ }
+ pub fn demote_current_thread_from_real_time_internal(_: RtPriorityHandle) -> Result<(), AudioThreadPriorityError> {
+ // no-op
+ Ok(())
+ }
+ pub fn set_real_time_hard_limit(
+ _: u32,
+ _: u32,
+ ) -> Result<(), AudioThreadPriorityError> {
+ Ok(())
+ }
+ pub fn get_current_thread_info_internal() -> Result<RtPriorityThreadInfo, AudioThreadPriorityError> {
+ Ok(RtPriorityThreadInfo{_dummy: 0})
+ }
+ pub fn promote_thread_to_real_time_internal(
+ _: RtPriorityThreadInfo,
+ _: u32,
+ audio_samplerate_hz: u32,
+ ) -> Result<RtPriorityHandle, AudioThreadPriorityError> {
+ if audio_samplerate_hz == 0 {
+ return Err(AudioThreadPriorityError::new("sample rate is zero"));
+ }
+ return Ok(RtPriorityHandle{});
+ }
+
+ pub fn demote_thread_from_real_time_internal(_: RtPriorityThreadInfo) -> Result<(), AudioThreadPriorityError> {
+ return Ok(());
+ }
+ #[no_mangle]
+ /// Size of a RtPriorityThreadInfo or atp_thread_info struct, for use in FFI.
+ pub static ATP_THREAD_INFO_SIZE: usize = std::mem::size_of::<RtPriorityThreadInfo>();
+ }
+}
+
+/// Opaque handle to a thread handle structure.
+pub type RtPriorityHandle = RtPriorityHandleInternal;
+
+cfg_if! {
+ if #[cfg(target_os = "linux")] {
+/// Opaque handle to a thread info.
+///
+/// This can be serialized to raw bytes to be sent via IPC.
+///
+/// This call is useful on Linux desktop only, when the process is sandboxed and
+/// cannot promote itself directly.
+pub type RtPriorityThreadInfo = RtPriorityThreadInfoInternal;
+
+
+/// Get the calling thread's information, to be able to promote it to real-time from somewhere
+/// else, later.
+///
+/// This call is useful on Linux desktop only, when the process is sandboxed and
+/// cannot promote itself directly.
+///
+/// # Return value
+///
+/// Ok in case of success, with an opaque structure containing relevant info for the platform, Err
+/// otherwise.
+pub fn get_current_thread_info() -> Result<RtPriorityThreadInfo, AudioThreadPriorityError> {
+ get_current_thread_info_internal()
+}
+
+/// Return a byte buffer containing serialized information about a thread, to promote it to
+/// real-time from elsewhere.
+///
+/// This call is useful on Linux desktop only, when the process is sandboxed and
+/// cannot promote itself directly.
+pub fn thread_info_serialize(
+ thread_info: RtPriorityThreadInfo,
+) -> [u8; std::mem::size_of::<RtPriorityThreadInfo>()] {
+ thread_info.serialize()
+}
+
+/// From a byte buffer, return a `RtPriorityThreadInfo`.
+///
+/// This call is useful on Linux desktop only, when the process is sandboxed and
+/// cannot promote itself directly.
+///
+/// # Arguments
+///
+/// A byte buffer containing a serializezd `RtPriorityThreadInfo`.
+pub fn thread_info_deserialize(
+ bytes: [u8; std::mem::size_of::<RtPriorityThreadInfo>()],
+) -> RtPriorityThreadInfo {
+ RtPriorityThreadInfoInternal::deserialize(bytes)
+}
+
+/// Get the calling threads' information, to promote it from another process or thread, with a C
+/// API.
+///
+/// This is intended to call on the thread that will end up being promoted to real time priority,
+/// but that cannot do it itself (probably because of sandboxing reasons).
+///
+/// After use, it MUST be freed by calling `atp_free_thread_info`.
+///
+/// # Return value
+///
+/// A pointer to a struct that can be serialized and deserialized, and that can be passed to
+/// `atp_promote_thread_to_real_time`, even from another process.
+#[no_mangle]
+pub extern "C" fn atp_get_current_thread_info() -> *mut atp_thread_info {
+ match get_current_thread_info() {
+ Ok(thread_info) => Box::into_raw(Box::new(atp_thread_info(thread_info))),
+ _ => std::ptr::null_mut(),
+ }
+}
+
+/// Frees a thread info, with a c api.
+///
+/// # Arguments
+///
+/// thread_info: the `atp_thread_info` structure to free.
+///
+/// # Return value
+///
+/// 0 in case of success, 1 otherwise (if `thread_info` is NULL).
+///
+/// # Safety
+///
+/// This function is safe only and only if the pointer comes from this library, of if is null.
+#[no_mangle]
+pub unsafe extern "C" fn atp_free_thread_info(thread_info: *mut atp_thread_info) -> i32 {
+ if thread_info.is_null() {
+ return 1;
+ }
+ drop(Box::from_raw(thread_info));
+ 0
+}
+
+/// Return a byte buffer containing serialized information about a thread, to promote it to
+/// real-time from elsewhere, with a C API.
+///
+/// `bytes` MUST be `std::mem::size_of<RtPriorityThreadInfo>()` bytes long.
+///
+/// This is exposed in the C API as `ATP_THREAD_INFO_SIZE`.
+///
+/// This call is useful on Linux desktop only, when the process is sandboxed, cannot promote itself
+/// directly, and the `atp_thread_info` struct must be passed via IPC.
+///
+/// # Safety
+///
+/// This function is safe only and only if the first pointer comes from this library, and the
+/// second pointer is at least ATP_THREAD_INFO_SIZE bytes long.
+#[no_mangle]
+pub unsafe extern "C" fn atp_serialize_thread_info(
+ thread_info: *mut atp_thread_info,
+ bytes: *mut libc::c_void,
+) {
+ let thread_info = &mut *thread_info;
+ let source = thread_info.0.serialize();
+ std::ptr::copy(source.as_ptr(), bytes as *mut u8, source.len());
+}
+
+/// From a byte buffer, return a `RtPriorityThreadInfo`, with a C API.
+///
+/// This call is useful on Linux desktop only, when the process is sandboxed and
+/// cannot promote itself directly.
+///
+/// # Arguments
+///
+/// A byte buffer containing a serializezd `RtPriorityThreadInfo`.
+///
+/// # Safety
+///
+/// This function is safe only and only if pointer is at least ATP_THREAD_INFO_SIZE bytes long.
+#[no_mangle]
+pub unsafe extern "C" fn atp_deserialize_thread_info(
+ in_bytes: *mut u8,
+) -> *mut atp_thread_info {
+ let bytes = *(in_bytes as *mut [u8; std::mem::size_of::<RtPriorityThreadInfoInternal>()]);
+ let thread_info = RtPriorityThreadInfoInternal::deserialize(bytes);
+ Box::into_raw(Box::new(atp_thread_info(thread_info)))
+}
+
+/// Promote a particular thread thread to real-time priority.
+///
+/// This call is useful on Linux desktop only, when the process is sandboxed and
+/// cannot promote itself directly.
+///
+/// # Arguments
+///
+/// * `thread_info` - informations about the thread to promote, gathered using
+/// `get_current_thread_info`.
+/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be
+/// rendered each callback, or 0 for a sensible default value.
+/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz.
+///
+/// # Return value
+///
+/// This function returns a `Result<RtPriorityHandle>`, which is an opaque struct to be passed to
+/// `demote_current_thread_from_real_time` to revert to the previous thread priority.
+pub fn promote_thread_to_real_time(
+ thread_info: RtPriorityThreadInfo,
+ audio_buffer_frames: u32,
+ audio_samplerate_hz: u32,
+) -> Result<RtPriorityHandle, AudioThreadPriorityError> {
+ if audio_samplerate_hz == 0 {
+ return Err(AudioThreadPriorityError::new("sample rate is zero"));
+ }
+ promote_thread_to_real_time_internal(
+ thread_info,
+ audio_buffer_frames,
+ audio_samplerate_hz,
+ )
+}
+
+/// Demotes a thread from real-time priority.
+///
+/// # Arguments
+///
+/// * `thread_info` - An opaque struct returned from a successful call to
+/// `get_current_thread_info`.
+///
+/// # Return value
+///
+/// `Ok` in case of success, `Err` otherwise.
+pub fn demote_thread_from_real_time(thread_info: RtPriorityThreadInfo) -> Result<(), AudioThreadPriorityError> {
+ demote_thread_from_real_time_internal(thread_info)
+}
+
+/// Opaque info to a particular thread.
+#[allow(non_camel_case_types)]
+pub struct atp_thread_info(RtPriorityThreadInfo);
+
+/// Promote a specific thread to real-time, with a C API.
+///
+/// This is useful when the thread to promote cannot make some system calls necessary to promote
+/// it.
+///
+/// # Arguments
+///
+/// `thread_info` - the information of the thread to promote to real-time, gather from calling
+/// `atp_get_current_thread_info` on the thread to promote.
+/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be
+/// rendered each callback, or 0 for a sensible default value.
+/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz.
+///
+/// # Return value
+///
+/// A pointer to an `atp_handle` in case of success, NULL otherwise.
+///
+/// # Safety
+///
+/// This function is safe as long as the first pointer comes from this library.
+#[no_mangle]
+pub unsafe extern "C" fn atp_promote_thread_to_real_time(
+ thread_info: *mut atp_thread_info,
+ audio_buffer_frames: u32,
+ audio_samplerate_hz: u32,
+) -> *mut atp_handle {
+ let thread_info = &mut *thread_info;
+ match promote_thread_to_real_time(thread_info.0, audio_buffer_frames, audio_samplerate_hz) {
+ Ok(handle) => Box::into_raw(Box::new(atp_handle(handle))),
+ _ => std::ptr::null_mut(),
+ }
+}
+
+/// Demote a thread promoted to from real-time, with a C API.
+///
+/// # Arguments
+///
+/// `handle` - an opaque struct received from a promoting function.
+///
+/// # Return value
+///
+/// 0 in case of success, non-zero otherwise.
+///
+/// # Safety
+///
+/// This function is safe as long as the first pointer comes from this library, or is null.
+#[no_mangle]
+pub unsafe extern "C" fn atp_demote_thread_from_real_time(thread_info: *mut atp_thread_info) -> i32 {
+ if thread_info.is_null() {
+ return 1;
+ }
+ let thread_info = (*thread_info).0;
+
+ match demote_thread_from_real_time(thread_info) {
+ Ok(_) => 0,
+ _ => 1,
+ }
+}
+
+/// Set a real-time limit for the calling thread.
+///
+/// # Arguments
+///
+/// `audio_buffer_frames` - the number of frames the audio callback has to render each quantum. 0
+/// picks a rather high default value.
+/// `audio_samplerate_hz` - the sample-rate of the audio stream.
+///
+/// # Return value
+///
+/// 0 in case of success, 1 otherwise.
+#[no_mangle]
+pub extern "C" fn atp_set_real_time_limit(audio_buffer_frames: u32,
+ audio_samplerate_hz: u32) -> i32 {
+ let r = set_real_time_hard_limit(audio_buffer_frames, audio_samplerate_hz);
+ if r.is_err() {
+ return 1;
+ }
+ 0
+}
+
+}
+}
+
+/// Promote the calling thread thread to real-time priority.
+///
+/// # Arguments
+///
+/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be
+/// rendered each callback, or 0 for a sensible default value.
+/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz.
+///
+/// # Return value
+///
+/// This function returns a `Result<RtPriorityHandle>`, which is an opaque struct to be passed to
+/// `demote_current_thread_from_real_time` to revert to the previous thread priority.
+pub fn promote_current_thread_to_real_time(
+ audio_buffer_frames: u32,
+ audio_samplerate_hz: u32,
+) -> Result<RtPriorityHandle, AudioThreadPriorityError> {
+ if audio_samplerate_hz == 0 {
+ return Err(AudioThreadPriorityError::new("sample rate is zero"));
+ }
+ promote_current_thread_to_real_time_internal(audio_buffer_frames, audio_samplerate_hz)
+}
+
+/// Demotes the calling thread from real-time priority.
+///
+/// # Arguments
+///
+/// * `handle` - An opaque struct returned from a successful call to
+/// `promote_current_thread_to_real_time`.
+///
+/// # Return value
+///
+/// `Ok` in scase of success, `Err` otherwise.
+pub fn demote_current_thread_from_real_time(
+ handle: RtPriorityHandle,
+) -> Result<(), AudioThreadPriorityError> {
+ demote_current_thread_from_real_time_internal(handle)
+}
+
+/// Opaque handle for the C API
+#[allow(non_camel_case_types)]
+pub struct atp_handle(RtPriorityHandle);
+
+/// Promote the calling thread thread to real-time priority, with a C API.
+///
+/// # Arguments
+///
+/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be
+/// rendered each callback, or 0 for a sensible default value.
+/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz.
+///
+/// # Return value
+///
+/// This function returns `NULL` in case of error: if it couldn't bump the thread, or if the
+/// `audio_samplerate_hz` is zero. It returns an opaque handle, to be passed to
+/// `atp_demote_current_thread_from_real_time` to demote the thread.
+///
+/// Additionaly, NULL can be returned in sandboxed processes on Linux, when DBUS cannot be used in
+/// the process (for example because the socket to DBUS cannot be created). If this is the case,
+/// it's necessary to get the information from the thread to promote and ask another process to
+/// promote it (maybe via another privileged process).
+#[no_mangle]
+pub extern "C" fn atp_promote_current_thread_to_real_time(
+ audio_buffer_frames: u32,
+ audio_samplerate_hz: u32,
+) -> *mut atp_handle {
+ match promote_current_thread_to_real_time(audio_buffer_frames, audio_samplerate_hz) {
+ Ok(handle) => Box::into_raw(Box::new(atp_handle(handle))),
+ _ => std::ptr::null_mut(),
+ }
+}
+/// Demotes the calling thread from real-time priority, with a C API.
+///
+/// # Arguments
+///
+/// * `atp_handle` - An opaque struct returned from a successful call to
+/// `atp_promote_current_thread_to_real_time`.
+///
+/// # Return value
+///
+/// 0 in case of success, non-zero in case of error.
+///
+/// # Safety
+///
+/// Only to be used with a valid pointer from this library -- not after having released it via
+/// atp_free_handle.
+#[no_mangle]
+pub unsafe extern "C" fn atp_demote_current_thread_from_real_time(handle: *mut atp_handle) -> i32 {
+ assert!(!handle.is_null());
+ let handle = Box::from_raw(handle);
+
+ match demote_current_thread_from_real_time(handle.0) {
+ Ok(_) => 0,
+ _ => 1,
+ }
+}
+
+/// Frees a handle, with a C API.
+///
+/// This is useful when it impractical to call `atp_demote_current_thread_from_real_time` on the
+/// right thread. Access to the handle must be synchronized externaly, or the thread that was
+/// promoted to real-time priority must have exited.
+///
+/// # Arguments
+///
+/// * `atp_handle` - An opaque struct returned from a successful call to
+/// `atp_promote_current_thread_to_real_time`.
+///
+/// # Return value
+///
+/// 0 in case of success, non-zero in case of error.
+///
+/// # Safety
+///
+/// Should only be called to free something from this crate.
+#[no_mangle]
+pub unsafe extern "C" fn atp_free_handle(handle: *mut atp_handle) -> i32 {
+ if handle.is_null() {
+ return 1;
+ }
+ let _handle = Box::from_raw(handle);
+ 0
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ #[cfg(feature = "terminal-logging")]
+ use simple_logger;
+ #[test]
+ fn it_works() {
+ #[cfg(feature = "terminal-logging")]
+ simple_logger::init().unwrap();
+ {
+ assert!(promote_current_thread_to_real_time(0, 0).is_err());
+ }
+ {
+ match promote_current_thread_to_real_time(0, 44100) {
+ Ok(rt_prio_handle) => {
+ demote_current_thread_from_real_time(rt_prio_handle).unwrap();
+ assert!(true);
+ }
+ Err(e) => {
+ eprintln!("{}", e);
+ assert!(false);
+ }
+ }
+ }
+ {
+ match promote_current_thread_to_real_time(512, 44100) {
+ Ok(rt_prio_handle) => {
+ demote_current_thread_from_real_time(rt_prio_handle).unwrap();
+ assert!(true);
+ }
+ Err(e) => {
+ eprintln!("{}", e);
+ assert!(false);
+ }
+ }
+ }
+ {
+ // Try larger values to test https://github.com/mozilla/audio_thread_priority/pull/23
+ match promote_current_thread_to_real_time(0, 192000) {
+ Ok(rt_prio_handle) => {
+ demote_current_thread_from_real_time(rt_prio_handle).unwrap();
+ assert!(true);
+ }
+ Err(e) => {
+ eprintln!("{}", e);
+ assert!(false);
+ }
+ }
+ }
+ {
+ // Try larger values to test https://github.com/mozilla/audio_thread_priority/pull/23
+ match promote_current_thread_to_real_time(8192, 48000) {
+ Ok(rt_prio_handle) => {
+ demote_current_thread_from_real_time(rt_prio_handle).unwrap();
+ assert!(true);
+ }
+ Err(e) => {
+ eprintln!("{}", e);
+ assert!(false);
+ }
+ }
+ }
+ {
+ match promote_current_thread_to_real_time(512, 44100) {
+ Ok(_) => {
+ assert!(true);
+ }
+ Err(e) => {
+ eprintln!("{}", e);
+ assert!(false);
+ }
+ }
+ // automatically deallocated, but not demoted until the thread exits.
+ }
+ }
+ cfg_if! {
+ if #[cfg(target_os = "linux")] {
+ use nix::unistd::*;
+ use nix::sys::signal::*;
+
+ #[test]
+ fn test_linux_api() {
+ {
+ let info = get_current_thread_info().unwrap();
+ match promote_thread_to_real_time(info, 512, 44100) {
+ Ok(_) => {
+ assert!(true);
+ }
+ Err(e) => {
+ eprintln!("{}", e);
+ assert!(false);
+ }
+ }
+ }
+ {
+ let info = get_current_thread_info().unwrap();
+ let bytes = info.serialize();
+ let info2 = RtPriorityThreadInfo::deserialize(bytes);
+ assert!(info == info2);
+ }
+ {
+ let info = get_current_thread_info().unwrap();
+ let bytes = thread_info_serialize(info);
+ let info2 = thread_info_deserialize(bytes);
+ assert!(info == info2);
+ }
+ }
+ #[test]
+ fn test_remote_promotion() {
+ let (rd, wr) = pipe().unwrap();
+
+ match unsafe { fork().expect("fork failed") } {
+ ForkResult::Parent{ child } => {
+ eprintln!("Parent PID: {}", getpid());
+ let mut bytes = [0_u8; std::mem::size_of::<RtPriorityThreadInfo>()];
+ match read(rd, &mut bytes) {
+ Ok(_) => {
+ let info = RtPriorityThreadInfo::deserialize(bytes);
+ match promote_thread_to_real_time(info, 0, 44100) {
+ Ok(_) => {
+ eprintln!("thread promotion in the child from the parent succeeded");
+ assert!(true);
+ }
+ Err(_) => {
+ eprintln!("promotion Err");
+ kill(child, SIGKILL).expect("Could not kill the child?");
+ assert!(false);
+ }
+ }
+ }
+ Err(e) => {
+ eprintln!("could not read from the pipe: {}", e);
+ }
+ }
+ kill(child, SIGKILL).expect("Could not kill the child?");
+ }
+ ForkResult::Child => {
+ let r = set_real_time_hard_limit(0, 44100);
+ if r.is_err() {
+ eprintln!("Could not set RT limit, the test will fail.");
+ }
+ eprintln!("Child pid: {}", getpid());
+ let info = get_current_thread_info().unwrap();
+ let bytes = info.serialize();
+ match write(wr, &bytes) {
+ Ok(_) => {
+ loop {
+ std::thread::sleep(std::time::Duration::from_millis(1000));
+ eprintln!("child sleeping, waiting to be promoted...");
+ }
+ }
+ Err(_) => {
+ eprintln!("write error on the pipe.");
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/third_party/rust/audio_thread_priority/src/mach_sys.rs b/third_party/rust/audio_thread_priority/src/mach_sys.rs
new file mode 100644
index 0000000000..f5798adf43
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/src/mach_sys.rs
@@ -0,0 +1,36 @@
+/* automatically generated by rust-bindgen */
+
+pub const THREAD_EXTENDED_POLICY: u32 = 1;
+pub const THREAD_TIME_CONSTRAINT_POLICY: u32 = 2;
+pub const THREAD_PRECEDENCE_POLICY: u32 = 3;
+pub type __darwin_natural_t = ::std::os::raw::c_uint;
+pub type __darwin_mach_port_name_t = __darwin_natural_t;
+pub type __darwin_mach_port_t = __darwin_mach_port_name_t;
+pub type boolean_t = ::std::os::raw::c_uint;
+pub type natural_t = __darwin_natural_t;
+pub type integer_t = ::std::os::raw::c_int;
+pub type mach_port_t = __darwin_mach_port_t;
+pub type thread_t = mach_port_t;
+pub type thread_policy_flavor_t = natural_t;
+pub type thread_policy_t = *mut integer_t;
+#[repr(C)]
+#[derive(Debug, Copy, Clone)]
+pub struct thread_extended_policy {
+ pub timeshare: boolean_t,
+}
+pub type thread_extended_policy_data_t = thread_extended_policy;
+#[repr(C)]
+#[derive(Debug, Copy, Clone)]
+pub struct thread_time_constraint_policy {
+ pub period: u32,
+ pub computation: u32,
+ pub constraint: u32,
+ pub preemptible: boolean_t,
+}
+pub type thread_time_constraint_policy_data_t = thread_time_constraint_policy;
+#[repr(C)]
+#[derive(Debug, Copy, Clone)]
+pub struct thread_precedence_policy {
+ pub importance: integer_t,
+}
+pub type thread_precedence_policy_data_t = thread_precedence_policy;
diff --git a/third_party/rust/audio_thread_priority/src/rt_linux.rs b/third_party/rust/audio_thread_priority/src/rt_linux.rs
new file mode 100644
index 0000000000..100b9e6b14
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/src/rt_linux.rs
@@ -0,0 +1,319 @@
+/* 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/. */
+
+/* Widely copied from dbus-rs/dbus/examples/rtkit.rs */
+
+extern crate dbus;
+extern crate libc;
+
+use std::cmp;
+use std::convert::TryInto;
+use std::error::Error;
+use std::io::Error as OSError;
+
+use dbus::{BusType, Connection, Message, MessageItem, Props};
+
+use crate::AudioThreadPriorityError;
+
+const DBUS_SOCKET_TIMEOUT: i32 = 10_000;
+const RT_PRIO_DEFAULT: u32 = 10;
+// This is different from libc::pid_t, which is 32 bits, and is defined in sys/types.h.
+#[allow(non_camel_case_types)]
+type kernel_pid_t = libc::c_long;
+
+impl From<dbus::Error> for AudioThreadPriorityError {
+ fn from(error: dbus::Error) -> Self {
+ AudioThreadPriorityError::new(&format!(
+ "{}:{}",
+ error.name().unwrap_or("?"),
+ error.message().unwrap_or("?")
+ ))
+ }
+}
+
+impl From<Box<dyn Error>> for AudioThreadPriorityError {
+ fn from(error: Box<dyn Error>) -> Self {
+ AudioThreadPriorityError::new(&error.to_string())
+ }
+}
+
+#[repr(C)]
+#[derive(Clone, Copy)]
+pub struct RtPriorityThreadInfoInternal {
+ /// System-wise thread id, use to promote the thread via dbus.
+ thread_id: kernel_pid_t,
+ /// Process-local thread id, used to restore scheduler characteristics. This information is not
+ /// useful in another process, but is useful tied to the `thread_id`, when back into the first
+ /// process.
+ pthread_id: libc::pthread_t,
+ /// The PID of the process containing `thread_id` below.
+ pid: libc::pid_t,
+ /// ...
+ policy: libc::c_int,
+}
+
+impl RtPriorityThreadInfoInternal {
+ /// Serialize a RtPriorityThreadInfoInternal to a byte buffer.
+ pub fn serialize(&self) -> [u8; std::mem::size_of::<Self>()] {
+ unsafe { std::mem::transmute::<Self, [u8; std::mem::size_of::<Self>()]>(*self) }
+ }
+ /// Get an RtPriorityThreadInfoInternal from a byte buffer.
+ pub fn deserialize(bytes: [u8; std::mem::size_of::<Self>()]) -> Self {
+ unsafe { std::mem::transmute::<[u8; std::mem::size_of::<Self>()], Self>(bytes) }
+ }
+}
+
+impl PartialEq for RtPriorityThreadInfoInternal {
+ fn eq(&self, other: &Self) -> bool {
+ self.thread_id == other.thread_id && self.pthread_id == other.pthread_id
+ }
+}
+
+/*#[derive(Debug)]*/
+pub struct RtPriorityHandleInternal {
+ thread_info: RtPriorityThreadInfoInternal,
+}
+
+fn item_as_i64(i: MessageItem) -> Result<i64, AudioThreadPriorityError> {
+ match i {
+ MessageItem::Int32(i) => Ok(i as i64),
+ MessageItem::Int64(i) => Ok(i),
+ _ => Err(AudioThreadPriorityError::new(&format!(
+ "Property is not integer ({:?})",
+ i
+ ))),
+ }
+}
+
+fn rtkit_set_realtime(thread: u64, pid: u64, prio: u32) -> Result<(), Box<dyn Error>> {
+ let m = if unsafe { libc::getpid() as u64 } == pid {
+ let mut m = Message::new_method_call(
+ "org.freedesktop.RealtimeKit1",
+ "/org/freedesktop/RealtimeKit1",
+ "org.freedesktop.RealtimeKit1",
+ "MakeThreadRealtime",
+ )?;
+ m.append_items(&[thread.into(), prio.into()]);
+ m
+ } else {
+ let mut m = Message::new_method_call(
+ "org.freedesktop.RealtimeKit1",
+ "/org/freedesktop/RealtimeKit1",
+ "org.freedesktop.RealtimeKit1",
+ "MakeThreadRealtimeWithPID",
+ )?;
+ m.append_items(&[pid.into(), thread.into(), prio.into()]);
+ m
+ };
+ let c = Connection::get_private(BusType::System)?;
+ c.send_with_reply_and_block(m, DBUS_SOCKET_TIMEOUT)?;
+ Ok(())
+}
+
+/// Returns the maximum priority, maximum real-time time slice, and the current real-time time
+/// slice for this process.
+fn get_limits() -> Result<(i64, u64, libc::rlimit), AudioThreadPriorityError> {
+ let c = Connection::get_private(BusType::System)?;
+
+ let p = Props::new(
+ &c,
+ "org.freedesktop.RealtimeKit1",
+ "/org/freedesktop/RealtimeKit1",
+ "org.freedesktop.RealtimeKit1",
+ DBUS_SOCKET_TIMEOUT,
+ );
+ let mut current_limit = libc::rlimit {
+ rlim_cur: 0,
+ rlim_max: 0,
+ };
+
+ let max_prio = item_as_i64(p.get("MaxRealtimePriority")?)?;
+ if max_prio < 0 {
+ return Err(AudioThreadPriorityError::new(
+ "invalid negative MaxRealtimePriority",
+ ));
+ }
+
+ let max_rttime = item_as_i64(p.get("RTTimeUSecMax")?)?;
+ if max_rttime < 0 {
+ return Err(AudioThreadPriorityError::new(
+ "invalid negative RTTimeUSecMax",
+ ));
+ }
+
+ if unsafe { libc::getrlimit(libc::RLIMIT_RTTIME, &mut current_limit) } < 0 {
+ return Err(AudioThreadPriorityError::new_with_inner(
+ "getrlimit",
+ Box::new(OSError::last_os_error()),
+ ));
+ }
+
+ Ok((max_prio, (max_rttime as u64), current_limit))
+}
+
+fn set_limits(request: u64, max: u64) -> Result<(), AudioThreadPriorityError> {
+ // Set a soft limit to the limit requested, to be able to handle going over the limit using
+ // SIGXCPU. Set the hard limit to the maximum slice to prevent getting SIGKILL.
+ #[allow(clippy::useless_conversion)]
+ let new_limit = libc::rlimit {
+ rlim_cur: request
+ .try_into()
+ .map_err(|_| AudioThreadPriorityError::new("setrlimit"))?,
+ rlim_max: max
+ .try_into()
+ .map_err(|_| AudioThreadPriorityError::new("setrlimit"))?,
+ };
+ if unsafe { libc::setrlimit(libc::RLIMIT_RTTIME, &new_limit) } < 0 {
+ return Err(AudioThreadPriorityError::new_with_inner(
+ "setrlimit",
+ Box::new(OSError::last_os_error()),
+ ));
+ }
+
+ Ok(())
+}
+
+pub fn promote_current_thread_to_real_time_internal(
+ audio_buffer_frames: u32,
+ audio_samplerate_hz: u32,
+) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> {
+ let thread_info = get_current_thread_info_internal()?;
+ promote_thread_to_real_time_internal(thread_info, audio_buffer_frames, audio_samplerate_hz)
+}
+
+pub fn demote_current_thread_from_real_time_internal(
+ rt_priority_handle: RtPriorityHandleInternal,
+) -> Result<(), AudioThreadPriorityError> {
+ assert!(unsafe { libc::pthread_self() } == rt_priority_handle.thread_info.pthread_id);
+
+ let param = unsafe { std::mem::zeroed::<libc::sched_param>() };
+
+ if unsafe {
+ libc::pthread_setschedparam(
+ rt_priority_handle.thread_info.pthread_id,
+ rt_priority_handle.thread_info.policy,
+ &param,
+ )
+ } < 0
+ {
+ return Err(AudioThreadPriorityError::new_with_inner(
+ "could not demote thread",
+ Box::new(OSError::last_os_error()),
+ ));
+ }
+ Ok(())
+}
+
+/// This can be called by sandboxed code, it only restores priority to what they were.
+pub fn demote_thread_from_real_time_internal(
+ thread_info: RtPriorityThreadInfoInternal,
+) -> Result<(), AudioThreadPriorityError> {
+ let param = unsafe { std::mem::zeroed::<libc::sched_param>() };
+
+ // https://github.com/rust-lang/libc/issues/1511
+ const SCHED_RESET_ON_FORK: libc::c_int = 0x40000000;
+
+ if unsafe {
+ libc::pthread_setschedparam(
+ thread_info.pthread_id,
+ libc::SCHED_OTHER | SCHED_RESET_ON_FORK,
+ &param,
+ )
+ } < 0
+ {
+ return Err(AudioThreadPriorityError::new_with_inner(
+ "could not demote thread",
+ Box::new(OSError::last_os_error()),
+ ));
+ }
+ Ok(())
+}
+
+/// Get the current thread information, as an opaque struct, that can be serialized and sent
+/// accross processes. This is enough to capture the current state of the scheduling policy, and
+/// an identifier to have another thread promoted to real-time.
+pub fn get_current_thread_info_internal(
+) -> Result<RtPriorityThreadInfoInternal, AudioThreadPriorityError> {
+ let thread_id = unsafe { libc::syscall(libc::SYS_gettid) };
+ let pthread_id = unsafe { libc::pthread_self() };
+ let mut param = unsafe { std::mem::zeroed::<libc::sched_param>() };
+ let mut policy = 0;
+
+ if unsafe { libc::pthread_getschedparam(pthread_id, &mut policy, &mut param) } < 0 {
+ return Err(AudioThreadPriorityError::new_with_inner(
+ "pthread_getschedparam",
+ Box::new(OSError::last_os_error()),
+ ));
+ }
+
+ let pid = unsafe { libc::getpid() };
+
+ Ok(RtPriorityThreadInfoInternal {
+ pid,
+ thread_id,
+ pthread_id,
+ policy,
+ })
+}
+
+/// This set the RLIMIT_RTTIME resource to something other than "unlimited". It's necessary for the
+/// rtkit request to succeed, and needs to hapen in the child. We can't get the real limit here,
+/// because we don't have access to DBUS, so it is hardcoded to 200ms, which is the default in the
+/// rtkit package.
+pub fn set_real_time_hard_limit_internal(
+ audio_buffer_frames: u32,
+ audio_samplerate_hz: u32,
+) -> Result<(), AudioThreadPriorityError> {
+ let buffer_frames = if audio_buffer_frames > 0 {
+ audio_buffer_frames
+ } else {
+ // 50ms slice. This "ought to be enough for anybody".
+ audio_samplerate_hz / 20
+ };
+ let budget_us = buffer_frames as u64 * 1_000_000 / audio_samplerate_hz as u64;
+
+ // It's only necessary to set RLIMIT_RTTIME to something when in the child, skip it if it's a
+ // remoting call.
+ let (_, max_rttime, _) = get_limits()?;
+
+ // Only take what we need, or cap at the system limit, no further.
+ let rttime_request = cmp::min(budget_us, max_rttime);
+ set_limits(rttime_request, max_rttime)?;
+
+ Ok(())
+}
+
+/// Promote a thread (possibly in another process) identified by its tid, to real-time.
+pub fn promote_thread_to_real_time_internal(
+ thread_info: RtPriorityThreadInfoInternal,
+ audio_buffer_frames: u32,
+ audio_samplerate_hz: u32,
+) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> {
+ let RtPriorityThreadInfoInternal { pid, thread_id, .. } = thread_info;
+
+ let handle = RtPriorityHandleInternal { thread_info };
+
+ set_real_time_hard_limit_internal(audio_buffer_frames, audio_samplerate_hz)?;
+
+ let r = rtkit_set_realtime(thread_id as u64, pid as u64, RT_PRIO_DEFAULT);
+
+ match r {
+ Ok(_) => Ok(handle),
+ Err(e) => {
+ let (_, _, limits) = get_limits()?;
+ if limits.rlim_cur != libc::RLIM_INFINITY
+ && unsafe { libc::setrlimit(libc::RLIMIT_RTTIME, &limits) } < 0
+ {
+ return Err(AudioThreadPriorityError::new_with_inner(
+ "setrlimit",
+ Box::new(OSError::last_os_error()),
+ ));
+ }
+ Err(AudioThreadPriorityError::new_with_inner(
+ "Thread promotion error",
+ e,
+ ))
+ }
+ }
+}
diff --git a/third_party/rust/audio_thread_priority/src/rt_mach.rs b/third_party/rust/audio_thread_priority/src/rt_mach.rs
new file mode 100644
index 0000000000..3e1d57b15e
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/src/rt_mach.rs
@@ -0,0 +1,162 @@
+use crate::mach_sys::*;
+use crate::AudioThreadPriorityError;
+use libc::{pthread_self, pthread_t};
+use log::info;
+use mach::kern_return::{kern_return_t, KERN_SUCCESS};
+use mach::mach_time::{mach_timebase_info, mach_timebase_info_data_t};
+use mach::message::mach_msg_type_number_t;
+use mach::port::mach_port_t;
+use std::mem::size_of;
+
+extern "C" {
+ fn pthread_mach_thread_np(tid: pthread_t) -> mach_port_t;
+ // Those functions are commented out in thread_policy.h but somehow it works just fine !?
+ fn thread_policy_set(
+ thread: thread_t,
+ flavor: thread_policy_flavor_t,
+ policy_info: thread_policy_t,
+ count: mach_msg_type_number_t,
+ ) -> kern_return_t;
+ fn thread_policy_get(
+ thread: thread_t,
+ flavor: thread_policy_flavor_t,
+ policy_info: thread_policy_t,
+ count: &mut mach_msg_type_number_t,
+ get_default: &mut boolean_t,
+ ) -> kern_return_t;
+}
+
+// can't use size_of in const fn just now in stable, use a macro for now.
+macro_rules! THREAD_TIME_CONSTRAINT_POLICY_COUNT {
+ () => {
+ (size_of::<thread_time_constraint_policy_data_t>() / size_of::<integer_t>()) as u32
+ };
+}
+
+#[derive(Debug)]
+pub struct RtPriorityHandleInternal {
+ tid: mach_port_t,
+ previous_time_constraint_policy: thread_time_constraint_policy_data_t,
+}
+
+impl Default for RtPriorityHandleInternal {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl RtPriorityHandleInternal {
+ pub fn new() -> RtPriorityHandleInternal {
+ RtPriorityHandleInternal {
+ tid: 0,
+ previous_time_constraint_policy: thread_time_constraint_policy_data_t {
+ period: 0,
+ computation: 0,
+ constraint: 0,
+ preemptible: 0,
+ },
+ }
+ }
+}
+
+pub fn demote_current_thread_from_real_time_internal(
+ rt_priority_handle: RtPriorityHandleInternal,
+) -> Result<(), AudioThreadPriorityError> {
+ unsafe {
+ let mut h = rt_priority_handle;
+ let rv: kern_return_t = thread_policy_set(
+ h.tid,
+ THREAD_TIME_CONSTRAINT_POLICY,
+ (&mut h.previous_time_constraint_policy) as *mut _ as thread_policy_t,
+ THREAD_TIME_CONSTRAINT_POLICY_COUNT!(),
+ );
+ if rv != KERN_SUCCESS {
+ return Err(AudioThreadPriorityError::new(
+ "thread demotion error: thread_policy_get: RT",
+ ));
+ }
+
+ info!("thread {} priority restored.", h.tid);
+ }
+
+ Ok(())
+}
+
+pub fn promote_current_thread_to_real_time_internal(
+ audio_buffer_frames: u32,
+ audio_samplerate_hz: u32,
+) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> {
+ let mut rt_priority_handle = RtPriorityHandleInternal::new();
+
+ let buffer_frames = if audio_buffer_frames > 0 {
+ audio_buffer_frames
+ } else {
+ audio_samplerate_hz / 20
+ };
+
+ unsafe {
+ let tid: mach_port_t = pthread_mach_thread_np(pthread_self());
+ let mut time_constraints = thread_time_constraint_policy_data_t {
+ period: 0,
+ computation: 0,
+ constraint: 0,
+ preemptible: 0,
+ };
+
+ // Get current thread attributes, to revert back to the correct setting later if needed.
+ rt_priority_handle.tid = tid;
+
+ // false: we want to get the current value, not the default value. If this is `false` after
+ // returning, it means there are no current settings because of other factor, and the
+ // default was returned instead.
+ let mut get_default: boolean_t = 0;
+ let mut count: mach_msg_type_number_t = THREAD_TIME_CONSTRAINT_POLICY_COUNT!();
+ let mut rv: kern_return_t = thread_policy_get(
+ tid,
+ THREAD_TIME_CONSTRAINT_POLICY,
+ (&mut time_constraints) as *mut _ as thread_policy_t,
+ &mut count,
+ &mut get_default,
+ );
+
+ if rv != KERN_SUCCESS {
+ return Err(AudioThreadPriorityError::new(
+ "thread promotion error: thread_policy_get: time_constraint",
+ ));
+ }
+
+ rt_priority_handle.previous_time_constraint_policy = time_constraints;
+
+ let cb_duration = buffer_frames as f32 / (audio_samplerate_hz as f32) * 1000.;
+ // The multiplicators are somwhat arbitrary for now.
+
+ let mut timebase_info = mach_timebase_info_data_t { denom: 0, numer: 0 };
+ mach_timebase_info(&mut timebase_info);
+
+ let ms2abs: f32 = ((timebase_info.denom as f32) / timebase_info.numer as f32) * 1000000.;
+
+ // Computation time is half of constraint, per macOS 12 behaviour.
+ time_constraints = thread_time_constraint_policy_data_t {
+ period: (cb_duration * ms2abs) as u32,
+ computation: (cb_duration / 2.0 * ms2abs) as u32,
+ constraint: (cb_duration * ms2abs) as u32,
+ preemptible: 1, // true
+ };
+
+ rv = thread_policy_set(
+ tid,
+ THREAD_TIME_CONSTRAINT_POLICY,
+ (&mut time_constraints) as *mut _ as thread_policy_t,
+ THREAD_TIME_CONSTRAINT_POLICY_COUNT!(),
+ );
+ if rv != KERN_SUCCESS {
+ return Err(AudioThreadPriorityError::new(
+ "thread promotion error: thread_policy_set: time_constraint",
+ ));
+ }
+
+ info!("thread {} bumped to real time priority.", tid);
+ }
+
+ Ok(rt_priority_handle)
+}
diff --git a/third_party/rust/audio_thread_priority/src/rt_win.rs b/third_party/rust/audio_thread_priority/src/rt_win.rs
new file mode 100644
index 0000000000..3a2bbf29d9
--- /dev/null
+++ b/third_party/rust/audio_thread_priority/src/rt_win.rs
@@ -0,0 +1,69 @@
+use windows_sys::s;
+use windows_sys::Win32::Foundation::GetLastError;
+use windows_sys::Win32::Foundation::FALSE;
+use windows_sys::Win32::Foundation::HANDLE;
+use windows_sys::Win32::System::Threading::{
+ AvRevertMmThreadCharacteristics, AvSetMmThreadCharacteristicsA,
+};
+
+use crate::AudioThreadPriorityError;
+
+use log::info;
+
+#[derive(Debug)]
+pub struct RtPriorityHandleInternal {
+ mmcss_task_index: u32,
+ task_handle: HANDLE,
+}
+
+impl RtPriorityHandleInternal {
+ pub fn new(mmcss_task_index: u32, task_handle: HANDLE) -> RtPriorityHandleInternal {
+ RtPriorityHandleInternal {
+ mmcss_task_index,
+ task_handle,
+ }
+ }
+}
+
+pub fn demote_current_thread_from_real_time_internal(
+ rt_priority_handle: RtPriorityHandleInternal,
+) -> Result<(), AudioThreadPriorityError> {
+ let rv = unsafe { AvRevertMmThreadCharacteristics(rt_priority_handle.task_handle) };
+ if rv == FALSE {
+ return Err(AudioThreadPriorityError::new(&format!(
+ "Unable to restore the thread priority ({:?})",
+ unsafe { GetLastError() }
+ )));
+ }
+
+ info!(
+ "task {} priority restored.",
+ rt_priority_handle.mmcss_task_index
+ );
+
+ Ok(())
+}
+
+pub fn promote_current_thread_to_real_time_internal(
+ _audio_buffer_frames: u32,
+ _audio_samplerate_hz: u32,
+) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> {
+ let mut task_index = 0u32;
+
+ let handle = unsafe { AvSetMmThreadCharacteristicsA(s!("Audio"), &mut task_index) };
+ let handle = RtPriorityHandleInternal::new(task_index, handle);
+
+ if handle.task_handle == 0 {
+ return Err(AudioThreadPriorityError::new(&format!(
+ "Unable to restore the thread priority ({:?})",
+ unsafe { GetLastError() }
+ )));
+ }
+
+ info!(
+ "task {} bumped to real time priority.",
+ handle.mmcss_task_index
+ );
+
+ Ok(handle)
+}