Threads created explicitly by models are called foreign threads. Such threads run in Threaded Context. There are also various API functions that registers callbacks that are called in FTC, like SIM_run_in_thread and SIM_notify_on_socket with the run_in_thread argument set to 1.
Many of the things stated in the preceding section is also relevant to foreign threads. One difference, however, is that foreign threads can be created by models using the Standard Device Model.
The following outlines how a foreign thread can interact with the rest of the simulation:
/* foreign thread */ SIM_ACQUIRE_CELL(obj, &lock); /* safe to access the device */ SIM_RELEASE_CELL(obj, &lock);
static void global_context_callback(void *data) { /* this code runs in Global Context */ } { /* ... Threaded Context ... */ SIM_thread_safe_callback(global_context_callback, data); }
It should be noted that the function posting the callback returns immediately, usually before the callback has started executing. Also, posting a Global Context callback is a relatively expensive operation since it involves stopping all running CPUs.