avtExecutionManager.C 5.15 KB
Newer Older
camp's avatar
camp committed
1 2 3 4
#if defined(VISIT_THREADS)

#include <stdlib.h>
#include <DebugStream.h>
5
#include <VisItInit.h>
camp's avatar
camp committed
6 7 8 9 10 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
#include "avtExecutionManager.h"


// Singleton for the thread pool. Use macro avtExecutionManager to access it.
avtExecutionManager *avtExecutionManager::_avtExecutionManagerPtr = NULL;

avtExecutionManager * avtExecutionManager::GetAvtThreadPool()
{
    if( _avtExecutionManagerPtr == NULL )
    {
        atexit( avtExecutionManager::DeleteAvtThreadPool );

        _avtExecutionManagerPtr = new avtExecutionManager();
    }
    return( _avtExecutionManagerPtr );
}

void avtExecutionManager::DeleteAvtThreadPool()
{
    if( _avtExecutionManagerPtr )
    {
        delete _avtExecutionManagerPtr;
        _avtExecutionManagerPtr = NULL;
    }
}

avtExecutionManager::avtExecutionManager()
{
    tPool = NULL;
    MutexInit( &mutexMapLock );
    numThreads = 0;
}

avtExecutionManager::~avtExecutionManager()
{
    MutexDestroy( &mutexMapLock, false );

    // Destroy any mutexes in the list.
    std::map<MUTEX_ID, MUTEX *>::iterator it;
    for(it=mutexMap.begin(); it != mutexMap.end(); ++it)
    {
        MutexDestroy( it->second );
    }
}

void avtExecutionManager::SetNumberOfThreads( int nt )
{
    if( nt == 0 )
    {
        numThreads = 4;
    }
    else
    {
        numThreads = nt;
    }

62
    tPool = ThreadPool::Create( numThreads, (numThreads * 100), false );
camp's avatar
camp committed
63 64 65 66 67
    if( tPool == NULL )
    {
        // Failed to create threads, we need to exit.
        // TODO: I think we need to throw an error.
    }
68 69 70 71
    else
    {
        VisItInit::SetNumberOfThreads(numThreads);
    }
camp's avatar
camp committed
72 73 74 75
}
 
void avtExecutionManager::ScheduleWork( void (*workerThreadFunction)(void *), void *cbdata )
{
76 77 78 79 80 81 82 83
    if (tPool == NULL)
    {
        workerThreadFunction(cbdata);
    }
    else
    {
        tPool->AddWork( workerThreadFunction, cbdata );
    }
camp's avatar
camp committed
84 85 86 87
}
 
void avtExecutionManager::FinishWork()
{
88 89 90
    if (tPool == NULL)
        return;

camp's avatar
camp committed
91 92 93 94 95
    tPool->JoinNoExit();
}
 
MUTEX * avtExecutionManager::FindMutex( const MUTEX_ID id )
{
96 97 98
    if (tPool == NULL)
        return NULL;

camp's avatar
camp committed
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
    std::map<MUTEX_ID, MUTEX *>::iterator it;
    MUTEX *lock;

    MutexLock( &mutexMapLock );

    it = mutexMap.find( id );
    if( it == mutexMap.end() )
    {
        // Not found, create it.
        lock = new MUTEX;
        MutexInit( lock );

        mutexMap.insert( std::pair<MUTEX_ID, MUTEX *>(id, lock) );
    }
    else
    {
        lock = it->second;
    }

    MutexUnlock( &mutexMapLock );

    return( lock );
}

MUTEX * avtExecutionManager::RemoveMutex( const MUTEX_ID id )
{
125 126 127
    if (tPool == NULL)
        return NULL;

camp's avatar
camp committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    std::map<MUTEX_ID, MUTEX *>::iterator it;
    MUTEX *lock;

    MutexLock( &mutexMapLock );

    it = mutexMap.find( id );
    if( it != mutexMap.end() )
    {
        // Remove from list.
        lock = it->second;
        mutexMap.erase( it );
    }

    MutexUnlock( &mutexMapLock );

    return( lock );
}

