@@ -24,8 +24,9 @@ private static readonly
2424 private World ? _world ;
2525 private ArraySegment < SparseUpdateMethod > _update ;
2626 private StrongBox < int > ? _counter ;
27+ private Stack < Exception > ? _exceptions ;
2728
28- public static void UnsafeQueueWork ( World world , ArraySegment < SparseUpdateMethod > method , StrongBox < int > counter )
29+ public static void UnsafeQueueWork ( World world , ArraySegment < SparseUpdateMethod > method , StrongBox < int > counter , Stack < Exception > exceptions )
2930 {
3031 if ( method . Count == 0 )
3132 return ;
@@ -42,25 +43,39 @@ public static void UnsafeQueueWork(World world, ArraySegment<SparseUpdateMethod>
4243 workItem . _world = world ;
4344 workItem . _update = method ;
4445 workItem . _counter = counter ;
46+ workItem . _exceptions = exceptions ;
4547
4648 ThreadPool . UnsafeQueueUserWorkItem ( static o =>
4749 {
4850 SparseSetWorkItem workItem = UnsafeExtensions . UnsafeCast < SparseSetWorkItem > ( o ! ) ;
4951
50- Span < SparseUpdateMethod > methods = workItem . _update . AsSpan ( ) ;
51- ComponentSparseSetBase set = methods [ 0 ] . SparseSet ;
52+ try
53+ {
54+ Span < SparseUpdateMethod > methods = workItem . _update . AsSpan ( ) ;
55+ ComponentSparseSetBase set = methods [ 0 ] . SparseSet ;
5256
53- foreach ( SparseUpdateMethod method in methods )
57+ foreach ( SparseUpdateMethod method in methods )
58+ {
59+ int entityId = 0 ;
60+ method . Runner . RunSparse ( set , workItem . _world ! , ref entityId ) ;
61+ }
62+ }
63+ catch ( Exception e )
5464 {
55- int entityId = 0 ;
56- method . Runner . RunSparse ( set , workItem . _world ! , ref entityId ) ;
65+ lock ( workItem . _exceptions ! )
66+ {
67+ workItem . _exceptions . Push ( e ) ;
68+ }
69+ }
70+ finally
71+ {
72+ Interlocked . Decrement ( ref workItem . _counter ! . Value ) ;
5773 }
58-
59- Interlocked . Decrement ( ref workItem . _counter ! . Value ) ;
6074
6175 workItem . _world = default ;
6276 workItem . _update = default ;
6377 workItem . _counter = default ;
78+ workItem . _exceptions = default ;
6479
6580 lock ( s_poolLock )
6681 {
@@ -85,8 +100,9 @@ private static readonly
85100 private Stack < ArchetypeUpdateSpan > ? _archetypes ;
86101 private ArchetypeUpdateMethod [ ] ? _componentStorageBases ;
87102 private StrongBox < int > ? _counter ;
103+ private Stack < Exception > ? _exceptions ;
88104
89- public static void UnsafeQueueWork ( World world , Stack < ArchetypeUpdateSpan > archetypes , ArchetypeUpdateMethod [ ] componentStorageBases , StrongBox < int > counter )
105+ public static void UnsafeQueueWork ( World world , Stack < ArchetypeUpdateSpan > archetypes , ArchetypeUpdateMethod [ ] componentStorageBases , StrongBox < int > counter , Stack < Exception > exceptions )
90106 {
91107 Interlocked . Increment ( ref counter . Value ) ;
92108
@@ -101,34 +117,48 @@ public static void UnsafeQueueWork(World world, Stack<ArchetypeUpdateSpan> arche
101117 workItem . _componentStorageBases = componentStorageBases ;
102118 workItem . _world = world ;
103119 workItem . _counter = counter ;
120+ workItem . _exceptions = exceptions ;
104121
105122 ThreadPool . UnsafeQueueUserWorkItem ( static o =>
106123 {
107124 MultipleArchetypeWorkItem workItem = UnsafeExtensions . UnsafeCast < MultipleArchetypeWorkItem > ( o ! ) ;
108125
109- World world = workItem . _world ! ;
110-
111- while ( workItem . _archetypes ! . TryPop ( out var record ) )
126+ try
112127 {
113- ( Archetype archetype , int start , int count ) = record ;
114-
115- Span < ArchetypeUpdateMethod > methods = workItem . _componentStorageBases . AsSpan ( start , count ) ;
116- ref ComponentStorageRecord storageStart = ref MemoryMarshal . GetArrayDataReference ( archetype . Components ) ;
128+ World world = workItem . _world ! ;
117129
118- foreach ( var method in methods )
130+ while ( workItem . _archetypes ! . TryPop ( out var record ) )
119131 {
120- Debug . Assert ( method . Index < archetype . Components . Length ) ;
132+ ( Archetype archetype , int start , int count ) = record ;
121133
122- method . Runner . RunArchetypical ( Unsafe . Add ( ref storageStart , method . Index ) . Buffer , archetype , world , 0 , archetype . EntityCount ) ;
134+ Span < ArchetypeUpdateMethod > methods = workItem . _componentStorageBases . AsSpan ( start , count ) ;
135+ ref ComponentStorageRecord storageStart = ref MemoryMarshal . GetArrayDataReference ( archetype . Components ) ;
136+
137+ foreach ( var method in methods )
138+ {
139+ Debug . Assert ( method . Index < archetype . Components . Length ) ;
140+
141+ method . Runner . RunArchetypical ( Unsafe . Add ( ref storageStart , method . Index ) . Buffer , archetype , world , 0 , archetype . EntityCount ) ;
142+ }
123143 }
124144 }
125-
126- Interlocked . Decrement ( ref workItem . _counter ! . Value ) ;
145+ catch ( Exception e )
146+ {
147+ lock ( workItem . _exceptions ! )
148+ {
149+ workItem . _exceptions . Push ( e ) ;
150+ }
151+ }
152+ finally
153+ {
154+ Interlocked . Decrement ( ref workItem . _counter ! . Value ) ;
155+ }
127156
128157 workItem . _archetypes = default ;
129158 workItem . _componentStorageBases = default ;
130159 workItem . _world = default ;
131160 workItem . _counter = default ;
161+ workItem . _exceptions = default ;
132162
133163 lock ( s_poolLock )
134164 {
@@ -153,6 +183,7 @@ private static readonly
153183 private ArchetypeUpdateSpan _archetypeRecord ;
154184 private ArchetypeUpdateMethod [ ] ? _components ;
155185 private StrongBox < int > ? _counter ;
186+ private Stack < Exception > ? _exceptions ;
156187 private int _start ;
157188 private int _count ;
158189
@@ -161,6 +192,7 @@ public static void UnsafeQueueWork(
161192 ArchetypeUpdateSpan archetypeUpdateRecord ,
162193 ArchetypeUpdateMethod [ ] componentStorageBases ,
163194 StrongBox < int > counter ,
195+ Stack < Exception > exceptions ,
164196 int start ,
165197 int count )
166198 {
@@ -177,40 +209,54 @@ public static void UnsafeQueueWork(
177209 workItem . _components = componentStorageBases ;
178210 workItem . _world = world ;
179211 workItem . _counter = counter ;
212+ workItem . _exceptions = exceptions ;
180213 workItem . _start = start ;
181214 workItem . _count = count ;
182215
183216 ThreadPool . UnsafeQueueUserWorkItem ( static o =>
184217 {
185218 SingleArchetypeWorkItem frentMultithreadWorkItem = UnsafeExtensions . UnsafeCast < SingleArchetypeWorkItem > ( o ! ) ;
186219
187- World world = frentMultithreadWorkItem . _world ! ;
188- ( Archetype archetype , int start , int count ) = frentMultithreadWorkItem . _archetypeRecord ;
189- Span < ArchetypeUpdateMethod > methods = frentMultithreadWorkItem . _components . AsSpan ( start , count ) ;
220+ try
221+ {
222+ World world = frentMultithreadWorkItem . _world ! ;
223+ ( Archetype archetype , int start , int count ) = frentMultithreadWorkItem . _archetypeRecord ;
224+ Span < ArchetypeUpdateMethod > methods = frentMultithreadWorkItem . _components . AsSpan ( start , count ) ;
225+
226+ int archetypeStart = frentMultithreadWorkItem . _start ;
227+ int archetypeCount = frentMultithreadWorkItem . _count ;
190228
191- int archetypeStart = frentMultithreadWorkItem . _start ;
192- int archetypeCount = frentMultithreadWorkItem . _count ;
229+ ref ComponentStorageRecord storageStart = ref MemoryMarshal . GetArrayDataReference ( archetype . Components ) ;
193230
194- ref ComponentStorageRecord storageStart = ref MemoryMarshal . GetArrayDataReference ( archetype . Components ) ;
231+ foreach ( var method in methods )
232+ {
233+ Debug . Assert ( method . Index < archetype . Components . Length ) ;
195234
196- foreach ( var method in methods )
235+ method . Runner . RunArchetypical (
236+ Unsafe . Add ( ref storageStart , method . Index ) . Buffer ,
237+ archetype ,
238+ world ,
239+ archetypeStart ,
240+ archetypeCount ) ;
241+ }
242+ }
243+ catch ( Exception e )
197244 {
198- Debug . Assert ( method . Index < archetype . Components . Length ) ;
199-
200- method . Runner . RunArchetypical (
201- Unsafe . Add ( ref storageStart , method . Index ) . Buffer ,
202- archetype ,
203- world ,
204- archetypeStart ,
205- archetypeCount ) ;
245+ lock ( frentMultithreadWorkItem . _exceptions ! )
246+ {
247+ frentMultithreadWorkItem . _exceptions . Push ( e ) ;
248+ }
249+ }
250+ finally
251+ {
252+ Interlocked . Decrement ( ref frentMultithreadWorkItem . _counter ! . Value ) ;
206253 }
207-
208- Interlocked . Decrement ( ref frentMultithreadWorkItem . _counter ! . Value ) ;
209254
210255 frentMultithreadWorkItem . _archetypeRecord = default ;
211256 frentMultithreadWorkItem . _components = default ;
212257 frentMultithreadWorkItem . _world = default ;
213258 frentMultithreadWorkItem . _counter = default ;
259+ frentMultithreadWorkItem . _exceptions = default ;
214260
215261 lock ( s_poolLock )
216262 {
@@ -221,4 +267,4 @@ public static void UnsafeQueueWork(
221267 }
222268
223269
224- }
270+ }
0 commit comments