ThreadPool.h 4.31 KB
Newer Older
camp's avatar
camp committed
1 2 3 4 5 6 7 8
/***********************************************************************
ThreadPool.h

        Thread pool class.
*/
#ifndef _THREAD_POOL_H_
#define _THREAD_POOL_H_

9 10
#if defined(VISIT_THREADS)

camp's avatar
camp committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <bitset>


// Define these types for the avtExecutionManager.
typedef pthread_mutex_t MUTEX;
typedef pthread_cond_t  COND;
typedef unsigned int MUTEX_ID;


class ThreadPool;

class ThreadPoolWorker
{
    public:
        virtual void ThreadWork( void *arg, int threadID ) = 0;
        virtual void ThreadDoneWithQueue( int threadID ) {}

        void *       GetWorkItem( ThreadPool *tpool );
};


class ThreadPool
{
    public:
        static ThreadPool * Create( int numWorkerThreads, int maxQueueSize, int doNotBlockWhenFull = false );

        int AddWork( void (*workerThreadFunction)(void *), void *arg );
        //int AddWork( ThreadPoolWorker *worker, void *data, unsigned long num, size_t size );

        int Destroy( int waitToFinish = true );

        // Join and work threads will exit.
        int Join();

        // Join and worker thread will not exit.
        int JoinNoExit();

        int GetNumThreads() const
        {
            return( numberThreads );
        }

    private:
        ThreadPool() {}
        ThreadPool( const ThreadPool &obj ) {}
        ~ThreadPool() {}
        ThreadPool& operator=( const ThreadPool& obj ) { return( *this ); }

        // Work structure.
        typedef struct ThreadPoolWorkStruct
        {
            void (*routine)(void *);              // Work function.
            void                        *arg;     // Arguments to work function.
            struct ThreadPoolWorkStruct *next;    // Next work structure.
        } ThreadPoolWork;

        // characteristics.
        int                 numberThreads;
        int                 maxQueueSize;
        int                 doNotBlockWhenFull;

        // state.
        pthread_t           *threads;
        int                 currentQueueSize;
        ThreadPoolWork      *queueHead;
        ThreadPoolWork      *queueTail;
        int                 queueClosed;
        int                 shutdown;
        // A bit array of threads that are currently working.
        // Used with the queueEmpty condition to know if all work is done.
// TODO: make a bitset class that can handle any number of threads
        std::bitset<64>     threadWorking;

        // synchronization.
        pthread_mutex_t     queueLock;
        pthread_cond_t      queueNotEmpty;
        pthread_cond_t      queueNotFull;
        pthread_cond_t      queueEmpty;

        friend void *ThreadPoolThread( void * );
        friend class ThreadPoolWorker;
};

inline void PthreadMutexLock( pthread_mutex_t &lock )
{
    int rtn;
    if( (rtn = pthread_mutex_lock(&lock)) )
    {
        fprintf( stderr, "pthread_mutex_lock %s", strerror(rtn) );
    }
}

inline void PthreadMutexUnlock( pthread_mutex_t &lock )
{
    int rtn;
    if( (rtn = pthread_mutex_unlock(&lock)) )
    {
        fprintf( stderr, "pthread_mutex_unlock %s", strerror(rtn) );
    }
}

inline void PthreadMutexInit( pthread_mutex_t &lock )
{
    int rtn;
    if( (rtn = pthread_mutex_init(&lock, NULL)) )
    {
        fprintf( stderr, "pthread_mutex_init %s", strerror(rtn) );
    }
}

inline void PthreadMutexDestroy( pthread_mutex_t &lock )
{
    int rtn;
    if( (rtn = pthread_mutex_destroy(&lock)) )
    {
        fprintf( stderr, "pthread_mutex_destroy %s", strerror(rtn) );
    }
}

inline void PthreadCondInit( pthread_cond_t &cond )
{
    int rtn;
    if( (rtn = pthread_cond_init(&cond, NULL)) != 0 )
    {
        fprintf( stderr, "pthread_cond_init %s", strerror(rtn) );
    }
}

inline void PthreadCondDestroy( pthread_cond_t &cond )
{
    int rtn;
    if( (rtn = pthread_cond_destroy(&cond)) != 0 )
    {
        fprintf( stderr, "pthread_cond_destroy %s", strerror(rtn) );
    }
}

inline void PthreadCondSignal( pthread_cond_t &cond )
{
    int rtn;
    if( (rtn = pthread_cond_signal(&cond)) != 0 )
    {
        fprintf( stderr, "pthread_cond_signal %s", strerror(rtn) );
    }
}

inline void PthreadCondWait( pthread_cond_t &cond, pthread_mutex_t &lock )
{
    int rtn;
    if( (rtn = pthread_cond_wait(&cond, &lock)) != 0 )
    {
        fprintf( stderr, "pthread_cond_wait %s", strerror(rtn) );
    }
}

169
#endif // VISIT_THREADS
camp's avatar
camp committed
170 171
#endif // _THREAD_POOL_H_