// thread C++0X header
#ifndef _THREAD_
#define _THREAD_
#include <exception>
#include <iosfwd>
#include <functional>
#include <chrono>
#include "Dinkum/threads/xthread"
#include "Dinkum/threads/xtimec.h"

_STD_BEGIN
class thread
	{	// class for observing and managing threads
public:
	class id;
	typedef _Thrd_t native_handle_type;

	thread()
		{	// construct with no thread
		_Thr_set_null(_Thr);
		}

 #if _HAS_VARIADIC_TEMPLATES
	template<class _Fn,
		class _DOTS _Args>
	explicit thread(_Fn _REFREF _Fx,
		_Args _REFREF _DOTS _Ax)
		{	// construct with _Fx(_Ax _DOTS)
		_Launch(&_Thr, tr1::bind(_Fx, _Ax _DOTS));
		}

 #else /* _HAS_VARIADIC_TEMPLATES */
 #define _INCL_FILE_xxthread
 #include <xfwrap>
 #endif /* _HAS_VARIADIC_TEMPLATES */

	~thread()
		{	// clean up
		if (joinable())
			_XSTD terminate();
		}

	thread(thread& _Other)
		: _Thr(_Other._Thr)
		{	// move from _Other
		_Thr_set_null(_Other._Thr);
		}

	thread& operator=(thread& _Other)
		{	// move from _Other
		return (_Move_thread(_Other));
		}

 #if _HAS_RVALUE_REFERENCES
	thread(thread&& _Other)
		: _Thr(_Other._Thr)
		{	// move from _Other
		_Thr_set_null(_Other._Thr);
		}

	thread& operator=(thread&& _Other)
		{	// move from _Other
		return (_Move_thread(_Other));
		}
 #endif /* _HAS_RVALUE_REFERENCES */

//private:
//	thread(const thread&);	// not defined
//	thread& operator=(const thread&);	// not defined
//public:
	void swap(thread& _Other)
		{	// swap with _Other
		_Thrd_t _Tmp = _Thr;
		_Thr = _Other._Thr;
		_Other._Thr = _Tmp;
		}

	bool joinable() const
		{	// return true if this thread can be joined
		return (!_Thr_is_null(_Thr));
		}

	void join();
	void detach();
	id get_id() const;

	static unsigned int hardware_concurrency()
		{	// return number of hardware thread contexts
		return (0);
// TODO: better estimate of number of hardware thread contexts
		}

	native_handle_type native_handle()
		{	// return thread identifier
		return (_Thr);
		}

private:
	thread& _Move_thread(thread& _Other)
		{	// move from _Other
		if (joinable())
			_XSTD terminate();
		_Thr = _Other._Thr;
		_Thr_set_null(_Other._Thr);
		return (*this);
		}

	_Thrd_t _Thr;
	};

	namespace this_thread {
thread::id get_id();

inline void yield()
	{	// give up balance of time slice
	_Thrd_yield();
	}

inline void sleep_until(const Dinkum::threads::xtime *_Abs_time)
	{	// sleep until _Abs_time
	_Thrd_sleep(_Abs_time);
	}

template<class _Clock,
	class _Duration> inline
	void sleep_until(
		const chrono::time_point<_Clock, _Duration>& _Abs_time)
	{	// sleep until time point
	Dinkum::threads::xtime _Tgt;
	_Tgt.sec = chrono::duration_cast<chrono::seconds>(
		_Abs_time.time_since_epoch()).count();
	_Tgt.nsec = (long)chrono::duration_cast<chrono::nanoseconds>(
		_Abs_time.time_since_epoch() - chrono::seconds(_Tgt.sec)).count();
	sleep_until(&_Tgt);
	}

template<class _Rep,
	class _Period> inline
	void sleep_for(const chrono::duration<_Rep, _Period>& _Rel_time)
	{	// sleep for duration
	Dinkum::threads::xtime _Tgt = _To_xtime(_Rel_time);
	sleep_until(&_Tgt);
	}
	}	// namespace this_thread

class thread::id
	{	// thread id
public:
	id()
		{	// id for no thread
		_Thr_set_null(_Thr);
		}

	template<class _Ch,
		class _Tr>
		basic_ostream<_Ch, _Tr>& _To_text(
			basic_ostream<_Ch, _Tr>& _Str)
		{	// insert representation into stream
		return (_Str << _Thr_val(_Thr));
		}

private:
	id(const thread& _Thrd)
		: _Thr(_Thrd._Thr)
		{	// construct from thread object
		}

	id(_Thrd_t _Thrd)
		: _Thr(_Thrd)
		{	// construct from thread identifier
		}

	_Thrd_t _Thr;

	friend thread::id thread::get_id() const;
	friend thread::id this_thread::get_id();
	friend bool operator==(thread::id _Left, thread::id _Right);
	friend bool operator<(thread::id _Left, thread::id _Right);
	};

inline thread::id thread::get_id() const
	{	// return id for this thread
	return (id(*this));
	}

inline thread::id this_thread::get_id()
	{	// return id for current thread
	return (_Thrd_current());
	}

inline void swap(thread& _Left, thread& _Right)
	{	// swap _Left with _Right
	_Left.swap(_Right);
	}

inline bool operator==(thread::id _Left, thread::id _Right)
	{	// return true if _Left and _Right identify the same thread
	return (_Thrd_equal(_Left._Thr, _Right._Thr));
	}

inline bool operator!=(thread::id _Left, thread::id _Right)
	{	// return true if _Left and _Right do not identify the same thread
	return (!(_Left == _Right));
	}

inline bool operator<(thread::id _Left, thread::id _Right)
	{	// return true if _Left precedes _Right
	return (_Thrd_lt(_Left._Thr, _Right._Thr));
	}

inline bool operator<=(thread::id _Left, thread::id _Right)
	{	// return true if _Left precedes or equals _Right
	return (!(_Right < _Left));
	}
inline bool operator>(thread::id _Left, thread::id _Right)
	{	// return true if _Left follows _Right
	return (_Right < _Left);
	}
inline bool operator>=(thread::id _Left, thread::id _Right)
	{	// return true if _Left follows or equals _Right
	return (!(_Left < _Right));
	}

template<class _Ch,
	class _Tr>
	basic_ostream<_Ch, _Tr>& operator<<(
		basic_ostream<_Ch, _Tr>& _Str,
		thread::id _Id)
	{	// insert id into stream
	return (_Str << _Id._To_text(_Str));
	}
_STD_END
#endif /* _THREAD_ */

/*
 * Copyright (c) 1992-2010 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
V5.30:0009 */
