thread.c 1.22 KB
#include <scot/thread.h>
#include <scot/exception.h>

int
thread_equal (THREAD_T a, THREAD_T b)
{
	return (a == b);
}

void
thread_end (THREAD_T thread, unsigned int timeout)
{
	int err;

	err = CANCEL_THREAD (thread);
	if (err != 0)
		THROW (EXC (EXC_ERROR, 
					err, 
					"[EVENT_SOURCE_THREAD]cancel request failed"));

	err = JOIN_THREAD (thread, timeout);
	if (err == JOIN_TIMEOUT)
		THROW (EXC (EXC_ERROR,
					err,
					"[EVENT_SOURCE_THREAD]timeout exceeded while waiting "
					"for threads end"));
	if (err == JOIN_ERROR)
		THROW (EXC (EXC_ERROR,
					err,
					"[EVENT_SOURCE_THREAD]failure on waiting for threads end"));
}

THREAD_T
thread_new (T_PROC_RET (*t_proc)(void *), void* arg)
{
	return _beginthreadex (NULL, 0, t_proc, arg, 0, NULL);
}

int
thread_join (THREAD_T thread, unsigned int timeout)
{
	switch (WaitForSingleObject (thread, timeout * 1000))
	{
		case WAIT_OBJECT_0:
			CloseHandle (thread);
			return JOIN_OK;
		case WAIT_TIMEOUT:
			return JOIN_TIMEOUT;
	}
}

int
thread_mutex_lock (THREAD_MUTEX_T * mutex)
{
	switch (WaitForSingleObject (*mutex, INFINITE))
	{
		case WAIT_OBJECT_0:
			return MUTEX_LOCK_OK;
		default:
			return MUTEX_LOCK_ERROR;
	}
}

THREAD_MUTEX_T 
thread_mutex_new  (void)
{
	return NULL;
}