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/. */
10 #include <mozilla/Sprintf.h>
11 #include <mozilla/Atomics.h>
12 #include "audio_thread_priority.h"
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
);
30 int rv
= sigaction(SIGXCPU
, &action
, &previous
);
33 SprintfLiteral(buf
, "sigaction(SIGXCPU, ...): %s", strerror(errno
));
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
) {
47 "SIGXCPU handler was already set by something else, dropping real-time "
49 rv
= sigaction(SIGXCPU
, &previous
, nullptr);
51 NS_WARNING("Could not restore previous handler for SIGXCPU.");
53 gRealtimeLimitReached
= true;
57 gRealtimeLimitReached
= false;
60 void DemoteThreadFromRealTime() {
61 atp_thread_info
* info
= atp_get_current_thread_info();
63 NS_WARNING("Could not get current thread info when demoting thread.");
66 int rv
= atp_demote_thread_from_real_time(info
);
68 NS_WARNING("Could not demote thread from real-time.");
71 rv
= atp_free_thread_info(info
);
73 NS_WARNING("Could not free atp_thread_info struct");
75 gRealtimeLimitReached
= false;
78 } // namespace mozilla