

#include "hbapi.h"
#include "mutex.h"

//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//
// CLASS Job
//
// This class is used to pack the input from the script interpreter and the 
// event context into one object, which is then passed to a worker thread.
//
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
class Job  
{
private:
    char           * _query;
    hbEventContext * _context;

    // protect parameterless constructor
    Job ();

public:


    // Constructor 
    Job ( const char *q, const hbEventContext *c );

    // Destructor 
    ~Job ();

    // Copy Constructor 
    Job ( Job  &arg );

    const char *Query() const
    {
       return _query;
    }

    const hbEventContext * Context()const
    {
       return _context;
    }
};


//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//
// CLASS JobQueue
//
// This class provides methods to store job requests coming from one thread and
// retrieve those jobs in a worker thread
//
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

#define MAX_JOBS 100

class JobQueue
{
private: 

    int          _elems;
    Mutex        _mutex;
    EventMutex   _elementAvailable;
    int          _topIdx;
    int          _bottomIdx;
    Job  * _queue[ MAX_JOBS ];

public: 
          JobQueue();
          ~JobQueue();
    void  stop();
    int   add( Job  &arg );
    Job * get( int wait );
};



//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//
// CLASS WorkerThread
//
// This class provides methods to start and stop a worker thread.
//
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

class WorkerThread
{
private:

    int       _stop;
#ifdef _WIN32
    unsigned long  _theThread;
    void (*_function)(void *);
#else
    pthread_t _theThread;
    void * (*_function)(void *);
#endif

    // protect parameterless constructor
    WorkerThread(); 

public:
    JobQueue  _jobs;

#ifdef _WIN32
         WorkerThread( void (*function) (void *));
#else
         WorkerThread( void * (*function) (void *));
#endif
         ~WorkerThread() {};
    int  start();
    void stop();
    int  stopped() { return ( _stop ); };
};

