#ifndef __QUEUE_H__
#define __QUEUE_H__ #include <stdint.h>
#include <stdlib.h>
#include <string.h> /*
* Queues can have more than one producer but only one consumer.
* This means that more than one task or interrupt handler is allowed to store
* new data in the queue but only one task is allowed toget data from the queue.
*
* Queues accept messages of various size. When putting a message into a queue,
* the message size is passed as a parameter.
*
* Retrieving a message from the queue does not copy the message, but returns
* a pointer to the message and its size. Thisenhances performance because the
* data is copied only once, when the message is written into the queue.
*
* The retrieving function has to delete every message after processing it.
* A new message can only be retrieved from the queue when the previous message
* was deleted from the queue.
*
* |---------------------- size -------------------------|
* | |------------ msgCnt ---------------| |
* [ .... ] [ size : message ] [ size : message ] [ .... ]
* | | |
* |pData |offsetFirst |offsetLast
*
*/
typedef struct TAG_QUEUE
{
uint8_t * Memory;
uint32_t Capacity;
uint32_t MessageCount;
uint32_t ReadIndex;
uint32_t WriteIndex;
uint32_t IsUsing;
uint32_t InProgressCount;
} QUEUE; // Creates and initializes a message queue.
QUEUE * Q_Create( uint32_t Capacity ); // Deletes a specific queue.
void Q_Delete( QUEUE * Queue ); // Initializes a message queue.
void Q_Init( QUEUE * Queue, uint8_t * Memory, uint32_t Capacity ); // Deletes the last retrieved message in a queue.
void Q_Purge( QUEUE * Queue ); // Deletes all message in a queue.
void Q_Clear( QUEUE * Queue ); // Returns the number of messages currently in a queue
uint32_t Q_GetCount( QUEUE * Queue ); // Returns the first message size
uint32_t Q_GetSize( QUEUE * Queue ); // Delivers information whether the queue is actually in use.
// A queue must not be cleared or deleted when it is in use.
uint32_t Q_IsUsing( QUEUE * Queue ); // Stores a new message of given size in a queue.
uint32_t Q_Wirte( QUEUE * Queue, void * Message, uint32_t Size ); // Retrieves a message from a queue
uint32_t Q_Read( QUEUE * Queue, void ** Message ); #endif /* __QUEUE_H__ */
#include "queue.h"

