Skip to content

Global state in SystemC #8

@nmeum

Description

@nmeum

I am currently working on a specialized SystemC project where I want to restart the SystemC simulation process multiple times. I have a working proof of concept which does this by providing a custom main function in which I reset the global SystemC simulation context (sc_core::sc_curr_simcontext) and call sc_core::sc_elab_and_sim in a loop. As an example, consider the following code:

int main(int argc, char **argv) {
        // …

	for (i = 0; !exit_simulation; i++) {
		printf("\n##\n# %zuth execution\n##\n", i);

		// Reset SystemC simulation context
		if (sc_core::sc_curr_simcontext)
			delete sc_core::sc_curr_simcontext;
		sc_core::sc_curr_simcontext = NULL;

		if ((ret = sc_core::sc_elab_and_sim(argc, argv)))
			return ret;
	}

        // …
}

I am aware that this code relies on specific implementation details of the sc_get_curr_simcontext() function and is thus not conforming to IEEE Std 1666-2001. In fact, the code above only works because sc_get_curr_simcontext() currently creates a new simulation context if the global sc_core::sc_curr_simcontext is NULL:

if( sc_curr_simcontext == 0 ) {
sc_default_global_context = new sc_simcontext;
sc_curr_simcontext = sc_default_global_context;
}

Apart from relying on implementation-defined behaviour, an additional problem with the code above is that SystemC contains global variables which are independent of the simulation context. Some of these variables even hold references to the simulation context which cannot be updated and thus cause a double free on program termination. One example of such variables is the sc_event::none variable which was added in 55da81d:

// never notified event
static const sc_event none;

As sc_event::none is a global variable, it is initialized on program startup and holds a reference to a simulation context (created through the invocation of sc_get_curr_simcontext() by the sc_event constructor). This reference is not updated by the loop from above, in fact in cannot be updated as the m_simc member of sc_event is private. An additional global variable with the same problem is sc_process_handle::non_event:

sc_event sc_process_handle::non_event( sc_event::kernel_event );

I am currently considering working on a modified version of SystemC which addresses issue related to such global variables. I created this issue to figure out if there is an interest in reducing global state in SystemC and if patches in this regard would be accepted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions