• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    625
  • comments
    1446
  • views
    1006470

Celebration time...

Sign in to follow this  
Followers 0
ApochPiQ

86 views

I finally got a lockless FIFO implemented for passing messages between Epoch tasks. Like most lockless code, it works by the skin of its teeth, and a little dose or two of pure luck.

Here's the final code for those interested:
class LocklessMailbox
{
public:
//
// Construct and initialize the read and write stacks
//
LocklessMailbox()
{
std::auto_ptr wh(new Node);
std::auto_ptr rh(new Node);

WriteHead = wh.get();
WriteHead->Next = NULL;
WriteHead->Payload = NULL;

ReadHead = rh.get();
ReadHead->Next = NULL;
ReadHead->Payload = NULL;

wh.release();
rh.release();
}

//
// Clean up the read, write, and cache stacks, freeing any
// remaining messages from each stack.
//
~LocklessMailbox()
{
Release();
}

public:
//
// Register a message from a producer thread. Any number of threads
// may call this function.
//
void AddMessage(MessageInfo* info)
{
Node* msgnode = new Node;
msgnode->Payload = info;

Node* oldval;
Node* newval;
Node* retval;

do
{
msgnode->Next = WriteHead;
__asm mfence;
oldval = msgnode->Next;
newval = msgnode;
Node** fieldaddr = &(WriteHead);

_asm
{
mfence
mov eax, oldval
mov ecx, newval
mov edx, dword ptr [fieldaddr]

lock cmpxchg dword ptr[edx], ecx
mov retval, eax
mfence
}
}
while(retval != oldval);
}

//
// Retrieve a message from the mailbox.
// IMPORTANT: only ONE consumer thread (per mailbox) should call this function
//
MessageInfo* GetMessage()
{
if(!PendingReads.empty())
{
Node* readnode = PendingReads.top();
MessageInfo* payload = readnode->Payload;
delete readnode;
PendingReads.pop();
return payload;
}

if(ReadHead->Next == NULL)
SwapReadAndWrite();

Node* n = ReadHead;
while(ReadHead->Next)
{
PendingReads.push(n);
n = n->Next;
ReadHead = n;
}

if(PendingReads.empty())
return NULL;

Node* readnode = PendingReads.top();
MessageInfo* payload = readnode->Payload;
delete readnode;
PendingReads.pop();
return payload;
}

private:
//
// Internal helper for swapping the read/write stacks
// See class comment for details
//
void SwapReadAndWrite()
{
Node* oldval;
Node* newval;
Node* retval;
Node* swappedreadhead;

do
{
swappedreadhead = WriteHead;
__asm mfence;
oldval = swappedreadhead;
newval = ReadHead;
Node** fieldaddr = &(WriteHead);

_asm
{
mfence
mov eax, oldval
mov ecx, newval
mov edx, dword ptr [fieldaddr]

lock cmpxchg dword ptr[edx], ecx
mov retval, eax
mfence
}
}
while(retval != oldval);

ReadHead = swappedreadhead;
}

//
// Release the mailbox and free any remaining messages
//
void Release()
{
for(std::stack::container_type::iterator iter = PendingReads.c.begin(); iter != PendingReads.c.end(); ++iter)
{
delete (*iter)->Payload;
delete *iter;
}

Node* n = WriteHead;
while(n)
{
delete n->Payload;
Node* nextn = n->Next;
delete n;
n = nextn;
}

n = ReadHead;
while(n)
{
delete n->Payload;
Node* nextn = n->Next;
delete n;
n = nextn;
}
}

private:
struct Node
{
Node* Next;
MessageInfo* Payload;
};

Node* WriteHead;
Node* ReadHead;
std::stack PendingReads;
};



(And if you happen to catch a bug, let me know; I'm banking pretty heavily on this code.)


So that takes care of all the big-ticket items on my list for the GDC release; all I have left to do is some thorough code cleanup, some documentation, and then put together the sales pitch documents.

Overall I'm very happy with this release; as usual it's a huge step beyond the previous release, and I already have a sketched-out roadmap for R7 that'll be even cooler.


Right... back to work [grin]

0
Sign in to follow this  
Followers 0


1 Comment


I've been following your Epoch work lately and it is all quite interesting.
In looking at your code, it seems to me that each thread would have it's own LocklessMailbox? Pretty nice class setup here. I've actually been getting started on a multithreaded game engine recently and have been trying to figure out how to set up a messaging system across all threads that didn't slow the rest of the engine down. My ideas always centered around having a single Messaging component for all threads to access. That, along with my lack of understanding of basic threading concepts, is probably why I wasn't able to write a good messaging class.

Keep up the good work, and congrats on the GDC 2009 release of Epoch!
-Wynter Woods
0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now