#include "cmsis_os.h"
#include "macro_misc.h" // Creates and initializes a message Queue.
QUEUE * Q_Create( uint32_t Capacity )
{
uint32_t Size = ALIGN_UP( sizeof(QUEUE), ) + ALIGN_UP( Capacity, );
QUEUE *Queue = (QUEUE *) osMalloc( Size, osWaitForever );
if ( Queue == NULL )
return NULL; uint8_t * Memory = //
(uint8_t *) ( ( (uint32_t) ( Queue ) ) + ALIGN_UP( sizeof(QUEUE), ) ); Q_Init( Queue, Memory, ALIGN_UP( Capacity, ) ); return Queue;
} // Deletes a specific Queue.
// A Queue must not be cleared or deleted when it is in use.
void Q_Delete( QUEUE * Queue )
{
if ( Queue->IsUsing == )
osFree( Queue );
} // Deletes all messages in a Queue.
// A Queue must not be cleared or deleted when it is in use.
void Q_Clear( QUEUE * Queue )
{
if ( Queue->IsUsing == )
Queue->MessageCount = ;
} // Initializes a message Queue.
void Q_Init( QUEUE * Queue, uint8_t * Memory, uint32_t Capacity )
{
int32_t Delta = (uint32_t) Memory & ;
if ( Delta )
{
Delta -= ;
Capacity += Delta;
Memory -= Delta;
}
memset( Queue, , sizeof(QUEUE) );
Queue->Capacity = Capacity;
Queue->Memory = Memory;
} // Returns the number of messages currently in a Queue
uint32_t Q_GetCount( QUEUE * Queue )
{
return Queue->MessageCount - Queue->InProgressCount;
} // Returns the first message size
uint32_t Q_GetSize( QUEUE * Queue )
{
uint32_t MessageSize = ;
if ( Queue->MessageCount )
MessageSize = *(uint32_t *) ( &Queue->Memory[ Queue->ReadIndex ] );
return MessageSize;
} // Delivers information whether the Queue is actually in use.
// A Queue must not be cleared or deleted when it is in use.
uint32_t Q_IsUsing( QUEUE * Queue )
{
return Queue->IsUsing;
} // Stores a new message of given size in a Queue.
// 0 : Queue could not be stored (Queue is full).
// 1 : Success; message stored.
uint32_t Q_Write( QUEUE * Queue, void * Message, uint32_t Size )
{
uint32_t ReadIndexVal;
uint32_t WriteIndexPending;
uint32_t WriteIndexVal;
uint32_t MessageSize = + ALIGN_UP( Size, );
int32_t * Memory = (int32_t *) Queue->Memory; uint32_t Value = osDisableInterrupt( ); if ( Queue->MessageCount == )
{
// read next message from head of memory
Queue->ReadIndex = ; // Queue could not be stored (memory is full).
WriteIndexVal = -; if ( Queue->Capacity >= MessageSize )
WriteIndexVal = ;
}
else
{
Memory = (int32_t *) Queue->Memory;
WriteIndexPending = Queue->WriteIndex;
int32_t SizePending = Memory[ WriteIndexPending ]; if ( SizePending < )
{
// other task is writting ... but it is preemptived by our task
// WriteIndexPending has been updated
// [ Last Queue ] [ --- Other Queue --- ] [ Our Mesage ]
// | WriteIndexPending
SizePending = -SizePending;
}
else
{
// [ Last Queue ] [ Our Mesage ]
// | WriteIndexPending
} // where our task will write ...
WriteIndexVal = WriteIndexPending + + ALIGN_UP( SizePending, );
ReadIndexVal = Queue->ReadIndex;
if ( ReadIndexVal >= WriteIndexVal )
{
// [ Our Mesage ] [ Last Queue ]
// |<------------>|ReadIndexVal
// |WriteIndexVal
if ( ReadIndexVal - WriteIndexVal < MessageSize )
WriteIndexVal = -;
}
else
{
// [ Our Mesage ] [ Available Space ]
// |WriteIndexVal |Capacity
// |<------------------------------>|
uint32_t sizeAvailableTail = Queue->Capacity - WriteIndexVal;
if ( sizeAvailableTail < MessageSize )
{
// try to write to head of memory
// [ Our Mesage ] [ Last Queue ]
// |<------------>|ReadIndexVal
// |0
if ( ReadIndexVal < MessageSize )
WriteIndexVal = -;
else if ( sizeAvailableTail > )
{
// can not read message from tail of memory
// Marker for Q_Purge()
Memory[ WriteIndexVal ] = ;
// write to head of memory
WriteIndexVal = ;
}
}
}
} // store message to memory
if ( WriteIndexVal != - )
{
// WriteIndexPending for other task if our task be preemptived
Queue->WriteIndex = WriteIndexVal;
Queue->MessageCount++;
Memory[ WriteIndexVal ] = -Size; // SizePending for other task
Queue->InProgressCount++; osRestoreInterrupt( Value );
//
memcpy( &Memory[ WriteIndexVal + ], Message, Size );
//
Value = osDisableInterrupt( );
Memory[ WriteIndexVal ] = Size; // Size for this message
Queue->InProgressCount--;
} osRestoreInterrupt( Value );
return ( WriteIndexVal != - );
} // Retrieves a message from a Queue
// not allowed while the queue is in use.
uint32_t Q_Read( QUEUE * Queue, void ** Message )
{
uint32_t MessageSize = ;
uint32_t * Memory = (uint32_t *) Queue->Memory; uint32_t Value = osDisableInterrupt( );
if ( ( Queue->IsUsing == ) && ( Queue->MessageCount ) )
{
MessageSize = Memory[ Queue->ReadIndex ];
*Message = (void *) ( (uint32_t) ( &Memory[ Queue->ReadIndex ] ) + );
Queue->IsUsing = ;
}
osRestoreInterrupt( Value );
return MessageSize;
} // Deletes the last retrieved message in a Queue.
void Q_Purge( QUEUE * Queue )
{
uint32_t Value = osDisableInterrupt( );
if ( Queue->IsUsing )
{
uint32_t * Memory = (uint32_t *) Queue->Memory;
uint32_t MessageSize = + ALIGN_UP( Memory[ Queue->ReadIndex ], );
Queue->MessageCount--;
uint32_t NextReadIndexVal = Queue->ReadIndex + MessageSize;
Queue->ReadIndex = NextReadIndexVal;
if ( Queue->Capacity - NextReadIndexVal < )
Queue->ReadIndex = ;
else if ( Queue->MessageCount )
{
// Marked by Q_Write(), Next readable message at head of memory
if ( Memory[ NextReadIndexVal ] == )
Queue->ReadIndex = ;
}
Queue->IsUsing = ;
}
osRestoreInterrupt( Value );
}

