beckhoff TwinCAT 3 — 12.7.1 CriticalSections

Programming Reference | Multi-task data access synchronization General synchronization options between the TcCOM modules are already described in chapter Module-to- module communication [ } 46] . The following mechanisms were described: Data exchange via the process image (IO mapping). During this process, the cycle transition, and thus TwinCAT, ensures data synchronicity between the sources and targets. Method calls via interfaces in which CriticalSections can be used. ADS, which transfers data in the form of a transport medium and thus ensures synchronicity of the data. Common data area to be protected by a CriticalSection.

Ideally, however, you should try to avoid the need for synchronization. Otherwise, the simplest way is usually the exchange via the process image, which copies the data between the cycles from the output process images to the input process images and thus ensures a consistent state. If this is not sufficient and data has to be accessed from different contexts, one of the following options can be used. It is important to differentiate between the task contexts that are to access the data. In the TwinCAT Runtime, a distinction is made between the Windows kernel mode thread contexts (Windows context for short) and the TwinCAT real-time task contexts (RT context).

During initialization of TwinCAT modules, the IP, PS, SP and PI transitions are executed in the Windows context. The SO and OS transitions are executed in the RT context. The SDK offers appropriate synchronization options for such scenarios. Nevertheless, the TcCOM modules should be designed such that they are independent of each other, at least in the Windows context, and thus do not require synchronization. The transitions in the RT context can use CriticalSections if required. For the CriticalSection and semaphores it applies that in case of a lock the task waits for the release. In the meantime the core is released for other tasks.

With CriticalSections the active task inherits the priority of the waiting task ("Priority Inheritance"). 12.7.1 CriticalSections | CriticalSections Instances of CriticalSections are created through TwinCAT real-time. This instance can be initialized in the Windows context as well as in the RT context. The CriticalSection instance can only be used in the RT context. TwinCAT real-time uses "priority inheritance" to prevent a task with lower priority from indirectly blocking a task with high priority. CCriticalSectionInstance The CCriticalSectionInstance class provides the interface to handle Critical Sections and has the required memory.

To create a Critical Section the class requires the object ID of the TwinCAT real-time instance, which is available via OID_TCRTIME_CTRL, as well as a reference to the TwinCAT object server via a pointer to ITComObjectServer. Methods: CCriticalSectionInstance(OTCID oid=0, ITComObjectServer* ipSrv=NULL); The default constructor that initializes the object ID of the Critical Section Provider. If the pointer is given to the object server, the Critical Section is also initialized. ~CCriticalSectionInstance(); The destructor deletes the Cricital Section instance.

void SetOidCriticalSection(OTCID oid); Sets the Critical Section Provider, which is given by TwinCAT real-time and whose object ID is available via OID_TCRTIME_CTRL. bool HasOidCriticalSection(); Returns TRUE if the object ID is set to a value other than 0, otherwise FALSE. The object ID is not checked to verify whether it belongs to a Critical Section Provider. bool IsInitializedCriticalSection(); Returns TRUE if the Critical Section was successfully initialized. TwinCAT 3 154 Version: 1.19.1