Uploaded image for project: 'WiredTiger'
  1. WiredTiger
  2. WT-10898

Add assertions for owning a spinlock

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • WT11.2.0, 7.1.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Summary

      It would be useful to be able to add assertions about whether we own a spin lock, especially for functions that assume that a specific lock is acquired by the caller.

      Motivation

      A function that requires a specific lock to be acquired by the caller can start with an assertion verifying that the lock is taken and that it is owned by the current session. This has the following advantages:

      • Code readability: The locking protocol is self-documented in the code.
      • Correctness: The locking protocol is enforced (at least on debug builds).

      Additionally, if a lock knows whether it is currently acquired by the current session, we can simplify clean up at the end of functions by implementing a function that unlocks the spin lock only if it is locked by the current session.

      Suggested approach

      The implementation of this can be quite simple:

      1. Add a session ID field to __wt_spinlock, and clear it in __wt_spin_init.
      2. Set the session ID after acquiring the lock in __wt_spin_lock and __wt_spin_trylock. The lock function can even start with an assert checking that the lock is not currently acquired by the current session.
      3. Clear the session ID just before releasing the lock in __wt_spin_unlock.
      4. Create a function or a macro that checks the value of session ID to the value saved in the struct.

      Alternatively, we can store the pointer to the session itself inside the lock.

      Risks

      This adds more work on the critical path, but it should be only two memory stores, which shouldn't need to be atomic.

            Assignee:
            peter.macko@mongodb.com Peter Macko
            Reporter:
            peter.macko@mongodb.com Peter Macko
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: