|
| 1 | +// Copyright 2020 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | + |
| 17 | +#include "rcutils/thread.h" |
| 18 | +#include "rcutils/error_handling.h" |
| 19 | + |
| 20 | +#ifdef __linux__ |
| 21 | +#include <pthread.h> |
| 22 | +#endif // __linux__ |
| 23 | + |
| 24 | +TEST(test_thread, config_rt_thread) { |
| 25 | +#ifdef __linux__ |
| 26 | + const unsigned cpu_id = 0; |
| 27 | + const unsigned long cpu_bitmask = 1 << cpu_id; |
| 28 | + const int priority = THREAD_PRIORITY_MEDIUM; |
| 29 | + if (configure_native_realtime_thread(pthread_self(), priority, cpu_bitmask) != RCUTILS_RET_OK) { |
| 30 | + GTEST_SKIP() << "Unable to set realtime thread priority."; |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + struct sched_param params_self; |
| 35 | + int policy_self; |
| 36 | + EXPECT_EQ(0, pthread_getschedparam(pthread_self(), &policy_self, ¶ms_self)); |
| 37 | + int prio_calculated; |
| 38 | + EXPECT_EQ(RCUTILS_RET_OK, calculate_os_fifo_thread_priority(priority, &prio_calculated)); |
| 39 | + EXPECT_EQ(prio_calculated, params_self.sched_priority); |
| 40 | + EXPECT_EQ(SCHED_FIFO, policy_self); |
| 41 | + |
| 42 | + cpu_set_t cpuset_self; |
| 43 | + EXPECT_EQ(0, pthread_getaffinity_np(pthread_self(), sizeof(cpuset_self), &cpuset_self)); |
| 44 | + cpu_set_t cpuset; |
| 45 | + CPU_ZERO(&cpuset); |
| 46 | + CPU_SET(cpu_id, &cpuset); |
| 47 | + EXPECT_EQ(0, memcmp(&cpuset, &cpuset_self, sizeof(cpu_set_t))); |
| 48 | +#else |
| 49 | + GTEST_SKIP() << "Testcase not implemented for this platform." |
| 50 | +#endif // __linux__ |
| 51 | +} |
| 52 | + |
| 53 | +TEST(test_thread, calculate_rt_priorities) { |
| 54 | +#ifdef __linux__ |
| 55 | + int prio = -1; |
| 56 | + const int max_prio = sched_get_priority_max(SCHED_FIFO); |
| 57 | + const int min_prio = sched_get_priority_min(SCHED_FIFO); |
| 58 | + EXPECT_EQ(RCUTILS_RET_OK, calculate_os_fifo_thread_priority(THREAD_PRIORITY_LOWEST, &prio)); |
| 59 | + EXPECT_EQ(min_prio, prio); |
| 60 | + EXPECT_EQ(RCUTILS_RET_OK, calculate_os_fifo_thread_priority(THREAD_PRIORITY_MEDIUM, &prio)); |
| 61 | + EXPECT_EQ((max_prio + min_prio) / 2 - 1, prio); |
| 62 | + EXPECT_EQ(RCUTILS_RET_OK, calculate_os_fifo_thread_priority(THREAD_PRIORITY_HIGHEST, &prio)); |
| 63 | + EXPECT_EQ(max_prio, prio); |
| 64 | +#elif |
| 65 | + GTEST_SKIP() << "Testcase not implemented for this platform." |
| 66 | +#endif // __linux__ |
| 67 | +} |
0 commit comments