IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 6419


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.

Location:
trunk/psLib/src
Files:
4 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)
  • trunk/psLib/src/sys/psMemory.h

    r5216 r6419  
    1212 *  @ingroup MemoryManagement
    1313 *
    14  *  @version $Revision: 1.51 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2005-10-01 02:22:13 $
     14 *  @version $Revision: 1.52 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2006-02-10 02:44:46 $
    1616 *
    1717 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    169169 */
    170170psFreeFunc psMemGetDeallocator(
    171     const psPtr ptr                          ///< the memory block
     171    const psPtr ptr                    ///< the memory block
    172172);
    173173
     
    181181);
    182182
     183/** Activate or Deactivate thread safety and mutex locking in the memory management.
     184 *
     185 *  psMemThreadSafety shall turn on thread safety in the memory management functions if
     186 *  safe is true, and deactivate all mutex locking in the memory management functions if
     187 *  safe is false.  The function shall return the previous value of the thread safety.
     188 *  Note that the default behaviour of the library shall be for the locking to be performed.
     189 *
     190 *  @return bool:       The previous value of the thread safety.
     191 */
     192bool psMemThreadSafety(
     193    bool safe                          ///< boolean for turning on/off thread safety
     194);
    183195
    184196/** Set the memory as persistent so that it is ignored when detecting memory leaks.
  • trunk/psLib/src/types/psList.c

    r5511 r6419  
    66 *  @author Robert Daniel DeSonia, MHPCC
    77 *
    8  *  @version $Revision: 1.45 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2005-11-14 22:18:34 $
     8 *  @version $Revision: 1.46 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2006-02-10 02:44:46 $
    1010 *
    1111 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3333static psBool listIteratorRemove(psListIterator* iterator);
    3434
     35//private boolean for enabling/disabling thread safety.  Default = enabled.
     36static bool safeThreads = true;
     37
    3538static void listFree(psList* list)
    3639{
     
    3942    }
    4043
    41     pthread_mutex_lock(&list->p_lock);
     44    if (safeThreads) {
     45        pthread_mutex_lock(&list->p_lock);
     46        //        pthread_mutex_lock(&p_lock);
     47    }
    4248
    4349    // remove the associated iterators -- any references are invalid once psList is freed.
     
    6470    }
    6571
    66     pthread_mutex_unlock(&list->p_lock);
    67 
    68     pthread_mutex_destroy(&list->p_lock);
    69 
     72    if (safeThreads) {
     73        pthread_mutex_unlock(&list->p_lock);
     74        pthread_mutex_destroy(&list->p_lock);
     75        //        pthread_mutex_unlock(&p_lock);
     76        //        pthread_mutex_destroy(&p_lock);
     77    } else {
     78        safeThreads = true;
     79    }
    7080}
    7181
     
    100110    int index = iterator->index;
    101111
    102     pthread_mutex_lock(&list->p_lock);
    103 
     112    if (safeThreads) {
     113        pthread_mutex_lock(&list->p_lock);
     114        //        pthread_mutex_lock(&p_lock);
     115    }
    104116    if (elem == list->head) {        // head of list?
    105117        list->head = elem->next;
     
    126138    list->n--;
    127139
    128     pthread_mutex_unlock(&list->p_lock)
    129     ;
     140    if (safeThreads) {
     141        pthread_mutex_unlock(&list->p_lock);
     142        //        pthread_mutex_unlock(&p_lock);
     143    }
    130144
    131145    // OK, delete orphaned list element and its data
     
    150164    psListIteratorAlloc(list,PS_LIST_HEAD,true);
    151165
    152     pthread_mutex_init(&(list->p_lock), NULL);
     166    if (safeThreads) {
     167        pthread_mutex_init(&(list->p_lock), NULL);
     168        //        pthread_mutex_init(&p_lock, NULL);
     169    }
    153170
    154171    if (data != NULL) {
     
    164181}
    165182
    166 
     183bool p_psListThreadSafety(bool safe)
     184{
     185    bool out = safeThreads;
     186    safeThreads = safe;
     187    return out;
     188}
    167189
    168190psListIterator* psListIteratorAlloc(psList* list, long location, bool mutable)
     
    326348    psListElem* elem = psAlloc(sizeof(psListElem));
    327349
    328     pthread_mutex_lock(&list->p_lock);
     350    if (safeThreads) {
     351        pthread_mutex_lock(&list->p_lock);
     352        //        pthread_mutex_lock(&p_lock);
     353    }
    329354
    330355    // set the new list element's attributes
     
    362387    }
    363388
    364     pthread_mutex_unlock(&list->p_lock);
     389    if (safeThreads) {
     390        pthread_mutex_unlock(&list->p_lock);
     391        //        pthread_mutex_unlock(&p_lock);
     392    }
    365393
    366394    return true;
     
    399427    psListElem* elem = psAlloc(sizeof(psListElem));
    400428
    401     pthread_mutex_lock(&list->p_lock);
     429    if (safeThreads) {
     430        pthread_mutex_lock(&list->p_lock);
     431        //        pthread_mutex_lock(&p_lock);
     432    }
    402433
    403434    // set the new list element's attributes
     
    435466    }
    436467
    437     pthread_mutex_unlock(&list->p_lock);
     468    if (safeThreads) {
     469        pthread_mutex_unlock(&list->p_lock);
     470        //        pthread_mutex_unlock(&p_lock);
     471    }
    438472
    439473    return true;
  • trunk/psLib/src/types/psList.h

    r6348 r6419  
    77 *  @ingroup LinkedList
    88 *
    9  *  @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2006-02-07 23:36:15 $
     9 *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2006-02-10 02:44:46 $
    1111 *
    1212 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    9494;
    9595
     96/** Private function used by psMemThreadSafety to activate/deactivate thread safety.
     97 *
     98 *  @return bool:       The previous value of the thread safety.
     99 */
     100bool p_psListThreadSafety(
     101    bool safe                          ///< current value of thread safety to set
     102);
    96103
    97104/** Creates a psList linked list object.
Note: See TracChangeset for help on using the changeset viewer.