/*
 *  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
 */

#ifndef NATIVETHREAD_H
#define NATIVETHREAD_H

namespace winBe {

	class NativeThread;
	typedef boost::shared_ptr<NativeThread> NativeThreadPtr;
	typedef std::map<std::string, NativeThread*> NativeThreadLookup;


	class NativeThread : public boost::noncopyable
	{
	public:
		NativeThread ();
		NativeThread (thread_func function_name, const std::string& thread_name, int32 priority, void* arg);
		~NativeThread ();

		bool resume ();
		int wait_for_termination ();
		long getThreadId ();
		bool postMessage (long message);
		void postQuitMessage ();

		const std::string& getName () const { return name; };
		bool isStarted () const { return boostThread == 0 ? false : true; };

		static NativeThread* find (std::string name);
		static NativeThread* findCurrentThread ();

	private:

		// NativeThreadInitializer
		class NativeThreadInitializer
		{
		public:
			void operator () ();
		
			thread_func entryFunction;
			void* entryArg;
			NativeThread* owner;
		};

		friend class NativeThreadInitializer;


		std::string name;
		boost::thread* boostThread;
		NativeThreadInitializer initializer;
		long threadId;
		int exitValue;

		static NativeThreadLookup threadLookup; 
		static boost::mutex lookupMutex;
	};


}

#endif // NATIVETHREAD_H
