OpenBarnyard
 
Loading...
Searching...
No Matches
TFifo.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "TFifo.h"
3
4//-----------------------------------------------------------------------------
5// Enables memory debugging.
6// Note: Should be the last include!
7//-----------------------------------------------------------------------------
9
11
12TBOOL TGenericFifo::Create( TCHAR* a_pBuffer, TINT a_iMaxItems, TINT a_iItemSize )
13{
14 TASSERT( a_iMaxItems > 0, "Max items is less than zero" );
15 TASSERT( a_iItemSize > 0, "Item size is less than zero" );
16
17 m_iItemSize = a_iItemSize;
18 m_iMaxItems = a_iMaxItems;
19 m_pDataPopCursor = a_pBuffer;
20 m_pDataPushCursor = a_pBuffer;
21 m_pDataBegin = a_pBuffer;
22 m_pDataEnd = a_pBuffer + ( a_iMaxItems * a_iItemSize );
23
24 TBOOL bResult;
25 bResult = m_Semaphore1.Create( m_iMaxItems, m_iMaxItems );
26 TASSERT( bResult != TFALSE, "Unable to create semaphore for TGenericFifo" );
27 bResult = m_Semaphore2.Create( 0, m_iMaxItems );
28 TASSERT( bResult != TFALSE, "Unable to create semaphore for TGenericFifo" );
29
30 InitializeCriticalSection( &m_CriticalSection );
31 return TTRUE;
32}
33
35{
36 DeleteCriticalSection( &m_CriticalSection );
37 m_Semaphore1.Destroy();
38 m_Semaphore2.Destroy();
39 return TTRUE;
40}
41
42TBOOL TGenericFifo::Push( void* a_pItem, Flags a_iFlags )
43{
44 TBOOL bResult = ( a_iFlags & Flags_PollSemaphore ) ? m_Semaphore1.Poll() : m_Semaphore1.Wait();
45 if ( !bResult ) return TFALSE;
46
47 EnterCriticalSection( &m_CriticalSection );
48
49 // Copy data to the buffer and move cursor
50 Toshi::TUtil::MemCopy( m_pDataPushCursor, a_pItem, m_iItemSize );
51 m_pDataPushCursor += m_iItemSize;
52
53 if ( m_pDataPushCursor == m_pDataEnd )
54 {
55 // Set cursor to the beginning if reached the end
56 m_pDataPushCursor = m_pDataBegin;
57 }
58
59 LeaveCriticalSection( &m_CriticalSection );
60
61 bResult = m_Semaphore2.Signal();
62 TASSERT( bResult != TFALSE, "TSemaphore::Signal returned TFALSE" );
63
64 return TTRUE;
65}
66
67TBOOL TGenericFifo::Pop( void* a_pOut, Flags a_iFlags )
68{
69 TBOOL bResult = ( a_iFlags & Flags_PollSemaphore ) ? m_Semaphore2.Poll() : m_Semaphore2.Wait();
70 if ( !bResult ) return TFALSE;
71
72 EnterCriticalSection( &m_CriticalSection );
73
74 // Copy data to the output and move cursor
75 TUtil::MemCopy( a_pOut, m_pDataPopCursor, m_iItemSize );
76 m_pDataPopCursor += m_iItemSize;
77
78 if ( m_pDataPopCursor == m_pDataEnd )
79 {
80 // Set cursor to the beginning if reached the end
81 m_pDataPopCursor = m_pDataBegin;
82 }
83
84 LeaveCriticalSection( &m_CriticalSection );
85
86 bResult = m_Semaphore1.Signal();
87 TASSERT( bResult != TFALSE, "TSemaphore::Signal returned TFALSE" );
88
89 return TTRUE;
90}
91
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
char TCHAR
Definition Typedefs.h:20
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
TBOOL Push(void *a_pItem, Flags a_iFlags)
Definition TFifo.cpp:42
uint8_t Flags
Definition TFifo.h:13
@ Flags_PollSemaphore
Definition TFifo.h:17
TBOOL Pop(void *a_pOut, Flags a_iFlags)
Definition TFifo.cpp:67
TBOOL Destroy()
Definition TFifo.cpp:34
TBOOL Create(TCHAR *a_pBuffer, TINT a_iMaxItems, TINT a_iItemSize)
Definition TFifo.cpp:12
static void * MemCopy(void *dst, const void *src, TSIZE size)
Definition TUtil.h:90