SystemC 等待异步事件解决方案

2024-04-19 02:44

本文主要是介绍SystemC 等待异步事件解决方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文为实现 SystemC 响应异步事件 解决方案。

应用场景:

SystemC是一个支持系统事务级、行为级建模的开源的C++ library;

我们将SystemC仿真的模拟叫做模拟器。在很多场景下,模拟器要保持alive,等待异步async事件,做出对应的处理。例如设计一个SystemC消费者模拟器,而生产者程序不属于SystemC仿真范畴,消费者模拟器需要一直保持等待,并在出现数据后进行处理。

         世界上没有东西是完美的啊,倒不如说,同时拥有光明和阴影才是完美的,这样的你才是真正的你。。

                                                                                       ------ 大家好啊 我是 暮冬Z羡慕

以上应用场景应当很常见,但是无论中文网站搜索、SystemC社区、谷歌搜索、Stack Overflow等,都没有合适的解决方案。笔者在综合了解相关问题及做了不少尝试后,给出了较为合适的解决方案。感兴趣的伙伴可以查看以下相关帖子:

Example of main thread controlling sub_thread(systemc module) to complete instructions - SystemC Language - Accellera Systems Initiative Forums

How to make a "single-only" sc_thread wait for a notify() from external host thread - SystemC Language - Accellera Systems Initiative Forums

https://workspace.accellera.org/document/dl/10932

async_request_update example - SystemC Language - Accellera Systems Initiative Forums

c++ - async_request_update() example in SystemC - Stack Overflow

这里是解决方案:

