POSIX semaphores

POSIX semaphores are a real pain. Resolving all the issues I was having with transferring packets for my glDebug application and library, I decided to move the sending and receiving onto a separate thread.

While this improve performance, allowing my OpenGL application to run at 60FPS instead of 38FPS, if it crashed because of some changes I was doing, the semaphore would hang around.

I found the way to solve this is to call unlink.

//
dispatch_queue_t gQueue;
pthread_mutex_t gMutex;
sem_t * gSemaphore;
uint32_t gMTU;
 
//
void glDebugComm_init(const uint32_t inMTU)
{
	int err;
 
	//
	gMTU = inMTU;
 
	//
	gSemaphore = sem_open("glDebug", O_CREAT, 0, 0);
	assert(gSemaphore != SEM_FAILED);
 
	err = sem_unlink("glDebug");
	assert(err == 0);
 
	//
	err = pthread_mutex_init(&gMutex, NULL);
	assert(err == 0);
 
	//
	gQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
	dispatch_async(gQueue,
		^(void)
		{
			while(TRUE)
			{
				glDebugComm_dispatch_thread();
			}
		}
	);
}

This prevented the issue. I think it was happening originally because I’m using named semaphores, and both the application and the debugging lib share the same communication library which use the same name, so when triggered on one, it could accept on the other first.

This does pose potential uses for a cross application semaphore though.