23 #include "pedigree/kernel/LockGuard.h" 24 #include "pedigree/kernel/compiler.h" 25 #include "pedigree/kernel/process/ConditionVariable.h" 26 #include "pedigree/kernel/process/Mutex.h" 27 #include "pedigree/kernel/processor/types.h" 28 #include "pedigree/kernel/time/Time.h" 29 #include "pedigree/kernel/utilities/List.h" 30 #include "pedigree/kernel/utilities/new" 33 #include "pedigree/kernel/process/Thread.h" 68 : m_RingSize(ringSize), m_WriteCondition(), m_ReadCondition(), m_Ring(),
79 void write(
const T &obj, Time::Timestamp &timeout)
85 if (m_Ring.count() >= m_RingSize)
88 m_WriteCondition.wait(m_Lock, timeout);
89 if (result.hasError())
107 m_ReadCondition.signal();
110 void write(
const T &obj)
112 Time::Timestamp timeout = Time::Infinity;
117 size_t write(
const T *obj,
size_t n, Time::Timestamp &timeout)
123 for (i = 0; i < n && timeout > 0; ++i)
125 write(obj[i], timeout);
131 size_t write(
const T *obj,
size_t n)
133 Time::Timestamp timeout = Time::Infinity;
134 return write(obj, n, timeout);
138 T
read(Time::Timestamp &timeout)
142 Time::Timestamp origTimeout = timeout;
148 if (m_Ring.count() == 0)
151 m_ReadCondition.wait(m_Lock, timeout);
152 if (result.hasError())
161 ret = m_Ring.popFront();
170 m_WriteCondition.signal();
177 Time::Timestamp timeout = 0;
178 return read(timeout);
182 size_t read(T *out,
size_t n, Time::Timestamp &timeout)
188 for (i = 0; i < n && timeout > 0; ++i)
190 out[i] = read(timeout);
196 size_t read(T *out,
size_t n)
198 Time::Timestamp timeout = Time::Infinity;
199 return read(out, n, timeout);
206 return m_Ring.count() > 0;
213 return m_Ring.count() < m_RingSize;
217 bool waitFor(RingBufferWait::WaitType wait, Time::Timestamp &timeout)
220 if (wait == RingBufferWait::Writing)
224 if (m_Ring.count() < m_RingSize)
231 m_WriteCondition.wait(m_Lock, timeout);
232 if (result.hasError())
249 m_ReadCondition.wait(m_Lock, timeout);
250 if (result.hasError())
261 bool waitFor(RingBufferWait::WaitType wait)
263 Time::Timestamp timeout = Time::Infinity;
264 return waitFor(wait, timeout);
283 m_MonitorTargets.pushBack(
new MonitorTarget(pThread, pEvent));
292 m_MonitorTargets.begin();
293 it != m_MonitorTargets.end(); it++)
295 MonitorTarget *pMT = *it;
297 if (pMT->pThread == pThread)
300 m_MonitorTargets.erase(it);
301 it = m_MonitorTargets.begin();
302 if (it == m_MonitorTargets.end())
316 m_MonitorTargets.begin();
317 it != m_MonitorTargets.end(); it++)
319 MonitorTarget *pMT = *it;
321 pMT->pThread->sendEvent(pMT->pEvent);
324 m_MonitorTargets.clear();
T read(Time::Timestamp &timeout)
read - read a byte from the ring buffer.
RingBuffer(size_t ringSize)
Constructor - pass in the desired size of the ring buffer.
size_t write(const T *obj, size_t n, Time::Timestamp &timeout)
write - write the given number of objects to the ring buffer.
bool canWrite()
canWrite - is it possible to write to the ring buffer without blocking?
bool waitFor(RingBufferWait::WaitType wait, Time::Timestamp &timeout)
waitFor - block until the given condition is true (readable/writeable)
size_t read(T *out, size_t n, Time::Timestamp &timeout)
read - read up to the given number of objects from the ring buffer
void write(const T &obj, Time::Timestamp &timeout)
write - write a byte to the ring buffer.
bool dataReady()
dataReady - is data ready for reading from the ring buffer?
void monitor(Thread *pThread, Event *pEvent)
monitor - add a new Event to be fired when something happens
::Iterator< T, node_t > Iterator
void cullMonitorTargets(Thread *pThread)
Cull all monitor targets pointing to pThread.
~RingBuffer()
Destructor - destroys the ring; ensure nothing is calling waitFor.
Utility class to provide a ring buffer.
void notifyMonitors()
Trigger event for threads waiting on us.