Edit

Share via


Warning C26105

Lock order violation. Acquiring lock 'lock' with level 'level' causes order inversion.

Remarks

Concurrency SAL supports lock levels. To declare a lock level, which is denoted by a string literal without double quotes, use _Create_lock_level_. You can impose an order of acquisition between two lock levels by using the annotation _Set_lock_level_order_(A,B), which states that locks that have level A must be acquired before locks that have level B. To establish a lock order hierarchy (a partial order among lock levels), use multiple _Set_lock_level_order_ annotations. To associate a lock with a lock level, use the _Set_lock_level_ annotation when you declare the lock. Warning C26105 is issued when a lock ordering violation is detected.

Example

The following example generates warning C26105 because there's a lock order inversion in the function OrderInversion.

_Create_lock_level_(MutexLockLevel);
_Create_lock_level_(TunnelLockLevel);
_Create_lock_level_(ChannelLockLevel);
_Lock_level_order_(MutexLockLevel, TunnelLockLevel);
_Lock_level_order_(TunnelLockLevel, ChannelLockLevel);
_Has_lock_level_(MutexLockLevel) HANDLE gMutex;

struct Tunnel
{
    _Has_lock_level_(TunnelLockLevel) CRITICAL_SECTION cs;
};

struct Channel
{
    _Has_lock_level_(ChannelLockLevel) CRITICAL_SECTION cs;
};

void OrderInversion(Channel* pChannel, Tunnel* pTunnel)
{
    EnterCriticalSection(&pChannel->cs);
    // Warning C26105
    WaitForSingleObject(gMutex, INFINITE);
    EnterCriticalSection(&pTunnel->cs);
    LeaveCriticalSection(&pTunnel->cs);
    LeaveCriticalSection(&pChannel->cs);
}