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:

ADS_E_INVALIDPARM

Invalid Critical Section Provider

ADS_E_NOINTERFACE

The object ID is set to a reference that is not a Critical Section Provider, i.e. ITcRTime is not implemented.

E_FAIL

Internal error from the Critical Section Provider.

EnterCriticalSection() and LeaveCriticalSection() must be called in the RT context, otherwise the following return value is returned:

ADS_E_INVALIDCONTEXT

Return value if Critical Section is entered outside the RT context. The Critical Section is not entered.

If these methods are used without initializing the Critical Section, S_OK is returned without further action. All other methods can be used in the RT context as well as in the Windows context.

In the CCriticalSectionInstance class, the EnterCriticalSection() can be called multiple times by the same task.  Leave() must then be called just as often.

CriticalSections allow nested calls and require an associated LeaveCriticalSection() call for each EnterCriticalSection() call. The Critical Section is released when the last LeaveCritcalSection() is called.

Sample:

The sample shows how to use a CriticalSection to prevent concurrent access to a date via an interface method call.

Critical Section Concurrent

The CCriticalSectionInstanceConcurrent class allows concurrent access. This can be used if several tasks have read access to the data to be protected, but all other accesses must be prevented in the case of write access.

Methods:

ADS_E_INVALIDPARM

Invalid Critical Section Provider

ADS_E_NOINTERFACE

The object ID is set to a reference that is not a Critical Section Provider, i.e. ITcRTime is not implemented.

E_FAIL

Internal error from the Critical Section Provider.

EnterCriticalSection(), EnterCriticalSectionConcurrent() and LeaveCriticalSection() must be called in the RT context, otherwise the following return value is returned:

ADS_E_INVALIDCONTEXT

Return value if Critical Section is entered outside the RT context. The Critical Section is not entered.


If these methods are used without initializing the Critical Section, S_OK is returned without further action. All other methods can be used in the RT context as well as in the Windows context.

For the CCriticalSectionInstanceConcurrent class, it is not allowed for the EnterCriticalSection() method to be called multiple times by the same task if the concurrent parameter is greater than 1 during initialization.