/*  $Id: thread_pthread.cpp,v 1.13 2003/10/08 17:43:49 mbn Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
**
*/

#include "Core/precomp.h"

#include <pthread.h>
#include "API/Core/System/cl_assert.h"
#include "API/Core/System/log.h"
#include "thread_pthread.h"

/////////////////////////////////////////////////////////////////////////////
// CL_Thread_Helper unix implementation:

class CL_Thread_Helper : public CL_Runnable
{
public:
	CL_Thread_Helper(int (*func)(void*), void *value)
		: func(func), value(value)
	{
	}

	virtual void run()
	{
		func(value);
	}

private:
	int (*func)(void*);

	void *value;
};


/////////////////////////////////////////////////////////////////////////////
// CL_Thread unix/pthread implementation:

CL_Thread::CL_Thread(int (*func)(void*), void *value)
: impl(new CL_Thread_Generic)
{
	impl->ref_count = 1;
	impl->runnable = new CL_Thread_Helper(func, value);
	impl->delete_runnable = true;
	impl->running = false;
}

CL_Thread::CL_Thread(CL_Runnable *runnable, bool delete_runnable)
: impl(new CL_Thread_Generic)
{
	impl->ref_count = 1;
	impl->runnable = runnable;
	impl->delete_runnable = delete_runnable;
	impl->running = false;
}

CL_Thread::CL_Thread(const CL_Thread &copy)
: impl(copy.impl)
{
	if (impl) impl->ref_count++;
}

CL_Thread::CL_Thread()
: impl(0)
{
}

CL_Thread::~CL_Thread()
{
	if (impl)
	{
		impl->ref_count--;
		if (impl->ref_count == 0)
		{
			terminate();
			if (impl->delete_runnable) delete impl->runnable;
			delete impl;
		}
	}
}

CL_Thread &CL_Thread::operator =(const CL_Thread &copy)
{
	if (impl)
	{
		impl->ref_count--;
		if (impl->ref_count == 0)
		{
			terminate();
			if (impl->delete_runnable) delete impl->runnable;
			delete impl;
		}
	}

	impl = copy.impl;
	if (impl) impl->ref_count++;
	return *this;
}

void CL_Thread::start()
{
	if (impl->running) return;

	cl_assert(
		pthread_create(
			&impl->thread,
			NULL,
			impl->run_init,
			impl)==0);

	impl->running = true;
}

void *CL_Thread_Generic::run_init(void *_self)
{
	CL_Thread_Generic *self = (CL_Thread_Generic *) _self;

	// kill thread immidiately - no cancelation point...
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

	self->runnable->run();
	return NULL;
}

void CL_Thread::terminate()
{
	if (impl->running) pthread_cancel(impl->thread);
	impl->running = false;
}

void CL_Thread::wait()
{
	if (impl->running) pthread_join(impl->thread, NULL);
	impl->running = false;
}

void CL_Thread::set_priority(EThreadPriority priority)
{
	CL_Log::log("debug", "CL_Thread_Posix::set_priority not implemented yet.");
}

int CL_Thread::get_current_id()
{
	return pthread_self();
}
