Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OS_Kernel.cpp Source File

OS_Kernel.cpp

00001 //******************************************************************************
00002 //*
00003 //*     FULLNAME:  Single-Chip Microcontroller Real-Time Operating System
00004 //*
00005 //*     NICKNAME:  scmRTOS
00006 //*
00007 //*     PURPOSE:  OS Kernel Source
00008 //*
00009 //*     Version: 3.10
00010 //*
00011 //*     $Revision: 256 $
00012 //*     $Date:: 2010-01-22 #$
00013 //*
00014 //*     Copyright (c) 2003-2010, Harry E. Zhurov
00015 //*
00016 //*     Permission is hereby granted, free of charge, to any person 
00017 //*     obtaining  a copy of this software and associated documentation 
00018 //*     files (the "Software"), to deal in the Software without restriction, 
00019 //*     including without limitation the rights to use, copy, modify, merge, 
00020 //*     publish, distribute, sublicense, and/or sell copies of the Software, 
00021 //*     and to permit persons to whom the Software is furnished to do so, 
00022 //*     subject to the following conditions:
00023 //*
00024 //*     The above copyright notice and this permission notice shall be included 
00025 //*     in all copies or substantial portions of the Software.
00026 //*
00027 //*     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
00028 //*     EXPRESS  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
00029 //*     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
00030 //*     IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
00031 //*     CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
00032 //*     TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH 
00033 //*     THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00034 //*
00035 //*     =================================================================
00036 //*     See http://scmrtos.sourceforge.net for documentation, latest
00037 //*     information, license and contact details.
00038 //*     =================================================================
00039 //*
00040 //******************************************************************************
00041 
00042 #include "scmRTOS.h"
00043 
00044 using namespace OS;
00045 //------------------------------------------------------------------------------
00046 OS::TKernel OS::Kernel;
00047 
00048 //------------------------------------------------------------------------------
00049 #if scmRTOS_CONTEXT_SWITCH_SCHEME == 0
00050 void TKernel::Sched()
00051 {
00052     byte NextPrty = GetHighPriority(ReadyProcessMap);
00053     if(NextPrty != CurProcPriority)
00054     {
00055         TStackItem*  Next_SP = ProcessTable[NextPrty]->StackPointer;
00056         TStackItem** Curr_SP_addr = &(ProcessTable[CurProcPriority]->StackPointer);
00057         CurProcPriority = NextPrty;
00058         OS_ContextSwitcher(Curr_SP_addr, Next_SP);
00059     }
00060 }
00061 #else
00062 //------------------------------------------------------------------------------
00063 void TKernel::Sched()
00064 {
00065     byte NextPrty = GetHighPriority(ReadyProcessMap);
00066     if(NextPrty != CurProcPriority)
00067     {
00068         SchedProcPriority = NextPrty;
00069     
00070         RaiseContextSwitch();
00071         do
00072         {
00073             EnableContextSwitch();
00074             DUMMY_INSTR();
00075             DisableContextSwitch();
00076         } 
00077         while(!IsContextSwitchDone());
00078     }
00079 }
00080 //------------------------------------------------------------------------------
00081 TStackItem* OS_ContextSwitchHook(TStackItem* sp) { return OS::Kernel.ContextSwitchHook(sp); }
00082 //------------------------------------------------------------------------------
00083 #endif // scmRTOS_CONTEXT_SWITCH_SCHEME
00084 //------------------------------------------------------------------------------
00085 void TBaseProcess::Sleep(TTimeout timeout)
00086 {
00087     TCritSect cs;
00088 
00089     Kernel.ProcessTable[Kernel.CurProcPriority]->Timeout = timeout;
00090     Kernel.SetProcessUnready(Kernel.CurProcPriority);
00091     Kernel.Scheduler();
00092 }
00093 //------------------------------------------------------------------------------
00094 void OS::WakeUpProcess(TBaseProcess& p)
00095 {
00096     TCritSect cs;
00097 
00098     if(p.Timeout)
00099     {
00100         p.Timeout = 0;
00101         Kernel.SetProcessReady(p.Priority);
00102         Kernel.Scheduler();
00103     }
00104 }
00105 //------------------------------------------------------------------------------
00106 void OS::ForceWakeUpProcess(TBaseProcess& p)
00107 {
00108     TCritSect cs;
00109 
00110     p.Timeout = 0;
00111     Kernel.SetProcessReady(p.Priority);
00112     Kernel.Scheduler();
00113 }
00114 //------------------------------------------------------------------------------
00115