1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
#include "UnityPrefix.h"
#include "BatchDeleteObjects.h"
#include "Runtime/Threads/Thread.h"
#include "Runtime/BaseClasses/BaseObject.h"
#include "Runtime/Mono/MonoIncludes.h"
#include "Runtime/Profiler/Profiler.h"
#include "Runtime/Threads/ThreadedStreamBuffer.h"
PROFILER_INFORMATION(gBatchDeleteObjects, "BatchDeleteObjects", kProfilerLoading)
PROFILER_INFORMATION(gBatchDeleteObjectsThread, "BatchDeleteObjectsThread", kProfilerLoading)
// @TODO: On Angrybots on OS X. We spent 4ms deleting objects in singlethreaded mode on the main thread and 1ms in multithreaded mode on the main thread.
#define MULTI_THREADED_DELETE 0
// Some classes do not support being deallocated on the main thread.
bool DoesClassRequireMainThreadDeallocation (int classID)
{
if (classID == ClassID(Shader) || classID == ClassID(PhysicMaterial) || classID == ClassID(Mesh) ||
classID == ClassID(Texture) || classID == ClassID(Texture2D) || classID == ClassID(Texture3D) || classID == ClassID(Cubemap) )
return true;
else
return false;
}
#if MULTI_THREADED_DELETE
#define kTerminateInstruction reinterpret_cast<Object*> (0x1)
struct BatchDeleteManager;
static BatchDeleteManager* gBatchDeleteManager = NULL;
static void* BatchDeleteStep2Threaded (void* userData);
struct BatchDeleteManager
{
Thread thread;
ThreadedStreamBuffer streamBuffer;
///////@TODO: Handle when we are out of ring buffer memory.
BatchDeleteManager ()
: streamBuffer (ThreadedStreamBuffer::kModeThreaded, 1024 * 256 * sizeof(Object*) )
{
}
};
void InitializeBatchDelete ()
{
gBatchDeleteManager = UNITY_NEW(BatchDeleteManager, kMemGarbageCollector);
gBatchDeleteManager->thread.SetName ("BatchDeleteObjects");
gBatchDeleteManager->thread.Run(BatchDeleteStep2Threaded, mono_domain_get());
}
void CleanupBatchDelete ()
{
// Send terminate instruction & wait for thread to complete
gBatchDeleteManager->streamBuffer.WriteValueType<Object*> (kTerminateInstruction);
gBatchDeleteManager->streamBuffer.WriteSubmitData ();
gBatchDeleteManager->thread.WaitForExit();
UNITY_DELETE(gBatchDeleteManager, kMemGarbageCollector);
}
BatchDelete CreateBatchDelete (size_t size)
{
size_t allocationSize = sizeof(Object*)*size;
void* objectArray = gBatchDeleteManager->streamBuffer.GetWriteDataPointer (allocationSize, sizeof(Object*));
BatchDelete batchInfo;
batchInfo.reservedObjectCount = size;
batchInfo.objectCount = 0;
batchInfo.objects = reinterpret_cast<Object**> (objectArray);
return batchInfo;
}
static void* BatchDeleteStep2Threaded (void* userData)
{
// Attach mono thread
MonoThread* thread = mono_thread_attach((MonoDomain*)userData);
ThreadedStreamBuffer& threadedStreamBuffer = gBatchDeleteManager->streamBuffer;
while (true)
{
Object* object = threadedStreamBuffer.ReadValueType<Object*> ();
// Terminate instruction. Stop the thread.
if (object == kTerminateInstruction)
return NULL;
// Delete the object
if (object != NULL)
delete_object_internal_step2 (object);
threadedStreamBuffer.ReadReleaseData ();
}
// Detach mono thread
mono_thread_detach(thread);
return NULL;
}
void SharkBeginRemoteProfiling ();
void SharkEndRemoteProfiling ();
/// Callbacks like ScriptableObject.OnDestroy etc must be called before invoking this function.
void BatchDeleteObjectInternal (const SInt32* unloadObjects, int size)
{
if (size == 0)
return;
PROFILER_AUTO(gBatchDeleteObjects, NULL)
BatchDelete batch = CreateBatchDelete (size);
int destroyObjectIndex = 0;
for (int i=0;i<size;i++)
{
Object* object = Object::IDToPointer(unloadObjects[i]);
batch.objects[destroyObjectIndex] = object;
}
batch.objectCount = destroyObjectIndex;
CommitBatchDelete(batch);
}
void CommitBatchDelete (BatchDelete& batchDelete)
{
Assert(batchDelete.reservedObjectCount >= batchDelete.objectCount);
LockObjectCreation();
for (int i=0;i<batchDelete.objectCount;i++)
{
Object* object = batchDelete.objects[i];
if (object == NULL)
continue;
delete_object_internal_step1 (object);
int classID = object->GetClassID();
if (DoesClassRequireMainThreadDeallocation (classID))
{
bool requiresThreadCleanup = object->MainThreadCleanup ();
/// DoesClassRequireMainThreadDeallocation does not agree with MainThreadCleanup
/// Fix DoesClassRequireMainThreadDeallocation or MainThreadCleanup for that class.
Assert(requiresThreadCleanup);
}
}
for (int i=batchDelete.objectCount;i<batchDelete.reservedObjectCount;i++)
batchDelete.objects[i] = NULL;
UnlockObjectCreation();
gBatchDeleteManager->streamBuffer.WriteSubmitData ();
}
#else
BatchDelete CreateBatchDelete (size_t size)
{
BatchDelete batch;
batch.objects = (Object**)UNITY_MALLOC(kMemGarbageCollector, sizeof(Object*)*size);
batch.reservedObjectCount = size;
batch.objectCount = 0;
return batch;
}
void CommitBatchDelete (BatchDelete& batchDelete)
{
PROFILER_AUTO(gBatchDeleteObjectsThread, NULL)
Assert(batchDelete.reservedObjectCount >= batchDelete.objectCount);
LockObjectCreation();
for (int i=0;i<batchDelete.objectCount;i++)
{
Object* object = batchDelete.objects[i];
if (object != NULL)
{
delete_object_internal_step1 (object);
delete_object_internal_step2 (object);
}
}
UnlockObjectCreation();
// Cleanup temp storage
UNITY_FREE(kMemGarbageCollector, batchDelete.objects);
}
void BatchDeleteObjectInternal (const SInt32* unloadObjects, int size)
{
PROFILER_AUTO(gBatchDeleteObjectsThread, NULL)
BatchDelete batchInfo = CreateBatchDelete (size);
batchInfo.objectCount = size;
for (int i=0;i<size;i++)
batchInfo.objects[i] = Object::IDToPointer(unloadObjects[i]);
CommitBatchDelete (batchInfo);
}
void InitializeBatchDelete ()
{
}
void CleanupBatchDelete ()
{
}
#endif
|