IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 9, 2006, 4:44:46 PM (20 years ago)
Author:
drobbin
Message:

Added psMemThreadSafety in psMemory and updated corresponding mutex's to comply with specs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psMemory.c

    r6314 r6419  
    88*  @author Robert Lupton, Princeton University
    99*
    10 *  @version $Revision: 1.67 $ $Name: not supported by cvs2svn $
    11 *  @date $Date: 2006-02-03 00:11:59 $
     10*  @version $Revision: 1.68 $ $Name: not supported by cvs2svn $
     11*  @date $Date: 2006-02-10 02:44:46 $
    1212*
    1313*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    5858static pthread_mutex_t recycleMemBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
    5959
     60//private boolean for enabling/disabling thread safety.  Default = enabled.
     61static bool safeThreads = true;
     62
    6063static psS32 recycleBins = 13;
    6164static psS32 recycleBinSize[14] = {
     
    8386{
    8487    psPtr ptr = NULL;
    85 
    86     pthread_mutex_lock(&recycleMemBlockListMutex);
     88    if (safeThreads) {
     89        pthread_mutex_lock(&recycleMemBlockListMutex);
     90    }
    8791    psS32 level = recycleBins - 1;
    8892
     
    97101        level--;
    98102    }
    99     pthread_mutex_unlock(&recycleMemBlockListMutex);
     103
     104    if (safeThreads) {
     105        pthread_mutex_unlock(&recycleMemBlockListMutex);
     106    }
    100107
    101108    return ptr;
     
    271278    psMemId id;
    272279
    273     pthread_mutex_lock(&memIdMutex);
     280    if (safeThreads) {
     281        pthread_mutex_lock(&memIdMutex);
     282    }
     283
    274284    id = memid + 1;
    275     pthread_mutex_unlock(&memIdMutex);
     285
     286    if (safeThreads) {
     287        pthread_mutex_unlock(&memIdMutex);
     288    }
    276289
    277290    return id;
     
    287300
    288301    for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock) {
    289         pthread_mutex_unlock(&memBlockListMutex);
     302        if (safeThreads) {
     303            pthread_mutex_unlock(&memBlockListMutex);
     304        }
    290305        failure = checkMemBlock(iter, __func__);
    291         pthread_mutex_lock(&memBlockListMutex);
     306        if (safeThreads) {
     307            pthread_mutex_lock(&memBlockListMutex);
     308        }
    292309        if ( failure ) {
    293310            nbad++;
     
    297314            if (abort_on_error) {
    298315                // release the lock on the memblock list
    299                 pthread_mutex_unlock(&memBlockListMutex);
     316                if (safeThreads) {
     317                    pthread_mutex_unlock(&memBlockListMutex);
     318                }
    300319                psAbort(__func__, "Detected memory corruption");
    301320                return nbad;
     
    305324
    306325    // release the lock on the memblock list
    307     pthread_mutex_unlock(&memBlockListMutex);
     326    if (safeThreads) {
     327        pthread_mutex_unlock(&memBlockListMutex);
     328    }
    308329    return nbad;
    309330}
     
    331352            size = recycleBinSize[level];  // round-up size to next sized bin.
    332353
    333             pthread_mutex_lock(&recycleMemBlockListMutex);
     354            if (safeThreads) {
     355                pthread_mutex_lock(&recycleMemBlockListMutex);
     356            }
    334357
    335358            if (recycleMemBlockList[level] != NULL) {
     
    342365            }
    343366
    344             pthread_mutex_unlock(&recycleMemBlockListMutex);
     367            if (safeThreads) {
     368                pthread_mutex_unlock(&recycleMemBlockListMutex);
     369            }
    345370        }
    346371    }
     
    359384        *(psPtr*)&ptr->endblock = P_PS_MEMMAGIC;
    360385        ptr->userMemorySize = size;
    361         pthread_mutex_init(&ptr->refCounterMutex, NULL);
     386        if (safeThreads) {
     387            pthread_mutex_init(&ptr->refCounterMutex, NULL);
     388        }
    362389    }
    363390    // increment the memory id safely.
    364     pthread_mutex_lock(&memBlockListMutex);
     391    if (safeThreads) {
     392        pthread_mutex_lock(&memBlockListMutex);
     393    }
    365394    *(psMemId* ) & ptr->id = ++memid;
    366     pthread_mutex_unlock(&memBlockListMutex);
    367 
     395    if (safeThreads) {
     396        pthread_mutex_unlock(&memBlockListMutex);
     397    }
    368398    ptr->file = file;
    369399    ptr->freeFunc = NULL;
     
    376406
    377407    // need exclusive access of the memory block list now...
    378     pthread_mutex_lock(&memBlockListMutex);
     408    if (safeThreads) {
     409        pthread_mutex_lock(&memBlockListMutex);
     410    }
    379411
    380412    // insert the new block to the front of the memBlock linked-list
     
    385417    lastMemBlockAllocated = ptr;
    386418
    387     pthread_mutex_unlock(&memBlockListMutex);
     419    if (safeThreads) {
     420        pthread_mutex_unlock(&memBlockListMutex);
     421    }
    388422
    389423    // Did the user ask to be informed about this allocation?
     
    414448        }
    415449
    416         pthread_mutex_lock(&memBlockListMutex);
     450        if (safeThreads) {
     451            pthread_mutex_lock(&memBlockListMutex);
     452        }
    417453
    418454        isBlockLast = (ptr == lastMemBlockAllocated);
     
    441477        }
    442478
    443         pthread_mutex_unlock(&memBlockListMutex);
     479        if (safeThreads) {
     480            pthread_mutex_unlock(&memBlockListMutex);
     481        }
    444482
    445483        // Did the user ask to be informed about this allocation?
     
    487525    psMemBlock* topBlock = lastMemBlockAllocated;
    488526
    489     pthread_mutex_lock(&memBlockListMutex);
     527    if (safeThreads) {
     528        pthread_mutex_lock(&memBlockListMutex);
     529    }
    490530
    491531    for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) {
     
    506546    }
    507547
    508     pthread_mutex_unlock(&memBlockListMutex);
     548    if (safeThreads) {
     549        pthread_mutex_unlock(&memBlockListMutex);
     550    }
    509551
    510552    if (nleak == 0 || array == NULL) {
     
    513555
    514556    *array = p_psAlloc(nleak * sizeof(psMemBlock), __FILE__, __LINE__);
    515     pthread_mutex_lock(&memBlockListMutex);
     557    if (safeThreads) {
     558        pthread_mutex_lock(&memBlockListMutex);
     559    }
    516560
    517561    for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) {
     
    527571    }
    528572
    529     pthread_mutex_unlock(&memBlockListMutex);
     573    if (safeThreads) {
     574        pthread_mutex_unlock(&memBlockListMutex);
     575    }
    530576
    531577    return nleak;
     
    552598    }
    553599
    554     pthread_mutex_lock(&ptr2->refCounterMutex);
     600    if (safeThreads) {
     601        pthread_mutex_lock(&ptr2->refCounterMutex);
     602    }
    555603    refCount = ptr2->refCounter;
    556     pthread_mutex_unlock(&ptr2->refCounterMutex);
     604    if (safeThreads) {
     605        pthread_mutex_unlock(&ptr2->refCounterMutex);
     606    }
    557607
    558608    return refCount;
     
    576626    }
    577627
    578     pthread_mutex_lock(&ptr->refCounterMutex);
     628    if (safeThreads) {
     629        pthread_mutex_lock(&ptr->refCounterMutex);
     630    }
    579631    ptr->refCounter++;
    580     pthread_mutex_unlock(&ptr->refCounterMutex);
     632    if (safeThreads) {
     633        pthread_mutex_unlock(&ptr->refCounterMutex);
     634    }
    581635
    582636    // Did the user ask to be informed about this allocation?
     
    588642}
    589643
    590 psPtr p_psMemSetRefCounter(
    591     psPtr vptr,
    592     psReferenceCount count,
    593     const char *file,
    594     psS32 lineno)
     644psPtr p_psMemSetRefCounter(psPtr vptr,
     645                           psReferenceCount count,
     646                           const char *file,
     647                           psS32 lineno)
    595648{
    596649    psMemBlock* ptr;
     
    610663    }
    611664
    612     pthread_mutex_lock(&ptr->refCounterMutex);
     665    if (safeThreads) {
     666        pthread_mutex_lock(&ptr->refCounterMutex);
     667    }
    613668    ptr->refCounter = count;
    614     pthread_mutex_unlock(&ptr->refCounterMutex);
     669    if (safeThreads) {
     670        pthread_mutex_unlock(&ptr->refCounterMutex);
     671    }
    615672
    616673    if (count < 1) {
     
    642699    }
    643700
    644     pthread_mutex_lock(&ptr->refCounterMutex);
     701    if (safeThreads) {
     702        pthread_mutex_lock(&ptr->refCounterMutex);
     703    }
    645704
    646705    if (ptr->refCounter > 1) {
    647706        ptr->refCounter--;                 // multiple references, just decrement the count.
    648         pthread_mutex_unlock(&ptr->refCounterMutex);
     707        if (safeThreads) {
     708            pthread_mutex_unlock(&ptr->refCounterMutex);
     709        }
    649710
    650711    } else {
    651         pthread_mutex_unlock(&ptr->refCounterMutex);
     712        if (safeThreads) {
     713            pthread_mutex_unlock(&ptr->refCounterMutex);
     714        }
    652715
    653716        if (ptr->freeFunc != NULL) {
     
    655718        }
    656719
    657         pthread_mutex_lock(&memBlockListMutex);
     720        if (safeThreads) {
     721            pthread_mutex_lock(&memBlockListMutex);
     722        }
    658723
    659724        // cut the memBlock out of the memBlock list
     
    668733        }
    669734
    670         pthread_mutex_unlock(&memBlockListMutex);
     735        if (safeThreads) {
     736            pthread_mutex_unlock(&memBlockListMutex);
     737        }
    671738
    672739        // do we need to recycle?
     
    683750            ptr->previousBlock = NULL;
    684751
    685             pthread_mutex_lock(&recycleMemBlockListMutex);
     752            if (safeThreads) {
     753                pthread_mutex_lock(&recycleMemBlockListMutex);
     754            }
    686755            ptr->nextBlock = recycleMemBlockList[level];
    687756            if (recycleMemBlockList[level] != NULL) {
     
    689758            }
    690759            recycleMemBlockList[level] = ptr;
    691             pthread_mutex_unlock(&recycleMemBlockListMutex);
     760            if (safeThreads) {
     761                pthread_mutex_unlock(&recycleMemBlockListMutex);
     762            }
    692763
    693764        } else {
     
    703774            #else // #ifdef PS_MEM_DEBUG
    704775
    705             pthread_mutex_destroy(&ptr->refCounterMutex);
     776            if (safeThreads) {
     777                pthread_mutex_destroy(&ptr->refCounterMutex);
     778            }
    706779            free(ptr);
    707780            #endif // #else - #ifdef PS_MEM_DEBUG
     
    731804
    732805}
     806
    733807psFreeFunc psMemGetDeallocator(const psPtr ptr)
    734808{
     
    9981072}
    9991073
     1074bool psMemThreadSafety(bool safe)
     1075{
     1076    bool out = safeThreads;
     1077    safeThreads = safe;
     1078    if ( out != p_psListThreadSafety(safe) ) {
     1079        psWarning("Thread safety setting in psMemory didn't match that of psList.\n");
     1080    }
     1081    return out;
     1082}
    10001083
    10011084bool p_psMemGetPersistent(psPtr vptr)
Note: See TracChangeset for help on using the changeset viewer.