Removes the task class from wpilib (#314)

Was required back on the cRIO, but there are much better alternatives
nowadays.
This commit is contained in:
Thad House
2016-11-01 20:12:08 -07:00
committed by Peter Johnson
parent bc492bb40e
commit 9bbdaf300b
15 changed files with 17 additions and 316 deletions

View File

@@ -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;

View File

@@ -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 <pthread.h>
#include <stdint.h>
#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

View File

@@ -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 <signal.h>
#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, &param) == 0) {
param.sched_priority = priority;
if (pthread_setschedparam(*task, SCHED_FIFO, &param) == 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, &param) == 0) {
*priority = param.sched_priority;
return OK;
} else {
return ERROR;
}
}
} // extern "C"

View File

@@ -11,15 +11,13 @@
#include <condition_variable>
#include <memory>
#include <string>
#include <thread>
#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<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
// Internal Driver Station thread
Task m_task;
std::thread m_dsThread;
std::atomic<bool> m_isRunning{false};
// WPILib WaitForData control variables

View File

@@ -13,7 +13,6 @@
#include <vector>
#include "ErrorBase.h"
#include "Task.h"
#include "networktables/NetworkTable.h"
#include "tables/ITableListener.h"

View File

@@ -11,12 +11,12 @@
#include <memory>
#include <set>
#include <string>
#include <thread>
#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<Ultrasonic*> m_sensors; // ultrasonic sensors
static std::atomic<bool> m_automaticEnabled; // automatic round robin mode

View File

@@ -8,6 +8,7 @@
#include "ADXRS450_Gyro.h"
#include "DriverStation.h"
#include "HAL/HAL.h"
#include "LiveWindow/LiveWindow.h"
#include "Timer.h"

View File

@@ -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);
}
/**

View File

@@ -10,6 +10,7 @@
#include <cmath>
#include "DriverStation.h"
#include "HAL/HAL.h"
#include "WPIErrors.h"
const int Joystick::kDefaultXAxis;

View File

@@ -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 <cerrno>
#include <cstdio>
#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;
}

View File

@@ -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<bool> Ultrasonic::m_automaticEnabled{false};
std::set<Ultrasonic*> 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

View File

@@ -12,10 +12,11 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include "ErrorBase.h"
#include "HAL/HAL.h"
#include "Task.h"
#include "nivision.h"
/**

View File

@@ -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 <iostream>
#include <string>
#include <thread>
#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 <class Function, class... Args>
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"

View File

@@ -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 <atomic>
#include <string>
#include <utility>
#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 <class Function, class... Args>
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<std::decay_t<Function>>(function),
std::forward<Args>(args)...);
// TODO: lvuser does not currently have permissions to set the priority.
// SetPriority(kDefaultPriority);
static std::atomic<int32_t> instances{0};
instances++;
HAL_Report(HALUsageReporting::kResourceType_Task, instances, 0,
m_taskName.c_str());
}

View File

@@ -8,6 +8,7 @@
#include <cstdlib>
#include "DriverStation.h"
#include "HAL/HAL.h"
#include "LiveWindow/LiveWindow.h"
#include "Timer.h"
#include "gtest/gtest.h"