embkernel
 All Classes Functions Variables Typedefs Groups Pages
RtosTask.cpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
8 #include "RtosTask.hpp"
9 #include "Rtos.hpp"
10 #include "RtosPortable.hpp"
11 #include "RtosHeap.hpp"
12 #include "RtosMacros.hpp"
13 
21 RtosTask::RtosTask(Rtos::PRIORITY priority, const char* name, size_t stackSize, void (*run)(RtosTask*)) {
22  initVariables(priority, name, stackSize);
23  RTOS_ASSERT(mStack);
24  mCurrentStackPointer = RtosPortable::stackInit(mStack, stackSize, (void (*)(void*))run, this);
25  Rtos::addTask(this);
26 }
27 
35 RtosTask::RtosTask(Rtos::PRIORITY priority, const char* name, size_t stackSize, RtosRunnable& runnable) {
36  initVariables(priority, name, stackSize);
37  RTOS_ASSERT(mStack);
38  mCurrentStackPointer = RtosPortable::stackInit(mStack, stackSize, (void (*)(void*))caller, &runnable);
39  Rtos::addTask(this);
40 }
41 
45 RtosTask::~RtosTask() {
46  Rtos::removeTask(this);
47 }
48 
55 void RtosTask::initVariables(Rtos::PRIORITY priority, const char* name, size_t stackSize) {
56  mName = name;
57  mPriority = priority;
58  mIterableCore.mSortValue = mPriority;
59  mIterableCore.mTaskOwner = this;
60  mIterableSyncObj.mTaskOwner = this;
61  mStack = RtosHeap::alloc(stackSize);
62  mStackSize = stackSize;
63 }
64 
69 void RtosTask::caller(RtosRunnable* runnable) {
70  runnable->run();
71 }
72 
76 void RtosTask::suspend() {
77  RtosPortable::enterCriticalSection();
78  if (mIterableSyncObj.mListOwner) {
79  mIterableSyncObj.mListOwner->remove(mIterableSyncObj);
80  }
81  mIterableCore.mListOwner->remove(mIterableCore);
82  Rtos::sListSuspended.insertAtEnd(mIterableCore);
83  RtosPortable::yield();
84  RtosPortable::exitCriticalSection();
85 }
86 
90 void RtosTask::resume() {
91  RtosPortable::enterCriticalSection();
92  if (mIterableSyncObj.mListOwner) {
93  mIterableSyncObj.mListOwner->remove(mIterableSyncObj);
94  }
95  mIterableCore.mListOwner->remove(mIterableCore);
96  Rtos::sListReady[mPriority].insertAtEnd(mIterableCore);
97  RtosPortable::yield();
98  RtosPortable::exitCriticalSection();
99 }
100 
105 const char* RtosTask::getName() {
106  return mName;
107 }
108 
114  return mStackSize;
115 }
116 
122  return RtosPortable::getFreeStackSize(mStack, mStackSize);
123 }