IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10886


Ignore:
Timestamp:
Jan 2, 2007, 6:29:10 PM (19 years ago)
Author:
jhoblitt
Message:

add MUTEX_[LOCK|UNLOCK] macros
replace most uses of "if (safeThreads) {" with MUTEX_[LOCK|UNLOCK]

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/jch-memory/psLib/src/sys/psMemory.c

    r10884 r10886  
    88*  @author Robert Lupton, Princeton University
    99*
    10 *  @version $Revision: 1.88.2.1 $ $Name: not supported by cvs2svn $
    11 *  @date $Date: 2007-01-03 03:36:38 $
     10*  @version $Revision: 1.88.2.2 $ $Name: not supported by cvs2svn $
     11*  @date $Date: 2007-01-03 04:29:10 $
    1212*
    1313*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3838#include "psHistogram.h"
    3939
     40#define MUTEX_LOCK(mutexPtr) \
     41if (safeThreads) { \
     42    pthread_mutex_lock(mutexPtr); \
     43}
     44
     45#define MUTEX_UNLOCK(mutexPtr) \
     46if (safeThreads) { \
     47    pthread_mutex_unlock(mutexPtr); \
     48}
     49
    4050#define P_PS_LARGE_BLOCK_SIZE 65536        // size where under, we try to recycle
    4151
     
    262272psMemId psMemGetLastId(void)
    263273{
    264     if (safeThreads) {
    265         pthread_mutex_lock(&memIdMutex);
    266     }
     274    MUTEX_LOCK(&memIdMutex);
    267275
    268276    psMemId id = memid;
    269277
    270     if (safeThreads) {
    271         pthread_mutex_unlock(&memIdMutex);
    272     }
     278    MUTEX_UNLOCK(&memIdMutex);
    273279
    274280    return id;
     
    281287
    282288    // get exclusive access to the memBlock list to avoid it changing on us while we use it.
    283     //    pthread_mutex_lock(&memBlockListMutex);
     289    //    MUTEX_LOCK(&memBlockListMutex);
    284290
    285291    for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock) {
    286         if (safeThreads) {
    287             pthread_mutex_unlock(&memBlockListMutex);
    288         }
     292        MUTEX_UNLOCK(&memBlockListMutex);
    289293        failure = checkMemBlock(iter, __func__);
    290         if (safeThreads) {
    291             pthread_mutex_lock(&memBlockListMutex);
    292         }
     294        MUTEX_LOCK(&memBlockListMutex);
    293295        if ( failure ) {
    294296            nbad++;
     
    298300            if (abort_on_error) {
    299301                // release the lock on the memblock list
    300                 if (safeThreads) {
    301                     pthread_mutex_unlock(&memBlockListMutex);
    302                 }
     302                MUTEX_UNLOCK(&memBlockListMutex);
    303303                psAbort(__func__, "Detected memory corruption");
    304304                return nbad;
     
    308308
    309309    // release the lock on the memblock list
    310     if (safeThreads) {
    311         pthread_mutex_unlock(&memBlockListMutex);
    312     }
     310    MUTEX_UNLOCK(&memBlockListMutex);
     311
    313312    return nbad;
    314313}
     
    353352    }
    354353    // increment the memory id safely.
    355     if (safeThreads) {
    356         pthread_mutex_lock(&memBlockListMutex);
    357     }
     354    MUTEX_LOCK(&memBlockListMutex);
    358355    *(psMemId* ) & ptr->id = ++memid;
    359     if (safeThreads) {
    360         pthread_mutex_unlock(&memBlockListMutex);
    361     }
     356    MUTEX_UNLOCK(&memBlockListMutex);
     357
    362358    ptr->file = file;
    363359    ptr->freeFunc = NULL;
     
    370366
    371367    // need exclusive access of the memory block list now...
    372     if (safeThreads) {
    373         pthread_mutex_lock(&memBlockListMutex);
    374     }
     368    MUTEX_LOCK(&memBlockListMutex);
    375369
    376370    // insert the new block to the front of the memBlock linked-list
     
    381375    lastMemBlockAllocated = ptr;
    382376
    383     if (safeThreads) {
    384         pthread_mutex_unlock(&memBlockListMutex);
    385     }
     377    MUTEX_UNLOCK(&memBlockListMutex);
    386378
    387379    // Did the user ask to be informed about this allocation?
     
    417409    // Reallocate the memory
    418410
    419     if (safeThreads) {
    420         pthread_mutex_lock(&memBlockListMutex);
    421     }
     411    MUTEX_LOCK(&memBlockListMutex);
    422412
    423413    bool isBlockLast = (ptr == lastMemBlockAllocated); // Is this the last block we allocated?
     
    445435    }
    446436
    447     if (safeThreads) {
    448         pthread_mutex_unlock(&memBlockListMutex);
    449     }
     437    MUTEX_UNLOCK(&memBlockListMutex);
    450438
    451439    // Did the user ask to be informed about this allocation?
     
    492480    psMemBlock* topBlock = lastMemBlockAllocated;
    493481
    494     if (safeThreads) {
    495         pthread_mutex_lock(&memBlockListMutex);
    496     }
     482    MUTEX_LOCK(&memBlockListMutex);
    497483
    498484    for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) {
     
    513499    }
    514500
    515     if (safeThreads) {
    516         pthread_mutex_unlock(&memBlockListMutex);
    517     }
     501    MUTEX_UNLOCK(&memBlockListMutex);
    518502
    519503    if (nleak == 0 || array == NULL) {
     
    522506
    523507    *array = p_psAlloc(nleak * sizeof(psMemBlock), __FILE__, __LINE__);
    524     if (safeThreads) {
    525         pthread_mutex_lock(&memBlockListMutex);
    526     }
     508    MUTEX_LOCK(&memBlockListMutex);
    527509
    528510    for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) {
     
    538520    }
    539521
    540     if (safeThreads) {
    541         pthread_mutex_unlock(&memBlockListMutex);
    542     }
     522    MUTEX_UNLOCK(&memBlockListMutex);
    543523
    544524    return nleak;
     
    565545    }
    566546
    567     if (safeThreads) {
    568         pthread_mutex_lock(&ptr2->refCounterMutex);
    569     }
     547    MUTEX_LOCK(&ptr2->refCounterMutex);
    570548    refCount = ptr2->refCounter;
    571     if (safeThreads) {
    572         pthread_mutex_unlock(&ptr2->refCounterMutex);
    573     }
     549    MUTEX_UNLOCK(&ptr2->refCounterMutex);
    574550
    575551    return refCount;
     
    593569    }
    594570
    595     if (safeThreads) {
    596         pthread_mutex_lock(&ptr->refCounterMutex);
    597     }
     571    MUTEX_LOCK(&ptr->refCounterMutex);
    598572    ptr->refCounter++;
    599     if (safeThreads) {
    600         pthread_mutex_unlock(&ptr->refCounterMutex);
    601     }
     573    MUTEX_UNLOCK(&ptr->refCounterMutex);
    602574
    603575    // Did the user ask to be informed about this allocation?
     
    630602    }
    631603
    632     if (safeThreads) {
    633         pthread_mutex_lock(&ptr->refCounterMutex);
    634     }
     604    MUTEX_LOCK(&ptr->refCounterMutex);
    635605    ptr->refCounter = count;
    636     if (safeThreads) {
    637         pthread_mutex_unlock(&ptr->refCounterMutex);
    638     }
     606    MUTEX_UNLOCK(&ptr->refCounterMutex);
    639607
    640608    if (count < 1) {
     
    666634    }
    667635
    668     if (safeThreads) {
    669         pthread_mutex_lock(&ptr->refCounterMutex);
    670     }
     636    MUTEX_LOCK(&ptr->refCounterMutex);
    671637
    672638    if (ptr->refCounter > 1) {
    673639        ptr->refCounter--;                 // multiple references, just decrement the count.
    674         if (safeThreads) {
    675             pthread_mutex_unlock(&ptr->refCounterMutex);
    676         }
     640        MUTEX_UNLOCK(&ptr->refCounterMutex);
    677641
    678642    } else {
    679         if (safeThreads) {
    680             pthread_mutex_unlock(&ptr->refCounterMutex);
    681         }
     643        MUTEX_UNLOCK(&ptr->refCounterMutex);
    682644
    683645        if (ptr->freeFunc != NULL) {
     
    685647        }
    686648
    687         if (safeThreads) {
    688             pthread_mutex_lock(&memBlockListMutex);
    689         }
     649        MUTEX_LOCK(&memBlockListMutex);
    690650
    691651        // cut the memBlock out of the memBlock list
     
    700660        }
    701661
    702         if (safeThreads) {
    703             pthread_mutex_unlock(&memBlockListMutex);
    704         }
     662        MUTEX_UNLOCK(&memBlockListMutex);
    705663
    706664        vptr = NULL;                       // since we freed it, make sure we return NULL.
     
    10721030    }
    10731031
    1074     if (safeThreads) {
    1075         pthread_mutex_lock(&memBlockListMutex);
    1076     }
     1032    MUTEX_LOCK(&memBlockListMutex);
    10771033    /*
    10781034     * All memory that's currently allocated, whether persistent or not
     
    11071063    }
    11081064
    1109     if (safeThreads) {
    1110         pthread_mutex_unlock(&memBlockListMutex);
    1111     }
     1065    MUTEX_UNLOCK(&memBlockListMutex);
    11121066
    11131067    return *allocated + *persistent;
Note: See TracChangeset for help on using the changeset viewer.