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

 

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


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



// 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 };
#endif

// 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 };

#endif

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;
#endif

#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;
#endif
};

class Thread
{
public:

                                  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);

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


private:
    thread_var             T_Var;

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

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


};