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