shithub: npe

ref: 979ae53cfee913c2d303ab0f95aaa65dace1c7b5
dir: /libnpe_sdl3/mutex.c/

View raw version
#include "_sdl.h"

SDL_Mutex*
SDL_CreateMutex(void)
{
	SDL_Mutex *m;

	m = mallocz(sizeof(*m), 1);
	return m;
}

void
SDL_DestroyMutex(SDL_Mutex *m)
{
	free(m);
}

bool
SDL_LockMutex(SDL_Mutex *m)
{
	lock(&m->l);
	return true;
}

bool
SDL_UnlockMutex(SDL_Mutex *m)
{
	unlock(&m->l);
	return true;
}

SDL_Condition*
SDL_CreateCondition(void)
{
	SDL_Condition *p;

	if((p = mallocz(sizeof *p, 1)) == nil)
		sysfatal("SDL_CreateCondition: %r");
	p->l = &p->QLock;
	return p;
}

void
SDL_SignalCondition(SDL_Condition *p)
{
	qlock(p);
	rwakeup(p);
	qunlock(p);
}

void
SDL_WaitCondition(SDL_Condition *p, SDL_Mutex *m)
{
	SDL_UnlockMutex(m);
	qlock(p);
	rsleep(p);
	qunlock(p);
	SDL_LockMutex(m);
}

void
SDL_DestroyCondition(SDL_Condition *p)
{
	free(p);
}