Quick and dirty port of scmRTOS demo to mbed 1768. scmRTOS is a small RTOS written using C++. Offers (static) processes, critical sections, mutexes, messages, channels.
scmRTOS/Common/OS_Kernel.cpp@0:a405220cf420, 2010-09-09 (annotated)
- Committer:
- igorsk
- Date:
- Thu Sep 09 21:19:01 2010 +0000
- Revision:
- 0:a405220cf420
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
igorsk | 0:a405220cf420 | 1 | //****************************************************************************** |
igorsk | 0:a405220cf420 | 2 | //* |
igorsk | 0:a405220cf420 | 3 | //* FULLNAME: Single-Chip Microcontroller Real-Time Operating System |
igorsk | 0:a405220cf420 | 4 | //* |
igorsk | 0:a405220cf420 | 5 | //* NICKNAME: scmRTOS |
igorsk | 0:a405220cf420 | 6 | //* |
igorsk | 0:a405220cf420 | 7 | //* PURPOSE: OS Kernel Source |
igorsk | 0:a405220cf420 | 8 | //* |
igorsk | 0:a405220cf420 | 9 | //* Version: 3.10 |
igorsk | 0:a405220cf420 | 10 | //* |
igorsk | 0:a405220cf420 | 11 | //* $Revision: 256 $ |
igorsk | 0:a405220cf420 | 12 | //* $Date:: 2010-01-22 #$ |
igorsk | 0:a405220cf420 | 13 | //* |
igorsk | 0:a405220cf420 | 14 | //* Copyright (c) 2003-2010, Harry E. Zhurov |
igorsk | 0:a405220cf420 | 15 | //* |
igorsk | 0:a405220cf420 | 16 | //* Permission is hereby granted, free of charge, to any person |
igorsk | 0:a405220cf420 | 17 | //* obtaining a copy of this software and associated documentation |
igorsk | 0:a405220cf420 | 18 | //* files (the "Software"), to deal in the Software without restriction, |
igorsk | 0:a405220cf420 | 19 | //* including without limitation the rights to use, copy, modify, merge, |
igorsk | 0:a405220cf420 | 20 | //* publish, distribute, sublicense, and/or sell copies of the Software, |
igorsk | 0:a405220cf420 | 21 | //* and to permit persons to whom the Software is furnished to do so, |
igorsk | 0:a405220cf420 | 22 | //* subject to the following conditions: |
igorsk | 0:a405220cf420 | 23 | //* |
igorsk | 0:a405220cf420 | 24 | //* The above copyright notice and this permission notice shall be included |
igorsk | 0:a405220cf420 | 25 | //* in all copies or substantial portions of the Software. |
igorsk | 0:a405220cf420 | 26 | //* |
igorsk | 0:a405220cf420 | 27 | //* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
igorsk | 0:a405220cf420 | 28 | //* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
igorsk | 0:a405220cf420 | 29 | //* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
igorsk | 0:a405220cf420 | 30 | //* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
igorsk | 0:a405220cf420 | 31 | //* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
igorsk | 0:a405220cf420 | 32 | //* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
igorsk | 0:a405220cf420 | 33 | //* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
igorsk | 0:a405220cf420 | 34 | //* |
igorsk | 0:a405220cf420 | 35 | //* ================================================================= |
igorsk | 0:a405220cf420 | 36 | //* See http://scmrtos.sourceforge.net for documentation, latest |
igorsk | 0:a405220cf420 | 37 | //* information, license and contact details. |
igorsk | 0:a405220cf420 | 38 | //* ================================================================= |
igorsk | 0:a405220cf420 | 39 | //* |
igorsk | 0:a405220cf420 | 40 | //****************************************************************************** |
igorsk | 0:a405220cf420 | 41 | |
igorsk | 0:a405220cf420 | 42 | #include "scmRTOS.h" |
igorsk | 0:a405220cf420 | 43 | |
igorsk | 0:a405220cf420 | 44 | using namespace OS; |
igorsk | 0:a405220cf420 | 45 | //------------------------------------------------------------------------------ |
igorsk | 0:a405220cf420 | 46 | OS::TKernel OS::Kernel; |
igorsk | 0:a405220cf420 | 47 | |
igorsk | 0:a405220cf420 | 48 | //------------------------------------------------------------------------------ |
igorsk | 0:a405220cf420 | 49 | #if scmRTOS_CONTEXT_SWITCH_SCHEME == 0 |
igorsk | 0:a405220cf420 | 50 | void TKernel::Sched() |
igorsk | 0:a405220cf420 | 51 | { |
igorsk | 0:a405220cf420 | 52 | byte NextPrty = GetHighPriority(ReadyProcessMap); |
igorsk | 0:a405220cf420 | 53 | if(NextPrty != CurProcPriority) |
igorsk | 0:a405220cf420 | 54 | { |
igorsk | 0:a405220cf420 | 55 | TStackItem* Next_SP = ProcessTable[NextPrty]->StackPointer; |
igorsk | 0:a405220cf420 | 56 | TStackItem** Curr_SP_addr = &(ProcessTable[CurProcPriority]->StackPointer); |
igorsk | 0:a405220cf420 | 57 | CurProcPriority = NextPrty; |
igorsk | 0:a405220cf420 | 58 | OS_ContextSwitcher(Curr_SP_addr, Next_SP); |
igorsk | 0:a405220cf420 | 59 | } |
igorsk | 0:a405220cf420 | 60 | } |
igorsk | 0:a405220cf420 | 61 | #else |
igorsk | 0:a405220cf420 | 62 | //------------------------------------------------------------------------------ |
igorsk | 0:a405220cf420 | 63 | void TKernel::Sched() |
igorsk | 0:a405220cf420 | 64 | { |
igorsk | 0:a405220cf420 | 65 | byte NextPrty = GetHighPriority(ReadyProcessMap); |
igorsk | 0:a405220cf420 | 66 | if(NextPrty != CurProcPriority) |
igorsk | 0:a405220cf420 | 67 | { |
igorsk | 0:a405220cf420 | 68 | SchedProcPriority = NextPrty; |
igorsk | 0:a405220cf420 | 69 | |
igorsk | 0:a405220cf420 | 70 | RaiseContextSwitch(); |
igorsk | 0:a405220cf420 | 71 | do |
igorsk | 0:a405220cf420 | 72 | { |
igorsk | 0:a405220cf420 | 73 | EnableContextSwitch(); |
igorsk | 0:a405220cf420 | 74 | DUMMY_INSTR(); |
igorsk | 0:a405220cf420 | 75 | DisableContextSwitch(); |
igorsk | 0:a405220cf420 | 76 | } |
igorsk | 0:a405220cf420 | 77 | while(!IsContextSwitchDone()); |
igorsk | 0:a405220cf420 | 78 | } |
igorsk | 0:a405220cf420 | 79 | } |
igorsk | 0:a405220cf420 | 80 | //------------------------------------------------------------------------------ |
igorsk | 0:a405220cf420 | 81 | TStackItem* OS_ContextSwitchHook(TStackItem* sp) { return OS::Kernel.ContextSwitchHook(sp); } |
igorsk | 0:a405220cf420 | 82 | //------------------------------------------------------------------------------ |
igorsk | 0:a405220cf420 | 83 | #endif // scmRTOS_CONTEXT_SWITCH_SCHEME |
igorsk | 0:a405220cf420 | 84 | //------------------------------------------------------------------------------ |
igorsk | 0:a405220cf420 | 85 | void TBaseProcess::Sleep(TTimeout timeout) |
igorsk | 0:a405220cf420 | 86 | { |
igorsk | 0:a405220cf420 | 87 | TCritSect cs; |
igorsk | 0:a405220cf420 | 88 | |
igorsk | 0:a405220cf420 | 89 | Kernel.ProcessTable[Kernel.CurProcPriority]->Timeout = timeout; |
igorsk | 0:a405220cf420 | 90 | Kernel.SetProcessUnready(Kernel.CurProcPriority); |
igorsk | 0:a405220cf420 | 91 | Kernel.Scheduler(); |
igorsk | 0:a405220cf420 | 92 | } |
igorsk | 0:a405220cf420 | 93 | //------------------------------------------------------------------------------ |
igorsk | 0:a405220cf420 | 94 | void OS::WakeUpProcess(TBaseProcess& p) |
igorsk | 0:a405220cf420 | 95 | { |
igorsk | 0:a405220cf420 | 96 | TCritSect cs; |
igorsk | 0:a405220cf420 | 97 | |
igorsk | 0:a405220cf420 | 98 | if(p.Timeout) |
igorsk | 0:a405220cf420 | 99 | { |
igorsk | 0:a405220cf420 | 100 | p.Timeout = 0; |
igorsk | 0:a405220cf420 | 101 | Kernel.SetProcessReady(p.Priority); |
igorsk | 0:a405220cf420 | 102 | Kernel.Scheduler(); |
igorsk | 0:a405220cf420 | 103 | } |
igorsk | 0:a405220cf420 | 104 | } |
igorsk | 0:a405220cf420 | 105 | //------------------------------------------------------------------------------ |
igorsk | 0:a405220cf420 | 106 | void OS::ForceWakeUpProcess(TBaseProcess& p) |
igorsk | 0:a405220cf420 | 107 | { |
igorsk | 0:a405220cf420 | 108 | TCritSect cs; |
igorsk | 0:a405220cf420 | 109 | |
igorsk | 0:a405220cf420 | 110 | p.Timeout = 0; |
igorsk | 0:a405220cf420 | 111 | Kernel.SetProcessReady(p.Priority); |
igorsk | 0:a405220cf420 | 112 | Kernel.Scheduler(); |
igorsk | 0:a405220cf420 | 113 | } |
igorsk | 0:a405220cf420 | 114 | //------------------------------------------------------------------------------ |
igorsk | 0:a405220cf420 | 115 |