mongodb/mongo — pull request #622 by jsbattig (2014-01-31 19:21:29+00:00)
Title: Changed spinning code that waits for more data on tailable cursors for event based awakening

Current implementation in tailable cursor imposes on the reader a wait of at least 2ms when cursor is exhausted even if new fresh data is inserted from another session.
When using MongoDB to fuel a service-bus implementation, this imposes a very expensive 2ms penalty on the majority of the requests going thru the bus.

A more scalable model implies using event notification while waiting for more data to avoid this unnecesary wait.

This wait has to be designed in a such a way to avoid imposing a too onerous cost on insertion of documents on collection object but yet achieving the goal of quick awakening of tailing cursors when waiting for more data.

On the collection side important to be noted that I have used an rwlock which is in the list of no-no for kernel, but I think it's justified in this case as it can allow concurrent sessions inserting documents to reduce contention when they notify subscribers.
An exclusive lock is acquired when new cursors register subscribers to wait for new documents when tailing.
There's an optimization on collection object to avoid creating the list<> that contains the subscribers unless it's necesary (first subscriber subscribing to collection).