Created
August 11, 2012 10:13
-
-
Save stoyannk/3323464 to your computer and use it in GitHub Desktop.
High level implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an idealized pseudo-implementation. Details are omitted for clarity | |
SMDataQueueBase::SMDataQueueBase(bool initializeMemory) | |
{ | |
CreateSharedMemoryRegion(); | |
if(initializeMemory) | |
{ | |
new(m_SharedData) SharedData; | |
// Allocate an empty node | |
m_SharedData->m_Head = *AllocateNode(sizeof(size_t)); | |
m_SharedData->m_Tail = m_SharedData->m_Head; | |
} | |
} | |
// Producer interface | |
void* SMDataQueueProducer::RequestNode(size_t sz) | |
{ | |
m_SharedData->m_NodeInProduction = *AllocateNode(sz); | |
return m_SharedData->m_NodeInProduction; | |
} | |
void* SMDataQueueProducer::EnqueueMore(size_t sz) | |
{ | |
m_SharedData->m_NodeInProduction = *ExpandNode(m_SharedData->m_NodeInProduction, sz); | |
return m_SharedData->m_NodeInProduction; | |
} | |
void SMDataQueueProducer::CommitNode() | |
{ | |
m_SharedData->m_Tail = m_SharedData->m_NodeInProduction; | |
m_SharedData->m_NodeInProduction = Node(); | |
} | |
//Consumer interface | |
void* SMDataQueueConsumer::Consume(size_t& size) | |
{ | |
// operator!= and operator= must be atomic to each other for the implementation to work | |
// changing the identity of the object (so that operator!= start returning true) must be done atomically | |
if(m_SharedData->m_Head != m_SharedData->m_Tail) | |
{ | |
m_SharedData->m_Head = m_SharedData->m_Head->Successor; | |
return GetNodeData(m_SharedData->m_Head); | |
} | |
else | |
{ | |
return nullptr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment