Now you can download a copy of these docs so you can use them offline! Download now
Task.cpp
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5 /*----------------------------------------------------------------------------*/
6 
7 #include "Task.h"
8 
9 #include "NetworkCommunication/UsageReporting.h"
10 #include "WPIErrors.h"
11 #include <errnoLib.h>
12 #include <string.h>
13 #include <taskLib.h>
14 #include <usrLib.h>
15 
16 const uint32_t Task::kDefaultPriority;
17 const int32_t Task::kInvalidTaskID;
18 
26 Task::Task(const char* name, FUNCPTR function, int32_t priority, uint32_t stackSize)
27 {
28  m_taskID = kInvalidTaskID;
29  m_function = function;
30  m_priority = priority;
31  m_stackSize = stackSize;
32  m_taskName = new char[strlen(name) + 5];
33  strcpy(m_taskName, "FRC_");
34  strcpy(m_taskName+4, name);
35 
36  static int32_t instances = 0;
37  instances++;
38  nUsageReporting::report(nUsageReporting::kResourceType_Task, instances, 0, m_taskName);
39 }
40 
41 Task::~Task()
42 {
43  if (m_taskID != kInvalidTaskID) Stop();
44  delete [] m_taskName;
45  m_taskName = NULL;
46 }
47 
52 bool Task::Start(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4,
53  uint32_t arg5, uint32_t arg6, uint32_t arg7, uint32_t arg8, uint32_t arg9)
54 {
55  m_taskID = taskSpawn(m_taskName,
56  m_priority,
57  VX_FP_TASK, // options
58  m_stackSize, // stack size
59  m_function, // function to start
60  arg0, arg1, arg2, arg3, arg4, // parameter 1 - pointer to this class
61  arg5, arg6, arg7, arg8, arg9);// additional unused parameters
62  bool ok = HandleError(m_taskID);
63  if (!ok) m_taskID = kInvalidTaskID;
64  return ok;
65 }
66 
73 {
74  return HandleError(taskRestart(m_taskID));
75 }
76 
81 bool Task::Stop()
82 {
83  bool ok = true;
84  if (Verify())
85  {
86  ok = HandleError(taskDelete(m_taskID));
87  }
88  m_taskID = kInvalidTaskID;
89  return ok;
90 }
91 
97 {
98  return taskIsReady(m_taskID);
99 }
100 
106 {
107  return taskIsSuspended(m_taskID);
108 }
109 
115 {
116  return HandleError(taskSuspend(m_taskID));
117 }
118 
124 {
125  return HandleError(taskResume(m_taskID));
126 }
127 
133 {
134  return taskIdVerify(m_taskID) == OK;
135 }
136 
142 {
143  if (HandleError(taskPriorityGet(m_taskID, &m_priority)))
144  return m_priority;
145  else
146  return 0;
147 }
148 
156 bool Task::SetPriority(int32_t priority)
157 {
158  m_priority = priority;
159  return HandleError(taskPrioritySet(m_taskID, m_priority));
160 }
161 
166 const char* Task::GetName()
167 {
168  return m_taskName;
169 }
170 
175 int32_t Task::GetID()
176 {
177  if (Verify())
178  return m_taskID;
179  return kInvalidTaskID;
180 }
181 
185 bool Task::HandleError(STATUS results)
186 {
187  if (results != ERROR) return true;
188  switch(errnoGet())
189  {
190  case S_objLib_OBJ_ID_ERROR:
191  wpi_setWPIErrorWithContext(TaskIDError, m_taskName);
192  break;
193 
194  case S_objLib_OBJ_DELETED:
195  wpi_setWPIErrorWithContext(TaskDeletedError, m_taskName);
196  break;
197 
198  case S_taskLib_ILLEGAL_OPTIONS:
199  wpi_setWPIErrorWithContext(TaskOptionsError, m_taskName);
200  break;
201 
202  case S_memLib_NOT_ENOUGH_MEMORY:
203  wpi_setWPIErrorWithContext(TaskMemoryError, m_taskName);
204  break;
205 
206  case S_taskLib_ILLEGAL_PRIORITY:
207  wpi_setWPIErrorWithContext(TaskPriorityError, m_taskName);
208  break;
209 
210  default:
211  printErrno(errnoGet());
212  wpi_setWPIErrorWithContext(TaskError, m_taskName);
213  }
214  return false;
215 }
216 
bool Start(uint32_t arg0=0, uint32_t arg1=0, uint32_t arg2=0, uint32_t arg3=0, uint32_t arg4=0, uint32_t arg5=0, uint32_t arg6=0, uint32_t arg7=0, uint32_t arg8=0, uint32_t arg9=0)
Definition: Task.cpp:52
const char * GetName()
Definition: Task.cpp:166
bool Verify()
Definition: Task.cpp:132
bool IsSuspended()
Definition: Task.cpp:105
bool Resume()
Definition: Task.cpp:123
bool Suspend()
Definition: Task.cpp:114
int32_t GetPriority()
Definition: Task.cpp:141
bool SetPriority(int32_t priority)
Definition: Task.cpp:156
bool Stop()
Definition: Task.cpp:81
bool Restart()
Definition: Task.cpp:72
int32_t GetID()
Definition: Task.cpp:175
bool IsReady()
Definition: Task.cpp:96
Task(const char *name, FUNCPTR function, int32_t priority=kDefaultPriority, uint32_t stackSize=20000)
Definition: Task.cpp:26

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6