/*
    PIP - Platform Independent Primitives
    Thread
    Copyright (C) 2014  Ivan Pelipenko peri4ko@gmail.com

    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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "pithread.h"
#include "pisystemtests.h"


/*! \class PIThread
 * \brief Thread class
 * \details This class allow you exec your code in separate thread.
 * 
 * \section PIThread_sec0 Synopsis
 * Multithreading allow you to write program which will be executed
 * in several threads simultaneously. This trend allow you to use all 
 * cores of modern processors, but there are many dangers.
 * 
 * This class provide virtual functions \a begin(), \a run() and \a end(),
 * which describes start, execution and finish work of some process.
 * These functions executes in \b separate thread. When you execute
 * \a start(), %PIThread create separate system thread and sequentially
 * executes function \a begin(), \a run() and \a end(). You can
 * reimplement each function and write your own code to execute.
 * Scheme of functions executing:
\code{.cpp}
begin();
event started();
while (isRunning()) {
    run();
    ThreadFunc();
    msleep(timer_delay);
}
event stopped();
end();
\endcode
 * Unlike from directly using "pthread" or some similar you doesn`t need
 * to write your own main thread cycle and sleep at every cycle end.
 * %PIThread make it for you, and your job is to set sleep value from
 * contructor or when starting thread, and reimplement \a begin(), \a run()
 * and \a end() functions.
 * 
 * \section PIThread_sec1 Using without subclassing
 * You can use %PIThread without subclassing by using "ThreadFunc" pointer
 * that can be set from constructor or by overloaded function \a start(ThreadFunc func, int timer_delay).
 * If "func" if not null this function will be executed as \a run(). ThreadFunc is any static
 * function with format void func(void * data). "Data" is custom data set from constructor or
 * with function \a setData(). \n Also you can connect to event \a started(), but
 * in this case you should to white your thread main cycle, because this event raised only one time.
 * 
 * \section PIThread_sec2 Locking
 * %PIThread has inrternal mutex that can be locked and unlocked every \a run() if you set this flag
 * with function \a needLockRun(bool). Also you can access to this mutex by functions \a lock(), \a unlock()
 * and \a mutex(). Using this functions together with needLockRun(true) can guarantee one-thread access to
 * some data.
 * 
 */


PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay): PIObject() {
	piMonitor.threads++;
	thread = 0;
	data_ = data;
	ret_func = func;
	running = lockRun = false;
	priority_ = piNormal;
	timer = timer_delay;
	if (startNow) start(timer_delay);
}


PIThread::PIThread(bool startNow, int timer_delay): PIObject() {
	piMonitor.threads++;
	thread = 0;
	ret_func = 0;
	running = lockRun = false;
	priority_ = piNormal;
	timer = timer_delay;
	if (startNow) start(timer_delay);
}


PIThread::~PIThread() {
	piMonitor.threads--;
	if (!running || thread == 0) return;
#ifndef WINDOWS
#  ifdef ANDROID
	pthread_kill(thread, SIGSTOP);
#  else
	pthread_cancel(thread);
#  endif
#else
	TerminateThread(thread, 0);
	CloseHandle(thread);
#endif
}


bool PIThread::start(int timer_delay) {
	if (running) return false;
	terminating = running = false;
	timer = timer_delay;
#ifndef WINDOWS
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setschedparam(&attr, &sparam);
	if (pthread_create(&thread, &attr, thread_function, this) == 0) {
		setPriority(priority_);
		running = true;
		return true;
	}
#else
	thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function, this, 0, 0);
	if (thread != 0) {
		setPriority(priority_);
		running = true;
		return true;
	}
#endif
	return false;
}


bool PIThread::startOnce() {
	if (running) return false;
	terminating = running = false;
#ifndef WINDOWS
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setschedparam(&attr, &sparam);
	if (pthread_create(&thread, &attr, thread_function_once, this) == 0) {
		setPriority(priority_);
		running = true;
		return true;
	}
#else
	thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_function_once, this, 0, 0);
	if (thread != 0) {
		setPriority(priority_);
		running = true;
		return false;
	}
#endif
	return false;
}


void PIThread::terminate() {
	if (thread == 0) return;
	running = false;
#ifndef WINDOWS
#  ifdef ANDROID
	pthread_kill(thread, SIGSTOP);
#  else
	pthread_cancel(thread);
#  endif
#else
	TerminateThread(thread, 0);
	CloseHandle(thread);
#endif
	thread = 0;
	end();
}


void * PIThread::thread_function(void * t) {
#ifndef WINDOWS
#  ifndef ANDROID
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
#  endif
#else
	__PISetTimerResolution();
#endif
	PIThread & ct = *((PIThread * )t);
	ct.running = true;
	ct.begin();
	ct.started();
	while (!ct.terminating) {
		if (ct.lockRun) ct.mutex_.lock();
		ct.run();
		if (ct.ret_func != 0) ct.ret_func(ct.data_);
		if (ct.lockRun) ct.mutex_.unlock();
		if (ct.timer > 0) msleep(ct.timer);
	}
	ct.stopped();
	ct.end();
	ct.running = false;
	//cout << "thread " << t << " exiting ... " << endl;
#ifndef WINDOWS
	pthread_exit(0);
#else
	ExitThread(0);
#endif
	return 0;
}


void * PIThread::thread_function_once(void * t) {
#ifndef WINDOWS
#  ifndef ANDROID
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
#  endif
#else
	__PISetTimerResolution();
#endif
	PIThread & ct = *((PIThread * )t);
	ct.running = true;
	ct.begin();
	ct.started();
	if (ct.lockRun) ct.mutex_.lock();
	ct.run();
	if (ct.ret_func != 0) ct.ret_func(ct.data_);
	if (ct.lockRun) ct.mutex_.unlock();
	ct.stopped();
	ct.end();
	ct.running = false;
	//cout << "thread " << t << " exiting ... " << endl;
#ifndef WINDOWS
	pthread_exit(0);
#else
	ExitThread(0);
#endif
	return 0;
}


void PIThread::setPriority(PIThread::Priority prior) {
	priority_ = prior;
#ifndef WINDOWS
#  ifndef LINUX
	sparam.sched_priority = (int)priority_;
#  else
	sparam.__sched_priority = (int)priority_;
#  endif
	if (!running) return;
	pthread_getschedparam(thread, &policy, &sparam);
	pthread_setschedparam(thread, policy, &sparam);
#else
	if (!running) return;
	SetThreadPriority(thread, -(int)priority_);
#endif
}


bool PIThread::waitForFinish(int timeout_msecs) {
	if (timeout_msecs < 0) {
		while (running)
			msleep(1);
		return true;
	}
	int cnt = 0;
	while (running && cnt < timeout_msecs) {
		msleep(1);
		++cnt;
	}
	return cnt < timeout_msecs;
}


bool PIThread::waitForStart(int timeout_msecs) {
	if (timeout_msecs < 0) {
		while (!running)
			msleep(1);
		return true;
	}
	int cnt = 0;
	while (!running && cnt < timeout_msecs) {
		msleep(1);
		++cnt;
	}
	return cnt < timeout_msecs;
}
