MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ThreadQueue.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  ThreadQueue.h Thread-Safe byte buffer
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef QUE_H
28 #define QUE_H
29 
30 #include "itype.h"
31 #include "Mutex.h"
32 #include "Logger.h"
33 
34 #define THREADQUEUE_MAX_BYTES (2048) // How big does this actually need to be?
35 
36 namespace MFM
37 {
43  {
44  private:
45 
50  Mutex m_mutex;
51 
55  struct MoreInputIsAvailable : public Mutex::Predicate
56  {
57  ThreadQueue & m_threadQueue;
58  MoreInputIsAvailable(ThreadQueue & tq) : Predicate(tq.m_mutex), m_threadQueue(tq) { }
59 
60  virtual bool EvaluatePrecondition()
61  {
62  bool ret = m_threadQueue.m_heldBytes == 0;
63  if (!ret)
64  {
65  LOG.Error("MoreInputIsAvailable precondition failed: m_heldBytes = %d",
66  m_threadQueue.m_heldBytes);
67  }
68  return ret;
69  }
70 
71  virtual bool EvaluatePredicate()
72  {
73  return m_threadQueue.m_heldBytes > 0;
74  }
75  } m_moreInputIsAvailable;
76 
80  u8 m_queueData[THREADQUEUE_MAX_BYTES];
81 
88  u32 m_writeHead;
89 
96  u32 m_readHead;
97 
102  u32 m_heldBytes;
103 
123  u32 UnsafeRead(u8* bytes, u32 length);
124 
125  public:
126 
131  ThreadQueue();
132 
136  ~ThreadQueue();
137 
150  void Write(u8* bytes, u32 length);
151 
164  void ReadBlocking(u8* bytes, u32 length);
165 
181  u32 Read(u8* bytes, u32 length);
182 
195  void PeekRead(u8* toBuffer, u32 index, u32 length);
196 
204  u32 BytesAvailable() ;
205 
213  void Flush();
214 
215  };
216 }
217 
218 #endif /* QUE_H */
u32 Read(u8 *bytes, u32 length)
Definition: ThreadQueue.cpp:75
void Write(u8 *bytes, u32 length)
Definition: ThreadQueue.cpp:20
void Error(const char *format,...)
Definition: Logger.h:259
void PeekRead(u8 *toBuffer, u32 index, u32 length)
Definition: ThreadQueue.cpp:91
Definition: Mutex.h:42
~ThreadQueue()
Definition: ThreadQueue.cpp:17
Definition: Mutex.h:99
ThreadQueue()
Definition: ThreadQueue.cpp:12
void Flush()
Definition: ThreadQueue.cpp:118
void ReadBlocking(u8 *bytes, u32 length)
Definition: ThreadQueue.cpp:61
u32 BytesAvailable()
Definition: ThreadQueue.cpp:112
Definition: ThreadQueue.h:42