| 1 |
// GStreamer |
| 2 |
// |
| 3 |
// Copyright (C) 2015-2023 Sebastian Dröge <[email protected]> |
| 4 |
// |
| 5 |
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0. |
| 6 |
// If a copy of the MPL was not distributed with this file, You can obtain one at |
| 7 |
// <https://mozilla.org/MPL/2.0/>. |
| 8 |
// |
| 9 |
// SPDX-License-Identifier: MPL-2.0 |
| 10 |
|
| 11 |
use crate::error::Error; |
| 12 |
|
| 13 |
pub fn set_priority() -> Result<(), Error> { |
| 14 |
#[cfg(unix)] |
| 15 |
{ |
| 16 |
use std::io; |
| 17 |
|
| 18 |
use crate::{bail, ffi::unix::*}; |
| 19 |
|
| 20 |
// SAFETY: Setting the process priority can happen at any time. A negative |
| 21 |
// priority require special permissions, which should've been given to the process. |
| 22 |
// |
| 23 |
// On error it returns a negative value. |
| 24 |
unsafe { |
| 25 |
if setpriority(PRIO_PROCESS, 0, -5) < 0 { |
| 26 |
bail!( |
| 27 |
source: io::Error::last_os_error(), |
| 28 |
"Failed to set process priority" |
| 29 |
); |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
#[cfg(windows)] |
| 34 |
{ |
| 35 |
use std::io; |
| 36 |
|
| 37 |
use crate::{bail, ffi::windows::*}; |
| 38 |
|
| 39 |
// SAFETY: Getting a handle to the current thread is safe at any time |
| 40 |
let thread = unsafe { GetCurrentThread() }; |
| 41 |
|
| 42 |
// SAFETY: SetThreadPriority() requires a valid thread handle, which was given above, |
| 43 |
// and will return 0 on errors. |
| 44 |
unsafe { |
| 45 |
if SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL) == 0 { |
| 46 |
bail!( |
| 47 |
source: io::Error::last_os_error(), |
| 48 |
"Failed to set thread priority" |
| 49 |
); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
Ok(()) |
| 54 |
} |