blob: 8e8bc896cae3a95128249a3da0b7175f6f0ba777 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2016-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "HAL/Threads.h"
9
10#include <pthread.h>
11#include <sched.h>
12
13#include "HAL/Errors.h"
14
15extern "C" {
16/**
17 * Get the thread priority for the specified thread.
18 *
19 * @param handle Native handle pointer to the thread to get the priority for
20 * @param isRealTime Set to true if thread is realtime, otherwise false
21 * @param status Error status variable. 0 on success
22 * @return The current thread priority. Scaled 1-99, with 1 being highest.
23 */
24int32_t HAL_GetThreadPriority(NativeThreadHandle handle, HAL_Bool* isRealTime,
25 int32_t* status) {
26 sched_param sch;
27 int policy;
28 int success = pthread_getschedparam(*handle, &policy, &sch);
29 if (success == 0) {
30 *status = 0;
31 } else {
32 *status = HAL_THREAD_PRIORITY_ERROR;
33 return -1;
34 }
35 if (policy == SCHED_FIFO || policy == SCHED_RR) {
36 *isRealTime = true;
37 return sch.sched_priority;
38 } else {
39 *isRealTime = false;
40 // 0 is the only suppored priority for non-realtime, so scale to 1
41 return 1;
42 }
43}
44
45/**
46 * Get the thread priority for the current thread.
47 *
48 * @param handle Native handle pointer to the thread to get the priority for
49 * @param isRealTime Set to true if thread is realtime, otherwise false
50 * @param status Error status variable. 0 on success
51 * @return The current thread priority. Scaled 1-99, with 1 being highest.
52 */
53int32_t HAL_GetCurrentThreadPriority(HAL_Bool* isRealTime, int32_t* status) {
54 auto thread = pthread_self();
55 return HAL_GetThreadPriority(&thread, isRealTime, status);
56}
57
58/**
59 * Sets the thread priority for the specified thread
60 *
61 * @param thread Reference to the thread to set the priority of
62 * @param realTime Set to true to set a realtime priority, false for standard
63 * priority
64 * @param priority Priority to set the thread to. Scaled 1-99, with 1 being
65 * highest
66 * @param status Error status variable. 0 on success
67 *
68 * @return The success state of setting the priority
69 */
70HAL_Bool HAL_SetThreadPriority(NativeThreadHandle handle, HAL_Bool realTime,
71 int32_t priority, int32_t* status) {
72 if (handle == nullptr) {
73 *status = NULL_PARAMETER;
74 return false;
75 }
76
77 int scheduler = realTime ? SCHED_FIFO : SCHED_OTHER;
78 if (realTime) {
79 // We don't support setting priorities for non RT threads
80 // so we don't need to check for proper range
81 if (priority < sched_get_priority_min(scheduler) ||
82 priority > sched_get_priority_max(scheduler)) {
83 *status = HAL_THREAD_PRIORITY_RANGE_ERROR;
84 return false;
85 }
86 }
87
88 sched_param sch;
89 int policy;
90 pthread_getschedparam(*handle, &policy, &sch);
91 if (scheduler == SCHED_FIFO || scheduler == SCHED_RR)
92 sch.sched_priority = priority;
93 else
94 // Only need to set 0 priority for non RT thread
95 sch.sched_priority = 0;
96 if (pthread_setschedparam(*handle, scheduler, &sch)) {
97 *status = HAL_THREAD_PRIORITY_ERROR;
98 return false;
99 } else {
100 *status = 0;
101 return true;
102 }
103}
104
105/**
106 * Sets the thread priority for the current thread
107 *
108 * @param thread Reference to the thread to set the priority of
109 * @param realTime Set to true to set a realtime priority, false for standard
110 * priority
111 * @param priority Priority to set the thread to. Scaled 1-99, with 1 being
112 * highest
113 * @param status Error status variable. 0 on success
114 *
115 * @return The success state of setting the priority
116 */
117HAL_Bool HAL_SetCurrentThreadPriority(HAL_Bool realTime, int32_t priority,
118 int32_t* status) {
119 auto thread = pthread_self();
120 return HAL_SetThreadPriority(&thread, realTime, priority, status);
121}
122}