MCPcopy Create free account
hub / github.com/alliedmodders/sourcemod / ThreadMain

Method ThreadMain

core/logic/Database.cpp:439–487  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

437}
438
439void DBManager::ThreadMain()
440{
441 std::unique_lock<std::mutex> lock(m_Lock);
442
443 while (true) {
444 // The lock has been acquired. Grab everything we can out of the
445 // queue. Since we want to flush the queue even if we're terminated,
446 // we process all operations we can before checking to terminate.
447 // There's no risk of starvation since the main thread blocks on us
448 // terminating.
449 auto queue = &m_OpQueue.GetLikelyQueue();
450 if (queue->empty()) {
451 // If the queue is empty and we've been asked to stop, leave now.
452 if (m_Terminate)
453 return;
454
455 // Otherwise, wait for something to happen.
456 m_QueueEvent.wait(lock);
457 continue;
458 }
459
460 IDBThreadOperation *op = queue->first();
461 queue->pop();
462
463 // Unlock the queue when we run the query, so the main thread can
464 // keep pumping events. We re-acquire the lock to check for more
465 // items. It's okay if we terminate while unlocked; the main
466 // thread would be blocked and we'd need to flush the queue
467 // anyway, so after we've depleted the queue here, we'll just
468 // reach the terminate at the top of the loop.
469 lock.unlock();
470 op->RunThreadPart();
471
472 // Re-acquire the lock and give the data back to the main thread
473 // immediately. We use a separate lock to minimize game thread
474 // contention.
475 {
476 std::lock_guard<std::mutex> think_lock(m_ThinkLock);
477 m_ThinkQueue.push(op);
478 }
479
480 // Note that we add a 20ms delay after processing a query. This is
481 // questionable but the intent is to avoid starving the game thread.
482 if (!m_Terminate)
483 std::this_thread::sleep_for(20ms);
484
485 lock.lock();
486 }
487}
488
489void DBManager::RunFrame()
490{

Callers

nothing calls this directly

Calls 4

emptyMethod · 0.80
popMethod · 0.80
RunThreadPartMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected