2.7 Threaded Device Model 2.9 Simics Scheduler
API Reference Manual  /  2 Threading Model  / 

2.8 Foreign Threads

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.

2.8.1 Device Interactions

The following outlines how a foreign thread can interact with the rest of the simulation:

Accessing a Device Object
A foreign thread can enter Cell Context using the SIM_ACQUIRE_CELL function. Once in Cell Context, the thread can interact with the object just like a normal device would do, and without needing any additional locking.
      /* foreign thread */
      SIM_ACQUIRE_CELL(obj, &lock);
      /* safe to access the device */
      SIM_RELEASE_CELL(obj, &lock);
    

Entering Global Context
A foreign thread can post callbacks that are run in Global Context, and hence allowed to access everything in the simulation. This is done using SIM_thread_safe_callback
      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.

2.7 Threaded Device Model 2.9 Simics Scheduler