v_msg.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "v_msg.h"
  4. TMsg * TMsg::pInstance = NULL; // 初始化
  5. TMsg::TMsg()
  6. {
  7. }
  8. TInt sem_post(TSem *pSem)
  9. {
  10. if (NULL == pSem)
  11. {
  12. return VOS_FALSE;
  13. }
  14. //pSem->post();
  15. ReleaseSemaphore(pSem, 1, NULL);
  16. return VOS_TRUE;
  17. }
  18. TInt sem_wait(TSem *pSem, TULong ulMilliseconds)
  19. {
  20. if (NULL == pSem)
  21. {
  22. return VOS_FALSE;
  23. }
  24. //pSem->wait();
  25. if (0 != WaitForSingleObject(pSem, ulMilliseconds)) //信号量值-1
  26. {
  27. return VOS_FALSE;
  28. }
  29. return VOS_TRUE;
  30. }
  31. void TMsgInfo::init(int iSize)
  32. {
  33. m_pMsgHead = new TMsgNode[iSize];
  34. m_iMaxSize = iSize;
  35. m_pSem = CreateSemaphore(NULL //信号量的安全特性
  36. , 0 //设置信号量的初始计数。可设置零到最大值之间的一个值
  37. , m_iMaxSize + 1 //设置信号量的最大计数
  38. , NULL //指定信号量对象的名称
  39. ); // 初始化 信号量
  40. if (NULL == m_pSem)
  41. {
  42. clear();
  43. return;
  44. }
  45. m_pSpinMutex = new TSpinMutex();
  46. if (NULL == m_pSpinMutex)
  47. {
  48. clear();
  49. return;
  50. }
  51. }
  52. void TMsgInfo::clear()
  53. {
  54. if (NULL != m_pSpinMutex)
  55. {
  56. delete m_pSpinMutex;
  57. }
  58. CloseHandle(m_pSem);
  59. if (NULL != m_pMsgHead)
  60. {
  61. delete m_pMsgHead;
  62. }
  63. }
  64. TInt TMsgInfo::Push_Msg(void *pMsgContent)
  65. {
  66. {
  67. TMutex mutex(m_pSpinMutex);
  68. m_pMsgHead[m_iTailPos].pMsg = pMsgContent;
  69. m_iTailPos = (++m_iTailPos) % m_iMaxSize;
  70. ++m_iMsgSize;
  71. }
  72. if (VOS_TRUE != sem_post(m_pSem))
  73. {
  74. return VOS_FALSE;
  75. }
  76. return VOS_TRUE;
  77. }
  78. void *TMsgInfo::Pop_Msg(TULong ulMilliseconds)
  79. {
  80. void *pMsg = NULL;
  81. if (VOS_TRUE != sem_wait(m_pSem, ulMilliseconds))
  82. {
  83. return NULL;
  84. }
  85. {
  86. TMutex lock(m_pSpinMutex);
  87. --m_iMsgSize;
  88. pMsg = m_pMsgHead[m_iHeadPos].pMsg;
  89. m_iHeadPos = (++m_iHeadPos) % m_iMaxSize;
  90. }
  91. return pMsg;
  92. }
  93. TMsg *TMsg::GetInstance()
  94. {
  95. if (NULL == pInstance)
  96. {
  97. pInstance = new TMsg();
  98. pInstance->InitMsgInfo();
  99. }
  100. return pInstance;
  101. }
  102. TInt TMsg::InitMsgInfo()
  103. {
  104. const int iMsgSize = MAX_MSG_QUE_SIZE;
  105. m_pMsgQue = new TMsgInfo(iMsgSize);
  106. m_pFreeMsgHead = (TMsgBody *)malloc(sizeof(TMsgHead));
  107. m_pFreeHead = (TMsgHead *)m_pFreeMsgHead;
  108. m_pFreeHead->pPrev = m_pFreeHead->pNext = m_pFreeMsgHead;
  109. m_pFreeHead->iReserve = m_pFreeHead->iFreeSize = 0;
  110. TMsgBody *pstMsgNode = new TMsgBody[iMsgSize];
  111. m_pSpinMutex = new TSpinMutex();
  112. for (int i = 0; i < iMsgSize; ++i)
  113. {
  114. InsertFree(pstMsgNode + i);
  115. }
  116. return VOS_TRUE;
  117. }
  118. inline void TMsg::InsertFree(TMsgBody *pMsgNode)
  119. {
  120. {
  121. TMutex mutex(m_pSpinMutex);
  122. pMsgNode->pNext = m_pFreeMsgHead;
  123. pMsgNode->pPrev = m_pFreeMsgHead->pPrev;
  124. m_pFreeMsgHead->pPrev->pNext = pMsgNode;
  125. m_pFreeMsgHead->pPrev = pMsgNode;
  126. ++(m_pFreeHead->iFreeSize);
  127. }
  128. }
  129. // 获取到的默认内存大小为4080
  130. void *TMsg::ObtainMsg()
  131. {
  132. return PopFreeMsg();
  133. }
  134. void TMsg::FreeMsg(void *pvMsg)
  135. {
  136. if (NULL == pvMsg)
  137. {
  138. return;
  139. }
  140. TMsgBody *pMsgNode = (TMsgBody *)((char *)pvMsg - sizeof(void *) * 2);
  141. InsertFree(pMsgNode);
  142. pvMsg = NULL;
  143. return;
  144. }
  145. void *TMsg::PopFreeMsg()
  146. {
  147. TMsgBody *pMsgNode = NULL;
  148. if (IsEmpty())
  149. {
  150. return NULL;
  151. }
  152. {
  153. TMutex mutex(m_pSpinMutex);
  154. if (IsEmpty()) { return NULL; }
  155. pMsgNode = m_pFreeMsgHead->pNext;
  156. pMsgNode->pNext->pPrev = pMsgNode->pPrev;
  157. m_pFreeMsgHead->pNext = pMsgNode->pNext;
  158. --(m_pFreeHead->iFreeSize);
  159. }
  160. return pMsgNode->acBody;
  161. }
  162. inline TBool TMsg::IsEmpty()
  163. {
  164. return ((m_pFreeMsgHead->pNext == m_pFreeMsgHead) ? VOS_TRUE : VOS_FALSE);
  165. }
  166. TInt TMsg::SendMsg(void *pvMsg, TInt iMsgLen)
  167. {
  168. TMsgBody *pMsgNode = (TMsgBody *)((char *)pvMsg - sizeof(void *) * 2);
  169. return m_pMsgQue->Push_Msg(pMsgNode);
  170. }
  171. void *TMsg::RecvMsg(TULong ulMilliseconds)
  172. {
  173. void *pvMsg = m_pMsgQue->Pop_Msg(ulMilliseconds);
  174. return ((pvMsg == NULL) ? pvMsg : (char *)(pvMsg)+sizeof(void *) * 2);
  175. }