process and event

1、Processes 进程

SystemC simulation processes are simply C++ functions designated by the programmer to be used as processes. You simply tell the simulation kernel which functions are to be used as simulation processes. The action is known as process registration(进程注册). The simulation kernel then schedules and calls each of these functions as needed.

---- SC_METHOD(a_method); 

Execute its body from the beginning to the end. Invoked multiple times, Can not be suspended.

---- SC_THREAD(b_method);

Invoked only once, can be suspended using wait(). Systemc thread simulation processes typically begin execution at the start of simulation and continue in an endless loop until the simulation ends.

Thread processes are started once; if they terminate, they cannot be restarted.

Thread processes are required to periodically return control to (释放控制权给仿真内核)the simulation kernel, allowing other processes to run.

A thread process returns control to the simulation kernel by executing a wait() method call specifying an event or a time-out. Each time a thread returns control to the simulation kernel, its execution state is saved, which lets the process be resumed when the wait() returns.

---- SC_THREAD has a few restrictions:

>> It can be used only within a SystemC module, hence the function must be a member function (成员函数) of the module class.

>> It must be used only during the elaboration stage, and placing SC_THREAD in the module's constructor meets this requirement.

>> The member function must exist and the function can take no arguments and return no values.(无参数,无返回值)

---- Processes must voluntarily(自愿的) yield control. Yielding control may take one of two forms:

>> Simulation processes yield control by executing a return. For  the processes registered with SC_THREAD, executing return is uninteresting (无趣的,无意义的) because it means the process has ended permanently(永久的).

>>  The other form is calling SystemC's wait() function. The wait() function suspends the process temporarily while SystemC proceeds to execute other processes, and then the function resumes by returning.

2、Events and Notification:triggering events 时间触发和通知

---- Event-driven simulator

---- Happens at a specific instant in time 特定时间点

---- Has no value and no duration 没有持续时间,没有值

====================================================

---- Immediate notification

	sc_event a_event;
	a_event.notify();

---- Delayed notification: next cycle

	sc_event b_event;
	b_event.notify(SC_ZERO_TIME);

---- Timed notification

	sc_event c_event;
	c_event.notify(10,SC_NS);
	c_event.notify(5,SC_NS);

====================================================

----- Event queue: Multiple events

---- sc_event_queue action; no immediate notification

----- Sensitivity:敏感列表,Catching events for processes

---- Static Sensitivity

	//must follow process registration
	sensitive << event1 [<< event2]...;

---- Dynamic Sensitivity

SC_METHOD():next_trigger()

SC_THREAD():wait()

	next_trigger(time); //经过多少时间再次触发
	next_trigger(event1); //event1事件发生时触发
	next_trigger(event1 & event2); //event1事件和event2事件都发生时触发
	wait(10,SC_NS); //can suspend,10ns后继续执行

3、Examples 1

#include <systemc.h>

SC_MODULE(turn_of_event){
	SC_CTOR(turn_of_event){
		SC_THREAD(turn_knob_thread); //steering dirction
		SC_THREAD(turn_signal_on_thread);//signal light on
		SC_THREAD(turn_signal_off_thread);//signal light off
		SC_THREAD(turn_signal_right_thread);//signal light right
		SC_THREAD(turn_signal_left_thread);//signal light left
		signal = false;
	}
	sc_core::sc_event signal_on,signal_off,signal_right,signal_left;
	sc_core::sc_event knob_right,knob_left;
	void turn_knob_thread(void);
	void turn_signal_on_thread(void);
	void turn_signal_off_thread(void);
	void turn_signal_right_thread(void);
	void turn_signal_left_thread(void);
	bool signal;
};

turn_of_event.cpp

#include "turn_of_event.h"
#include <iostream>
using namespace sc_core;
using namespace std;

//多个线程不可改同一个全局变量,除非用volatile
void turn_of_event::turn_knob_thread(){
	enum cmds {right = 'R',left = 'L'};
	char cmd;
	wait(SC_ZERO_TIME);
	for(;;){
		std::cout<<"turn knob cmds['R'/'L']\n";
		cin >> cmd;
		switch(cmd){
		case 'R':		
			{
				if(!signal) 
				{
					signal = true;
					signal_on.notify();
				}
				knob_right.notify(SC_ZERO_TIME);
				wait(signal_right);
				cout<<"the right light is blinking!\n";
				break;
			}
		case 'L':
			{
				if(!signal){
					signal = true;
					signal_on.notify();
				} 
				knob_left.notify(SC_ZERO_TIME);
				wait(signal_left);
				cout<<"the left light is blinking!\n";
				break;
			}
		default:
			if(signal)  
				signal_off.notify();
			else 
				cout<<"signal already off!"<<endl;
			wait(SC_ZERO_TIME);
			signal = false;
			break;
		}
	}
}
void turn_of_event::turn_signal_on_thread(void){
	while(1){
	wait(signal_on);
	cout<<"signal oning!!\n";
	}
}
void turn_of_event::turn_signal_off_thread(void){
	while(1)
	{
		wait(signal_off);
		cout<<"signal offing!!\n";
	}
}
void turn_of_event::turn_signal_right_thread(void){
	while(1){
		wait(knob_right);
		cout<<"knob right event is triggered!"<<endl;
		signal_right.notify(SC_ZERO_TIME);
	}
}
void turn_of_event::turn_signal_left_thread(void){
	while(1){
		wait(knob_left);
		cout<<"knob left event is triggered!"<<endl;
		signal_left.notify(SC_ZERO_TIME);
	}
}


