Changeset 31576
- Timestamp:
- May 26, 2011, 6:47:53 PM (15 years ago)
- Location:
- branches/eam_branches/ipp-20110505
- Files:
-
- 3 edited
-
psLib/src/sys/psMemory.c (modified) (23 diffs)
-
psLib/src/sys/psMemory.h (modified) (1 diff)
-
psphot/src/psphot.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20110505/psLib/src/sys/psMemory.c
r31575 r31576 40 40 #define P_PS_MEMMAGIC (uint32_t)0xdeadbeef 41 41 42 # define USE_SPINLOCK 1 43 44 # if (USE_SPINLOCK) 45 #define MUTEX_LOCK(mutexPtr) \ 46 if (safeThreads) { \ 47 pthread_spin_lock(mutexPtr); \ 48 } 49 #define MUTEX_UNLOCK(mutexPtr) \ 50 if (safeThreads) { \ 51 pthread_spin_unlock(mutexPtr); \ 52 } 53 # else // !USE_SPINLOCK 42 54 #define MUTEX_LOCK(mutexPtr) \ 43 55 if (safeThreads) { \ 44 56 pthread_mutex_lock(mutexPtr); \ 45 57 } 46 47 58 #define MUTEX_UNLOCK(mutexPtr) \ 48 59 if (safeThreads) { \ 49 60 pthread_mutex_unlock(mutexPtr); \ 50 61 } 62 # endif // USE_SPINLOCK 51 63 52 64 // psAbort() calls functions that call psAlloc() so it is *UNSAFE* to use it … … 94 106 // 95 107 // 96 static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER; 108 109 # if (USE_SPINLOCK) 110 static pthread_spinlock_t memBlockListMutex; 111 # else 112 // static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER; 113 static pthread_mutex_t memBlockListMutex; 114 # endif 115 116 void psMemInit() { 117 118 # if (USE_SPINLOCK) 119 pthread_spin_init(&memBlockListMutex, 0); 120 # else 121 pthread_mutex_init(&memBlockListMutex, NULL); 122 # endif 123 124 } 97 125 98 126 /******** thread safety options management ********/ … … 335 363 336 364 # if (!HARDLOCKS) 337 static int setLock1 = 0; 338 static int clearLock1 = 0; 339 340 static int setLock2 = 0; 341 static int clearLock2 = 0; 342 343 static int setLockNext = 0; 344 static int clearLockNext = 0; 345 346 static int setLockPrev = 0; 347 static int clearLockPrev = 0; 365 static int setLock = 0; 366 static int clearLock = 0; 367 static int retryLock = 0; 348 368 # endif 349 369 350 370 # define BLOCK_SLEEP 100 351 352 static int callL1 = 0;353 static int callL2 = 0;354 371 355 372 # define MEM_ASSERT(COND,MSG) { if (!(COND)) { fprintf (stderr, MSG); abort(); } } … … 373 390 374 391 while (true) { 375 // we cannot set the lock on the marker while the lock is held376 while (lastMemBlockAllocated->inFlight) { usleep (BLOCK_SLEEP); }377 378 392 // set a lock, then lock this memblock and its neighbors 379 393 MUTEX_LOCK(&memBlockListMutex); … … 383 397 // nope, we need to try again 384 398 MUTEX_UNLOCK(&memBlockListMutex); 399 retryLock ++; 400 usleep (BLOCK_SLEEP); 385 401 continue; 386 402 } … … 388 404 // the marker is ours! 389 405 lastMemBlockAllocated->inFlight = true; 390 setLock 1++;406 setLock ++; 391 407 return; 392 408 } … … 410 426 411 427 while (true) { 412 // we cannot set the lock on the marker while the lock is held413 while (memBlock->inFlight) { usleep (BLOCK_SLEEP); }414 415 428 // set a lock, then lock this memblock and its neighbors 416 429 MUTEX_LOCK(&memBlockListMutex); … … 420 433 // nope, we need to try again 421 434 MUTEX_UNLOCK(&memBlockListMutex); 435 usleep (BLOCK_SLEEP); 436 retryLock ++; 422 437 continue; 423 438 } … … 425 440 // the marker is ours! 426 441 memBlock->inFlight = true; 427 setLock 1++;442 setLock ++; 428 443 if (keepMutex) return; 429 444 … … 455 470 } 456 471 memBlock->inFlight = false; 457 clearLock 1++;472 clearLock ++; 458 473 MUTEX_UNLOCK(&memBlockListMutex); 459 474 return; … … 475 490 // we cannot set the lock on the marker while the lock is held 476 491 // wait until all three markers are clear: 477 while (memBlock->inFlight) { usleep (BLOCK_SLEEP); }478 while (memBlock->nextBlock && memBlock->nextBlock->inFlight) { usleep (BLOCK_SLEEP); }479 while (memBlock->previousBlock && memBlock->previousBlock->inFlight) { usleep (BLOCK_SLEEP); }492 // while (memBlock->inFlight) { usleep (BLOCK_SLEEP); } 493 // while (memBlock->nextBlock && memBlock->nextBlock->inFlight) { usleep (BLOCK_SLEEP); } 494 // while (memBlock->previousBlock && memBlock->previousBlock->inFlight) { usleep (BLOCK_SLEEP); } 480 495 481 496 // set a lock, then lock this memblock and its neighbors … … 486 501 // nope, we need to try again 487 502 MUTEX_UNLOCK(&memBlockListMutex); 503 usleep (BLOCK_SLEEP); 504 retryLock ++; 488 505 continue; 489 506 } … … 491 508 // nope, we need to try again 492 509 MUTEX_UNLOCK(&memBlockListMutex); 510 usleep (BLOCK_SLEEP); 511 retryLock ++; 493 512 continue; 494 513 } … … 496 515 // nope, we need to try again 497 516 MUTEX_UNLOCK(&memBlockListMutex); 517 usleep (BLOCK_SLEEP); 518 retryLock ++; 498 519 continue; 499 520 } … … 501 522 // the markers are ours! 502 523 memBlock->inFlight = true; 503 setLock 2++;524 setLock ++; 504 525 if (memBlock->nextBlock) { 505 526 memBlock->nextBlock->inFlight = true; 506 setLockNext ++;507 527 } 508 528 if (memBlock->previousBlock) { 509 529 memBlock->previousBlock->inFlight = true; 510 setLockPrev ++;511 530 } 512 531 if (keepMutex) return; … … 530 549 MEM_ASSERT (memBlock->inFlight, "trying to clear an unlocked memBlock\n"); 531 550 memBlock->inFlight = false; 532 clearLock 2++;551 clearLock ++; 533 552 if (memBlock->nextBlock) { 534 553 MEM_ASSERT (memBlock->nextBlock->inFlight, "trying to clear an unlocked memBlock\n"); 535 554 memBlock->nextBlock->inFlight = false; 536 clearLockNext ++;537 555 } 538 556 if (memBlock->previousBlock) { 539 557 MEM_ASSERT (memBlock->previousBlock->inFlight, "trying to clear an unlocked memBlock\n"); 540 558 memBlock->previousBlock->inFlight = false; 541 clearLockPrev ++;542 559 } 543 560 MUTEX_UNLOCK(&memBlockListMutex); … … 621 638 // lastMemBlockAllocated is always true except the first allocation 622 639 BLOCKLAST_LOCK (); 623 callL1++;624 640 625 641 psMemBlock *last1 = lastMemBlockAllocated; … … 649 665 650 666 BLOCK_UNLOCK (memBlock->nextBlock, true); 651 callL1--;652 653 // if (callL1 > +7) abort();654 // if (callL1 < -7) abort();655 667 656 668 // And return the user the memory that they allocated … … 800 812 // set a lock, then lock this memblock and its neighbors. at the end of this call, 801 813 // the global mutex is released, but the memBlock and its neighbors are protected 802 //BLOCKSET_LOCK (memBlock, false);803 MUTEX_LOCK(&memBlockListMutex);814 BLOCKSET_LOCK (memBlock, false); 815 // MUTEX_LOCK(&memBlockListMutex); 804 816 805 817 psMemBlock *nextBlock = memBlock->nextBlock; … … 847 859 848 860 // all of the list modifications are done; set the lock and clear the per-memBlock locks 849 //BLOCKSET_UNLOCK(memBlock, false);850 MUTEX_UNLOCK(&memBlockListMutex);861 BLOCKSET_UNLOCK(memBlock, false); 862 // MUTEX_UNLOCK(&memBlockListMutex); 851 863 852 864 // XXX these are not actually guaranteed : another thread may already grab them before we get here … … 872 884 psS32 j = 0; 873 885 psMemBlock *topBlock = lastMemBlockAllocated; 886 887 // XXX move this elsewhere? 888 fprintf (stderr, "set %d locks, cleared %d locks, retry on %d locks (memID %ld)\n", setLock, clearLock, retryLock, memid); 874 889 875 890 // make sure that the memblock list is free of corruption before we crawl … … 1080 1095 // we need a MUTEX_LOCK here: otherwise, two functions can race on refCounter--; 1081 1096 BLOCK_LOCK (memBlock, true); 1082 callL2++;1083 1097 if (memBlock->refCounter > 1) { 1084 1098 memBlock->refCounter--; … … 1089 1103 } 1090 1104 BLOCK_UNLOCK(memBlock, true); 1091 callL2--;1092 1105 return ptr; 1093 1106 } 1094 1107 BLOCK_UNLOCK(memBlock, true); 1095 callL2--;1096 // if (callL2 > +7) abort();1097 // if (callL2 < -7) abort();1098 1108 1099 1109 // we can't invoke freeFunc() while we're holding memBlockListMutex as it -
branches/eam_branches/ipp-20110505/psLib/src/sys/psMemory.h
r31568 r31576 146 146 #endif // ifdef DOXYGEN 147 147 148 void psMemInit(void); 148 149 149 150 /** Set the deallocator routine -
branches/eam_branches/ipp-20110505/psphot/src/psphot.c
r31154 r31576 4 4 int main (int argc, char **argv) { 5 5 6 psMemInit(); 6 7 psTimerStart ("complete"); 7 8 pmErrorRegister(); // register psModule's error codes/messages
Note:
See TracChangeset
for help on using the changeset viewer.
