A process that attempts to use a locked object will return an error such as PMERR_HDC_BUSY. Although a device context is owned by a single application or process, the owner can access the device context (DC) through multiple threads. The presentation driver must provide a mechanism whereby a DC can register that it is busy and block access from other threads. In its simplest form, this is performed by the EnterDriver and LeaveDriver routines, which are called at the start and end of each function-handling routine in the presentation driver.
An example of a typical EnterDriver routine for a display driver is as follows:
/***************** Typical EnterDriver Routine ***********************/ enter_driver() { do { /* Lock DC for exclusive use of the current thread */ SemEnter(Device); /* Some functions do not pass a Device Context (DC)*/ /* handle */ if (hdc == NULL) return(SUCCESS); /* Check validity of the passed DC handle */ if (hdc == ERROR) { WinSetErrorInfo (SEVERITY_ERROR, PMERR_INV_HDC); SemLeave(Device); return(ERROR); } /* DC region must be validated before */ /* driver draws into it. */ if (hdc_is_not_dirty) /* Test the HDC_IS_DIRTY flag. */ /* If the flag is set, the DC */ return(SUCCESS); /* must be recalculated by the system */ SemLeave(Device); /* Unlock DC. Call back to engine */ /* to force DC calculation. */ VisRegionNotify(hdc); /* Loop back to reset lock and recheck*/ } while (TRUE); }