Dependencies:   keypad SDHCFileSystem mbed FPointer wave_player

Committer:
daryl2110
Date:
Mon Feb 20 07:36:06 2012 +0000
Revision:
0:879af6e11219

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daryl2110 0:879af6e11219 1 //******************************************************************************
daryl2110 0:879af6e11219 2 //*
daryl2110 0:879af6e11219 3 //* FULLNAME: Single-Chip Microcontroller Real-Time Operating System
daryl2110 0:879af6e11219 4 //*
daryl2110 0:879af6e11219 5 //* NICKNAME: scmRTOS
daryl2110 0:879af6e11219 6 //*
daryl2110 0:879af6e11219 7 //* PURPOSE: OS Services Header. Declarations And Definitions
daryl2110 0:879af6e11219 8 //*
daryl2110 0:879af6e11219 9 //* Version: 3.10
daryl2110 0:879af6e11219 10 //*
daryl2110 0:879af6e11219 11 //* $Revision: 256 $
daryl2110 0:879af6e11219 12 //* $Date:: 2010-01-22 #$
daryl2110 0:879af6e11219 13 //*
daryl2110 0:879af6e11219 14 //* Copyright (c) 2003-2010, Harry E. Zhurov
daryl2110 0:879af6e11219 15 //*
daryl2110 0:879af6e11219 16 //* Permission is hereby granted, free of charge, to any person
daryl2110 0:879af6e11219 17 //* obtaining a copy of this software and associated documentation
daryl2110 0:879af6e11219 18 //* files (the "Software"), to deal in the Software without restriction,
daryl2110 0:879af6e11219 19 //* including without limitation the rights to use, copy, modify, merge,
daryl2110 0:879af6e11219 20 //* publish, distribute, sublicense, and/or sell copies of the Software,
daryl2110 0:879af6e11219 21 //* and to permit persons to whom the Software is furnished to do so,
daryl2110 0:879af6e11219 22 //* subject to the following conditions:
daryl2110 0:879af6e11219 23 //*
daryl2110 0:879af6e11219 24 //* The above copyright notice and this permission notice shall be included
daryl2110 0:879af6e11219 25 //* in all copies or substantial portions of the Software.
daryl2110 0:879af6e11219 26 //*
daryl2110 0:879af6e11219 27 //* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
daryl2110 0:879af6e11219 28 //* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
daryl2110 0:879af6e11219 29 //* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
daryl2110 0:879af6e11219 30 //* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
daryl2110 0:879af6e11219 31 //* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
daryl2110 0:879af6e11219 32 //* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
daryl2110 0:879af6e11219 33 //* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
daryl2110 0:879af6e11219 34 //*
daryl2110 0:879af6e11219 35 //* =================================================================
daryl2110 0:879af6e11219 36 //* See http://scmrtos.sourceforge.net for documentation, latest
daryl2110 0:879af6e11219 37 //* information, license and contact details.
daryl2110 0:879af6e11219 38 //* =================================================================
daryl2110 0:879af6e11219 39 //*
daryl2110 0:879af6e11219 40 //******************************************************************************
daryl2110 0:879af6e11219 41
daryl2110 0:879af6e11219 42 #ifndef OS_SERVICES_H
daryl2110 0:879af6e11219 43 #define OS_SERVICES_H
daryl2110 0:879af6e11219 44
daryl2110 0:879af6e11219 45 namespace OS
daryl2110 0:879af6e11219 46 {
daryl2110 0:879af6e11219 47 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 48 //
daryl2110 0:879af6e11219 49 // NAME : Mutex
daryl2110 0:879af6e11219 50 //
daryl2110 0:879af6e11219 51 /// Binary semaphore for support of mutual exclusion
daryl2110 0:879af6e11219 52 //
daryl2110 0:879af6e11219 53 // DESCRIPTION:
daryl2110 0:879af6e11219 54 //
daryl2110 0:879af6e11219 55 //
daryl2110 0:879af6e11219 56 class TMutex
daryl2110 0:879af6e11219 57 {
daryl2110 0:879af6e11219 58 public:
daryl2110 0:879af6e11219 59 INLINE TMutex() : ProcessMap(0), ValueTag(0) { }
daryl2110 0:879af6e11219 60 void Lock();
daryl2110 0:879af6e11219 61 void Unlock();
daryl2110 0:879af6e11219 62 void UnlockISR();
daryl2110 0:879af6e11219 63
daryl2110 0:879af6e11219 64 INLINE bool LockSoftly() { TCritSect cs; if(ValueTag) return false; else Lock(); return true; }
daryl2110 0:879af6e11219 65 INLINE bool IsLocked() const { TCritSect cs; if(ValueTag) return true; else return false; }
daryl2110 0:879af6e11219 66
daryl2110 0:879af6e11219 67 private:
daryl2110 0:879af6e11219 68 TProcessMap ProcessMap;
daryl2110 0:879af6e11219 69 TProcessMap ValueTag;
daryl2110 0:879af6e11219 70
daryl2110 0:879af6e11219 71 };
daryl2110 0:879af6e11219 72 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 73
daryl2110 0:879af6e11219 74 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 75 //
daryl2110 0:879af6e11219 76 /// Event Flag
daryl2110 0:879af6e11219 77 ///
daryl2110 0:879af6e11219 78 /// Intended for processes synchronization and
daryl2110 0:879af6e11219 79 /// event notification one (or more) process by another
daryl2110 0:879af6e11219 80 //
daryl2110 0:879af6e11219 81 // DESCRIPTION:
daryl2110 0:879af6e11219 82 //
daryl2110 0:879af6e11219 83 //
daryl2110 0:879af6e11219 84 class TEventFlag
daryl2110 0:879af6e11219 85 {
daryl2110 0:879af6e11219 86 public:
daryl2110 0:879af6e11219 87 enum TValue { efOn = 1, efOff= 0 }; // prefix 'ef' means: "Event Flag"
daryl2110 0:879af6e11219 88
daryl2110 0:879af6e11219 89 public:
daryl2110 0:879af6e11219 90 INLINE TEventFlag(TValue init_val = efOff) : ProcessMap(0), Value(init_val) { }
daryl2110 0:879af6e11219 91
daryl2110 0:879af6e11219 92 bool Wait(TTimeout timeout = 0);
daryl2110 0:879af6e11219 93 void Signal();
daryl2110 0:879af6e11219 94 INLINE void Clear() { TCritSect cs; Value = efOff; }
daryl2110 0:879af6e11219 95 INLINE inline void SignalISR();
daryl2110 0:879af6e11219 96 INLINE bool IsSignaled() { TCritSect cs; if(Value == efOn) return true; else return false; }
daryl2110 0:879af6e11219 97
daryl2110 0:879af6e11219 98 private:
daryl2110 0:879af6e11219 99 TProcessMap ProcessMap;
daryl2110 0:879af6e11219 100 TValue Value;
daryl2110 0:879af6e11219 101 };
daryl2110 0:879af6e11219 102 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 103
daryl2110 0:879af6e11219 104 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 105 //
daryl2110 0:879af6e11219 106 /// TChannel
daryl2110 0:879af6e11219 107 ///
daryl2110 0:879af6e11219 108 /// Byte-wide data channel for transferring "raw" data
daryl2110 0:879af6e11219 109 //
daryl2110 0:879af6e11219 110 // DESCRIPTION:
daryl2110 0:879af6e11219 111 //
daryl2110 0:879af6e11219 112 //
daryl2110 0:879af6e11219 113 class TChannel
daryl2110 0:879af6e11219 114 {
daryl2110 0:879af6e11219 115 public:
daryl2110 0:879af6e11219 116 INLINE TChannel(byte* buf, byte size) : Cbuf(buf, size) { }
daryl2110 0:879af6e11219 117 void Push(byte x);
daryl2110 0:879af6e11219 118 byte Pop();
daryl2110 0:879af6e11219 119 void Write(const byte* data, const byte count);
daryl2110 0:879af6e11219 120 void Read(byte* const data, const byte count);
daryl2110 0:879af6e11219 121
daryl2110 0:879af6e11219 122 INLINE byte GetCount() const { TCritSect cs; return Cbuf.get_count(); }
daryl2110 0:879af6e11219 123
daryl2110 0:879af6e11219 124 private:
daryl2110 0:879af6e11219 125 TProcessMap ProducersProcessMap;
daryl2110 0:879af6e11219 126 TProcessMap ConsumersProcessMap;
daryl2110 0:879af6e11219 127 usr::TCbuf Cbuf;
daryl2110 0:879af6e11219 128
daryl2110 0:879af6e11219 129 private:
daryl2110 0:879af6e11219 130 void CheckWaiters(TProcessMap& pm);
daryl2110 0:879af6e11219 131 };
daryl2110 0:879af6e11219 132 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 133
daryl2110 0:879af6e11219 134
daryl2110 0:879af6e11219 135 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 136 //
daryl2110 0:879af6e11219 137 // NAME : channel
daryl2110 0:879af6e11219 138 //
daryl2110 0:879af6e11219 139 // PURPOSE : Data channel for transferring data
daryl2110 0:879af6e11219 140 // objects of arbitrary type
daryl2110 0:879af6e11219 141 //
daryl2110 0:879af6e11219 142 // DESCRIPTION:
daryl2110 0:879af6e11219 143 //
daryl2110 0:879af6e11219 144 //
daryl2110 0:879af6e11219 145 template<typename T, word Size, typename S = byte>
daryl2110 0:879af6e11219 146 /// channel
daryl2110 0:879af6e11219 147 ///
daryl2110 0:879af6e11219 148 /// Data channel for transferring data objects of arbitrary type
daryl2110 0:879af6e11219 149 class channel
daryl2110 0:879af6e11219 150 {
daryl2110 0:879af6e11219 151 public:
daryl2110 0:879af6e11219 152 INLINE channel() : ProducersProcessMap(0)
daryl2110 0:879af6e11219 153 , ConsumersProcessMap(0)
daryl2110 0:879af6e11219 154 , pool()
daryl2110 0:879af6e11219 155 {
daryl2110 0:879af6e11219 156 }
daryl2110 0:879af6e11219 157
daryl2110 0:879af6e11219 158 //----------------------------------------------------------------
daryl2110 0:879af6e11219 159 //
daryl2110 0:879af6e11219 160 // Data transfer functions
daryl2110 0:879af6e11219 161 //
daryl2110 0:879af6e11219 162 void write(const T* data, const S cnt);
daryl2110 0:879af6e11219 163 bool read (T* const data, const S cnt, TTimeout timeout = 0);
daryl2110 0:879af6e11219 164
daryl2110 0:879af6e11219 165 void push (const T& item);
daryl2110 0:879af6e11219 166 void push_front(const T& item);
daryl2110 0:879af6e11219 167
daryl2110 0:879af6e11219 168 bool pop (T& item, TTimeout timeout = 0);
daryl2110 0:879af6e11219 169 bool pop_back(T& item, TTimeout timeout = 0);
daryl2110 0:879af6e11219 170
daryl2110 0:879af6e11219 171
daryl2110 0:879af6e11219 172 //----------------------------------------------------------------
daryl2110 0:879af6e11219 173 //
daryl2110 0:879af6e11219 174 // Service functions
daryl2110 0:879af6e11219 175 //
daryl2110 0:879af6e11219 176 INLINE S get_count() const { TCritSect cs; return pool.get_count(); }
daryl2110 0:879af6e11219 177 INLINE S get_free_size() const { TCritSect cs; return pool.get_free_size(); }
daryl2110 0:879af6e11219 178 void flush();
daryl2110 0:879af6e11219 179 //const T& operator[](const S index) { TCritSect cs; return pool[index]; }
daryl2110 0:879af6e11219 180
daryl2110 0:879af6e11219 181
daryl2110 0:879af6e11219 182 private:
daryl2110 0:879af6e11219 183 TProcessMap ProducersProcessMap;
daryl2110 0:879af6e11219 184 TProcessMap ConsumersProcessMap;
daryl2110 0:879af6e11219 185 usr::ring_buffer<T, Size, S> pool;
daryl2110 0:879af6e11219 186
daryl2110 0:879af6e11219 187 private:
daryl2110 0:879af6e11219 188 void CheckWaiters(TProcessMap& pm);
daryl2110 0:879af6e11219 189 };
daryl2110 0:879af6e11219 190
daryl2110 0:879af6e11219 191 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 192
daryl2110 0:879af6e11219 193 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 194 //
daryl2110 0:879af6e11219 195 /// message
daryl2110 0:879af6e11219 196 ///
daryl2110 0:879af6e11219 197 /// Template for messages
daryl2110 0:879af6e11219 198 //
daryl2110 0:879af6e11219 199 // DESCRIPTION:
daryl2110 0:879af6e11219 200 //
daryl2110 0:879af6e11219 201 //
daryl2110 0:879af6e11219 202 class TBaseMessage
daryl2110 0:879af6e11219 203 {
daryl2110 0:879af6e11219 204 public:
daryl2110 0:879af6e11219 205 INLINE TBaseMessage() : ProcessMap(0), NonEmpty(false) { }
daryl2110 0:879af6e11219 206
daryl2110 0:879af6e11219 207 bool wait (TTimeout timeout = 0);
daryl2110 0:879af6e11219 208 void send();
daryl2110 0:879af6e11219 209 INLINE inline void sendISR();
daryl2110 0:879af6e11219 210 INLINE bool is_non_empty() const { TCritSect cs; return NonEmpty; }
daryl2110 0:879af6e11219 211 INLINE void reset () { TCritSect cs; NonEmpty = false; }
daryl2110 0:879af6e11219 212
daryl2110 0:879af6e11219 213 private:
daryl2110 0:879af6e11219 214 TProcessMap ProcessMap;
daryl2110 0:879af6e11219 215 bool NonEmpty;
daryl2110 0:879af6e11219 216 };
daryl2110 0:879af6e11219 217 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 218 template<typename T>
daryl2110 0:879af6e11219 219 class message : public TBaseMessage
daryl2110 0:879af6e11219 220 {
daryl2110 0:879af6e11219 221 public:
daryl2110 0:879af6e11219 222 INLINE message() : TBaseMessage() { }
daryl2110 0:879af6e11219 223 INLINE const T& operator=(const T& msg) { TCritSect cs; Msg = msg; return Msg; }
daryl2110 0:879af6e11219 224 INLINE operator T() const { TCritSect cs; return Msg; }
daryl2110 0:879af6e11219 225
daryl2110 0:879af6e11219 226 private:
daryl2110 0:879af6e11219 227 T Msg;
daryl2110 0:879af6e11219 228 };
daryl2110 0:879af6e11219 229 //--------------------------------------------------------------------------
daryl2110 0:879af6e11219 230 }
daryl2110 0:879af6e11219 231 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 232 //
daryl2110 0:879af6e11219 233 // Function-members implementation
daryl2110 0:879af6e11219 234 //
daryl2110 0:879af6e11219 235 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 236 void OS::TEventFlag::SignalISR()
daryl2110 0:879af6e11219 237 {
daryl2110 0:879af6e11219 238 TCritSect cs;
daryl2110 0:879af6e11219 239 if(ProcessMap) // if any process waits for event
daryl2110 0:879af6e11219 240 {
daryl2110 0:879af6e11219 241 TProcessMap Timeouted = Kernel.ReadyProcessMap; // Process has its tag set in ReadyProcessMap if timeout
daryl2110 0:879af6e11219 242 // expired, or it was waked up by OS::ForceWakeUpProcess()
daryl2110 0:879af6e11219 243 if( ProcessMap & ~Timeouted ) // if any process has to be waked up
daryl2110 0:879af6e11219 244 {
daryl2110 0:879af6e11219 245 SetPrioTag(Kernel.ReadyProcessMap, ProcessMap); // place all waiting processes to the ready map
daryl2110 0:879af6e11219 246 ClrPrioTag(ProcessMap, ~Timeouted); // remove all non-timeouted processes from the waiting map.
daryl2110 0:879af6e11219 247 return;
daryl2110 0:879af6e11219 248 }
daryl2110 0:879af6e11219 249 }
daryl2110 0:879af6e11219 250 Value = efOn;
daryl2110 0:879af6e11219 251 }
daryl2110 0:879af6e11219 252 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 253 template<typename T, word Size, typename S>
daryl2110 0:879af6e11219 254 void OS::channel<T, Size, S>::CheckWaiters(TProcessMap& pm)
daryl2110 0:879af6e11219 255 {
daryl2110 0:879af6e11219 256 if(pm)
daryl2110 0:879af6e11219 257 {
daryl2110 0:879af6e11219 258 TProcessMap Timeouted = Kernel.ReadyProcessMap;
daryl2110 0:879af6e11219 259
daryl2110 0:879af6e11219 260 SetPrioTag(Kernel.ReadyProcessMap, pm); // place all waiting processes to the ready map
daryl2110 0:879af6e11219 261 ClrPrioTag(pm, ~Timeouted); // remove waiting processes from the wait map
daryl2110 0:879af6e11219 262 Kernel.Scheduler();
daryl2110 0:879af6e11219 263 }
daryl2110 0:879af6e11219 264 }
daryl2110 0:879af6e11219 265 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 266 template<typename T, word Size, typename S>
daryl2110 0:879af6e11219 267 void OS::channel<T, Size, S>::push(const T& item)
daryl2110 0:879af6e11219 268 {
daryl2110 0:879af6e11219 269 TCritSect cs;
daryl2110 0:879af6e11219 270
daryl2110 0:879af6e11219 271 while(!pool.get_free_size())
daryl2110 0:879af6e11219 272 {
daryl2110 0:879af6e11219 273 TProcessMap PrioTag = GetPrioTag(Kernel.CurProcPriority);
daryl2110 0:879af6e11219 274 SetPrioTag(ProducersProcessMap, PrioTag); // channel is full, put current process to the wait map
daryl2110 0:879af6e11219 275 ClrPrioTag(Kernel.ReadyProcessMap, PrioTag); // remove current process from the ready map
daryl2110 0:879af6e11219 276 Kernel.Scheduler();
daryl2110 0:879af6e11219 277 }
daryl2110 0:879af6e11219 278
daryl2110 0:879af6e11219 279 pool.push_back(item);
daryl2110 0:879af6e11219 280 CheckWaiters(ConsumersProcessMap);
daryl2110 0:879af6e11219 281 }
daryl2110 0:879af6e11219 282 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 283 template<typename T, word Size, typename S>
daryl2110 0:879af6e11219 284 void OS::channel<T, Size, S>::push_front(const T& item)
daryl2110 0:879af6e11219 285 {
daryl2110 0:879af6e11219 286 TCritSect cs;
daryl2110 0:879af6e11219 287
daryl2110 0:879af6e11219 288 while(!pool.get_free_size())
daryl2110 0:879af6e11219 289 {
daryl2110 0:879af6e11219 290 TProcessMap PrioTag = GetPrioTag(Kernel.CurProcPriority);
daryl2110 0:879af6e11219 291 SetPrioTag(ProducersProcessMap, PrioTag); // channel is full, put current process to the wait map
daryl2110 0:879af6e11219 292 ClrPrioTag(Kernel.ReadyProcessMap, PrioTag); // remove current process from the ready map
daryl2110 0:879af6e11219 293 Kernel.Scheduler();
daryl2110 0:879af6e11219 294 }
daryl2110 0:879af6e11219 295
daryl2110 0:879af6e11219 296 pool.push_front(item);
daryl2110 0:879af6e11219 297 CheckWaiters(ConsumersProcessMap);
daryl2110 0:879af6e11219 298 }
daryl2110 0:879af6e11219 299 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 300 template<typename T, word Size, typename S>
daryl2110 0:879af6e11219 301 bool OS::channel<T, Size, S>::pop(T& item, TTimeout timeout)
daryl2110 0:879af6e11219 302 {
daryl2110 0:879af6e11219 303 TCritSect cs;
daryl2110 0:879af6e11219 304
daryl2110 0:879af6e11219 305 if(pool.get_count())
daryl2110 0:879af6e11219 306 {
daryl2110 0:879af6e11219 307 item = pool.pop();
daryl2110 0:879af6e11219 308 CheckWaiters(ProducersProcessMap);
daryl2110 0:879af6e11219 309 return true;
daryl2110 0:879af6e11219 310 }
daryl2110 0:879af6e11219 311 else
daryl2110 0:879af6e11219 312 {
daryl2110 0:879af6e11219 313 TBaseProcess* p = Kernel.ProcessTable[Kernel.CurProcPriority];
daryl2110 0:879af6e11219 314 p->Timeout = timeout;
daryl2110 0:879af6e11219 315
daryl2110 0:879af6e11219 316 TProcessMap PrioTag = GetPrioTag(Kernel.CurProcPriority);
daryl2110 0:879af6e11219 317 for(;;)
daryl2110 0:879af6e11219 318 {
daryl2110 0:879af6e11219 319 SetPrioTag(ConsumersProcessMap, PrioTag); // channel is empty, put current process to the wait map
daryl2110 0:879af6e11219 320 ClrPrioTag(Kernel.ReadyProcessMap, PrioTag); // remove current process from the ready map
daryl2110 0:879af6e11219 321 Kernel.Scheduler();
daryl2110 0:879af6e11219 322
daryl2110 0:879af6e11219 323 if(pool.get_count())
daryl2110 0:879af6e11219 324 {
daryl2110 0:879af6e11219 325 p->Timeout = 0;
daryl2110 0:879af6e11219 326 item = pool.pop();
daryl2110 0:879af6e11219 327 CheckWaiters(ProducersProcessMap);
daryl2110 0:879af6e11219 328 return true;
daryl2110 0:879af6e11219 329 }
daryl2110 0:879af6e11219 330
daryl2110 0:879af6e11219 331 if(ConsumersProcessMap & PrioTag) // waked up by timer when timeout expired
daryl2110 0:879af6e11219 332 { // or by OS::ForceWakeUpProcess()
daryl2110 0:879af6e11219 333
daryl2110 0:879af6e11219 334 p->Timeout = 0; // non-zero if waked up by ForceWakeUpProcess()
daryl2110 0:879af6e11219 335 ClrPrioTag(ConsumersProcessMap, PrioTag); // remove current process from the wait map
daryl2110 0:879af6e11219 336 return false;
daryl2110 0:879af6e11219 337 }
daryl2110 0:879af6e11219 338 }
daryl2110 0:879af6e11219 339 }
daryl2110 0:879af6e11219 340 }
daryl2110 0:879af6e11219 341 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 342 template<typename T, word Size, typename S>
daryl2110 0:879af6e11219 343 bool OS::channel<T, Size, S>::pop_back(T& item, TTimeout timeout)
daryl2110 0:879af6e11219 344 {
daryl2110 0:879af6e11219 345 TCritSect cs;
daryl2110 0:879af6e11219 346
daryl2110 0:879af6e11219 347 if(pool.get_count())
daryl2110 0:879af6e11219 348 {
daryl2110 0:879af6e11219 349 item = pool.pop_back();
daryl2110 0:879af6e11219 350 CheckWaiters(ProducersProcessMap);
daryl2110 0:879af6e11219 351 return true;
daryl2110 0:879af6e11219 352 }
daryl2110 0:879af6e11219 353 else
daryl2110 0:879af6e11219 354 {
daryl2110 0:879af6e11219 355 TBaseProcess* p = Kernel.ProcessTable[Kernel.CurProcPriority];
daryl2110 0:879af6e11219 356 p->Timeout = timeout;
daryl2110 0:879af6e11219 357
daryl2110 0:879af6e11219 358 TProcessMap PrioTag = GetPrioTag(Kernel.CurProcPriority);
daryl2110 0:879af6e11219 359 for(;;)
daryl2110 0:879af6e11219 360 {
daryl2110 0:879af6e11219 361 SetPrioTag(ConsumersProcessMap, PrioTag); // channel is empty, put current process to the wait map
daryl2110 0:879af6e11219 362 ClrPrioTag(Kernel.ReadyProcessMap, PrioTag); // remove current process from the ready map
daryl2110 0:879af6e11219 363 Kernel.Scheduler();
daryl2110 0:879af6e11219 364
daryl2110 0:879af6e11219 365 if(pool.get_count())
daryl2110 0:879af6e11219 366 {
daryl2110 0:879af6e11219 367 p->Timeout = 0;
daryl2110 0:879af6e11219 368 item = pool.pop_back();
daryl2110 0:879af6e11219 369 CheckWaiters(ProducersProcessMap);
daryl2110 0:879af6e11219 370 return true;
daryl2110 0:879af6e11219 371 }
daryl2110 0:879af6e11219 372
daryl2110 0:879af6e11219 373 if(ConsumersProcessMap & PrioTag) // waked up by timer when timeout expired
daryl2110 0:879af6e11219 374 { // or by OS::ForceWakeUpProcess()
daryl2110 0:879af6e11219 375
daryl2110 0:879af6e11219 376 p->Timeout = 0; // non-zero if waked up by ForceWakeUpProcess()
daryl2110 0:879af6e11219 377 ClrPrioTag(ConsumersProcessMap, PrioTag); // remove current process from the wait map
daryl2110 0:879af6e11219 378 return false;
daryl2110 0:879af6e11219 379 }
daryl2110 0:879af6e11219 380 }
daryl2110 0:879af6e11219 381 }
daryl2110 0:879af6e11219 382 }
daryl2110 0:879af6e11219 383 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 384 template<typename T, word Size, typename S>
daryl2110 0:879af6e11219 385 void OS::channel<T, Size, S>::flush()
daryl2110 0:879af6e11219 386 {
daryl2110 0:879af6e11219 387 TCritSect cs;
daryl2110 0:879af6e11219 388 pool.flush();
daryl2110 0:879af6e11219 389 CheckWaiters(ProducersProcessMap);
daryl2110 0:879af6e11219 390 }
daryl2110 0:879af6e11219 391 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 392 template<typename T, word Size, typename S>
daryl2110 0:879af6e11219 393 void OS::channel<T, Size, S>::write(const T* data, const S count)
daryl2110 0:879af6e11219 394 {
daryl2110 0:879af6e11219 395 TCritSect cs;
daryl2110 0:879af6e11219 396
daryl2110 0:879af6e11219 397 while(pool.get_free_size() < count)
daryl2110 0:879af6e11219 398 {
daryl2110 0:879af6e11219 399 TProcessMap PrioTag = GetPrioTag(Kernel.CurProcPriority);
daryl2110 0:879af6e11219 400 SetPrioTag(ProducersProcessMap, PrioTag); // channel does not have enough space, put current process to the wait map
daryl2110 0:879af6e11219 401 ClrPrioTag(Kernel.ReadyProcessMap, PrioTag); // remove current process from the ready map
daryl2110 0:879af6e11219 402 Kernel.Scheduler();
daryl2110 0:879af6e11219 403 }
daryl2110 0:879af6e11219 404
daryl2110 0:879af6e11219 405 pool.write(data, count);
daryl2110 0:879af6e11219 406 CheckWaiters(ConsumersProcessMap);
daryl2110 0:879af6e11219 407 }
daryl2110 0:879af6e11219 408 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 409 template<typename T, word Size, typename S>
daryl2110 0:879af6e11219 410 bool OS::channel<T, Size, S>::read(T* const data, const S count, TTimeout timeout)
daryl2110 0:879af6e11219 411 {
daryl2110 0:879af6e11219 412 TCritSect cs;
daryl2110 0:879af6e11219 413
daryl2110 0:879af6e11219 414 TBaseProcess* p = Kernel.ProcessTable[Kernel.CurProcPriority];
daryl2110 0:879af6e11219 415 p->Timeout = timeout;
daryl2110 0:879af6e11219 416
daryl2110 0:879af6e11219 417 TProcessMap PrioTag = GetPrioTag(Kernel.CurProcPriority);
daryl2110 0:879af6e11219 418 while(pool.get_count() < count)
daryl2110 0:879af6e11219 419 {
daryl2110 0:879af6e11219 420 SetPrioTag(ConsumersProcessMap, PrioTag); // channel doesn't contain enough data, put current process to the wait map
daryl2110 0:879af6e11219 421 ClrPrioTag(Kernel.ReadyProcessMap, PrioTag); // remove current process from the ready map
daryl2110 0:879af6e11219 422 Kernel.Scheduler();
daryl2110 0:879af6e11219 423
daryl2110 0:879af6e11219 424 if(ConsumersProcessMap & PrioTag) // waked up by timer when timeout expired
daryl2110 0:879af6e11219 425 { // or by OS::ForceWakeUpProcess()
daryl2110 0:879af6e11219 426
daryl2110 0:879af6e11219 427 p->Timeout = 0; // non-zero if waked up by ForceWakeUpProcess()
daryl2110 0:879af6e11219 428 ClrPrioTag(ConsumersProcessMap, PrioTag); // remove current process from the wait map
daryl2110 0:879af6e11219 429 return false;
daryl2110 0:879af6e11219 430 }
daryl2110 0:879af6e11219 431 }
daryl2110 0:879af6e11219 432
daryl2110 0:879af6e11219 433 p->Timeout = 0;
daryl2110 0:879af6e11219 434 pool.read(data, count);
daryl2110 0:879af6e11219 435 CheckWaiters(ProducersProcessMap);
daryl2110 0:879af6e11219 436
daryl2110 0:879af6e11219 437 return true;
daryl2110 0:879af6e11219 438 }
daryl2110 0:879af6e11219 439 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 440
daryl2110 0:879af6e11219 441 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 442 //
daryl2110 0:879af6e11219 443 // OS::message template
daryl2110 0:879af6e11219 444 //
daryl2110 0:879af6e11219 445 // Function-members implementation
daryl2110 0:879af6e11219 446 //
daryl2110 0:879af6e11219 447 //
daryl2110 0:879af6e11219 448 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 449 void OS::TBaseMessage::sendISR()
daryl2110 0:879af6e11219 450 {
daryl2110 0:879af6e11219 451 TCritSect cs;
daryl2110 0:879af6e11219 452
daryl2110 0:879af6e11219 453 if(ProcessMap)
daryl2110 0:879af6e11219 454 {
daryl2110 0:879af6e11219 455 TProcessMap Timeouted = OS::Kernel.ReadyProcessMap; // Process has it's tag set in ReadyProcessMap if timeout
daryl2110 0:879af6e11219 456 // expired, or it was waked up by OS::ForceWakeUpProcess()
daryl2110 0:879af6e11219 457 if( ProcessMap & ~Timeouted ) // if any process has to be waked up
daryl2110 0:879af6e11219 458 {
daryl2110 0:879af6e11219 459 SetPrioTag(Kernel.ReadyProcessMap, ProcessMap); // place all waiting processes to the ready map
daryl2110 0:879af6e11219 460 ClrPrioTag(ProcessMap, ~Timeouted); // remove all non-timeouted processes from the waiting map.
daryl2110 0:879af6e11219 461 return;
daryl2110 0:879af6e11219 462 }
daryl2110 0:879af6e11219 463 }
daryl2110 0:879af6e11219 464 NonEmpty = true;
daryl2110 0:879af6e11219 465 }
daryl2110 0:879af6e11219 466 //------------------------------------------------------------------------------
daryl2110 0:879af6e11219 467 #endif // OS_SERVICES_H