Concurrency

现实生活中,常常有一些活动是一起发生的,比如汽车的转弯,转向灯要亮起来,刹车减少引擎功率等同时进行。

SystemC用 simulation processes 来模拟并发,事件驱动仿真器,执行一小段code之后,交出控制权给return control to simulation kernel

1. SystemC如何用C++来模拟进程--- 指定特定的函数

--- 告诉simulation kernel 哪个函数是用来做 simulation process的。叫做进程注册process registration。

--- 用SC_THREAD来进行进程注册。

  SC_THREAD(member_function);

--- SC_THREAD 有一些限制:

--- 1)只能用在SystemC的模块中,因此参数必须是module类的一个成员函数

--- 2)必须只能用在elaboration 阶段,所以放在module的构造函数中可以满足此要求。

--- 3)成员函数必须存在,并且该函数没有参数没有返回值。(参数是void,返回类型是void)

回到控制权的话题,process必须自愿/自动(voluntarily)yield control (放弃/释放控制权),需要两种形式:

---- 执行return 语句,process永远终止。

---- 用wait() 函数,wait() 函数用来临时suspend 该进程,SystemC 继续去执行其他的进程。wait(time_delay,time_units); time_delay是负数是没有意义的。

>> SystemC simulations consist of four major stages: elaboration, initialization, simulation, post-processing.


1) In the elaboration stage, SystemC components(元件/组件) are instantiated and connected to create a model ready for simulation. This stage is also where process registration occurs. The elaboration stage ends with a call to sc_start(), which invokes the simulation kernel and begins the initialization stage.

2) In the initialization stage, the simulation kernel identifies all simulation processes and places them in either the runnable or waiting process set. By default, most simulation processes are placed into the set of runnable processes. Those processes explicitly requesting no initialization (dont_initialize()) are placed into the set of waiting processes.

3) The simulation stage is commonly described as a state machine that schedules processes to run and advances(推进) simulation time. In this simplified simulation stage there are two internal phases: evaluate and advance-time. Simulation begins with evaluate. During  evaluate, all runnable processes are run one at a time. Each process runs until either it executes a wait(time_delay) or a return. The evaluate continues until there are no runnable processes left.  Note that the ordering (顺序) of processes to run is unspecified (未指明的) in the standard. This means that when selecting which process to run from the set of runnable processes, any process may be chosen. Remember, we are simulating concurrency, which means that ideally we would run all of the processes simultaneously(同时的)。however, we have only one processor to actually execute the simulation and so one process at a time is chosen.

Once the set of all runnable processes has been emptied, then control passes to the advance-time phase to advance simulation time. Simulated time is moved forward to the closest time with a scheduled event. Time advancement moves processes waiting for that particular time into the runnable set, allowing the evaluation phase to continue.

4) The post-processing stage do cleanup.

2、实例

/*
   process.h
*/
#include <systemc.h>

SC_MODULE(two_processes){
	void wiper_thread(void); //process
	void blinker_thread(void); //process,void parameter,void return value
	SC_CTOR(two_processes){
		SC_THREAD(wiper_thread); //register process
		SC_THREAD(blinker_thread); //register process
		SC_THREAD(rain); 
		blinker = false;
	}
	sc_event wiper_right_event;
	sc_event wiper_left_event;
	sc_event blinker_on_event;
	sc_event blinker_off_event;
	bool blinker;
	void rain();
	void wipe_left();
	void wipe_right();
};
/*
	main.cpp
*/
#include "process.h"
#include <iostream>
using namespace std;

void two_processes::wiper_thread(){
	while(true){
		wait(wiper_left_event);
		wipe_left();
		wait(wiper_right_event);
		wipe_right();
	}
}

void two_processes::blinker_thread(){
	while(true)
	{
		wait(blinker_on_event);
		blinker = true;
		cout<<"Blink ON"<<endl;
		wait(100,SC_MS);
		wait(blinker_off_event);
		cout<<"Blink OFF"<<endl;
		blinker = false;
		wait(100,SC_MS);
	}
}
void two_processes::wipe_left()
{
	cout<<"The wipe direction is left."<<endl;
}

void two_processes::wipe_right()
{
	cout<<"The wipe direction is right."<<endl;
}

void two_processes::rain()
{
	wiper_left_event.notify(SC_ZERO_TIME);
	wait(100,SC_MS);
	blinker_on_event.notify(SC_ZERO_TIME);
	wiper_right_event.notify(SC_ZERO_TIME);	
	wait(100,SC_MS);
	blinker_off_event.notify(SC_ZERO_TIME);	
}
int sc_main(int sc_argc,char *argv[])
{
	two_processes process("");
	sc_time t(10,SC_SEC);
	sc_start(t);
	system("pause");
	return 0;
}

输出:

注意wait只能用在process中。

int sc_main(int sc_argc,char *argv[])
{
	two_processes process("two_test_process");
	sc_time t(10,SC_SEC);
	sc_start(t);
	cout<< "module name is "<<process.name()<<endl;
	system("pause");
	return 0;
}

输出:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值