#pragma once

// micro platform-dependent sys core

class Mutex
{
protected:
	void *handle;		// system-specific mutex handle
public:
	Mutex( int enabled = 1 );
	~Mutex();

	void lock();
	void unlock();
};

class MutexLock
{
protected:
	Mutex *mref;		// mutex refptr
public:
	MutexLock( Mutex &m, bool nolock = 0 );
	~MutexLock();
};

class Event
{
protected:
	void *handle, *handle2;
	bool autoReset;
	volatile bool flag;
public:
	Event( bool autoReset_ = 1 );
	~Event();

	bool wait( int ms = -1 );
	void reset();
	void signal();
};

// thread is not run/created until the first call to resume()
class Thread
{
protected:
	void *thrParam;
	void *handle;					// sys-specific handle
	volatile bool shouldTerminate;	// thread should terminate flag
	volatile bool runningFlag;		// running flag
public:
	Thread( void *param = 0 );
	virtual ~Thread();

	// run thread (first time)
	virtual bool run();

	// main worker proc
	// MUST be overridden
	virtual int work();

	// sets thread priority: 0 = normal, -1 = below normal, +1 = above normal
	bool setPriority( int prio );

	// sleep in ms
	static void sleep( int ms );
};

// get millisecond counter
int getMillisec();
