// Thread.h V0.1pre Header File, Provides thread class definition
// © 2003 Elliot Nierman


#ifdef POSIX
#include <pthread.h>
#include <signal.h>

#ifdef WIN
#include "windows.h"
#include "process.h"

// These values need to be reset by the user to correspond to the priority scheme on the target platform

#ifdef POSIX
enum priority_level { IDLE = 11,
                               LOWEST = 11,
                               BELOW_NORMAL = 11,
                               NORMAL = 11,
                               ABOVE_NORMAL = 11,
                               HIGHEST = 11,
                               TIME_CRITICAL = 11 };

// Enumeration for Win32 thread priority levels mapped directly
// on to the native Win32 thread priority levels

#ifdef WIN
enum priority_level { IDLE = THREAD_PRIORITY_IDLE,
                                LOWEST = THREAD_PRIORITY_LOWEST,
                                BELOW_NORMAL = THREAD_PRIORITY_BELOW_NORMAL,
                                NORMAL = THREAD_PRIORITY_NORMAL,
                                ABOVE_NORMAL = THREAD_PRIORITY_ABOVE_NORMAL,
                                HIGHEST = THREAD_PRIORITY_HIGHEST,
                                TIME_CRITICAL = THREAD_PRIORITY_TIME_CRITICAL };


struct thread_var{

    priority_level b_prio;
    int b_trueprio;
    bool b_term;
    bool b_susp;
    bool b_created;
    void * b_exitcode;

#ifdef WIN
    HANDLE w_handle;
    unsigned int w_id;

#ifdef POSIX
    pthread_t                 p_handle;
    struct sched_param  p_param;
    pthread_attr_t          p_attr;
    pthread_mutex_t      p_mux;
    pthread_cond_t       p_cond;

class Thread

    virtual                   ~Thread(){};
    bool                       CreateSuspended();
    bool                       CreateSuspended(unsigned long stack_size);
    bool                       Create();
    bool                       Create(unsigned long stack_size);
    unsigned int            GetID() const;
    bool                       Execute();
    void                       Resume();
    void                       Suspend();
    void                       Terminate();
    bool                       SetPriority(priority_level b_prio);
    char *                    GetPriority();
    bool                       Join() const;
    void                       SafePoint();
    void                       Suspend_Thread();
    void                       T_Sleep(unsigned long millisecs);

    virtual void             ThreadRoutine() = 0; // User overridden

    thread_var             T_Var;

#ifdef WIN
    static unsigned int    __stdcall w_threadfunc(void *args);

#ifdef POSIX
    static void               threadfunc(Thread*);
    static void               susp_threadfunc(Thread*);
    void                       Start();
    void                       Start_Suspended();