/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "Be.h"
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/xtime.hpp>
#include "NativeThread.h"

namespace winBe {

	extern NativeThread mainThread;

}


// spawn_thread
_IMPEXP_ROOT thread_id spawn_thread (thread_func function_name, const char *thread_name, int32 priority, void *arg)
{
	stdx::string name = thread_name;
	if (name.length () > B_OS_NAME_LENGTH)
		name.resize (B_OS_NAME_LENGTH);

	return reinterpret_cast<thread_id>(new winBe::NativeThread (function_name, name, priority, arg));
}


// resume_thread
_IMPEXP_ROOT status_t resume_thread (thread_id thread)
{
	(reinterpret_cast<winBe::NativeThread*>(thread))->resume ();
	return B_NO_ERROR; // B_BAD_THREAD_ID, B_BAD_THREAD_STATE
}


// resume_thread
_IMPEXP_ROOT status_t wait_for_thread (thread_id thread, status_t* thread_return_value)
{
	winBe::NativeThread* nativeThread = reinterpret_cast<winBe::NativeThread*>(thread);
	*thread_return_value = (reinterpret_cast<winBe::NativeThread*>(thread))->wait_for_termination ();
	return B_NO_ERROR; // B_BAD_THREAD_ID, B_INTERRUPTED
}


// find_thread
_IMPEXP_ROOT thread_id find_thread (const char *name)
{
	thread_id thread = 0;
	if (name)
		thread = reinterpret_cast<thread_id>(winBe::NativeThread::find (name));
	else
		thread = reinterpret_cast<thread_id>(winBe::NativeThread::findCurrentThread ());
	return thread == 0 ? B_NAME_NOT_FOUND : thread;
}


// get_thread_info
_IMPEXP_ROOT status_t _get_thread_info (thread_id thread, thread_info *info, size_t size)
{
	winBe::NativeThread* nativeThread = reinterpret_cast<winBe::NativeThread*>(thread);
	
	info->thread = nativeThread->getThreadId ();
	info->team = 0;
	strcpy (info->name, nativeThread->getName ().c_str ());
	info->state = nativeThread->isStarted () ? B_THREAD_RUNNING : B_THREAD_SUSPENDED;
	info->priority = 0;
	info->sem = 0;
	info->user_time = 0;
	info->kernel_time = 0;
	info->stack_base = 0;
	info->stack_end = 0;

	return B_NO_ERROR; //B_BAD_VALUE
}


// snooze
_IMPEXP_ROOT status_t snooze (bigtime_t microseconds)
{
	boost::xtime xt;
	boost::xtime_get (&xt, boost::TIME_UTC);

	xt.sec += microseconds / 1000000;
	xt.nsec += (microseconds % 100000) * 1000;

	boost::thread::sleep (xt);

	return B_NO_ERROR; // B_ERROR, B_INTERRUPTED
}


// send_data
_IMPEXP_ROOT status_t send_data (thread_id thread, int32 code, const void *buf, size_t buffer_size)
{
	winBe::NativeThread* nativeThread = reinterpret_cast<winBe::NativeThread*>(thread);
	return B_ERROR;
}


// receive_data
_IMPEXP_ROOT status_t receive_data (thread_id *sender, void *buf, size_t buffer_size)
{
	return B_ERROR;
}


// has_data
_IMPEXP_ROOT bool has_data (thread_id thread)
{
	//return reinterpret_cast<winBe::NativeThread*>(thread)->hasData ();
	return false;
}


/*
extern _IMPEXP_ROOT status_t kill_thread(thread_id thread);
extern _IMPEXP_ROOT status_t suspend_thread(thread_id thread);
extern _IMPEXP_ROOT status_t rename_thread(thread_id thread, const char *new_name);
extern _IMPEXP_ROOT status_t set_thread_priority (thread_id thread, int32 new_priority);
extern _IMPEXP_ROOT void exit_thread(status_t status);
extern _IMPEXP_ROOT status_t on_exit_thread(void (*callback)(void *), void *data);
extern _IMPEXP_ROOT status_t _get_next_thread_info(team_id tmid, int32 *cookie, thread_info *info, size_t size);
extern _IMPEXP_ROOT status_t _get_team_usage_info(team_id tmid, int32 who, team_usage_info *ti, size_t size);
*/
