📄 os_services.h
字号:
//******************************************************************************
//*
//* FULLNAME: Single-Chip Microcontroller Real-Time Operating System
//*
//* NICKNAME: scmRTOS
//*
//* PURPOSE: OS Services Header. Declarations And Definitions
//*
//* Version: 3.05
//*
//* $Revision: 195 $
//* $Date:: 2008-06-19 #$
//*
//* Copyright (c) 2003-2008, Harry E. Zhurov
//*
//* Permission is hereby granted, free of charge, to any person
//* obtaining a copy of this software and associated documentation
//* files (the "Software"), to deal in the Software without restriction,
//* including without limitation the rights to use, copy, modify, merge,
//* publish, distribute, sublicense, and/or sell copies of the Software,
//* and to permit persons to whom the Software is furnished to do so,
//* subject to the following conditions:
//*
//* The above copyright notice and this permission notice shall be included
//* in all copies or substantial portions of the Software.
//*
//* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
//* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
//* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
//* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
//* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
//* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//*
//* =================================================================
//* See http://scmrtos.sourceforge.net for documentation, latest
//* information, license and contact details.
//* =================================================================
//*
//******************************************************************************
#ifndef OS_SERVICES_H
#define OS_SERVICES_H
namespace OS
{
//--------------------------------------------------------------------------
//
// NAME : Mutex
//
/// Binary semaphore for support of mutual exclusion
//
// DESCRIPTION:
//
//
class TMutex
{
public:
INLINE TMutex() : ProcessMap(0), ValueTag(0) { }
void Lock();
void Unlock();
INLINE bool LockSoftly() { TCritSect cs; if(ValueTag) return false; else Lock(); return true; }
INLINE bool IsLocked() const { TCritSect cs; if(ValueTag) return true; else return false; }
private:
TProcessMap ProcessMap;
TProcessMap ValueTag;
};
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
/// Event Flag
///
/// Intended for processes synchronization and
/// event notification one (or more) process by another
//
// DESCRIPTION:
//
//
class TEventFlag
{
public:
enum TValue { efOn = 1, efOff= 0 }; // prefix 'ef' means: "Event Flag"
public:
INLINE TEventFlag(TValue init_val = efOff) : ProcessMap(0), Value(init_val) { }
bool Wait(TTimeout timeout = 0);
void Signal();
INLINE void Clear() { TCritSect cs; Value = efOff; }
INLINE inline void SignalISR();
INLINE bool IsSignaled() { TCritSect cs; if(Value == efOn) return true; else return false; }
private:
TProcessMap ProcessMap;
TValue Value;
};
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
/// TChannel
///
/// Byte-wide data channel for transferring "raw" data
//
// DESCRIPTION:
//
//
class TChannel
{
public:
INLINE TChannel(byte* buf, byte size) : Cbuf(buf, size) { }
void Push(byte x);
byte Pop();
void Write(const byte* data, const byte count);
void Read(byte* const data, const byte count);
INLINE byte GetCount() const { TCritSect cs; return Cbuf.get_count(); }
private:
TProcessMap ProducersProcessMap;
TProcessMap ConsumersProcessMap;
usr::TCbuf Cbuf;
private:
void CheckWaiters(TProcessMap& pm);
};
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
// NAME : channel
//
// PURPOSE : Data channel for transferring data
// objects of arbitrary type
//
// DESCRIPTION:
//
//
template<typename T, word Size, typename S = byte>
/// channel
///
/// Data channel for transferring data objects of arbitrary type
class channel
{
public:
INLINE channel() : ProducersProcessMap(0)
, ConsumersProcessMap(0)
, pool()
{
}
//----------------------------------------------------------------
//
// Data transfer functions
//
void write(const T* data, const S cnt);
bool read (T* const data, const S cnt, TTimeout timeout = 0);
void push (const T& item);
void push_front(const T& item);
bool pop (T& item, TTimeout timeout = 0);
bool pop_back(T& item, TTimeout timeout = 0);
//----------------------------------------------------------------
//
// Service functions
//
INLINE S get_count() const { TCritSect cs; return pool.get_count(); }
INLINE S get_free_size() const { TCritSect cs; return pool.get_free_size(); }
void flush();
//const T& operator[](const S index) { TCritSect cs; return pool[index]; }
private:
TProcessMap ProducersProcessMap;
TProcessMap ConsumersProcessMap;
usr::ring_buffer<T, Size, S> pool;
private:
void CheckWaiters(TProcessMap& pm);
};
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
/// message
///
/// Template for messages
//
// DESCRIPTION:
//
//
class TBaseMessage
{
public:
INLINE TBaseMessage() : ProcessMap(0), NonEmpty(false) { }
bool wait (TTimeout timeout = 0);
void send();
INLINE inline void sendISR();
INLINE bool is_non_empty() const { TCritSect cs; return NonEmpty; }
INLINE void reset () { TCritSect cs; NonEmpty = false; }
private:
TProcessMap ProcessMap;
bool NonEmpty;
};
//--------------------------------------------------------------------------
template<typename T>
class message : public TBaseMessage
{
public:
INLINE message() : TBaseMessage() { }
INLINE const T& operator=(const T& msg) { TCritSect cs; Msg = msg; return Msg; }
INLINE operator T() const { TCritSect cs; return Msg; }
private:
T Msg;
};
//--------------------------------------------------------------------------
}
//------------------------------------------------------------------------------
//
// Function-members implementation
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -