| 68 | } |
| 69 | |
| 70 | void ObjectsRemovalService::Flush(float dt, float gameDelta) |
| 71 | { |
| 72 | PROFILE_CPU(); |
| 73 | |
| 74 | PoolLocker.Lock(); |
| 75 | PoolCounter = 0; |
| 76 | |
| 77 | // Update timeouts and delete objects that timed out |
| 78 | for (auto i = Pool.Begin(); i.IsNotEnd(); ++i) |
| 79 | { |
| 80 | auto& bucket = *i; |
| 81 | Object* obj = bucket.Key; |
| 82 | const float ttl = bucket.Value - ((obj->Flags & ObjectFlags::UseGameTimeForDelete) != ObjectFlags::None ? gameDelta : dt); |
| 83 | if (ttl <= 0.0f) |
| 84 | { |
| 85 | Pool.Remove(i); |
| 86 | obj->OnDeleteObject(); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | bucket.Value = ttl; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // If any object was added to the pool while removing objects (by this thread) then retry removing any nested objects (but without delta time) |
| 95 | if (PoolCounter != 0) |
| 96 | { |
| 97 | RETRY: |
| 98 | PoolCounter = 0; |
| 99 | for (auto i = Pool.Begin(); i.IsNotEnd(); ++i) |
| 100 | { |
| 101 | if (i->Value <= 0.0f) |
| 102 | { |
| 103 | Object* obj = i->Key; |
| 104 | Pool.Remove(i); |
| 105 | obj->OnDeleteObject(); |
| 106 | } |
| 107 | } |
| 108 | if (PoolCounter != 0) |
| 109 | goto RETRY; |
| 110 | } |
| 111 | |
| 112 | PoolLocker.Unlock(); |
| 113 | } |
| 114 | |
| 115 | bool ObjectsRemoval::Init() |
| 116 | { |