1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <sched.h>
18 #include <sys/types.h>
19 
20 // TODO(b/369381361) Enfore -Wmissing-prototypes
21 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
22 
23 namespace {
24 constexpr int kRealTimeFifoSchedulingPriority = 1;
25 }  // namespace
26 
thread_scheduler_enable_real_time(pid_t linux_tid)27 bool thread_scheduler_enable_real_time(pid_t linux_tid) {
28   struct sched_param rt_params = {.sched_priority = kRealTimeFifoSchedulingPriority};
29   return sched_setscheduler(linux_tid, SCHED_FIFO, &rt_params) == 0;
30 }
31 
thread_scheduler_get_priority_range(int & min,int & max)32 bool thread_scheduler_get_priority_range(int& min, int& max) {
33   min = sched_get_priority_min(SCHED_FIFO);
34   max = sched_get_priority_max(SCHED_FIFO);
35   return (min != -1 && max != -1) ? true : false;
36 }
37