diff --git a/hal/include/HAL/HAL.h b/hal/include/HAL/HAL.h index 0a5642841b..53e058814a 100644 --- a/hal/include/HAL/HAL.h +++ b/hal/include/HAL/HAL.h @@ -33,7 +33,6 @@ #include "HAL/SPI.h" #include "HAL/SerialPort.h" #include "HAL/Solenoid.h" -#include "HAL/Task.h" #include "HAL/Types.h" namespace HALUsageReporting = nUsageReporting; diff --git a/hal/include/HAL/Task.h b/hal/include/HAL/Task.h deleted file mode 100644 index dce8dcba47..0000000000 --- a/hal/include/HAL/Task.h +++ /dev/null @@ -1,43 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2016. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -#pragma once - -#include -#include - -#ifndef _STATUS_DEFINED -#define _STATUS_DEFINED -typedef int32_t STATUS; -#endif /* _STATUS_DEFINED */ - -#ifndef OK -#define OK 0 -#endif /* OK */ -#ifndef ERROR -#define ERROR (-1) -#endif /* ERROR */ - -typedef pthread_t* TASK; - -#ifdef __cplusplus -extern "C" { -#endif -// Note: These constants used to be declared extern and were defined in -// Task.cpp. This caused issues with the JNI bindings for java, and so the -// instantiations were moved here. -const int32_t HAL_TaskLib_ILLEGAL_PRIORITY = 22; // 22 is EINVAL - -STATUS HAL_VerifyTaskID(TASK task); - -// valid priority [1..99] -STATUS HAL_SetTaskPriority(TASK task, int32_t priority); - -STATUS HAL_GetTaskPriority(TASK task, int32_t* priority); -#ifdef __cplusplus -} -#endif diff --git a/hal/lib/athena/Task.cpp b/hal/lib/athena/Task.cpp deleted file mode 100644 index c91d6cada2..0000000000 --- a/hal/lib/athena/Task.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2016. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -#include "HAL/Task.h" - -#include - -#ifndef OK -#define OK 0 -#endif /* OK */ -#ifndef ERROR -#define ERROR (-1) -#endif /* ERROR */ - -extern "C" { - -STATUS HAL_VerifyTaskID(TASK task) { - if (task != nullptr && pthread_kill(*task, 0) == 0) { - return OK; - } else { - return ERROR; - } -} - -STATUS HAL_SetTaskPriority(TASK task, int32_t priority) { - int32_t policy = 0; - struct sched_param param; - - if (HAL_VerifyTaskID(task) == OK && - pthread_getschedparam(*task, &policy, ¶m) == 0) { - param.sched_priority = priority; - if (pthread_setschedparam(*task, SCHED_FIFO, ¶m) == 0) { - return OK; - } else { - return ERROR; - } - } else { - return ERROR; - } -} - -STATUS HAL_GetTaskPriority(TASK task, int32_t* priority) { - int32_t policy = 0; - struct sched_param param; - - if (HAL_VerifyTaskID(task) == OK && - pthread_getschedparam(*task, &policy, ¶m) == 0) { - *priority = param.sched_priority; - return OK; - } else { - return ERROR; - } -} - -} // extern "C" diff --git a/wpilibc/athena/include/DriverStation.h b/wpilibc/athena/include/DriverStation.h index 7e7dee66d1..c5ce54fb9d 100644 --- a/wpilibc/athena/include/DriverStation.h +++ b/wpilibc/athena/include/DriverStation.h @@ -11,15 +11,13 @@ #include #include #include +#include +#include "HAL/DriverStation.h" #include "HAL/cpp/priority_condition_variable.h" #include "HAL/cpp/priority_mutex.h" #include "RobotState.h" #include "SensorBase.h" -#include "Task.h" - -struct HALControlWord; -class AnalogInput; /** * Provide access to the network communication data to / from the Driver @@ -114,7 +112,7 @@ class DriverStation : public SensorBase, public RobotStateInterface { std::unique_ptr m_joystickDescriptorCache; // Internal Driver Station thread - Task m_task; + std::thread m_dsThread; std::atomic m_isRunning{false}; // WPILib WaitForData control variables diff --git a/wpilibc/athena/include/Preferences.h b/wpilibc/athena/include/Preferences.h index 2a7a49c09e..dcb3905e99 100644 --- a/wpilibc/athena/include/Preferences.h +++ b/wpilibc/athena/include/Preferences.h @@ -13,7 +13,6 @@ #include #include "ErrorBase.h" -#include "Task.h" #include "networktables/NetworkTable.h" #include "tables/ITableListener.h" diff --git a/wpilibc/athena/include/Ultrasonic.h b/wpilibc/athena/include/Ultrasonic.h index b363a23d66..99fd8846fe 100644 --- a/wpilibc/athena/include/Ultrasonic.h +++ b/wpilibc/athena/include/Ultrasonic.h @@ -11,12 +11,12 @@ #include #include #include +#include #include "Counter.h" #include "LiveWindow/LiveWindowSendable.h" #include "PIDSource.h" #include "SensorBase.h" -#include "Task.h" class DigitalInput; class DigitalOutput; @@ -83,7 +83,8 @@ class Ultrasonic : public SensorBase, static constexpr double kMaxUltrasonicTime = 0.1; static constexpr double kSpeedOfSoundInchesPerSec = 1130.0 * 12.0; - static Task m_task; // task doing the round-robin automatic sensing + static std::thread + m_thread; // thread doing the round-robin automatic sensing static std::set m_sensors; // ultrasonic sensors static std::atomic m_automaticEnabled; // automatic round robin mode diff --git a/wpilibc/athena/src/ADXRS450_Gyro.cpp b/wpilibc/athena/src/ADXRS450_Gyro.cpp index bf847aeba3..e07354abff 100644 --- a/wpilibc/athena/src/ADXRS450_Gyro.cpp +++ b/wpilibc/athena/src/ADXRS450_Gyro.cpp @@ -8,6 +8,7 @@ #include "ADXRS450_Gyro.h" #include "DriverStation.h" +#include "HAL/HAL.h" #include "LiveWindow/LiveWindow.h" #include "Timer.h" diff --git a/wpilibc/athena/src/DriverStation.cpp b/wpilibc/athena/src/DriverStation.cpp index 54fbfd6474..db7a8b3730 100644 --- a/wpilibc/athena/src/DriverStation.cpp +++ b/wpilibc/athena/src/DriverStation.cpp @@ -11,6 +11,7 @@ #include "AnalogInput.h" #include "FRC_NetworkCommunication/FRCComm.h" +#include "HAL/HAL.h" #include "HAL/cpp/Log.h" #include "MotorSafetyHelper.h" #include "Timer.h" @@ -23,7 +24,7 @@ const int DriverStation::kJoystickPorts; DriverStation::~DriverStation() { m_isRunning = false; - m_task.join(); + m_dsThread.join(); } /** @@ -585,7 +586,7 @@ DriverStation::DriverStation() { m_joystickDescriptorCache[i].name[0] = '\0'; } - m_task = Task("DriverStation", &DriverStation::Run, this); + m_dsThread = std::thread(&DriverStation::Run, this); } /** diff --git a/wpilibc/athena/src/Joystick.cpp b/wpilibc/athena/src/Joystick.cpp index 1011240ca4..8ab0a8a1c7 100644 --- a/wpilibc/athena/src/Joystick.cpp +++ b/wpilibc/athena/src/Joystick.cpp @@ -10,6 +10,7 @@ #include #include "DriverStation.h" +#include "HAL/HAL.h" #include "WPIErrors.h" const int Joystick::kDefaultXAxis; diff --git a/wpilibc/athena/src/Task.cpp b/wpilibc/athena/src/Task.cpp deleted file mode 100644 index 8d7b236960..0000000000 --- a/wpilibc/athena/src/Task.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -#include "Task.h" - -#include -#include - -#include "WPIErrors.h" - -#ifndef OK -#define OK 0 -#endif /* OK */ -#ifndef ERROR -#define ERROR (-1) -#endif /* ERROR */ - -const int Task::kDefaultPriority; - -Task& Task::operator=(Task&& task) { - m_thread.swap(task.m_thread); - m_taskName = std::move(task.m_taskName); - - return *this; -} - -Task::~Task() { - if (m_thread.joinable()) { - std::cout << "[HAL] Exited task " << m_taskName << std::endl; - } -} - -bool Task::joinable() const noexcept { return m_thread.joinable(); } - -void Task::join() { m_thread.join(); } - -void Task::detach() { m_thread.detach(); } - -std::thread::id Task::get_id() const noexcept { return m_thread.get_id(); } - -std::thread::native_handle_type Task::native_handle() { - return m_thread.native_handle(); -} - -/** - * Verifies a task still exists. - * - * @return true on success. - */ -bool Task::Verify() { - auto id = m_thread.native_handle(); - return HAL_VerifyTaskID(&id) == OK; -} - -/** - * Gets the priority of a task. - * - * @return task priority or 0 if an error occured - */ -int Task::GetPriority() { - int priority; - auto id = m_thread.native_handle(); - if (HandleError(HAL_GetTaskPriority(&id, &priority))) - return priority; - else - return 0; -} - -/** - * This routine changes a task's priority to a specified priority. - * Priorities range from 1, the lowest priority, to 99, the highest priority. - * Default task priority is 60. - * - * @param priority The priority at which the internal thread should run. - * @return true on success. - */ -bool Task::SetPriority(int priority) { - auto id = m_thread.native_handle(); - return HandleError(HAL_SetTaskPriority(&id, priority)); -} - -/** - * Returns the name of the task. - * - * @return The name of the task. - */ -std::string Task::GetName() const { return m_taskName; } - -/** - * Handles errors generated by task related code. - */ -bool Task::HandleError(STATUS results) { - if (results != ERROR) return true; - int errsv = errno; - if (errsv == HAL_TaskLib_ILLEGAL_PRIORITY) { - wpi_setWPIErrorWithContext(TaskPriorityError, m_taskName.c_str()); - } else { - std::printf("ERROR: errno=%i", errsv); - wpi_setWPIErrorWithContext(TaskError, m_taskName.c_str()); - } - return false; -} diff --git a/wpilibc/athena/src/Ultrasonic.cpp b/wpilibc/athena/src/Ultrasonic.cpp index 5403d1f979..75712c0820 100644 --- a/wpilibc/athena/src/Ultrasonic.cpp +++ b/wpilibc/athena/src/Ultrasonic.cpp @@ -10,6 +10,7 @@ #include "Counter.h" #include "DigitalInput.h" #include "DigitalOutput.h" +#include "HAL/HAL.h" #include "LiveWindow/LiveWindow.h" #include "Timer.h" #include "Utility.h" @@ -22,7 +23,6 @@ const int Ultrasonic::kPriority; // Max time (ms) between readings. constexpr double Ultrasonic::kMaxUltrasonicTime; constexpr double Ultrasonic::kSpeedOfSoundInchesPerSec; -Task Ultrasonic::m_task; // automatic round robin mode std::atomic Ultrasonic::m_automaticEnabled{false}; std::set Ultrasonic::m_sensors; @@ -208,7 +208,7 @@ void Ultrasonic::SetAutomaticMode(bool enabling) { sensor->m_counter.Reset(); } - m_task = Task("UltrasonicChecker", &Ultrasonic::UltrasonicChecker); + m_thread = std::thread(&Ultrasonic::UltrasonicChecker); // TODO: Currently, lvuser does not have permissions to set task priorities. // Until that is the case, uncommenting this will break user code that calls @@ -216,7 +216,7 @@ void Ultrasonic::SetAutomaticMode(bool enabling) { // m_task.SetPriority(kPriority); } else { // Wait for background task to stop running - m_task.join(); + m_thread.join(); /* Clear all the counters (data now invalid) since automatic mode is * disabled. No synchronization is needed because the background task is diff --git a/wpilibc/athena/src/Utility.cpp b/wpilibc/athena/src/Utility.cpp index 5bac39be10..bcd09eb745 100644 --- a/wpilibc/athena/src/Utility.cpp +++ b/wpilibc/athena/src/Utility.cpp @@ -12,10 +12,11 @@ #include #include +#include #include +#include "ErrorBase.h" #include "HAL/HAL.h" -#include "Task.h" #include "nivision.h" /** diff --git a/wpilibc/shared/include/Task.h b/wpilibc/shared/include/Task.h deleted file mode 100644 index 3941710ce3..0000000000 --- a/wpilibc/shared/include/Task.h +++ /dev/null @@ -1,54 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -#pragma once - -#include -#include -#include - -#include "ErrorBase.h" -#include "HAL/Task.h" - -/** - * Wrapper class around std::thread that allows changing thread priority - */ -class Task : public ErrorBase { - public: - static const int kDefaultPriority = 60; - - Task() = default; - Task(const Task&) = delete; - Task& operator=(const Task&) = delete; - Task& operator=(Task&& task); - - template - Task(const std::string& name, Function&& function, Args&&... args); - - virtual ~Task(); - - bool joinable() const noexcept; - void join(); - void detach(); - std::thread::id get_id() const noexcept; - std::thread::native_handle_type native_handle(); - - bool Verify(); - - int GetPriority(); - - bool SetPriority(int priority); - - std::string GetName() const; - - private: - std::thread m_thread; - std::string m_taskName; - bool HandleError(STATUS results); -}; - -#include "Task.inc" diff --git a/wpilibc/shared/include/Task.inc b/wpilibc/shared/include/Task.inc deleted file mode 100644 index 5a81a19e54..0000000000 --- a/wpilibc/shared/include/Task.inc +++ /dev/null @@ -1,39 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2016. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -#pragma once - -#include -#include -#include - -#include "HAL/HAL.h" - -/** - * Create and launch a task. - * - * @param name The name of the task. "FRC_" will be prepended to the task name. - * @param function The address of the function to run as the new task. - * @param args A parameter pack of arguments to pass to the function. - */ -template -Task::Task(const std::string& name, Function&& function, Args&&... args) { - m_taskName = "FRC_"; - m_taskName += name; - - std::cout << "[HAL] Starting task " << m_taskName << "..." << std::endl; - - m_thread = std::thread(std::forward>(function), - std::forward(args)...); - // TODO: lvuser does not currently have permissions to set the priority. - // SetPriority(kDefaultPriority); - - static std::atomic instances{0}; - instances++; - HAL_Report(HALUsageReporting::kResourceType_Task, instances, 0, - m_taskName.c_str()); -} diff --git a/wpilibcIntegrationTests/src/TestEnvironment.cpp b/wpilibcIntegrationTests/src/TestEnvironment.cpp index 6d19ede5df..0c98ff3254 100644 --- a/wpilibcIntegrationTests/src/TestEnvironment.cpp +++ b/wpilibcIntegrationTests/src/TestEnvironment.cpp @@ -8,6 +8,7 @@ #include #include "DriverStation.h" +#include "HAL/HAL.h" #include "LiveWindow/LiveWindow.h" #include "Timer.h" #include "gtest/gtest.h"