Fifo.hpp
Go to the documentation of this file.
1 
7 #include <etk/types.hpp>
8 
9 #pragma once
10 
11 #include <mutex>
12 #include <vector>
13 #include <condition_variable>
14 
15 namespace etk {
21  template<class MY_TYPE=int32_t> class Fifo {
22  private :
23  std::mutex m_mutex;
24  std::condition_variable m_condition;
25  std::vector<MY_TYPE> m_data;
26  public :
30  Fifo() {
31  // nothing to do ...
32  };
36  ~Fifo() {
37  // nothing to do ...
38  };
45  bool wait(MY_TYPE &_data) {
46  std::unique_lock<std::mutex> lock(m_mutex);
47  // Check if data is not previously here
48  while(m_data.size() == 0) {
49  m_condition.wait(lock);
50  }
51  // End Waiting message :
52  if (m_data.size() > 0) {
53  // copy element :
54  std::swap(_data, m_data[0]);
55  // remove element :
56  m_data.erase(m_data.begin());
57  return true;
58  }
59  return false;
60  };
68  bool wait(MY_TYPE &_data, uint32_t _timeOutInUs) {
69  std::unique_lock<std::mutex> lock(m_mutex);
70  // Check if data is not previously here
71  while(m_data.size() == 0) {
72  if (m_condition.wait_for(lock, std::chrono::microseconds(_timeOutInUs)) == std::cv_status::timeout) {
73  return false;
74  }
75  }
76  // End Waiting message :
77  if (m_data.size() > 0) {
78  // copy element :
79  std::swap(_data, m_data[0]);
80  // remove element :
81  m_data.erase(0);
82  return true;
83  }
84  return false;
85  };
90  int32_t count() {
91  std::unique_lock<std::mutex> lock(m_mutex);
92  int32_t nbElement = m_data.size();
93  return nbElement;
94  };
99  void post(MY_TYPE &_data) {
100  std::unique_lock<std::mutex> lock(m_mutex);
101  m_data.push_back(_data);
102  m_condition.notify_all();
103  };
108  void post(const MY_TYPE &_data) {
109  std::unique_lock<std::mutex> lock(m_mutex);
110  m_data.push_back(_data);
111  m_condition.notify_all();
112  };
116  void clean() {
117  std::unique_lock<std::mutex> lock(m_mutex);
118  // remove data
119  m_data.clear();
120  m_condition.wait_for(lock, std::chrono::microseconds(0));
121  };
122  };
123 }
124 
void clean()
Remove all the message in the fifo.
Definition: Fifo.hpp:116
void post(MY_TYPE &_data)
Send a message at the other thread by setting a new message in the fifo.
Definition: Fifo.hpp:99
void post(const MY_TYPE &_data)
Send a message at the other thread by setting a new message in the fifo.
Definition: Fifo.hpp:108
int32_t count()
Get the number of message in the fifo.
Definition: Fifo.hpp:90
basic namespace of the etk library. (it might contain all the etk fuctions/class/structures without m...
Definition: Archive.hpp:16
Fifo()
Create a fifo with no message.
Definition: Fifo.hpp:30
~Fifo()
Remove the fifo and all message inside.
Definition: Fifo.hpp:36
Fifo tamplate is a simple messaged fifo element to transfer data message from a thead to an other...
Definition: Fifo.hpp:21
bool wait(MY_TYPE &_data)
Wait a message from the other thread. (no timeout set)
Definition: Fifo.hpp:45
bool wait(MY_TYPE &_data, uint32_t _timeOutInUs)
Wait a message from the other thread, with a specified timeout.
Definition: Fifo.hpp:68