Changeset View
Changeset View
Standalone View
Standalone View
src/httpserver.cpp
Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | private: | ||||
bool running; | bool running; | ||||
size_t maxDepth; | size_t maxDepth; | ||||
int numThreads; | int numThreads; | ||||
/** RAII object to keep track of number of running worker threads */ | /** RAII object to keep track of number of running worker threads */ | ||||
class ThreadCounter { | class ThreadCounter { | ||||
public: | public: | ||||
WorkQueue &wq; | WorkQueue &wq; | ||||
ThreadCounter(WorkQueue &w) : wq(w) { | explicit ThreadCounter(WorkQueue &w) : wq(w) { | ||||
std::lock_guard<std::mutex> lock(wq.cs); | std::lock_guard<std::mutex> lock(wq.cs); | ||||
wq.numThreads += 1; | wq.numThreads += 1; | ||||
} | } | ||||
~ThreadCounter() { | ~ThreadCounter() { | ||||
std::lock_guard<std::mutex> lock(wq.cs); | std::lock_guard<std::mutex> lock(wq.cs); | ||||
wq.numThreads -= 1; | wq.numThreads -= 1; | ||||
wq.cond.notify_all(); | wq.cond.notify_all(); | ||||
} | } | ||||
}; | }; | ||||
public: | public: | ||||
WorkQueue(size_t _maxDepth) | explicit WorkQueue(size_t _maxDepth) | ||||
: running(true), maxDepth(_maxDepth), numThreads(0) {} | : running(true), maxDepth(_maxDepth), numThreads(0) {} | ||||
/** Precondition: worker threads have all stopped (call WaitExit) */ | /** Precondition: worker threads have all stopped (call WaitExit) */ | ||||
~WorkQueue() {} | ~WorkQueue() {} | ||||
/** Enqueue a work item */ | /** Enqueue a work item */ | ||||
bool Enqueue(WorkItem *item) { | bool Enqueue(WorkItem *item) { | ||||
LOCK(cs); | LOCK(cs); | ||||
if (queue.size() >= maxDepth) { | if (queue.size() >= maxDepth) { | ||||
▲ Show 20 Lines • Show All 564 Lines • Show Last 20 Lines |