Check And Set (CAS)

CAS, meaning "Check and Set" or "Compare and Swap," is used by the DRI for the fast locking it uses. The DRM_CAS macro is located in xc/programs/Xserver/hw/xfree86/os-support/xf86drm.h and is really Check and Set. It takes a pointer to the lock, the possible old value, the new value, and a place to store the result to. Then, atomically, the lock is checked for the old value and set to the new value if equal. If this passes, the return value is set to 1, else 0.

The DRM's locking is based on this. To lock from userland, a CAS is tried with the old value being the context ID and the new value being the context ID ORed with DRM_LOCK_HELD. If this passes, then the client holds the lock. If not, then drmGetLock is called, which goes into the kernel, tries to get the lock, and sleeps on the lock if it's held.