2018-07-23 00:54:28 +00:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
|
|
|
//=============================================================================
|
|
|
|
//
|
2021-03-20 21:46:00 +00:00
|
|
|
// Code available from: https://verilator.org
|
|
|
|
//
|
2022-01-01 13:26:40 +00:00
|
|
|
// Copyright 2012-2022 by Wilson Snyder. This program is free software; you can
|
2020-03-21 15:24:24 +00:00
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
// Version 2.0.
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2018-07-23 00:54:28 +00:00
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
///
|
|
|
|
/// \file
|
2021-03-20 21:46:00 +00:00
|
|
|
/// \brief Verilated thread pool implementation code
|
|
|
|
///
|
|
|
|
/// This file must be compiled and linked against all Verilated objects
|
|
|
|
/// that use --threads.
|
|
|
|
///
|
|
|
|
/// Use "verilator --threads" to add this to the Makefile for the linker.
|
2018-07-23 00:54:28 +00:00
|
|
|
///
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
#include "verilatedos.h"
|
|
|
|
#include "verilated_threads.h"
|
2018-10-14 17:43:24 +00:00
|
|
|
|
2022-03-25 19:46:50 +00:00
|
|
|
#ifdef VL_PROFILER
|
|
|
|
#include "verilated_profiler.h"
|
|
|
|
#endif
|
|
|
|
|
2018-07-23 00:54:28 +00:00
|
|
|
#include <cstdio>
|
2022-01-09 21:49:38 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2018-07-23 00:54:28 +00:00
|
|
|
|
2021-03-05 00:23:40 +00:00
|
|
|
//=============================================================================
|
|
|
|
// Globals
|
|
|
|
|
|
|
|
// Internal note: Globals may multi-construct, see verilated.cpp top.
|
|
|
|
|
2019-10-07 23:27:31 +00:00
|
|
|
std::atomic<vluint64_t> VlMTaskVertex::s_yields;
|
2018-07-23 00:54:28 +00:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
// VlMTaskVertex
|
|
|
|
|
|
|
|
VlMTaskVertex::VlMTaskVertex(vluint32_t upstreamDepCount)
|
2020-08-16 13:55:36 +00:00
|
|
|
: m_upstreamDepsDone{0}
|
|
|
|
, m_upstreamDepCount{upstreamDepCount} {
|
2018-07-23 00:54:28 +00:00
|
|
|
assert(atomic_is_lock_free(&m_upstreamDepsDone));
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
// VlWorkerThread
|
|
|
|
|
2022-03-25 19:46:50 +00:00
|
|
|
VlWorkerThread::VlWorkerThread(uint32_t threadId, VerilatedContext* contextp,
|
|
|
|
VlExecutionProfiler* profilerp)
|
2021-07-25 17:38:27 +00:00
|
|
|
: m_ready_size{0}
|
2020-08-16 13:55:36 +00:00
|
|
|
, m_exiting{false}
|
2022-03-25 19:46:50 +00:00
|
|
|
, m_cthread{startWorker, this, threadId, profilerp}
|
2021-03-07 16:01:54 +00:00
|
|
|
, m_contextp{contextp} {}
|
2018-07-23 00:54:28 +00:00
|
|
|
|
|
|
|
VlWorkerThread::~VlWorkerThread() {
|
|
|
|
m_exiting.store(true, std::memory_order_release);
|
2019-10-07 23:27:31 +00:00
|
|
|
wakeUp();
|
2018-07-23 00:54:28 +00:00
|
|
|
// The thread should exit; join it.
|
|
|
|
m_cthread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VlWorkerThread::workerLoop() {
|
|
|
|
ExecRec work;
|
2020-08-15 14:12:55 +00:00
|
|
|
work.m_fnp = nullptr;
|
2018-07-23 00:54:28 +00:00
|
|
|
|
2020-04-04 02:31:54 +00:00
|
|
|
while (true) {
|
2020-04-04 17:45:24 +00:00
|
|
|
if (VL_LIKELY(!work.m_fnp)) dequeWork(&work);
|
2018-07-23 00:54:28 +00:00
|
|
|
|
|
|
|
// Do this here, not above, to avoid a race with the destructor.
|
2020-04-14 02:51:35 +00:00
|
|
|
if (VL_UNLIKELY(m_exiting.load(std::memory_order_acquire))) break;
|
2018-07-23 00:54:28 +00:00
|
|
|
|
|
|
|
if (VL_LIKELY(work.m_fnp)) {
|
2021-06-13 13:33:11 +00:00
|
|
|
work.m_fnp(work.m_selfp, work.m_evenCycle);
|
2020-08-15 14:12:55 +00:00
|
|
|
work.m_fnp = nullptr;
|
2018-07-23 00:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 19:46:50 +00:00
|
|
|
void VlWorkerThread::startWorker(VlWorkerThread* workerp, uint32_t threadId,
|
|
|
|
VlExecutionProfiler* profilerp) {
|
2021-03-07 16:01:54 +00:00
|
|
|
Verilated::threadContextp(workerp->m_contextp);
|
2022-03-25 19:46:50 +00:00
|
|
|
#ifdef VL_PROFILER
|
|
|
|
// Note: setupThread is not defined without VL_PROFILER, hence the #ifdef. Still, we might
|
|
|
|
// not be profiling execution (e.g.: PGO only), so profilerp might still be nullptr.
|
|
|
|
if (profilerp) profilerp->setupThread(threadId);
|
|
|
|
#endif
|
2021-03-07 16:01:54 +00:00
|
|
|
workerp->workerLoop();
|
|
|
|
}
|
2018-07-23 00:54:28 +00:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
// VlThreadPool
|
|
|
|
|
2022-03-25 19:46:50 +00:00
|
|
|
VlThreadPool::VlThreadPool(VerilatedContext* contextp, int nThreads,
|
|
|
|
VlExecutionProfiler* profiler) {
|
2018-07-23 00:54:28 +00:00
|
|
|
// --threads N passes nThreads=N-1, as the "main" threads counts as 1
|
2022-03-25 19:46:50 +00:00
|
|
|
++nThreads;
|
2021-06-19 02:19:35 +00:00
|
|
|
const unsigned cpus = std::thread::hardware_concurrency();
|
2022-03-25 19:46:50 +00:00
|
|
|
if (cpus < nThreads) {
|
2019-06-15 13:17:51 +00:00
|
|
|
static int warnedOnce = 0;
|
|
|
|
if (!warnedOnce++) {
|
|
|
|
VL_PRINTF_MT("%%Warning: System has %u CPUs but model Verilated with"
|
2020-04-04 17:45:24 +00:00
|
|
|
" --threads %d; may run slow.\n",
|
2022-03-25 19:46:50 +00:00
|
|
|
cpus, nThreads);
|
2019-06-15 13:17:51 +00:00
|
|
|
}
|
2018-07-23 00:54:28 +00:00
|
|
|
}
|
2022-03-25 19:46:50 +00:00
|
|
|
// Create worker threads
|
|
|
|
for (uint32_t threadId = 1; threadId < nThreads; ++threadId) {
|
|
|
|
m_workers.push_back(new VlWorkerThread{threadId, contextp, profiler});
|
2018-07-23 00:54:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VlThreadPool::~VlThreadPool() {
|
2020-11-11 02:40:14 +00:00
|
|
|
// Each ~WorkerThread will wait for its thread to exit.
|
|
|
|
for (auto& i : m_workers) delete i;
|
2018-07-23 00:54:28 +00:00
|
|
|
}
|