#include <systemc.h>
#include <pthread.h>
#include <unistd.h>using namespace std;
class ThreadSafeEventIf : public sc_interface {virtual void notify(sc_time delay = SC_ZERO_TIME) = 0;virtual const sc_event &default_event(void) const = 0;protected:virtual void update(void) = 0;
};class ThreadSafeEvent : public sc_prim_channel, public ThreadSafeEventIf {public:ThreadSafeEvent(const char *name = ""): event(name) {}void notify(sc_time delay = SC_ZERO_TIME) {this->delay = delay;async_request_update();}const sc_event &default_event(void) const {return event;}protected:virtual void update(void) {event.notify(delay);}sc_event event;sc_time delay;
};sc_event GenScEvent;
sc_event workingFinishEvent;  // finish event
int workingFlag = 0;  // maybe dnot need a lockSC_MODULE(Foo) {public:SC_CTOR(Foo) {SC_THREAD(main);SC_METHOD(eventTriggered);sensitive << threadSafeEvent;dont_initialize();SC_METHOD(stopTriggered);sensitive << threadStopEvent;dont_initialize();}private:void main() {   //extra forever thread to avoid simulation exitwhile (true) {usleep(0.05*1000*1000);  // check if there is any instruction every one sec.wait(SC_ZERO_TIME);if(workingFlag){    // check workingwait(workingFinishEvent);  // wait the working finish }usleep(0.05*1000*1000);}}void eventTriggered() {GenScEvent.notify();}void stopTriggered(){sc_stop();}public:ThreadSafeEvent threadSafeEvent;ThreadSafeEvent threadStopEvent;
};void* PollingThread(void* arg) {int cnt = 0;Foo *foo = (Foo*)(arg);while (cnt<3) {cnt++;printf("[POLL]: %d: Before generating event from PollingThread \n", cnt);usleep(3*1000*1000);foo->threadSafeEvent.notify();printf("[POLL]: %d: Event notified from PollingThread \n", cnt);}usleep(5*1000*1000);foo->threadStopEvent.notify();
};class sc_top : public sc_module {private:SC_HAS_PROCESS(sc_top);public:sc_top(sc_module_name name="SCTOP"): sc_module(name) {SC_THREAD(processing_thread);}void processing_thread(){int cnt =0;while (true) {printf("[PROC]: processing_thread called \n");cout << "[PROC]: Wait GenScEvent  time: " << sc_time_stamp();wait(GenScEvent);workingFlag = 1;cnt++;wait(10, SC_SEC);  // advance simulation timecout << "[PROC]: Process and Finish "<<cnt << " GenScEvent   time: " << sc_time_stamp();workingFinishEvent.notify();workingFlag = 0;}}
};int sc_main(int argc, char *argv[]) {Foo foo("foo");sc_top u_sc_top("u_sc_top");pthread_t thread;pthread_create(&thread, NULL, PollingThread, &foo); sc_start();return 0;
}

CMakeLists.txt

cmake_minimum_required (VERSION 3.5)
project (demo)
include_directories (${PROJECT_SOURCE_DIR}/include)find_library(SystemC_LIB systemc HINTS ${PROJECT_SOURCE_DIR}/lib)
set (syc_LIST ${PROJECT_SOURCE_DIR}/src/syc.cpp)
add_executable (syc ${syc_LIST})
find_package(Threads REQUIRED)
target_link_libraries (syc ${SystemC_LIB} Threads::Threads)

以上代码实现了 :

1.主线程中运行 SystemC仿真模型,子线程中运行异步触发程序 (也可以根据自己的需要反过来,子线程中运行SystemC仿真模型,主线程运行触发程序。)

2.子线程每隔3秒触发一次SystemC仿真模型,主线程中的SystemC进行响应。

3.子线程主动触发三次之后,睡眠5秒,告知SystemC仿真结束。

结果:

   SystemC 2.3.3-Accellera --- Mar 12 2024 15:33:04Copyright (c) 1996-2018 by all Contributors,ALL RIGHTS RESERVED
[POLL]: 1: Before generating event from PollingThread 
[PROC]: processing_thread called 
[PROC]: Wait GenScEvent  time: 0 s[POLL]: 1: Event notified from PollingThread 
[POLL]: 2: Before generating event from PollingThread 
[PROC]: Process and Finish 1 GenScEvent   time: 10 s[PROC]: processing_thread called 
[PROC]: Wait GenScEvent  time: 10 s[POLL]: 2: Event notified from PollingThread 
[POLL]: 3: Before generating event from PollingThread 
[PROC]: Process and Finish 2 GenScEvent   time: 20 s[PROC]: processing_thread called 
[PROC]: Wait GenScEvent  time: 20 s[POLL]: 3: Event notified from PollingThread 
[PROC]: Process and Finish 3 GenScEvent   time: 30 s[PROC]: processing_thread called 
[PROC]: Wait GenScEvent  time: 30 s
Info: /OSCI/SystemC: Simulation stopped by user.

这篇关于SystemC 等待异步事件解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/916452

相关文章

嵌入式Linux驱动中的异步通知机制详解

《嵌入式Linux驱动中的异步通知机制详解》:本文主要介绍嵌入式Linux驱动中的异步通知机制,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、异步通知的核心概念1. 什么是异步通知2. 异步通知的关键组件二、异步通知的实现原理三、代码示例分析1. 设备结构

MySQL主从同步延迟问题的全面解决方案

《MySQL主从同步延迟问题的全面解决方案》MySQL主从同步延迟是分布式数据库系统中的常见问题,会导致从库读取到过期数据,影响业务一致性,下面我将深入分析延迟原因并提供多层次的解决方案,需要的朋友可... 目录一、同步延迟原因深度分析1.1 主从复制原理回顾1.2 延迟产生的关键环节二、实时监控与诊断方案

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结

Windows Docker端口占用错误及解决方案总结

《WindowsDocker端口占用错误及解决方案总结》在Windows环境下使用Docker容器时,端口占用错误是开发和运维中常见且棘手的问题,本文将深入剖析该问题的成因,介绍如何通过查看端口分配... 目录引言Windows docker 端口占用错误及解决方案汇总端口冲突形成原因解析诊断当前端口情况解

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

《SpringBoot循环依赖原理、解决方案与最佳实践(全解析)》循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,:本文主要介绍SpringBoot循环依赖原理、解决方案与最... 目录一、循环依赖的本质与危害1.1 什么是循环依赖?1.2 核心危害二、Spring的三级缓存机制2.1 三

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程