void avtExecutionManager::MutexLock( const MUTEX_ID stringID )
{
148 149 150
    if (tPool == NULL)
        return;

camp's avatar
camp committed
151 152 153 154 155
    MutexLock( FindMutex(stringID) );
}

void avtExecutionManager::MutexUnlock( const MUTEX_ID stringID )
{
156 157 158
    if (tPool == NULL)
        return;

camp's avatar
camp committed
159 160 161 162 163
    MutexUnlock( FindMutex(stringID) );
}

void avtExecutionManager::MutexDestroy( const MUTEX_ID stringID )
{
164 165 166
    if (tPool == NULL)
        return;

camp's avatar
camp committed
167 168 169 170 171
    MutexDestroy( RemoveMutex(stringID) );
}
 
void avtExecutionManager::MutexInit( MUTEX *lock )
{
172 173 174
    if (tPool == NULL)
        return;

camp's avatar
camp committed
175 176 177 178 179 180 181 182 183
    int rtn;
    if( (rtn = pthread_mutex_init(lock, NULL)) )
    {
        debug1 << "pthread_mutex_init: " << strerror(rtn);
    }
}

void avtExecutionManager::MutexDestroy( MUTEX *lock, bool delMutex )
{
184 185 186
    if (tPool == NULL)
        return;

camp's avatar
camp committed
187 188 189 190 191 192 193 194 195 196 197
    int rtn;
    if( (rtn = pthread_mutex_destroy(lock)) )
    {
        debug1 << "pthread_mutex_destroy: " << strerror(rtn);
    }
    if( delMutex )
        delete lock;
}

void avtExecutionManager::MutexLock( MUTEX *lock )
{
198 199 200
    if (tPool == NULL)
        return;

camp's avatar
camp committed
201 202 203 204 205 206 207 208 209
    int rtn;
    if( (rtn = pthread_mutex_lock(lock)) )
    {
        debug1 << "pthread_mutex_lock: " << strerror(rtn);
    }
}

void avtExecutionManager::MutexUnlock( MUTEX *lock )
{
210 211 212
    if (tPool == NULL)
        return;

camp's avatar
camp committed
213 214 215 216 217 218 219 220 221
    int rtn;
    if( (rtn = pthread_mutex_unlock(lock)) )
    {
        debug1 << "pthread_mutex_unlock: " << strerror(rtn);
    }
}

void avtExecutionManager::CondInit( COND *cond )
{
222 223 224
    if (tPool == NULL)
        return;

camp's avatar
camp committed
225 226 227 228 229 230 231 232 233
    int rtn;
    if( (rtn = pthread_cond_init(cond, NULL)) )
    {
        debug1 << "pthread_cond_init: " << strerror(rtn);
    }
}

void avtExecutionManager::CondDestroy( COND *cond )
{
234 235 236
    if (tPool == NULL)
        return;

camp's avatar
camp committed
237 238 239 240 241 242 243 244 245
    int rtn;
    if( (rtn = pthread_cond_destroy(cond)) )
    {
        debug1 << "pthread_cond_destroy: " << strerror(rtn);
    }
}

void avtExecutionManager::CondSignal( COND *cond )
{
246 247 248
    if (tPool == NULL)
        return;

camp's avatar
camp committed
249 250 251 252 253 254 255 256 257
    int rtn;
    if( (rtn = pthread_cond_signal(cond)) )
    {
        debug1 << "pthread_cond_signal: " << strerror(rtn);
    }
}

void avtExecutionManager::CondWait( COND *cond, MUTEX *lock )
{
258 259 260
    if (tPool == NULL)
        return;

camp's avatar
camp committed
261 262 263 264 265 266 267 268 269
    int rtn;
    if( (rtn = pthread_cond_wait(cond, lock)) )
    {
        debug1 << "pthread_cond_wait: " << strerror(rtn);
    }
}

#endif // VISIT_THREADS