ptp-helper: Set thread priority to time-critical on Windows

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3889>
This commit is contained in:
Sebastian Dröge 2023-03-30 21:58:12 +03:00 committed by GStreamer Marge Bot
parent 3fdfcdf2f6
commit 572d344482
3 changed files with 43 additions and 0 deletions

View file

@ -546,6 +546,8 @@ pub mod windows {
pub const FILE_TYPE_CHAR: u32 = 0x0002;
pub const FILE_TYPE_PIPE: u32 = 0x0003;
pub const THREAD_PRIORITY_TIME_CRITICAL: i32 = 15;
#[link(name = "kernel32")]
extern "system" {
pub fn GetStdHandle(nstdhandle: i32) -> HANDLE;
@ -597,6 +599,9 @@ pub mod windows {
lpmem: *mut c_void,
dwbytes: usize,
) -> *mut c_void;
pub fn SetThreadPriority(pthread: HANDLE, npriority: i32) -> i32;
pub fn GetCurrentThread() -> HANDLE;
}
pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002;

View file

@ -29,6 +29,7 @@ mod io;
mod net;
mod privileges;
mod rand;
mod thread;
use error::{Context, Error};
use rand::rand;
@ -127,6 +128,8 @@ fn main() -> Result<(), Error> {
let general_socket =
create_socket(PTP_GENERAL_PORT).context("Failed creating general socket")?;
thread::set_priority().context("Failed to set thread priority")?;
privileges::drop().context("Failed dropping privileges")?;
let clock_id = join_multicast(&args, &event_socket, &general_socket)

View file

@ -0,0 +1,35 @@
// GStreamer
//
// Copyright (C) 2015-2023 Sebastian Dröge <sebastian@centricular.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at
// <https://mozilla.org/MPL/2.0/>.
//
// SPDX-License-Identifier: MPL-2.0
use crate::error::Error;
pub fn set_priority() -> Result<(), Error> {
#[cfg(windows)]
{
use std::io;
use crate::{bail, ffi::windows::*};
// SAFETY: Getting a handle to the current thread is safe at any time
let thread = unsafe { GetCurrentThread() };
// SAFETY: SetThreadPriority() requires a valid thread handle, which was given above,
// and will return 0 on errors.
unsafe {
if SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL) == 0 {
bail!(
source: io::Error::last_os_error(),
"Failed to set thread priority"
);
}
}
}
Ok(())
}