Skip to content

Commit f1f09ef

Browse files
committed
backup
WeakRefBase is now threadsafe and uses an actual weak_ptr to drive its logic, strongrefbase is next.
1 parent 1fed963 commit f1f09ef

6 files changed

Lines changed: 119 additions & 61 deletions

File tree

Engine/source/console/simManager.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ static void initRoot()
290290
gNameDictionary = new SimManagerNameDictionary;
291291
sgStreamingInstance = new SceneStreaming;
292292
sgStreamingInstance->smStreaming = false;
293+
293294
gRootGroup = new SimGroup();
294295
gRootGroup->incRefCount();
295296

@@ -631,20 +632,28 @@ void SimDataBlockGroup::sort()
631632

632633
void SceneStreaming::processTick()
633634
{
634-
if (true)
635+
if (smPendingRegister.empty())
636+
return;
637+
638+
U32 start = Platform::getRealMilliseconds();
639+
640+
while (!smPendingRegister.empty())
635641
{
636-
for (U32 i = 0; i < mMaxObjects && !smPendingRegister.empty(); i++)
637-
{
638-
SimObject* obj = smPendingRegister.first();
639-
smPendingRegister.pop_front();
642+
SimObject* obj = smPendingRegister.first();
643+
smPendingRegister.pop_front();
640644

641-
Sim::gIdDictionary->insert(obj);
645+
Sim::gIdDictionary->insert(obj);
642646

643-
Sim::gNameDictionary->insert(obj);
647+
Sim::gNameDictionary->insert(obj);
644648

645649

646-
if (!obj->onAdd())
647-
obj->unregisterObject();
648-
}
650+
if (!obj->onAdd())
651+
obj->unregisterObject();
652+
653+
U32 now = Platform::getRealMilliseconds();
654+
if ((now - start) >= 2)
655+
break;
656+
649657
}
658+
650659
}

Engine/source/console/simObject.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "scene/sceneObject.h"
4747
#include "T3D/camera.h"
4848
#include "T3D/player.h"
49+
#include "T3D/gameBase/gameConnection.h"
4950

