Posts Tagged ‘threading’

h1

threading the memory management and future dev plans

July 2, 2009

So, I finally got the memory management system threaded and defeated the only race condition, which could be found on the close down because of the order it waited for shit in. I fixed it with a shut down event that would poke when both mutexes were released in a timeout waiting loop so there was no wrong order to wait for them in. Anyway, I’m not here to show you that, but instead my threading library. It’s very bare bones, and only has a Win32 implementation for now, but could easily be implemented in another threading library, and I will do so with pthreads sometime in the future.

But I want to show it. I will show it now. SHOWING!

It makes use of factories and singletons, but those should be obvious in terms of their functionality. Factory inherits Factory<Singleton > as well. I might change this if it becomes more efficient to create multiple factory instances for different creation modes (i.e.) each has a different set of creation parameters.

threading.h
#ifndef THREADING_H
#define THREADING_H

#include
#include
#include
#include
#include
#include

#include “dlmalloc_object.h”
#include “factory.h”

namespace SpaceSim
{
class WaitingObject: public DLMallocAlloced
{
protected:
static std::list *wobjs;
public:
WaitingObject();
~WaitingObject();

static bool WaitForAll(std::list *objs = NULL);

virtual bool Wait() = 0;
};

class Mutex: public WaitingObject
{
public:
static Mutex *Create(bool is_global = false, unsigned long spincount = 0x80000400);

virtual bool Lock() = 0;
virtual void Unlock() = 0;

virtual unsigned long SpinCount() const = 0;
};

class GlobalMutex: public Mutex
{
protected:
HANDLE mutex;
public:
GlobalMutex();
~GlobalMutex();

virtual bool Lock();
virtual void Unlock();

virtual bool Wait();

virtual unsigned long SpinCount() const;
};

class LocalMutex: public Mutex
{
protected:
unsigned long spincount;
CRITICAL_SECTION mutex;

public:
LocalMutex(unsigned long spincount);
~LocalMutex();

virtual bool Lock();
virtual void Unlock();

virtual bool Wait();

virtual unsigned long SpinCount() const;
};

class Event: public WaitingObject
{
protected:
HANDLE event;

public:
static Event *Create();

Event();
~Event();

bool Wait();
void Poke();
void Reset();
};

typedef int (*ThreadMainProc)(void *args);
typedef unsigned long ThreadID;

class Thread: public WaitingObject
{
protected:
void *args;
HANDLE thread;
ThreadMainProc proc;
bool is_running;

public:
static Thread *Create(ThreadMainProc main_proc, void *args);
static ThreadID GetID();

Thread(ThreadMainProc main_proc, void *args);
~Thread();

bool Run();
bool Break();
bool Wait();

bool IsRunning() const;
};
};

#endif

threading.cpp
#include “threading.h”
#include “logger.h”

namespace SpaceSim
{
std::list *WaitingObject::wobjs = NULL;

WaitingObject::WaitingObject()
{
if (!wobjs)
wobjs = new std::list;

wobjs->push_back(this);
}

WaitingObject::~WaitingObject()
{
wobjs->remove(this);

if (wobjs->empty())
{
delete wobjs;
wobjs = NULL;
}
}

bool WaitingObject::WaitForAll(std::list *objs)
{
bool success = true;
if ((!objs) || objs->empty())
objs = wobjs;

std::list::iterator it;
for (it = objs->begin(); it != objs->end(); ++it)
success = success && (*it)->Wait();

return success;
}

class MutexFactoryParameters: public FactoryParameters
{
public:
bool is_global;
unsigned long spincount;

MutexFactoryParameters(bool is_global = false, unsigned long spincount = 0x80000400):
is_global(is_global), spincount(spincount) {}
};

template<> class Factory: public Singleton >
{
public:
virtual Mutex *Create(const FactoryParameters &params)
{
const MutexFactoryParameters &mparams = param_cast(params);

if (mparams.is_global)
return new GlobalMutex();
else
return new LocalMutex(mparams.spincount);
}
};

Mutex *Mutex::Create(bool is_global, unsigned long spincount)
{
return Factory::GetSingleton().Create(MutexFactoryParameters(is_global, spincount));
}

GlobalMutex::GlobalMutex()
{
static unsigned long mtctr = 0;
char name[256], cvbuf[16];

ltoa(mtctr++, cvbuf, 16);
strcpy(name, “SPACESIM_MUTEX_0x”);
strcat(name, cvbuf);

assert(mutex = CreateMutex(NULL, FALSE, name));
}

GlobalMutex::~GlobalMutex()
{
CloseHandle(mutex);
}

bool GlobalMutex::Lock()
{
return WaitForSingleObject(mutex, INFINITE) == WAIT_OBJECT_0;
}

void GlobalMutex::Unlock()
{
ReleaseMutex(mutex);
}

bool GlobalMutex::Wait()
{
bool ls = Lock();
Unlock();
return ls;
}

unsigned long GlobalMutex::SpinCount() const {return 0;}

LocalMutex::LocalMutex(unsigned long spincount): spincount(spincount)
{
assert(InitializeCriticalSectionAndSpinCount(&mutex, spincount));
}

LocalMutex::~LocalMutex()
{
DeleteCriticalSection(&mutex);
}

bool LocalMutex::Lock()
{
EnterCriticalSection(&mutex);

return true;
}

void LocalMutex::Unlock()
{
LeaveCriticalSection(&mutex);
}

bool LocalMutex::Wait()
{
bool ls = Lock();
Unlock();
return ls;
}

unsigned long LocalMutex::SpinCount() const {return spincount;}

Event *Event::Create()
{
return Factory::GetSingleton().Create(FactoryParameters());
}

Event::Event()
{
static unsigned long evctr = 0;
char name[256], cvbuf[16];

ltoa(evctr++, cvbuf, 16);
strcpy(name, “SPACESIM_EVENT_0x”);
strcat(name, cvbuf);

assert(event = CreateEvent(NULL, TRUE, FALSE, name));
}

Event::~Event()
{
CloseHandle(event);
}

bool Event::Wait()
{
return WaitForSingleObject(event, INFINITE) == WAIT_OBJECT_0;
}

void Event::Poke()
{
SetEvent(event);
}

void Event::Reset()
{
ResetEvent(event);
}

class ThreadFactoryParameters: public FactoryParameters
{
public:
void *args;
ThreadMainProc proc;

ThreadFactoryParameters(ThreadMainProc proc, void *args): proc(proc), args(args) {}
};

template<> class Factory: public Singleton >
{
public:
virtual Thread *Create(const FactoryParameters &params)
{
const ThreadFactoryParameters &mparams = param_cast(params);

return new Thread(mparams.proc, mparams.args);
}
};

Thread *Thread::Create(ThreadMainProc main_proc, void *args)
{
return Factory::GetSingleton().Create(ThreadFactoryParameters(main_proc, args));
}

struct proc_attribs
{
ThreadMainProc proc;
void *args;
Event *mevent;
};

static DWORD WINAPI __ThreadMainProc(LPVOID param)
{
proc_attribs &pa = *((proc_attribs *)param);

ThreadMainProc proc = pa.proc;
void *args = pa.args;

pa.mevent->Poke();

return proc(args);
}

ThreadID Thread::GetID() {return GetCurrentThreadId();}

Thread::Thread(ThreadMainProc main_proc, void *args): proc(main_proc), args(args) {}

Thread::~Thread()
{
if (is_running) Break();
}

bool Thread::Run()
{
if (is_running) return false;

DWORD temp;

proc_attribs pa;
pa.args = args;
pa.proc = proc;
pa.mevent = Event::Create();

thread = CreateThread(NULL, 0,
__ThreadMainProc, (void *)&pa, 0, &temp);

is_running = (bool)thread;

pa.mevent->Wait();
return is_running;
}

bool Thread::Break()
{
if (!is_running) return false;

bool success = CloseHandle(thread);
is_running = !success;
return success;
}

bool Thread::Wait()
{
if (!is_running) return false;

bool success = (WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0);
return success && Break();
}

bool Thread::IsRunning() const {return is_running;}
};

Now, for stuff more interesting to the consumer who buys the final game (yes… the game will be sold, and I don’t care if people see snippets of the code). The Actor and StaticObject classes will extend the SceneObject class, and all that good stuff will be Lua bound. Actors will be partly virtual and will have to have trigger functions such as being told where to go by the controller filled in. This would be in certain implementations, especially ships and the like. Anyway, that about says it and all that good stuff. I’m making tracks, and it is AWESOME.

Advertisements
h1

reference counter based memory management + bitching about life

June 8, 2009

The bitching about life part comes first.  I believe the DCFC song “No Sunlight” sums up my rant in that regard nicely, so I won’t waste the space here.  Okay I’m done with that.

Now, on to the not depressing stuff.  I was thinking of ways I could potentially speed up memory management with threads.  I was basically thinking along these lines:  I should queue my object deletions so that I can delete them on the side while the next iteration is in progress.  So I came up with an interesting system.  I haven’t implemented the threading yet, but I have seperated object deletion from the main loop entirely and it’s just tacked on the end for now.

Basically I implemented a reference counter object management system.  Each ManagedObject has a grab() and drop() method, used to count references.  When the number of references reaches zero, the drop() method calls the queue_deletion() method in the ObjectManager singleton.  The ObjectManager also keeps track of ManagedObject’s that are still kicking, so that they can be cleaned up at the very end if they weren’t properly dropped.  This should keep memory leaks from being as bad, though that doesn’t mean I won’t bust my ass trying to find all of them.  So the current code for updating is as follows:

void Engine::RunMainLoop(double timestep)
{
while (running)
{
if (!Update(timestep)) running = false;
if (!Render()) running = false;
ObjectManager::Singleton().CleanUp();
}
}

The ObjectManager deletes all things queued for deletion in CleanUp(). What I could do is simply start a cleanup thread that cleans up every second or so. For the threaded version it will lock the mutex for the queue to copy it over to a private queue and clear the public one so that it can be used again while the cleanup iterator goes through and deletes everything added previously. If you want the full classes, I shall have t3h c0dez in an article on custom memory management for C++ classes shortly.

EDIT: Okay, article’s up. It’s called “C++ custom memory management primer”. Link under My Articles in the sidebar.