Chinaunix首页 | 论坛 | 博客
  • 博客访问: 328216
  • 博文数量: 57
  • 博客积分: 146
  • 博客等级: 入伍新兵
  • 技术积分: 769
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-29 14:57
文章分类
文章存档

2014年(39)

2013年(13)

2012年(5)

我的朋友

分类: C/C++

2013-07-02 17:11:28

这部分代码是从某项目中看到的,写的挺好,值得我学习。

头文件:

点击(此处)折叠或打开

  1. #ifndef _XXX_MBX_H_
  2. #define _XXX_MBX_H_

  3. #include <XXX_msgq.h>

  4. #define XXX_MBX_WAIT_ACK 0x0002 ///< Message Flag : Wait for ACK
  5. #define XXX_MBX_FREE_PRM 0x0004 ///< Message Flag : Message parameters are malloc'ed and need to be free'ed

  6. #define XXX_MBX_BROADCAST_MAX 10 ///< Max message queues/PRC's that can be broadcast to


  7. #define XXX_memAlloc(size) (void*)malloc((size))


  8. /**
  9.   \brief Mailbox handle
  10. */
  11. typedef struct {

  12.   XXX_MsgqHndl rcvMbx; ///< Receive mailbox
  13.   XXX_MsgqHndl ackMbx; ///< ACK mailbox

  14. } XXX_MbxHndl;


  15. int XXX_mbxCreate(XXX_MbxHndl *pHndl);
  16. int XXX_mbxDelete(XXX_MbxHndl *pHndl);
  17. int XXX_mbxSendMsg(XXX_MbxHndl *pTo, XXX_MbxHndl *pFrom, Uint16 cmd, void *pPrm, Uint16 flags);
  18. int XXX_mbxBroadcastMsg(XXX_MbxHndl *pToList[], XXX_MbxHndl *pFrom, Uint16 cmd, void *pPrm, Uint16 flags);
  19. int XXX_mbxAckOrFreeMsg(XXX_MsgHndl *pMsg, int ackRetVal);
  20. int XXX_mbxWaitMsg(XXX_MbxHndl *pHndl, XXX_MsgHndl **pMsg);
  21. int XXX_mbxCheckMsg(XXX_MbxHndl *pHndl, XXX_MsgHndl **pMsg);
  22. int XXX_mbxWaitCmd(XXX_MbxHndl *pHndl, XXX_MsgHndl **pMsg, Uint16 waitCmd);
  23. int XXX_mbxFlush(XXX_MbxHndl *pHndl);


  24. #endif /* _XXX_MBX_H_ */


C文件:

点击(此处)折叠或打开

  1. #include <XXX_msgq.h>

  2. int XXX_msgqCreate(XXX_MsgqHndl *hndl)
  3. {
  4.   pthread_mutexattr_t mutex_attr;
  5.   pthread_condattr_t cond_attr;
  6.   int status=XXX_SOK;
  7.  
  8.   status |= pthread_mutexattr_init(&mutex_attr);
  9.   status |= pthread_condattr_init(&cond_attr);
  10.   
  11.   status |= pthread_mutex_init(&hndl->lock, &mutex_attr);
  12.   status |= pthread_cond_init(&hndl->condRd, &cond_attr);
  13.   status |= pthread_cond_init(&hndl->condWr, &cond_attr);

  14.   hndl->curRd = hndl->curWr = 0;
  15.   hndl->count = 0;
  16.   hndl->len = XXX_MSGQ_LEN_MAX;

  17.   if(status!=XXX_SOK)
  18.     XXX_ERROR("XXX_msgqCreate() = %d \r\n", status);
  19.     
  20.   pthread_condattr_destroy(&cond_attr);
  21.   pthread_mutexattr_destroy(&mutex_attr);
  22.     
  23.   return status;
  24. }

  25. int XXX_msgqDelete(XXX_MsgqHndl *hndl)
  26. {
  27.   pthread_cond_destroy(&hndl->condRd);
  28.   pthread_cond_destroy(&hndl->condWr);
  29.   pthread_mutex_destroy(&hndl->lock);
  30.   
  31.   return XXX_SOK;
  32. }

  33. XXX_MsgHndl *XXX_msgqAllocMsgHndl(XXX_MsgqHndl *to, XXX_MsgqHndl *from, Uint16 cmd, void *prm, Uint16 msgFlags)
  34. {
  35.   XXX_MsgHndl *msg;
  36.   
  37.   msg = XXX_memAlloc( sizeof(XXX_MsgHndl) );
  38.   
  39.   if(msg!=NULL) {
  40.     msg->pTo = to;
  41.     msg->pFrom = from;
  42.     msg->pPrm = prm;
  43.     msg->status = XXX_SOK;
  44.     msg->cmd = cmd;
  45.     msg->flags = msgFlags;
  46.   }

  47.   return msg;
  48. }

  49. int XXX_msgqSend(XXX_MsgqHndl *hndl, XXX_MsgHndl *msg, Uint32 timeout)
  50. {
  51.   int status = XXX_EFAIL;

  52.   pthread_mutex_lock(&hndl->lock);

  53.   while(1) {
  54.     if( hndl->count < hndl->len ) {
  55.       hndl->queue[hndl->curWr] = msg;
  56.       hndl->curWr = (hndl->curWr+1)%hndl->len; /* 周而复始 */
  57.       hndl->count++;
  58.       status = XXX_SOK;
  59.       pthread_cond_signal(&hndl->condRd);
  60.       break;
  61.     } else {
  62.       if(timeout == XXX_TIMEOUT_NONE)
  63.         break;

  64.       status = pthread_cond_wait(&hndl->condWr, &hndl->lock);
  65.     }
  66.   }

  67.   pthread_mutex_unlock(&hndl->lock);

  68.   return status;
  69. }


  70. int XXX_msgqSendMsg(XXX_MsgqHndl *to, XXX_MsgqHndl *from, Uint16 cmd, void *prm, Uint16 msgFlags, XXX_MsgHndl **msg)
  71. {
  72.   int status;
  73.   XXX_MsgHndl *data;

  74.   data = XXX_msgqAllocMsgHndl(to, from, cmd, prm, msgFlags);
  75.   if(data==NULL)
  76.     return XXX_EFAIL;
  77.   
  78.   status = XXX_msgqSend(to, data, XXX_TIMEOUT_FOREVER);
  79.   
  80.   if(status==XXX_SOK) {
  81.     if(msg!=NULL)
  82.       *msg = data;
  83.   }

  84.   return status;
  85. }

  86. int XXX_msgqRecvMsg(XXX_MsgqHndl *hndl, XXX_MsgHndl **msg, Uint32 timeout)
  87. {
  88.   int status = XXX_EFAIL;
  89.   
  90.   pthread_mutex_lock(&hndl->lock);
  91.   
  92.   while(1) {
  93.     if(hndl->count > 0 ) {

  94.       if(msg!=NULL) {
  95.         *msg = hndl->queue[hndl->curRd];
  96.       }
  97.       
  98.       hndl->curRd = (hndl->curRd+1)%hndl->len;
  99.       hndl->count--;
  100.       status = XXX_SOK;
  101.       pthread_cond_signal(&hndl->condWr);
  102.       break;
  103.     } else {
  104.       if(timeout == XXX_TIMEOUT_NONE)
  105.         break;

  106.       status = pthread_cond_wait(&hndl->condRd, &hndl->lock);
  107.     }
  108.   }

  109.   pthread_mutex_unlock(&hndl->lock);

  110.   return status;
  111. }

  112. int XXX_msgqSendAck(XXX_MsgHndl *msg, int ackRetVal)
  113. {
  114.   int status;

  115.   msg->status = ackRetVal;
  116.   
  117.   status = XXX_msgqSend(msg->pFrom, msg, XXX_TIMEOUT_FOREVER);

  118.   return status;
  119. }

  120. int XXX_msgqFreeMsgHndl(XXX_MsgHndl *msg)
  121. {
  122.   XXX_memFree(msg);
  123.   return XXX_SOK;
  124. }


阅读(1529) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~