Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
scmRTOS/Common/OS_Kernel.h@0:906c21fbf97c, 2010-09-27 (annotated)
- Committer:
- kasturir
- Date:
- Mon Sep 27 22:51:19 2010 +0000
- Revision:
- 0:906c21fbf97c
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kasturir | 0:906c21fbf97c | 1 | //****************************************************************************** |
| kasturir | 0:906c21fbf97c | 2 | //* |
| kasturir | 0:906c21fbf97c | 3 | //* FULLNAME: Single-Chip Microcontroller Real-Time Operating System |
| kasturir | 0:906c21fbf97c | 4 | //* |
| kasturir | 0:906c21fbf97c | 5 | //* NICKNAME: scmRTOS |
| kasturir | 0:906c21fbf97c | 6 | //* |
| kasturir | 0:906c21fbf97c | 7 | //* PURPOSE: OS Kernel Header. Declarations And Definitions |
| kasturir | 0:906c21fbf97c | 8 | //* |
| kasturir | 0:906c21fbf97c | 9 | //* Version: 3.10 |
| kasturir | 0:906c21fbf97c | 10 | //* |
| kasturir | 0:906c21fbf97c | 11 | //* $Revision: 256 $ |
| kasturir | 0:906c21fbf97c | 12 | //* $Date:: 2010-01-22 #$ |
| kasturir | 0:906c21fbf97c | 13 | //* |
| kasturir | 0:906c21fbf97c | 14 | //* Copyright (c) 2003-2010, Harry E. Zhurov |
| kasturir | 0:906c21fbf97c | 15 | //* |
| kasturir | 0:906c21fbf97c | 16 | //* Permission is hereby granted, free of charge, to any person |
| kasturir | 0:906c21fbf97c | 17 | //* obtaining a copy of this software and associated documentation |
| kasturir | 0:906c21fbf97c | 18 | //* files (the "Software"), to deal in the Software without restriction, |
| kasturir | 0:906c21fbf97c | 19 | //* including without limitation the rights to use, copy, modify, merge, |
| kasturir | 0:906c21fbf97c | 20 | //* publish, distribute, sublicense, and/or sell copies of the Software, |
| kasturir | 0:906c21fbf97c | 21 | //* and to permit persons to whom the Software is furnished to do so, |
| kasturir | 0:906c21fbf97c | 22 | //* subject to the following conditions: |
| kasturir | 0:906c21fbf97c | 23 | //* |
| kasturir | 0:906c21fbf97c | 24 | //* The above copyright notice and this permission notice shall be included |
| kasturir | 0:906c21fbf97c | 25 | //* in all copies or substantial portions of the Software. |
| kasturir | 0:906c21fbf97c | 26 | //* |
| kasturir | 0:906c21fbf97c | 27 | //* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| kasturir | 0:906c21fbf97c | 28 | //* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| kasturir | 0:906c21fbf97c | 29 | //* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| kasturir | 0:906c21fbf97c | 30 | //* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| kasturir | 0:906c21fbf97c | 31 | //* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| kasturir | 0:906c21fbf97c | 32 | //* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
| kasturir | 0:906c21fbf97c | 33 | //* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| kasturir | 0:906c21fbf97c | 34 | //* |
| kasturir | 0:906c21fbf97c | 35 | //* ================================================================= |
| kasturir | 0:906c21fbf97c | 36 | //* See http://scmrtos.sourceforge.net for documentation, latest |
| kasturir | 0:906c21fbf97c | 37 | //* information, license and contact details. |
| kasturir | 0:906c21fbf97c | 38 | //* ================================================================= |
| kasturir | 0:906c21fbf97c | 39 | //* |
| kasturir | 0:906c21fbf97c | 40 | //***************************************************************************** |
| kasturir | 0:906c21fbf97c | 41 | |
| kasturir | 0:906c21fbf97c | 42 | #ifndef OS_KERNEL_H |
| kasturir | 0:906c21fbf97c | 43 | #define OS_KERNEL_H |
| kasturir | 0:906c21fbf97c | 44 | |
| kasturir | 0:906c21fbf97c | 45 | #include <stddef.h> |
| kasturir | 0:906c21fbf97c | 46 | #include <commdefs.h> |
| kasturir | 0:906c21fbf97c | 47 | #include <usrlib.h> |
| kasturir | 0:906c21fbf97c | 48 | |
| kasturir | 0:906c21fbf97c | 49 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 50 | |
| kasturir | 0:906c21fbf97c | 51 | //============================================================================== |
| kasturir | 0:906c21fbf97c | 52 | extern "C" void OS_Start(TStackItem* sp); |
| kasturir | 0:906c21fbf97c | 53 | |
| kasturir | 0:906c21fbf97c | 54 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 0 |
| kasturir | 0:906c21fbf97c | 55 | extern "C" void OS_ContextSwitcher(TStackItem** Curr_SP, TStackItem* Next_SP); |
| kasturir | 0:906c21fbf97c | 56 | #else |
| kasturir | 0:906c21fbf97c | 57 | extern "C" TStackItem* OS_ContextSwitchHook(TStackItem* sp); |
| kasturir | 0:906c21fbf97c | 58 | #endif |
| kasturir | 0:906c21fbf97c | 59 | |
| kasturir | 0:906c21fbf97c | 60 | //============================================================================== |
| kasturir | 0:906c21fbf97c | 61 | |
| kasturir | 0:906c21fbf97c | 62 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 63 | // |
| kasturir | 0:906c21fbf97c | 64 | // |
| kasturir | 0:906c21fbf97c | 65 | // NAME : OS |
| kasturir | 0:906c21fbf97c | 66 | // |
| kasturir | 0:906c21fbf97c | 67 | // PURPOSE : Namespace for all OS stuff |
| kasturir | 0:906c21fbf97c | 68 | // |
| kasturir | 0:906c21fbf97c | 69 | // DESCRIPTION: Includes: Kernel, |
| kasturir | 0:906c21fbf97c | 70 | // Processes, |
| kasturir | 0:906c21fbf97c | 71 | // Mutexes, |
| kasturir | 0:906c21fbf97c | 72 | // Event Flags, |
| kasturir | 0:906c21fbf97c | 73 | // Byte-wide Channels, |
| kasturir | 0:906c21fbf97c | 74 | // Arbitrary-type Channels, |
| kasturir | 0:906c21fbf97c | 75 | // Messages |
| kasturir | 0:906c21fbf97c | 76 | // |
| kasturir | 0:906c21fbf97c | 77 | namespace OS |
| kasturir | 0:906c21fbf97c | 78 | { |
| kasturir | 0:906c21fbf97c | 79 | class TBaseProcess; |
| kasturir | 0:906c21fbf97c | 80 | |
| kasturir | 0:906c21fbf97c | 81 | INLINE inline void SetPrioTag(TProcessMap& pm, const TProcessMap PrioTag) { pm |= PrioTag; } |
| kasturir | 0:906c21fbf97c | 82 | INLINE inline void ClrPrioTag(TProcessMap& pm, const TProcessMap PrioTag) { pm &= ~PrioTag; } |
| kasturir | 0:906c21fbf97c | 83 | |
| kasturir | 0:906c21fbf97c | 84 | //-------------------------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 85 | // |
| kasturir | 0:906c21fbf97c | 86 | // NAME : TKernel |
| kasturir | 0:906c21fbf97c | 87 | // |
| kasturir | 0:906c21fbf97c | 88 | /// Implements kernel-level operations such as |
| kasturir | 0:906c21fbf97c | 89 | /// process management, process-level scheduling, |
| kasturir | 0:906c21fbf97c | 90 | /// ISR-level scheduling, system timing. |
| kasturir | 0:906c21fbf97c | 91 | // |
| kasturir | 0:906c21fbf97c | 92 | // DESCRIPTION: |
| kasturir | 0:906c21fbf97c | 93 | // |
| kasturir | 0:906c21fbf97c | 94 | // |
| kasturir | 0:906c21fbf97c | 95 | class TKernel |
| kasturir | 0:906c21fbf97c | 96 | { |
| kasturir | 0:906c21fbf97c | 97 | //----------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 98 | // |
| kasturir | 0:906c21fbf97c | 99 | // Declarations |
| kasturir | 0:906c21fbf97c | 100 | // |
| kasturir | 0:906c21fbf97c | 101 | |
| kasturir | 0:906c21fbf97c | 102 | |
| kasturir | 0:906c21fbf97c | 103 | friend class TISRW; |
| kasturir | 0:906c21fbf97c | 104 | friend class TISRW_SS; |
| kasturir | 0:906c21fbf97c | 105 | friend class TBaseProcess; |
| kasturir | 0:906c21fbf97c | 106 | friend class TMutex; |
| kasturir | 0:906c21fbf97c | 107 | friend class TEventFlag; |
| kasturir | 0:906c21fbf97c | 108 | friend class TChannel; |
| kasturir | 0:906c21fbf97c | 109 | friend class TBaseMessage; |
| kasturir | 0:906c21fbf97c | 110 | |
| kasturir | 0:906c21fbf97c | 111 | template<typename T, word size, class S> friend class channel; |
| kasturir | 0:906c21fbf97c | 112 | template<typename T> friend class message; |
| kasturir | 0:906c21fbf97c | 113 | |
| kasturir | 0:906c21fbf97c | 114 | friend void Run(); |
| kasturir | 0:906c21fbf97c | 115 | friend void WakeUpProcess(TBaseProcess& p); |
| kasturir | 0:906c21fbf97c | 116 | friend void ForceWakeUpProcess(TBaseProcess& p); |
| kasturir | 0:906c21fbf97c | 117 | friend inline bool IsProcessSleeping(const TBaseProcess& p); |
| kasturir | 0:906c21fbf97c | 118 | friend inline bool IsProcessSuspended(const TBaseProcess& p); |
| kasturir | 0:906c21fbf97c | 119 | friend inline dword GetTickCount(); |
| kasturir | 0:906c21fbf97c | 120 | |
| kasturir | 0:906c21fbf97c | 121 | //----------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 122 | // |
| kasturir | 0:906c21fbf97c | 123 | // Data |
| kasturir | 0:906c21fbf97c | 124 | // |
| kasturir | 0:906c21fbf97c | 125 | private: |
| kasturir | 0:906c21fbf97c | 126 | byte CurProcPriority; |
| kasturir | 0:906c21fbf97c | 127 | TProcessMap ReadyProcessMap; |
| kasturir | 0:906c21fbf97c | 128 | TBaseProcess* ProcessTable[scmRTOS_PROCESS_COUNT+1]; |
| kasturir | 0:906c21fbf97c | 129 | volatile byte ISR_NestCount; |
| kasturir | 0:906c21fbf97c | 130 | |
| kasturir | 0:906c21fbf97c | 131 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 1 |
| kasturir | 0:906c21fbf97c | 132 | byte SchedProcPriority; |
| kasturir | 0:906c21fbf97c | 133 | #endif |
| kasturir | 0:906c21fbf97c | 134 | |
| kasturir | 0:906c21fbf97c | 135 | #if scmRTOS_SYSTEM_TICKS_ENABLE == 1 |
| kasturir | 0:906c21fbf97c | 136 | volatile dword SysTickCount; |
| kasturir | 0:906c21fbf97c | 137 | #endif |
| kasturir | 0:906c21fbf97c | 138 | |
| kasturir | 0:906c21fbf97c | 139 | //----------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 140 | // |
| kasturir | 0:906c21fbf97c | 141 | // Functions |
| kasturir | 0:906c21fbf97c | 142 | // |
| kasturir | 0:906c21fbf97c | 143 | public: |
| kasturir | 0:906c21fbf97c | 144 | INLINE TKernel() |
| kasturir | 0:906c21fbf97c | 145 | : CurProcPriority(pr0) |
| kasturir | 0:906c21fbf97c | 146 | , ReadyProcessMap( (1 << (scmRTOS_PROCESS_COUNT + 1)) - 1) // set all processes ready |
| kasturir | 0:906c21fbf97c | 147 | , ISR_NestCount(0) |
| kasturir | 0:906c21fbf97c | 148 | { |
| kasturir | 0:906c21fbf97c | 149 | } |
| kasturir | 0:906c21fbf97c | 150 | |
| kasturir | 0:906c21fbf97c | 151 | private: |
| kasturir | 0:906c21fbf97c | 152 | INLINE inline void RegisterProcess(TBaseProcess* const p); |
| kasturir | 0:906c21fbf97c | 153 | |
| kasturir | 0:906c21fbf97c | 154 | void Sched(); |
| kasturir | 0:906c21fbf97c | 155 | INLINE void Scheduler() { if(ISR_NestCount) return; else Sched(); } |
| kasturir | 0:906c21fbf97c | 156 | INLINE inline void SchedISR(); |
| kasturir | 0:906c21fbf97c | 157 | |
| kasturir | 0:906c21fbf97c | 158 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 1 |
| kasturir | 0:906c21fbf97c | 159 | INLINE inline bool IsContextSwitchDone() const volatile; |
| kasturir | 0:906c21fbf97c | 160 | #endif |
| kasturir | 0:906c21fbf97c | 161 | INLINE void SetProcessReady (const byte pr) { TProcessMap PrioTag = GetPrioTag(pr); SetPrioTag( ReadyProcessMap, PrioTag); } |
| kasturir | 0:906c21fbf97c | 162 | INLINE void SetProcessUnready(const byte pr) { TProcessMap PrioTag = GetPrioTag(pr); ClrPrioTag( ReadyProcessMap, PrioTag); } |
| kasturir | 0:906c21fbf97c | 163 | |
| kasturir | 0:906c21fbf97c | 164 | public: |
| kasturir | 0:906c21fbf97c | 165 | INLINE inline void SystemTimer(); |
| kasturir | 0:906c21fbf97c | 166 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 1 |
| kasturir | 0:906c21fbf97c | 167 | INLINE inline TStackItem* ContextSwitchHook(TStackItem* sp); |
| kasturir | 0:906c21fbf97c | 168 | #endif |
| kasturir | 0:906c21fbf97c | 169 | |
| kasturir | 0:906c21fbf97c | 170 | }; // End of TKernel class definition |
| kasturir | 0:906c21fbf97c | 171 | //-------------------------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 172 | extern TKernel Kernel; |
| kasturir | 0:906c21fbf97c | 173 | |
| kasturir | 0:906c21fbf97c | 174 | //-------------------------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 175 | // |
| kasturir | 0:906c21fbf97c | 176 | /// BaseProcess |
| kasturir | 0:906c21fbf97c | 177 | /// |
| kasturir | 0:906c21fbf97c | 178 | /// Implements base class-type for application processes |
| kasturir | 0:906c21fbf97c | 179 | // |
| kasturir | 0:906c21fbf97c | 180 | // DESCRIPTION: |
| kasturir | 0:906c21fbf97c | 181 | // |
| kasturir | 0:906c21fbf97c | 182 | // |
| kasturir | 0:906c21fbf97c | 183 | class TBaseProcess |
| kasturir | 0:906c21fbf97c | 184 | { |
| kasturir | 0:906c21fbf97c | 185 | friend class TKernel; |
| kasturir | 0:906c21fbf97c | 186 | friend class TISRW; |
| kasturir | 0:906c21fbf97c | 187 | friend class TISRW_SS; |
| kasturir | 0:906c21fbf97c | 188 | friend class TEventFlag; |
| kasturir | 0:906c21fbf97c | 189 | friend class TMutex; |
| kasturir | 0:906c21fbf97c | 190 | friend class TBaseMessage; |
| kasturir | 0:906c21fbf97c | 191 | |
| kasturir | 0:906c21fbf97c | 192 | template<typename T, word size, class S> friend class channel; |
| kasturir | 0:906c21fbf97c | 193 | template<typename T> friend class message; |
| kasturir | 0:906c21fbf97c | 194 | |
| kasturir | 0:906c21fbf97c | 195 | |
| kasturir | 0:906c21fbf97c | 196 | friend void Run(); |
| kasturir | 0:906c21fbf97c | 197 | friend void WakeUpProcess(TBaseProcess& p); |
| kasturir | 0:906c21fbf97c | 198 | friend void ForceWakeUpProcess(TBaseProcess& p); |
| kasturir | 0:906c21fbf97c | 199 | friend bool IsProcessSleeping(const TBaseProcess& p); |
| kasturir | 0:906c21fbf97c | 200 | friend bool IsProcessSuspended(const TBaseProcess& p); |
| kasturir | 0:906c21fbf97c | 201 | |
| kasturir | 0:906c21fbf97c | 202 | public: |
| kasturir | 0:906c21fbf97c | 203 | #if SEPARATE_RETURN_STACK == 0 |
| kasturir | 0:906c21fbf97c | 204 | TBaseProcess( TStackItem* Stack, TPriority pr, void (*exec)() ); |
| kasturir | 0:906c21fbf97c | 205 | #else |
| kasturir | 0:906c21fbf97c | 206 | TBaseProcess( TStackItem* Stack, TStackItem* RStack, TPriority pr, void (*exec)() ); |
| kasturir | 0:906c21fbf97c | 207 | #endif |
| kasturir | 0:906c21fbf97c | 208 | |
| kasturir | 0:906c21fbf97c | 209 | static void Sleep(TTimeout timeout = 0); |
| kasturir | 0:906c21fbf97c | 210 | |
| kasturir | 0:906c21fbf97c | 211 | protected: |
| kasturir | 0:906c21fbf97c | 212 | TStackItem* StackPointer; |
| kasturir | 0:906c21fbf97c | 213 | TTimeout Timeout; |
| kasturir | 0:906c21fbf97c | 214 | TPriority Priority; |
| kasturir | 0:906c21fbf97c | 215 | }; |
| kasturir | 0:906c21fbf97c | 216 | //-------------------------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 217 | |
| kasturir | 0:906c21fbf97c | 218 | //-------------------------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 219 | // |
| kasturir | 0:906c21fbf97c | 220 | /// process |
| kasturir | 0:906c21fbf97c | 221 | /// |
| kasturir | 0:906c21fbf97c | 222 | /// Implements template for application processes instantiation |
| kasturir | 0:906c21fbf97c | 223 | // |
| kasturir | 0:906c21fbf97c | 224 | // DESCRIPTION: |
| kasturir | 0:906c21fbf97c | 225 | // |
| kasturir | 0:906c21fbf97c | 226 | // |
| kasturir | 0:906c21fbf97c | 227 | #if SEPARATE_RETURN_STACK == 0 |
| kasturir | 0:906c21fbf97c | 228 | |
| kasturir | 0:906c21fbf97c | 229 | template<TPriority pr, word stack_size> |
| kasturir | 0:906c21fbf97c | 230 | class process : public TBaseProcess |
| kasturir | 0:906c21fbf97c | 231 | { |
| kasturir | 0:906c21fbf97c | 232 | public: |
| kasturir | 0:906c21fbf97c | 233 | INLINE_PROCESS_CTOR process(); |
| kasturir | 0:906c21fbf97c | 234 | |
| kasturir | 0:906c21fbf97c | 235 | OS_PROCESS static void Exec(); |
| kasturir | 0:906c21fbf97c | 236 | |
| kasturir | 0:906c21fbf97c | 237 | private: |
| kasturir | 0:906c21fbf97c | 238 | TStackItem Stack[stack_size/sizeof(TStackItem)]; |
| kasturir | 0:906c21fbf97c | 239 | }; |
| kasturir | 0:906c21fbf97c | 240 | |
| kasturir | 0:906c21fbf97c | 241 | template<TPriority pr, word stack_size> |
| kasturir | 0:906c21fbf97c | 242 | OS::process<pr, stack_size>::process() : TBaseProcess( &Stack[stack_size/sizeof(TStackItem)] |
| kasturir | 0:906c21fbf97c | 243 | , pr |
| kasturir | 0:906c21fbf97c | 244 | , reinterpret_cast<void (*)()>(Exec) ) |
| kasturir | 0:906c21fbf97c | 245 | { |
| kasturir | 0:906c21fbf97c | 246 | } |
| kasturir | 0:906c21fbf97c | 247 | |
| kasturir | 0:906c21fbf97c | 248 | #else |
| kasturir | 0:906c21fbf97c | 249 | |
| kasturir | 0:906c21fbf97c | 250 | template<TPriority pr, word stack_size, word rstack_size> |
| kasturir | 0:906c21fbf97c | 251 | class process : public TBaseProcess |
| kasturir | 0:906c21fbf97c | 252 | { |
| kasturir | 0:906c21fbf97c | 253 | public: |
| kasturir | 0:906c21fbf97c | 254 | INLINE_PROCESS_CTOR process(); |
| kasturir | 0:906c21fbf97c | 255 | |
| kasturir | 0:906c21fbf97c | 256 | OS_PROCESS static void Exec(); |
| kasturir | 0:906c21fbf97c | 257 | |
| kasturir | 0:906c21fbf97c | 258 | private: |
| kasturir | 0:906c21fbf97c | 259 | TStackItem Stack [stack_size/sizeof(TStackItem)]; |
| kasturir | 0:906c21fbf97c | 260 | TStackItem RStack[rstack_size/sizeof(TStackItem)]; |
| kasturir | 0:906c21fbf97c | 261 | }; |
| kasturir | 0:906c21fbf97c | 262 | |
| kasturir | 0:906c21fbf97c | 263 | template<TPriority pr, word stack_size, word rstack_size> |
| kasturir | 0:906c21fbf97c | 264 | process<pr, stack_size, rstack_size>::process() : TBaseProcess( &Stack[stack_size/sizeof(TStackItem)] |
| kasturir | 0:906c21fbf97c | 265 | , &RStack[rstack_size/sizeof(TStackItem)] |
| kasturir | 0:906c21fbf97c | 266 | , pr |
| kasturir | 0:906c21fbf97c | 267 | , reinterpret_cast<void (*)()>(Exec)) |
| kasturir | 0:906c21fbf97c | 268 | { |
| kasturir | 0:906c21fbf97c | 269 | } |
| kasturir | 0:906c21fbf97c | 270 | |
| kasturir | 0:906c21fbf97c | 271 | #endif |
| kasturir | 0:906c21fbf97c | 272 | //-------------------------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 273 | |
| kasturir | 0:906c21fbf97c | 274 | //-------------------------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 275 | // |
| kasturir | 0:906c21fbf97c | 276 | // Miscellaneous |
| kasturir | 0:906c21fbf97c | 277 | // |
| kasturir | 0:906c21fbf97c | 278 | // |
| kasturir | 0:906c21fbf97c | 279 | INLINE inline void Run(); |
| kasturir | 0:906c21fbf97c | 280 | INLINE inline void LockSystemTimer() { TCritSect cs; LOCK_SYSTEM_TIMER(); } |
| kasturir | 0:906c21fbf97c | 281 | INLINE inline void UnlockSystemTimer() { TCritSect cs; UNLOCK_SYSTEM_TIMER(); } |
| kasturir | 0:906c21fbf97c | 282 | void WakeUpProcess(TBaseProcess& p); |
| kasturir | 0:906c21fbf97c | 283 | void ForceWakeUpProcess(TBaseProcess& p); |
| kasturir | 0:906c21fbf97c | 284 | INLINE inline void Sleep(TTimeout t = 0) { TBaseProcess::Sleep(t); } |
| kasturir | 0:906c21fbf97c | 285 | |
| kasturir | 0:906c21fbf97c | 286 | INLINE inline bool IsProcessSleeping(const TBaseProcess& p) |
| kasturir | 0:906c21fbf97c | 287 | { |
| kasturir | 0:906c21fbf97c | 288 | TCritSect cs; |
| kasturir | 0:906c21fbf97c | 289 | if(p.Timeout) |
| kasturir | 0:906c21fbf97c | 290 | return true; |
| kasturir | 0:906c21fbf97c | 291 | else |
| kasturir | 0:906c21fbf97c | 292 | return false; |
| kasturir | 0:906c21fbf97c | 293 | } |
| kasturir | 0:906c21fbf97c | 294 | |
| kasturir | 0:906c21fbf97c | 295 | INLINE inline bool IsProcessSuspended(const TBaseProcess& p) |
| kasturir | 0:906c21fbf97c | 296 | { |
| kasturir | 0:906c21fbf97c | 297 | TCritSect cs; |
| kasturir | 0:906c21fbf97c | 298 | if(Kernel.ReadyProcessMap & GetPrioTag(p.Priority)) |
| kasturir | 0:906c21fbf97c | 299 | return false; |
| kasturir | 0:906c21fbf97c | 300 | else |
| kasturir | 0:906c21fbf97c | 301 | return true; |
| kasturir | 0:906c21fbf97c | 302 | } |
| kasturir | 0:906c21fbf97c | 303 | //-------------------------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 304 | |
| kasturir | 0:906c21fbf97c | 305 | #if scmRTOS_SYSTEM_TICKS_ENABLE == 1 |
| kasturir | 0:906c21fbf97c | 306 | INLINE inline dword GetTickCount() { TCritSect cs; return Kernel.SysTickCount; } |
| kasturir | 0:906c21fbf97c | 307 | #endif |
| kasturir | 0:906c21fbf97c | 308 | |
| kasturir | 0:906c21fbf97c | 309 | #if scmRTOS_SYSTIMER_HOOK_ENABLE == 1 |
| kasturir | 0:906c21fbf97c | 310 | INLINE_SYS_TIMER_HOOK void SystemTimerUserHook(); |
| kasturir | 0:906c21fbf97c | 311 | #endif |
| kasturir | 0:906c21fbf97c | 312 | |
| kasturir | 0:906c21fbf97c | 313 | #if scmRTOS_CONTEXT_SWITCH_USER_HOOK_ENABLE == 1 |
| kasturir | 0:906c21fbf97c | 314 | INLINE_CONTEXT_SWITCH_HOOK void ContextSwitchUserHook(); |
| kasturir | 0:906c21fbf97c | 315 | #endif |
| kasturir | 0:906c21fbf97c | 316 | |
| kasturir | 0:906c21fbf97c | 317 | } |
| kasturir | 0:906c21fbf97c | 318 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 319 | |
| kasturir | 0:906c21fbf97c | 320 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 321 | // |
| kasturir | 0:906c21fbf97c | 322 | /// Register Process |
| kasturir | 0:906c21fbf97c | 323 | /// |
| kasturir | 0:906c21fbf97c | 324 | /// Places pointer to process in kernel's process table |
| kasturir | 0:906c21fbf97c | 325 | // |
| kasturir | 0:906c21fbf97c | 326 | void OS::TKernel::RegisterProcess(OS::TBaseProcess* const p) |
| kasturir | 0:906c21fbf97c | 327 | { |
| kasturir | 0:906c21fbf97c | 328 | ProcessTable[p->Priority] = p; |
| kasturir | 0:906c21fbf97c | 329 | } |
| kasturir | 0:906c21fbf97c | 330 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 331 | // |
| kasturir | 0:906c21fbf97c | 332 | /// System Timer Implementation |
| kasturir | 0:906c21fbf97c | 333 | /// |
| kasturir | 0:906c21fbf97c | 334 | /// Performs process's timeouts checking and |
| kasturir | 0:906c21fbf97c | 335 | /// moving processes to ready-to-run state |
| kasturir | 0:906c21fbf97c | 336 | // |
| kasturir | 0:906c21fbf97c | 337 | void OS::TKernel::SystemTimer() |
| kasturir | 0:906c21fbf97c | 338 | { |
| kasturir | 0:906c21fbf97c | 339 | SYS_TIMER_CRIT_SECT(); |
| kasturir | 0:906c21fbf97c | 340 | #if scmRTOS_SYSTEM_TICKS_ENABLE == 1 |
| kasturir | 0:906c21fbf97c | 341 | SysTickCount++; |
| kasturir | 0:906c21fbf97c | 342 | #endif |
| kasturir | 0:906c21fbf97c | 343 | |
| kasturir | 0:906c21fbf97c | 344 | #if scmRTOS_PRIORITY_ORDER == 0 |
| kasturir | 0:906c21fbf97c | 345 | const byte BaseIndex = 0; |
| kasturir | 0:906c21fbf97c | 346 | #else |
| kasturir | 0:906c21fbf97c | 347 | const byte BaseIndex = 1; |
| kasturir | 0:906c21fbf97c | 348 | #endif |
| kasturir | 0:906c21fbf97c | 349 | |
| kasturir | 0:906c21fbf97c | 350 | for(byte i = BaseIndex; i < (scmRTOS_PROCESS_COUNT + BaseIndex); i++) |
| kasturir | 0:906c21fbf97c | 351 | { |
| kasturir | 0:906c21fbf97c | 352 | TBaseProcess* p = ProcessTable[i]; |
| kasturir | 0:906c21fbf97c | 353 | |
| kasturir | 0:906c21fbf97c | 354 | if(p->Timeout > 0) |
| kasturir | 0:906c21fbf97c | 355 | { |
| kasturir | 0:906c21fbf97c | 356 | if(--p->Timeout == 0) |
| kasturir | 0:906c21fbf97c | 357 | { |
| kasturir | 0:906c21fbf97c | 358 | SetProcessReady(p->Priority); |
| kasturir | 0:906c21fbf97c | 359 | } |
| kasturir | 0:906c21fbf97c | 360 | } |
| kasturir | 0:906c21fbf97c | 361 | } |
| kasturir | 0:906c21fbf97c | 362 | } |
| kasturir | 0:906c21fbf97c | 363 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 364 | // |
| kasturir | 0:906c21fbf97c | 365 | /// ISR optimized scheduler |
| kasturir | 0:906c21fbf97c | 366 | /// |
| kasturir | 0:906c21fbf97c | 367 | /// !!! IMPORTANT: This function must be call from ISR services only !!! |
| kasturir | 0:906c21fbf97c | 368 | // |
| kasturir | 0:906c21fbf97c | 369 | // |
| kasturir | 0:906c21fbf97c | 370 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 0 |
| kasturir | 0:906c21fbf97c | 371 | void OS::TKernel::SchedISR() |
| kasturir | 0:906c21fbf97c | 372 | { |
| kasturir | 0:906c21fbf97c | 373 | byte NextPrty = GetHighPriority(ReadyProcessMap); |
| kasturir | 0:906c21fbf97c | 374 | if(NextPrty != CurProcPriority) |
| kasturir | 0:906c21fbf97c | 375 | { |
| kasturir | 0:906c21fbf97c | 376 | TStackItem* Next_SP = ProcessTable[NextPrty]->StackPointer; |
| kasturir | 0:906c21fbf97c | 377 | TStackItem** Curr_SP_addr = &(ProcessTable[CurProcPriority]->StackPointer); |
| kasturir | 0:906c21fbf97c | 378 | CurProcPriority = NextPrty; |
| kasturir | 0:906c21fbf97c | 379 | OS_ContextSwitcher(Curr_SP_addr, Next_SP); |
| kasturir | 0:906c21fbf97c | 380 | } |
| kasturir | 0:906c21fbf97c | 381 | } |
| kasturir | 0:906c21fbf97c | 382 | #else |
| kasturir | 0:906c21fbf97c | 383 | void OS::TKernel::SchedISR() |
| kasturir | 0:906c21fbf97c | 384 | { |
| kasturir | 0:906c21fbf97c | 385 | byte NextPrty = GetHighPriority(ReadyProcessMap); |
| kasturir | 0:906c21fbf97c | 386 | if(NextPrty != CurProcPriority) |
| kasturir | 0:906c21fbf97c | 387 | { |
| kasturir | 0:906c21fbf97c | 388 | SchedProcPriority = NextPrty; |
| kasturir | 0:906c21fbf97c | 389 | RaiseContextSwitch(); |
| kasturir | 0:906c21fbf97c | 390 | } |
| kasturir | 0:906c21fbf97c | 391 | } |
| kasturir | 0:906c21fbf97c | 392 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 393 | bool OS::TKernel::IsContextSwitchDone() const volatile |
| kasturir | 0:906c21fbf97c | 394 | { |
| kasturir | 0:906c21fbf97c | 395 | byte cur = CurProcPriority; ///< reading to temporary vars is performed due to |
| kasturir | 0:906c21fbf97c | 396 | byte sched = SchedProcPriority; ///< suppress warning about order of volatile access |
| kasturir | 0:906c21fbf97c | 397 | return cur == sched; |
| kasturir | 0:906c21fbf97c | 398 | } |
| kasturir | 0:906c21fbf97c | 399 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 400 | TStackItem* OS::TKernel::ContextSwitchHook(TStackItem* sp) |
| kasturir | 0:906c21fbf97c | 401 | { |
| kasturir | 0:906c21fbf97c | 402 | ProcessTable[CurProcPriority]->StackPointer = sp; |
| kasturir | 0:906c21fbf97c | 403 | sp = ProcessTable[SchedProcPriority]->StackPointer; |
| kasturir | 0:906c21fbf97c | 404 | |
| kasturir | 0:906c21fbf97c | 405 | #if scmRTOS_CONTEXT_SWITCH_USER_HOOK_ENABLE == 1 |
| kasturir | 0:906c21fbf97c | 406 | ContextSwitchUserHook(); |
| kasturir | 0:906c21fbf97c | 407 | #endif |
| kasturir | 0:906c21fbf97c | 408 | |
| kasturir | 0:906c21fbf97c | 409 | CurProcPriority = SchedProcPriority; |
| kasturir | 0:906c21fbf97c | 410 | return sp; |
| kasturir | 0:906c21fbf97c | 411 | } |
| kasturir | 0:906c21fbf97c | 412 | //------------------------------------------------------------------------------ |
| kasturir | 0:906c21fbf97c | 413 | #endif // scmRTOS_CONTEXT_SWITCH_SCHEME |
| kasturir | 0:906c21fbf97c | 414 | |
| kasturir | 0:906c21fbf97c | 415 | //----------------------------------------------------------------------------- |
| kasturir | 0:906c21fbf97c | 416 | /// Start Operation |
| kasturir | 0:906c21fbf97c | 417 | INLINE inline void OS::Run() |
| kasturir | 0:906c21fbf97c | 418 | { |
| kasturir | 0:906c21fbf97c | 419 | TStackItem* sp = Kernel.ProcessTable[pr0]->StackPointer; |
| kasturir | 0:906c21fbf97c | 420 | OS_Start(sp); |
| kasturir | 0:906c21fbf97c | 421 | } |
| kasturir | 0:906c21fbf97c | 422 | |
| kasturir | 0:906c21fbf97c | 423 | #include <OS_Services.h> |
| kasturir | 0:906c21fbf97c | 424 | |
| kasturir | 0:906c21fbf97c | 425 | #endif // OS_KERNEL_H |
| kasturir | 0:906c21fbf97c | 426 |