/*
List of mutex flags we need to use for various things

*/
#define MUTEX_MAIN 1
#include "all.h"

void mutex_init(void)
{
	int i;
	for (i=0; m_names[i]!=NULL; i++) {
		mutexes[i] = th_mutex_new(m_names[i]);
	}
}
void mutex_wait(int i)
{
	if ((i<0) || (i>=MAX_MUTEX)) {
		emsg("No such mutex\n");
		abort();
	}
	if (mutexes[i]==NULL) return;
	if (!th_mutex_wait(mutexes[i],600000)) { /* 10 minutes */
		emsg("Cannot get mutex, fatal problem, dieing %s\n",m_names[i]);
		abort();
	}
}
void mutex_release(int i)
{
	if (mutexes[i]==NULL) return;
	if (!th_mutex_release(mutexes[i])) {
		emsg("Cannot free mutex, fatal problem, %s\n",m_names[i]);
		abort();
	}
}
void mutex_exit(void)
{
	int i;
	for (i=0; m_names[i]!=NULL; i++) {
		th_mutex_free(mutexes[i]);
		mutexes[i] = NULL;
	}
}
