| 146 | } |
| 147 | |
| 148 | void MainThreadManager::endBatch() { |
| 149 | size_t flushId; |
| 150 | |
| 151 | // See if we are the last endBatch() call, otherwise decrement the batchCount |
| 152 | { |
| 153 | std::lock_guard<std::mutex> lockGuard(_mutex); |
| 154 | SC_ASSERT(_batchCount > 0, "Unbalanced beginBatch() endBatch() call"); |
| 155 | if (_batchCount > 1) { |
| 156 | // Only the last endBatch() call will flush the TaskQueue |
| 157 | _batchCount--; |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | flushId = _batchFlushId; |
| 162 | // Increase flush id sequence, so that if there is a beginBatch() call |
| 163 | // that occurs while we are flushing the tasks of this batch, the tasks |
| 164 | // will be flushed on the next main thread tick. |
| 165 | _flushIdSequence++; |
| 166 | } |
| 167 | |
| 168 | // Flush all pending tasks. |
| 169 | flushTasksWithId(flushId); |
| 170 | |
| 171 | // Disable batching |
| 172 | std::lock_guard<std::mutex> lockGuard(_mutex); |
| 173 | SC_ASSERT(_batchCount == 1, "Unbalanced beginBatch() endBatch() call"); |
| 174 | _batchCount = 0; |
| 175 | |
| 176 | if (flushId != _batchFlushId) { |
| 177 | // Our batch flush id changed, which means that a main thread batch task |
| 178 | // has been enqueued while we were flushing the tasks. We should flush |
| 179 | // them on the next main thread tick. |
| 180 | scheduleFlush(_batchFlushId, false); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void MainThreadManager::onIdle(const Ref<ValueFunction>& callback) { |
| 185 | std::lock_guard<std::mutex> lockGuard(_mutex); |
no outgoing calls