Improve documentation of MPMCQueue size and sizeGuess methods v2016.09.26.00
authorEli Pozniansky <elip@fb.com>
Fri, 23 Sep 2016 20:08:23 +0000 (13:08 -0700)
committerFacebook Github Bot 5 <facebook-github-bot-5-bot@fb.com>
Fri, 23 Sep 2016 20:23:29 +0000 (13:23 -0700)
Summary: See title.

Reviewed By: nbronson

Differential Revision: D3914188

fbshipit-source-id: dd9ccd0c48911632d229ae675cc40d835ea14724

folly/MPMCQueue.h

index 2bf89c5301e41b4bde6d87a5108652db3f04e419..02a2bf12ac268be2ada6916a9bd391a5d31e63b5 100644 (file)
@@ -696,9 +696,15 @@ class MPMCQueueBase<Derived<T, Atom, Dynamic>> : boost::noncopyable {
     delete[] slots_;
   }
 
-  /// Returns the number of successful reads minus the number of successful
-  /// writes.  Waiting blockingRead and blockingWrite calls are included,
-  /// so this value can be negative.
+  /// Returns the number of writes (including threads that are blocked waiting
+  /// to write) minus the number of reads (including threads that are blocked
+  /// waiting to read). So effectively, it becomes:
+  /// elements in queue + pending(calls to write) - pending(calls to read).
+  /// If nothing is pending, then the method returns the actual number of
+  /// elements in the queue.
+  /// The returned value can be negative if there are no writers and the queue
+  /// is empty, but there is one reader that is blocked waiting to read (in
+  /// which case, the returned size will be -1).
   ssize_t size() const noexcept {
     // since both pushes and pops increase monotonically, we can get a
     // consistent snapshot either by bracketing a read of popTicket_ with
@@ -737,7 +743,11 @@ class MPMCQueueBase<Derived<T, Atom, Dynamic>> : boost::noncopyable {
   }
 
   /// Returns is a guess at size() for contexts that don't need a precise
-  /// value, such as stats.
+  /// value, such as stats. More specifically, it returns the number of writes
+  /// minus the number of reads, but after reading the number of writes, more
+  /// writers could have came before the number of reads was sampled,
+  /// and this method doesn't protect against such case.
+  /// The returned value can be negative.
   ssize_t sizeGuess() const noexcept {
     return writeCount() - readCount();
   }