Bug 1772053 - Enable dynamic code disable mitigations only on Windows 10 1703+ r...
[gecko.git] / dom / media / UnderrunHandlerLinux.cpp
blob05646fbeb8f317fafeb6b2127350fc6f220785a5
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include <csignal>
7 #include <cerrno>
8 #include <pthread.h>
10 #include <mozilla/Sprintf.h>
11 #include <mozilla/Atomics.h>
12 #include "audio_thread_priority.h"
13 #include "nsDebug.h"
15 namespace mozilla {
17 Atomic<bool, MemoryOrdering::ReleaseAcquire> gRealtimeLimitReached;
19 void UnderrunHandler(int signum) { gRealtimeLimitReached = true; }
21 bool SoftRealTimeLimitReached() { return gRealtimeLimitReached; }
23 void InstallSoftRealTimeLimitHandler() {
24 struct sigaction action, previous;
26 action.sa_handler = UnderrunHandler;
27 sigemptyset(&action.sa_mask);
28 action.sa_flags = 0;
30 int rv = sigaction(SIGXCPU, &action, &previous);
31 if (rv != 0) {
32 char buf[256];
33 SprintfLiteral(buf, "sigaction(SIGXCPU, ...): %s", strerror(errno));
34 NS_WARNING(buf);
35 return;
38 void* previous_handler = previous.sa_flags == SA_SIGINFO
39 ? reinterpret_cast<void*>(previous.sa_sigaction)
40 : reinterpret_cast<void*>(previous.sa_handler);
42 MOZ_ASSERT(previous_handler != UnderrunHandler,
43 "Only install the SIGXCPU handler once per process.");
45 if (previous_handler != SIG_DFL && previous_handler != UnderrunHandler) {
46 NS_WARNING(
47 "SIGXCPU handler was already set by something else, dropping real-time "
48 "priority.");
49 rv = sigaction(SIGXCPU, &previous, nullptr);
50 if (rv != 0) {
51 NS_WARNING("Could not restore previous handler for SIGXCPU.");
53 gRealtimeLimitReached = true;
54 return;
57 gRealtimeLimitReached = false;
60 void DemoteThreadFromRealTime() {
61 atp_thread_info* info = atp_get_current_thread_info();
62 if (!info) {
63 NS_WARNING("Could not get current thread info when demoting thread.");
64 return;
66 int rv = atp_demote_thread_from_real_time(info);
67 if (rv) {
68 NS_WARNING("Could not demote thread from real-time.");
69 return;
71 rv = atp_free_thread_info(info);
72 if (rv) {
73 NS_WARNING("Could not free atp_thread_info struct");
75 gRealtimeLimitReached = false;
78 } // namespace mozilla