Advertisement
ffilz

Untitled

Sep 15th, 2021
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. I/O ot get/set attributes:
  2. --------------------------
  3.  
  4. // may block here while waiting for open/upgrade/downgrade/close
  5. take read lock
  6. increment io_counter
  7. release read lock
  8. // no open/ypgrade/downgrade/close can be in progress now
  9. initiate I/O
  10.    
  11.  
  12. on I/O complete (async or immediate:
  13. ------------------------------------
  14.  
  15. if decrement io_counter == 0
  16.     lock mutex
  17.     if io_counter == 0
  18.         signal condition var
  19.     unlock mutex
  20.  
  21. open/upgrade/downgrade/close:
  22. -----------------------------
  23.  
  24. while true
  25.     take write lock
  26.     if io_counter == 0
  27.         // we're safe to proceed, retain the write lock
  28.         re-open fd in new mode or close it
  29.         release write lock
  30.         return 
  31.     release write lock
  32.     lock mutex
  33.     while io_counter != 0
  34.         wait on condition var
  35.     unlock mutex
  36.     continue
  37.  
  38. ===========================================================================
  39. this refinement gets rid of the rw lock in the I/O path and provides a bit
  40. more fairness to open/upgrade/downgrade/close:
  41.  
  42. increment io_counter
  43. if fd_counter != 0
  44.     lock mutex
  45.     if fd_counter != 0
  46.         // fd work is waiting, block until it's done
  47.         if decrement io_counter == 0
  48.             signal condition var
  49.         while fd_counter != 0
  50.             wait on condition var
  51.         // we are done waiting for fd work, so resume I/O
  52.         increment io_counter
  53.     unlock mutex
  54. // now we know no fd work is waiting or in progress and can't start
  55. initiate I/O
  56.  
  57. on I/O complete (async or immediate:
  58. ------------------------------------
  59.  
  60. if decrement io_counter == 0
  61.     lock mutex
  62.     if io_counter == 0
  63.         signal condition var
  64.     unlock mutex
  65.  
  66. open/upgrade/downgrade/close:
  67. -----------------------------
  68.  
  69. increment fd_counter
  70. lock mutex
  71. while io_counter != 0
  72.     // now fd_counter is non-zero and io_counter is zero, any I/O that
  73.     // tries to start will block until fd_counter goes to zero
  74.     wait on cond var
  75.  
  76. // io_counter was zero, and because we checked while holding the mutex, but made
  77. // fd_counter non-zero before taking the mutex any I/O threads that try to start
  78. // after we took the mutex MUST be blocked, proceed
  79. unlock mutex
  80.  
  81. // serialize any open/upgrade/downgrade/close
  82. take write lock
  83. re-open fd in new mode or close it
  84. release write lock
  85.  
  86. // now ready to allow I/O to resume
  87. if decrement fd_counter == 0
  88.     lock mutex
  89.     if fd_counter == 0
  90.         signal cond var
  91.     unlock mutex
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement