2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Task.h"
|
|
|
|
|
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2015-06-25 01:54:20 -07:00
|
|
|
#ifndef OK
|
|
|
|
|
#define OK 0
|
|
|
|
|
#endif /* OK */
|
|
|
|
|
#ifndef ERROR
|
|
|
|
|
#define ERROR (-1)
|
|
|
|
|
#endif /* ERROR */
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
const uint32_t Task::kDefaultPriority;
|
|
|
|
|
|
2014-08-14 00:07:02 -07:00
|
|
|
Task& Task::operator=(Task&& task) {
|
|
|
|
|
m_thread.swap(task.m_thread);
|
|
|
|
|
m_taskName = std::move(task.m_taskName);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-08-14 00:07:02 -07:00
|
|
|
return *this;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-14 00:07:02 -07:00
|
|
|
Task::~Task() {
|
|
|
|
|
if (m_thread.joinable()) {
|
|
|
|
|
std::cout << "[HAL] Exited task " << m_taskName << std::endl;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-14 00:07:02 -07:00
|
|
|
bool Task::joinable() const noexcept {
|
|
|
|
|
return m_thread.joinable();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-14 00:07:02 -07:00
|
|
|
void Task::join() {
|
|
|
|
|
m_thread.join();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-08-14 00:07:02 -07:00
|
|
|
void Task::detach() {
|
|
|
|
|
m_thread.detach();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-08-14 00:07:02 -07:00
|
|
|
std::thread::id Task::get_id() const noexcept {
|
|
|
|
|
return m_thread.get_id();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-08-14 00:07:02 -07:00
|
|
|
std::thread::native_handle_type Task::native_handle() {
|
|
|
|
|
return m_thread.native_handle();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verifies a task still exists.
|
2015-11-01 00:41:10 -07:00
|
|
|
*
|
|
|
|
|
* @return true on success.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2014-08-14 00:07:02 -07:00
|
|
|
bool Task::Verify() {
|
|
|
|
|
TASK id = (TASK)m_thread.native_handle();
|
|
|
|
|
return verifyTaskID(id) == OK;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the priority of a task.
|
2015-11-01 00:41:10 -07:00
|
|
|
*
|
|
|
|
|
* @return task priority or 0 if an error occured
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t Task::GetPriority() {
|
2014-08-14 00:07:02 -07:00
|
|
|
int priority;
|
|
|
|
|
auto id = m_thread.native_handle();
|
|
|
|
|
if (HandleError(getTaskPriority(&id, &priority)))
|
|
|
|
|
return priority;
|
2015-06-25 15:07:55 -04:00
|
|
|
else
|
|
|
|
|
return 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This routine changes a task's priority to a specified priority.
|
2015-08-12 03:36:42 -07:00
|
|
|
* Priorities range from 1, the lowest priority, to 99, the highest priority.
|
|
|
|
|
* Default task priority is 60.
|
2015-11-01 00:41:10 -07:00
|
|
|
*
|
2015-08-12 03:36:42 -07:00
|
|
|
* @param priority The priority at which the internal thread should run.
|
2015-11-01 00:41:10 -07:00
|
|
|
* @return true on success.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::SetPriority(int32_t priority) {
|
2014-08-14 00:07:02 -07:00
|
|
|
auto id = m_thread.native_handle();
|
|
|
|
|
return HandleError(setTaskPriority(&id, priority));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the name of the task.
|
2015-11-01 00:41:10 -07:00
|
|
|
*
|
|
|
|
|
* @return The name of the task.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2014-08-14 00:07:02 -07:00
|
|
|
std::string Task::GetName() const { return m_taskName; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles errors generated by task related code.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::HandleError(STATUS results) {
|
|
|
|
|
if (results != ERROR) return true;
|
|
|
|
|
int errsv = errno;
|
2014-08-14 00:07:02 -07:00
|
|
|
if (errsv == HAL_taskLib_ILLEGAL_PRIORITY) {
|
|
|
|
|
wpi_setWPIErrorWithContext(TaskPriorityError, m_taskName.c_str());
|
2015-06-25 15:07:55 -04:00
|
|
|
} else {
|
|
|
|
|
printf("ERROR: errno=%i", errsv);
|
2014-08-14 00:07:02 -07:00
|
|
|
wpi_setWPIErrorWithContext(TaskError, m_taskName.c_str());
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|