2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Task.h"
|
|
|
|
|
|
2014-01-06 10:12:21 -05:00
|
|
|
//#include "NetworkCommunication/UsageReporting.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
2014-09-25 20:36:59 -04:00
|
|
|
#include "HAL/HAL.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
const uint32_t Task::kDefaultPriority;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create but don't 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 priority The VxWorks priority for the task.
|
|
|
|
|
* @param stackSize The size of the stack for the task
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Task::Task(const char* name, FUNCPTR function, int32_t priority,
|
|
|
|
|
uint32_t stackSize) {
|
|
|
|
|
m_function = function;
|
|
|
|
|
m_priority = priority;
|
|
|
|
|
m_stackSize = stackSize;
|
|
|
|
|
m_taskName = new char[strlen(name) + 5];
|
|
|
|
|
strcpy(m_taskName, "FRC_");
|
|
|
|
|
strcpy(m_taskName + 4, name);
|
|
|
|
|
|
|
|
|
|
static int32_t instances = 0;
|
|
|
|
|
instances++;
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Task, instances, 0, m_taskName);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Task::~Task() {
|
|
|
|
|
if (m_taskID != NULL_TASK) Stop();
|
|
|
|
|
delete[] m_taskName;
|
2015-06-23 04:49:51 -07:00
|
|
|
m_taskName = nullptr;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starts this task.
|
|
|
|
|
* If it is already running or unable to start, it fails and returns false.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::Start(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3,
|
|
|
|
|
uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
|
|
|
|
|
uint32_t arg8, uint32_t arg9) {
|
|
|
|
|
m_taskID = spawnTask(
|
|
|
|
|
m_taskName, m_priority,
|
|
|
|
|
VXWORKS_FP_TASK, // options
|
|
|
|
|
m_stackSize, // stack size
|
|
|
|
|
m_function, // function to start
|
|
|
|
|
arg0, arg1, arg2, arg3, arg4, // parameter 1 - pointer to this class
|
|
|
|
|
arg5, arg6, arg7, arg8, arg9); // additional unused parameters
|
|
|
|
|
if (m_taskID == NULL_TASK) {
|
|
|
|
|
HandleError(ERROR);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Restarts a running task.
|
|
|
|
|
* If the task isn't started, it starts it.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return false if the task is running and we are unable to kill the previous
|
|
|
|
|
* instance
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::Restart() { return HandleError(restartTask(m_taskID)); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Kills the running task.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @returns true on success false if the task doesn't exist or we are unable to
|
|
|
|
|
* kill it.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::Stop() {
|
|
|
|
|
bool ok = true;
|
|
|
|
|
if (Verify()) {
|
|
|
|
|
ok = HandleError(deleteTask(m_taskID));
|
|
|
|
|
}
|
|
|
|
|
m_taskID = NULL_TASK;
|
|
|
|
|
return ok;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns true if the task is ready to execute (i.e. not suspended, delayed, or
|
|
|
|
|
* blocked).
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return true if ready, false if not ready.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::IsReady() const { return isTaskReady(m_taskID); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if the task was explicitly suspended by calling Suspend()
|
|
|
|
|
* @return true if suspended, false if not suspended.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::IsSuspended() const { return isTaskSuspended(m_taskID); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pauses a running task.
|
|
|
|
|
* Returns true on success, false if unable to pause or the task isn't running.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::Suspend() { return HandleError(suspendTask(m_taskID)); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resumes a paused task.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns true on success, false if unable to resume or if the task isn't
|
|
|
|
|
* running/paused.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::Resume() { return HandleError(resumeTask(m_taskID)); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verifies a task still exists.
|
|
|
|
|
* @returns true on success.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::Verify() const { return verifyTaskID(m_taskID) == OK; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the priority of a task.
|
|
|
|
|
* @returns task priority or 0 if an error occured
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t Task::GetPriority() {
|
|
|
|
|
if (HandleError(getTaskPriority(m_taskID, &m_priority)))
|
|
|
|
|
return m_priority;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This routine changes a task's priority to a specified priority.
|
|
|
|
|
* Priorities range from 0, the highest priority, to 255, the lowest priority.
|
|
|
|
|
* Default task priority is 100.
|
|
|
|
|
* @param priority The priority the task should run at.
|
|
|
|
|
* @returns true on success.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Task::SetPriority(int32_t priority) {
|
|
|
|
|
m_priority = priority;
|
|
|
|
|
return HandleError(setTaskPriority(m_taskID, m_priority));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the name of the task.
|
2015-06-23 04:49:51 -07:00
|
|
|
* @returns Pointer to the name of the task or nullptr if not allocated
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
const char* Task::GetName() const { return m_taskName; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the ID of a task
|
2015-06-25 15:07:55 -04:00
|
|
|
* @returns Task ID of this task. Task::kInvalidTaskID (-1) if the task has not
|
|
|
|
|
* been started or has already exited.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
TASK Task::GetID() const {
|
|
|
|
|
if (Verify()) return m_taskID;
|
|
|
|
|
return NULL_TASK;
|
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;
|
|
|
|
|
if (errsv == HAL_objLib_OBJ_ID_ERROR) {
|
|
|
|
|
wpi_setWPIErrorWithContext(TaskIDError, m_taskName);
|
|
|
|
|
} else if (errsv == HAL_objLib_OBJ_DELETED) {
|
|
|
|
|
wpi_setWPIErrorWithContext(TaskDeletedError, m_taskName);
|
|
|
|
|
} else if (errsv == HAL_taskLib_ILLEGAL_OPTIONS) {
|
|
|
|
|
wpi_setWPIErrorWithContext(TaskOptionsError, m_taskName);
|
|
|
|
|
} else if (errsv == HAL_memLib_NOT_ENOUGH_MEMORY) {
|
|
|
|
|
wpi_setWPIErrorWithContext(TaskMemoryError, m_taskName);
|
|
|
|
|
} else if (errsv == HAL_taskLib_ILLEGAL_PRIORITY) {
|
|
|
|
|
wpi_setWPIErrorWithContext(TaskPriorityError, m_taskName);
|
|
|
|
|
} else {
|
|
|
|
|
printf("ERROR: errno=%i", errsv);
|
|
|
|
|
wpi_setWPIErrorWithContext(TaskError, m_taskName);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|