blob: 2ac0f65144888ff5bcafb1f84c3d09ac09996863 [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#include "Threads.h"
10
11#include "ErrorBase.h"
12#include "HAL/HAL.h"
13
14namespace frc {
15/**
16 * Get the thread priority for the specified thread.
17 *
18 * @param thread Reference to the thread to get the priority for
19 * @param isRealTime Set to true if thread is realtime, otherwise false
20 * @return The current thread priority. Scaled 1-99, with 1 being highest.
21 */
22int GetThreadPriority(std::thread& thread, bool* isRealTime) {
23 int32_t status = 0;
24 HAL_Bool rt = false;
25 auto native = thread.native_handle();
26 auto ret = HAL_GetThreadPriority(&native, &rt, &status);
27 wpi_setGlobalErrorWithContext(status, HAL_GetErrorMessage(status));
28 *isRealTime = rt;
29 return ret;
30}
31
32/**
33 * Get the thread priority for the current thread
34 *
35 * @param isRealTime Set to true if thread is realtime, otherwise false
36 * @return The current thread priority. Scaled 1-99.
37 */
38int GetCurrentThreadPriority(bool* isRealTime) {
39 int32_t status = 0;
40 HAL_Bool rt = false;
41 auto ret = HAL_GetCurrentThreadPriority(&rt, &status);
42 wpi_setGlobalErrorWithContext(status, HAL_GetErrorMessage(status));
43 *isRealTime = rt;
44 return ret;
45}
46
47/**
48 * Sets the thread priority for the specified thread
49 *
50 * @param thread Reference to the thread to set the priority of
51 * @param realTime Set to true to set a realtime priority, false for standard
52 * priority
53 * @param priority Priority to set the thread to. Scaled 1-99, with 1 being
54 * highest. On RoboRIO, priority is ignored for non realtime setting
55 *
56 * @return The success state of setting the priority
57 */
58bool SetThreadPriority(std::thread& thread, bool realTime, int priority) {
59 int32_t status = 0;
60 auto native = thread.native_handle();
61 auto ret = HAL_SetThreadPriority(&native, realTime, priority, &status);
62 wpi_setGlobalErrorWithContext(status, HAL_GetErrorMessage(status));
63 return ret;
64}
65
66/**
67 * Sets the thread priority for the current thread
68 *
69 * @param realTime Set to true to set a realtime priority, false for standard
70 * priority
71 * @param priority Priority to set the thread to. Scaled 1-99, with 1 being
72 * highest. On RoboRIO, priority is ignored for non realtime setting
73 *
74 * @return The success state of setting the priority
75 */
76bool SetCurrentThreadPriority(bool realTime, int priority) {
77 int32_t status = 0;
78 auto ret = HAL_SetCurrentThreadPriority(realTime, priority, &status);
79 wpi_setGlobalErrorWithContext(status, HAL_GetErrorMessage(status));
80 return ret;
81}
82} // namespace frc