| 2563 | #if NP_ENABLE_THREAD_CHECKS |
| 2564 | |
| 2565 | NpScene::StartWriteResult::Enum NpScene::startWrite(bool allowReentry) |
| 2566 | { |
| 2567 | PX_COMPILE_TIME_ASSERT(sizeof(ThreadReadWriteCount) == 4); |
| 2568 | |
| 2569 | if (mScene.getFlags() & PxSceneFlag::eREQUIRE_RW_LOCK) |
| 2570 | { |
| 2571 | ThreadReadWriteCount localCounts(TlsGetValue(mThreadReadWriteDepth)); |
| 2572 | |
| 2573 | if (mBetweenFetchResults) |
| 2574 | return StartWriteResult::eIN_FETCHRESULTS; |
| 2575 | |
| 2576 | // ensure we already have the write lock |
| 2577 | return localCounts.writeLockDepth > 0 ? StartWriteResult::eOK : StartWriteResult::eNO_LOCK; |
| 2578 | } |
| 2579 | |
| 2580 | { |
| 2581 | ThreadReadWriteCount localCounts(TlsGetValue(mThreadReadWriteDepth)); |
| 2582 | StartWriteResult::Enum result; |
| 2583 | |
| 2584 | if (mBetweenFetchResults) |
| 2585 | result = StartWriteResult::eIN_FETCHRESULTS; |
| 2586 | |
| 2587 | // check we are the only thread reading (allows read->write order on a single thread) and no other threads are writing |
| 2588 | else if (mConcurrentReadCount != localCounts.readDepth || mConcurrentWriteCount != localCounts.writeDepth) |
| 2589 | result = StartWriteResult::eRACE_DETECTED; |
| 2590 | |
| 2591 | else |
| 2592 | result = StartWriteResult::eOK; |
| 2593 | |
| 2594 | // increment shared write counter |
| 2595 | Ps::atomicIncrement(&mConcurrentWriteCount); |
| 2596 | |
| 2597 | // in the normal case (re-entry is allowed) then we simply increment |
| 2598 | // the writeDepth by 1, otherwise (re-entry is not allowed) increment |
| 2599 | // by 2 to force subsequent writes to fail by creating a mismatch between |
| 2600 | // the concurrent write counter and the local counter, any value > 1 will do |
| 2601 | localCounts.writeDepth += allowReentry ? 1 : 2; |
| 2602 | TlsSetValue(mThreadReadWriteDepth, localCounts.getData()); |
| 2603 | |
| 2604 | if (result != StartWriteResult::eOK) |
| 2605 | Ps::atomicIncrement(&mConcurrentErrorCount); |
| 2606 | |
| 2607 | return result; |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | void NpScene::stopWrite(bool allowReentry) |
| 2612 | { |
no test coverage detected