scc 2025.09
SystemC components library
async_thread.h
1#pragma once
2
3#include <atomic>
4#include <future>
5#include <scc/report.h>
6#include <sysc/communication/sc_prim_channel.h>
7#include <sysc/kernel/sc_simcontext.h>
8#include <systemc>
9#include <thread>
10
11namespace scc {
12struct async_thread : sc_core::sc_prim_channel {
13
14 async_thread() = default;
15
16 explicit async_thread(const char* nm)
17 : sc_core::sc_prim_channel{nm} {}
18
19 ~async_thread() {
20 if(active)
21 t1.detach();
22 }
23
24 void start(std::function<sc_core::sc_time()> const& f) {
25 SCCTRACE(SCMOD) << "Starting new thread";
26 t1 = std::move(std::thread([this, f]() {
27 try {
28 finish_time.store(f().value());
29 async_request_update();
30 } catch(std::future_error& e) {
31 }
32 }));
33 active = true;
34 }
35
36 const sc_core::sc_event& thread_finish_event() const { return finish_event; }
37
38private:
39 void update() override {
40 t1.join();
41 auto end_time = sc_core::sc_time::from_value(finish_time.load());
42 finish_event.notify(end_time > sc_core::sc_time_stamp() ? end_time - sc_core::sc_time_stamp() : sc_core::SC_ZERO_TIME);
43 active = false;
44 SCCTRACEALL(SCOBJ) << "Finished execution of thread";
45 }
46 std::thread t1;
47 sc_core::sc_event finish_event;
48 std::atomic<uint64_t> finish_time;
49 bool active{false};
50};
51} // namespace scc
SCC TLM utilities.