ffilz

Untitled

Sep 16th, 2021 (edited)
183
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. this refinement gets rid of the rw lock in the I/O path and provides a bit
  2. more fairness to open/upgrade/downgrade/close and uses dec_and_lock to
  3. better prevent spurious wakeup due to a race that is not habdled above
  4.  
  5. increment io_counter
  6. if fd_counter != 0
  7.     lock mutex
  8.     if fd_counter != 0
  9.         // fd work is waiting, block until it's done
  10.         if decrement io_counter == 0
  11.             signal condition var
  12.         while fd_counter != 0
  13.             wait on condition var
  14.         // we are done waiting for fd work, so resume I/O
  15.         increment io_counter
  16.     unlock mutex
  17. // now we know no fd work is waiting or in progress and can't start
  18. initiate I/O
  19.  
  20. on I/O complete (async or immediate):
  21. -------------------------------------
  22.  
  23. if decrement_and_lock io_counter
  24.     if fd_counter != 0
  25.         signal condition var
  26.     unlock mutex
  27.  
  28. open/upgrade/downgrade/close:
  29. -----------------------------
  30.  
  31. increment fd_counter
  32. lock mutex
  33. while io_counter != 0
  34.     // now fd_counter is non-zero and io_counter is zero, any I/O that
  35.     // tries to start will block until fd_counter goes to zero
  36.     wait on cond var
  37.  
  38. // io_counter was zero, and because we checked while holding the mutex, but made
  39. // fd_counter non-zero before taking the mutex any I/O threads that try to start
  40. // after we took the mutex MUST be blocked, proceed
  41. unlock mutex
  42.  
  43. // serialize any open/upgrade/downgrade/close
  44. take write lock
  45. re-open fd in new mode or close it
  46. release write lock
  47.  
  48. // now ready to allow I/O to resume
  49. if decrement_and_lock fd_counter
  50.     if fd_counter == 0
  51.         signal cond var
  52.     unlock mutex
  53.  
  54.  
RAW Paste Data