Device Driver Tutorial

Locking Rules for Quote Of The Day Version 3

    The locking rules for this qotd_3 driver are as follows:

  1. You must have exclusive access to do any of the following operations. To have exclusive access, you must own the mutex or you must set QOTD_BUSY. Threads must wait on QOTD_BUSY.

    • Test the contents of the storage buffer.

    • Modify the contents of the storage buffer.

    • Modify the size of the storage buffer.

    • Modify variables that refer to the address of the storage buffer.

    1. If your operation does not need to sleep, do the following actions:

    2. Acquire the mutex.

    3. Wait until QOTD_BUSY is cleared. When the thread that set QOTD_BUSY clears QOTD_BUSY, that thread also should signal threads waiting on the condition variable and then drop the mutex.

    4. Perform your operation. You do not need to set QOTD_BUSY before you perform your operation.

    5. Drop the mutex.

    The following code sample illustrates this rule:

    mutex_enter(&qsp->lock);
    while (qsp->flags & QOTD_BUSY) {
            if (cv_wait_sig(&qsp->cv, &qsp->lock) == 0) {
                    mutex_exit(&qsp->lock);
                    ddi_umem_free(new_cookie);
                    return (EINTR);
            }
    }
    memcpy(new_qotd, qsp->qotd, min(qsp->qotd_len, new_len));
    ddi_umem_free(qsp->qotd_cookie);
    qsp->qotd = new_qotd;
    qsp->qotd_cookie = new_cookie;
    qsp->qotd_len = new_len;
    qsp->flags |= QOTD_CHANGED;
    mutex_exit(&qsp->lock);
    1. If your operation must sleep, do the following actions:

    2. Acquire the mutex.

    3. Set QOTD_BUSY.

    4. Drop the mutex.

    5. Perform your operation.

    6. Reacquire the mutex.

    7. Signal any threads waiting on the condition variable.

    8. Drop the mutex.

These locking rules are very simple. These three rules ensure consistent access to the buffer and its metadata. Realistic drivers probably have more complex locking requirements. For example, drivers that use ring buffers or drivers that manage multiple register sets or multiple devices have more complex locking requirements.