-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Catalog and Routing
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Create a class that abstracts the concurrency limited queue behavior present in the range deleter in order to decouple the task scheduling logic from the range deleter.
The class would follow a layout similar to this:
/** * A FIFO queue whose consumers are admitted only while fewer than `limit` previously admitted * tasks are still running. The limit may be changed at any time; a limit of zero admits nothing. */ template <typename T> class ConcurrencyLimitedQueue { public: class Slot { public: explicit Slot(unique_function<void()> release); Slot(Slot&& other) noexcept; ~Slot(); private: unique_function<void()> _release; }; struct Admitted { T task; Slot slot; }; explicit ConcurrencyLimitedQueue(size_t limit); /** * Appends a task to the back of the queue. */ void push(T task); /** * Changes the concurrency limit and wakes any waiter so it re-evaluates admission. */ void setLimit(size_t limit); /** * Blocks until a task is queued and a slot is free, then returns the front task together with * its slot. Throws if the operation context is interrupted. */ Admitted waitForAdmittedTask(OperationContext* opCtx); private: Slot _acquireSlot(); void _releaseSlot(); std::mutex _mutex; stdx::condition_variable _cv; std::queue<T> _queue; size_t _inFlightCount{0}; size_t _limit; };