5051
ImplementBitfieldType(GameTypeMasksType,
5152
"The type of animation effect to apply to this material.\n"
@@ -725,9 +726,7 @@ bool SimObject::registerObject()
725726
AssertFatal(Sim::gIdDictionary && Sim::gNameDictionary,
726727
"SimObject::registerObject - tried to register an object before Sim::init()!");
727728

728-
729-
if (true && dynamic_cast<SceneObject*>(this) &&
730-
!(dynamic_cast<Camera*>(this) || dynamic_cast<Player*>(this)) &&
729+
if (Sim::sgStreamingInstance->smStreaming && dynamic_cast<SceneObject*>(this) &&
731730
!gEditingMission
732731
)
733732
{

Engine/source/console/simObject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ class SimObject: public ConsoleObject, public TamlCallbacks
609609

610610
/// @name Events
611611
/// @{
612+
//virtual void onPrepare();
612613

613614
/// Called when the object is added to the sim.
614615
virtual bool onAdd();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "refBase.h"
2+
3+
WeakRefBase::~WeakRefBase()
4+
{
5+
if (mControl)
6+
mControl->object = nullptr;
7+
}
8+
9+
WeakControlBlock::WeakControlBlock(WeakRefBase* obj)
10+
: object(obj)
11+
{
12+
}
13+
14+
WeakControlBlock::~WeakControlBlock()
15+
{
16+
17+
}

Engine/source/core/util/refBase.h

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,53 +29,74 @@
2929
#ifndef _TYPETRAITS_H_
3030
# include "platform/typetraits.h"
3131
#endif
32+
#include <memory>
3233

34+
class WeakRefBase;
35+
36+
struct WeakControlBlock
37+
{
38+
explicit WeakControlBlock(WeakRefBase* obj);
39+
~WeakControlBlock();
40+
41+
WeakRefBase* object;
42+
};
3343

3444
/// Base class for objects which can be weakly referenced
3545
/// (i.e., reference goes away when object is destroyed).
3646
class WeakRefBase
3747
{
3848
public:
3949

40-
/// Weak reference to WeakRefBase.
4150
class WeakReference
4251
{
4352
public:
4453

45-
[[nodiscard]] constexpr WeakRefBase* get() const { return mObject; }
46-
[[nodiscard]] constexpr U32 getRefCount() const { return mRefCount; }
54+
WeakRefBase* get() const
55+
{
56+
auto locked = mWeak.lock();
57+
return locked ? locked->object : NULL;
58+
}
4759

48-
constexpr void incRefCount() { mRefCount++; }
49-
constexpr void decRefCount() {
50-
AssertFatal( mRefCount > 0, "WeakReference - decrementing count of zero!" );
51-
if (--mRefCount==0)
52-
delete this;
60+
uint32_t getRefCount() const
61+
{
62+
return (uint32_t)mWeak.use_count();
5363
}
54-
private:
5564

56-
friend class WeakRefBase;
57-
constexpr explicit WeakReference(WeakRefBase *object) :mObject(object), mRefCount(0) {}
65+
void incRefCount() { /* compatibility no-op */ }
66+
void decRefCount() { /* compatibility no-op */ }
5867

59-
~WeakReference() { AssertFatal(mObject==NULL, "Deleting weak reference which still points at an object."); }
68+
private:
69+
friend class WeakRefBase;
6070

61-
// Object we reference
62-
WeakRefBase *mObject;
71+
explicit WeakReference(const std::shared_ptr<WeakControlBlock>& ctrl)
72+
: mWeak(ctrl) {
73+
}
6374

64-
// reference count for this structure (not WeakObjectRef itself)
65-
U32 mRefCount;
75+
std::weak_ptr<WeakControlBlock> mWeak;
6676
};
6777

6878
public:
69-
constexpr WeakRefBase() : mReference(NULL) {}
70-
virtual ~WeakRefBase() { clearWeakReferences(); }
79+
constexpr WeakRefBase() {}
80+
virtual ~WeakRefBase();
7181

72-
WeakReference* getWeakReference();
82+
WeakReference* getWeakReference()
83+
{
84+
ensureControl();
85+
return new WeakReference(mControl);
86+
}
7387

7488
protected:
75-
void clearWeakReferences();
7689

90+
void ensureControl()
91+
{
92+
if (!mControl)
93+
mControl = std::shared_ptr<WeakControlBlock>(new WeakControlBlock(this));
94+
}
95+
96+
std::shared_ptr<WeakControlBlock> mControl;
7797
private:
78-
WeakReference * mReference;
98+
99+
79100
};
80101

81102
template< typename T > class SimObjectPtr;
@@ -192,7 +213,12 @@ class StrongRefBase : public WeakRefBase
192213
friend class StrongObjectRef;
193214

194215
public:
195-
StrongRefBase() { mRefCount = 0; }
216+
StrongRefBase()
217+
{
218+
mRefCount = 0;
219+
ensureControl();
220+
mStrongControlRef = mControl;
221+
}
196222

197223
U32 getRefCount() const { return mRefCount; }
198224

@@ -209,12 +235,13 @@ class StrongRefBase : public WeakRefBase
209235
void decRefCount()
210236
{
211237
AssertFatal(mRefCount, "Decrementing a reference with refcount 0!");
212-
if(!--mRefCount)
238+
if (!--mRefCount)
213239
destroySelf();
214240
}
215241

216242
protected:
217243
U32 mRefCount; ///< reference counter for StrongRefPtr objects
244+
std::shared_ptr<WeakControlBlock> mStrongControlRef;
218245
};
219246

220247
/// Base class for StrongRefBase strong reference pointers.
@@ -415,27 +442,25 @@ class StrongWeakRefPtr
415442
};
416443

417444
//---------------------------------------------------------------
418-
419-
inline void WeakRefBase::clearWeakReferences()
420-
{
421-
if (mReference)
422-
{
423-
mReference->mObject = NULL;
424-
mReference->decRefCount();
425-
mReference = NULL;
426-
}
427-
}
428-
429-
inline WeakRefBase::WeakReference* WeakRefBase::getWeakReference()
430-
{
431-
if (!mReference)
432-
{
433-
mReference = new WeakReference(this);
434-
mReference->incRefCount();
435-
}
436-
return mReference;
437-
}
438-
445+
//inline void WeakRefBase::clearWeakReferences()
446+
//{
447+
// if (mReference)
448+
// {
449+
// mReference->mObject = NULL;
450+
// mReference->decRefCount();
451+
// mReference = NULL;
452+
// }
453+
//}
454+
//
455+
//inline WeakRefBase::WeakReference* WeakRefBase::getWeakReference()
456+
//{
457+
// if (!mReference)
458+
// {
459+
// mReference = new WeakReference(this);
460+
// mReference->incRefCount();
461+
// }
462+
// return mReference;
463+
//}
439464
//---------------------------------------------------------------
440465

441466
template< typename T >

Engine/source/gui/controls/guiPopUpCtrlEx.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,10 +1651,17 @@ void GuiPopUpMenuCtrlEx::addChildren()
16511651
16521652
void GuiPopUpMenuCtrlEx::removeChildren()
16531653
{
1654-
mTl->deleteObject();
1655-
mSc->deleteObject();
1656-
mSearchEdit->deleteObject();
1657-
mBackground->deleteObject();
1654+
if (mTl && !mTl->isDeleted())
1655+
mTl->deleteObject();
1656+
1657+
if (mSc && !mSc->isDeleted())
1658+
mSc->deleteObject();
1659+
1660+
if (mSearchEdit && !mSearchEdit->isDeleted())
1661+
mSearchEdit->deleteObject();
1662+
1663+
if (mBackground && !mBackground->isDeleted())
1664+
mBackground->deleteObject();
16581665
}
16591666
16601667
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)