embkernel
 All Classes Functions Variables Typedefs Groups Pages
RtosMsgBox.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
8 #ifndef RTOS_MSG_BOX_HPP_
9 #define RTOS_MSG_BOX_HPP_
10 
11 #include "RtosSyncObject.hpp"
12 
18 template<class T, int N>
19 class RtosMsgBox: public RtosSyncObject {
20 public:
21  RtosMsgBox();
22  ~RtosMsgBox();
23 
24  T mArray[N];
25 
26  int mIdxIn;
27  int mIdxOut;
28 
29  bool give(T& msg, Rtos::TICK tick);
30  bool giveFromInt(T& msg);
31  bool take(T& msg, Rtos::TICK tick);
32  bool takeFromInt(T& msg);
33 };
34 
38 template<class T, int N>
40  RtosSyncObject(N, 0) {
41  mIdxIn = 0;
42  mIdxOut = 0;
43 }
44 
48 template<class T, int N>
50 
51 }
52 
59 template<class T, int N>
60 bool RtosMsgBox<T, N>::give(T& msg, Rtos::TICK tick) {
61  bool result = internalGive(tick, 1);
62  if (result) {
63  mArray[mIdxIn] = msg;
64  mIdxIn = (mIdxIn + 1) % N;
65  internalWakeUpWaitingToTake();
66  }
67  exitCritical();
68  return result;
69 }
70 
76 template<class T, int N>
78  if (mCurrentCount < mMaxCount) {
79  mCurrentCount++;
80  mArray[mIdxIn] = msg;
81  mIdxIn = (mIdxIn + 1) % N;
82  internalWakeUpWaitingToTake();
83  return true;
84  }
85  return false;
86 }
87 
94 template<class T, int N>
95 bool RtosMsgBox<T, N>::take(T& msg, Rtos::TICK tick) {
96  bool result = internalTake(tick, 1);
97  if (result) {
98  msg = mArray[mIdxOut];
99  mIdxOut = (mIdxOut + 1) % N;
100  internalWakeUpWaitingToGive();
101  }
102  exitCritical();
103  return result;
104 }
105 
111 template<class T, int N>
113  if (mCurrentCount >= 0) {
114  mCurrentCount--;
115  msg = mArray[mIdxOut];
116  mIdxOut = (mIdxOut + 1) % N;
117  internalWakeUpWaitingToGive();
118  return true;
119  }
120  return false;
121 }
122 
123 #endif /* RTOS_MSG_BOX_HPP_ */
124