int sc_main(int sc_argc,char * argv[])
{
	turn_of_event turn_event("");
	sc_start(60,SC_NS);
	system("pause");
	return 0;
}

运行后结果:

4、Examples 2

turn_of_event.h volatile可有,可无

#include <systemc.h>

SC_MODULE(turn_of_event){
	SC_CTOR(turn_of_event){
		SC_THREAD(turn_knob_thread); //steering dirction
		SC_THREAD(turn_signal_on_thread);//signal light on
		SC_THREAD(turn_signal_off_thread);//signal light off
		SC_THREAD(turn_signal_right_thread);//signal light right
		SC_THREAD(turn_signal_left_thread);//signal light left
		signal = false;
	}
	sc_core::sc_event signal_on,signal_off,signal_right,signal_left;
	sc_core::sc_event knob_right,knob_left;
	void turn_knob_thread(void);
	void turn_signal_on_thread(void);
	void turn_signal_off_thread(void);
	void turn_signal_right_thread(void);
	void turn_signal_left_thread(void);
	volatile bool signal;//this variable is volatile,so read from memory
};
turn_of_event.cpp
#include "turn_of_event.h"
#include <iostream>
using namespace sc_core;
using namespace std;

//多个线程不可改同一个全局变量
void turn_of_event::turn_knob_thread(){
	enum cmds {right = 'R',left = 'L'};
	char cmd;
	wait(SC_ZERO_TIME);
	for(;;){
		std::cout<<"turn knob cmds['R'/'L']\n";
		cin >> cmd;
		switch(cmd){
		case 'R':		
			{
				if(!signal) 
				{
					signal_on.notify();				
				}
				wait(SC_ZERO_TIME);
				while(!signal);
				knob_right.notify(SC_ZERO_TIME);
				wait(signal_right);
				cout<<"the right light is blinking!\n";
				break;
			}
		case 'L':
			{
				if(!signal){
					signal_on.notify();
				} 
				wait(SC_ZERO_TIME);
				while(!signal);
				knob_left.notify(SC_ZERO_TIME);
				wait(signal_left);
				cout<<"the left light is blinking!\n";
				break;
			}
		default:
			if(signal)  
				signal_off.notify();
			else 
				cout<<"signal already off!"<<endl;		
			wait(SC_ZERO_TIME);
			while(signal);
			break;
		}
	}
}
void turn_of_event::turn_signal_on_thread(void){
	while(1){
	wait(signal_on);
	signal = true;
	cout<<"signal oning!!\n";
	}
}
void turn_of_event::turn_signal_off_thread(void){
	while(1)
	{
		wait(signal_off);
		signal = false;
		cout<<"signal offing!!\n";
	}
}
void turn_of_event::turn_signal_right_thread(void){
	while(1){
		wait(knob_right);
		cout<<"knob right event is triggered!"<<endl;
		signal_right.notify(SC_ZERO_TIME);
	}
}
void turn_of_event::turn_signal_left_thread(void){
	while(1){
		wait(knob_left);
		cout<<"knob left event is triggered!"<<endl;
		signal_left.notify(SC_ZERO_TIME);
	}
}


int sc_main(int sc_argc,char * argv[])
{
	turn_of_event turn_event("");
	sc_start(60,SC_NS);
	system("pause");
	return 0;
}

运行后:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于EDA, EDP领域是本很好的入门书。通过一个比较实际的应用例子,让读者体会到在某些领域,以事件为中心建模的解决方案是更好的方法。更自然、更松耦合,更具扩展性。 Part 1. 没啥可说的。与request-response pattern interaction做了对比。EDA大部分应用场景应该也是分布式环境,所以它的特点包括了分布式系统的特点。 Part 2. 从过程式编程的角度来理解,做个对比。如果将EPN(Network)比作一个函数的话,Event Producer是函数参数,Consumer是返回值(多返回值)。而每个EPA就是一个表达式。Context是临时数据,而Global State就是全局数据。差别就在于EPN中元素带有时间次序。简单地说就是用EPA来表达业务逻辑对Event编程。 本书最核心的是Flower Store例子,需要仔细体会,学习设计思路。。。具体CEP系统的实现细节、适用场景基本是没有的。各种EPP Language对复杂事件的表达能力比较是没有的。所以只能定位为入门书了。 Unlike traditional information systems which work by issuing requests and waiting for responses, event-driven systems are designed to process events as they occur, allowing the system to observe, react dynamically, and issue personalized data depending on the recipient and situation. Event Processing in Action introduces the major concepts of event-driven architectures and shows how to use, design, and build event processing systems and applications. Written for working software architects and developers, the book looks at practical examples and provides an in-depth explanation of their architecture and implementation. Since patterns connect the events that occur in any system, the book also presents common event-driven patterns and explains how to detect and implement them. Throughout the book, readers follow a comprehensive use case that incorporates all event processing programming styles in practice today.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值