Queue and Message的更多相关文章

  1. MSMQ(Microsoft Message Queue)

    http://www.cnblogs.com/sk-net/archive/2011/11/25/2232341.html 利用 MSMQ(Microsoft Message Queue),应用程序开 ...

  2. hdu 1509 Windows Message Queue

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1509 Windows Message Queue Description Message queue ...

  3. 消息队列(Message Queue)基本概念(转)

    背景 之前做日志收集模块时,用到flume.另外也有的方案,集成kafaka来提升系统可扩展性,其中涉及到消息队列当时自己并不清楚为什么要使用消息队列.而在我自己提出的原始日志采集方案中不适用消息队列 ...

  4. 常见的Message Queue应用场景

    在最近的工作的里面有同事问到我说,为什么我们需要一个Message Queue,Message Queue可以在哪些地方用,经过一些整理,大概能找到这些点,假如你有这方面的要求,也可以考虑使用Mess ...

  5. 什么是 Message Queue

    Message Queue 是一种非同步的从一个服务到另一个服务的交流形式, 被用于无服务器架构和微服务架构中. Messages 被储存在一个队列中直到被处理了或被删除. 每个Messages只会被 ...

  6. 消息队列(Message Queue)简介及其使用

    消息队列(Message Queue)简介及其使用 摘要:利用 MSMQ(Microsoft Message Queue),应用程序开发人员可以通过发送和接收消息方便地与应用程序进行快速可靠的通信.消 ...

  7. RabbitMQ笔记四:Binding,Queue,Message概念

    Binding详解   黄线部分就是binding Exchange与Exchange,Queue之间的虚拟连接,Binding中可以包含Routing key或者参数   创建binding 注意: ...

  8. Top 10 Uses For A Message Queue

    We’ve been working with, building, and evangelising message queues for the last year, and it’s no se ...

  9. C++ message queue 消息队列入门

    说明:当我们有多个线程以不同的速度运行并且我们想要以特定的顺序从一个线程向另一个线程发送信息时,消息队列可能会有用. 这个想法是,发送线程将消息推送到队列中,而接收线程将消息按自己的步调弹出. 只要发 ...

随机推荐

  1. MySQL与Oracle 差异比较之五存储过程&Function

    存储过程&Function 编号 类别 ORACLE MYSQL 注释 1 创建存储过程语句不同 create or replace procedure P_ADD_FAC(   id_fac ...

  2. REST构架风格介绍:状态表述转移

    REST(Representational State Transfer)是HTTP协议的作者Roy Fielding博士在其博士论文中提出的一种互联网应用构架风格.与以远程对象为核心的ORB和以服务 ...

  3. poj 3270(置换群)

    题意:给定n头母牛的脾气大小,然后让你通过交换任意两头母牛的位置使得最后的母牛序列的脾气值从小到大,交换两头母牛的代价是两个脾气之和,使得代价最小. 分析:以前做过一道题,只有一个地方和这道题不同,但 ...

  4. C# "error CS1729: 'XXClass' does not contain a constructor that takes 0 arguments"的解决方案

    出现这种错误的原因时,没有在子类的构造函数中指出仅有带参构造函数的父类的构造参数. 具体来讲就是: 当子类要重用父类的构造函数时, C# 语法通常会在子类构造函数后面调用 : base( para_t ...

  5. jvm调优之四:生产环境参数实例及分析【生产环境实例增加中】

    java application项目(非web项目) 改进前: -Xms128m-Xmx128m-XX:NewSize=64m-XX:PermSize=64m-XX:+UseConcMarkSweep ...

  6. mysql优化SQL语句的一般步骤及常用方法

    一.优化SQL语句的一般步骤 1. 通过show status命令了解各种SQL的执行频率 mysqladmin extended-status 或: show [session|global]sta ...

  7. Linux 通过YUM安装rzsz

    yum自动安装: yum install lrzsz

  8. js保留小数点后N位的方法介绍

    js保留小数点后N位的方法介绍 利用toFixed函数 代码如下 复制代码 <script language="javascript"> document.write( ...

  9. DHTML 教程学习进度备忘

    书签:跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1.这个学习进度和前面几个学习进度,只是学习了 ...

  10. PHP相关图书推荐

    PHP和MySQL Web开发(原书第4版) 作      者 [澳] Luke Welling,[澳] Luke Welling 著:武欣 等 译 出 版 社 机械工业出版社 出版时间 2009-0 ...