From 738991494a863ea74080202e6bc9e869a46f32e9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 14 Jan 2014 11:45:49 +0100 Subject: db/upnp/WorkQueue: initialize "ok" to false, eliminate redundant checks --- src/db/upnp/WorkQueue.hxx | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) (limited to 'src/db/upnp/WorkQueue.hxx') diff --git a/src/db/upnp/WorkQueue.hxx b/src/db/upnp/WorkQueue.hxx index 285fb8c4e..ed58cb170 100644 --- a/src/db/upnp/WorkQueue.hxx +++ b/src/db/upnp/WorkQueue.hxx @@ -75,7 +75,7 @@ public: WorkQueue(const char *_name, size_t hi = 0, size_t lo = 1) :name(_name), high(hi), low(lo), n_workers_exited(0), - ok(true), + ok(false), n_threads(0), threads(nullptr) { } @@ -97,6 +97,7 @@ public: const ScopeLock protect(mutex); assert(nworkers > 0); + assert(!ok); assert(n_threads == 0); assert(threads == nullptr); @@ -110,6 +111,8 @@ public: return false; } } + + ok = true; return true; } @@ -121,10 +124,10 @@ public: { const ScopeLock protect(mutex); - while (IsOK() && high > 0 && queue.size() >= high) { - // Keep the order: we test IsOK() AFTER the sleep... + while (ok && high > 0 && queue.size() >= high) { + // Keep the order: we test ok AFTER the sleep... client_cond.wait(mutex); - if (!IsOK()) + if (!ok) return false; } @@ -163,7 +166,6 @@ public: // Reset to start state. n_workers_exited = 0; - ok = true; } /** Take task from queue. Called from worker. @@ -175,22 +177,15 @@ public: { const ScopeLock protect(mutex); - if (!IsOK()) { + if (!ok) return false; - } - while (IsOK() && queue.size() < low) { + while (ok && queue.size() < low) { if (queue.empty()) client_cond.broadcast(); worker_cond.wait(mutex); - if (!IsOK()) { - // !ok is a normal condition when shutting down - if (IsOK()) { - LOGERR(("WorkQueue::take:%s: cond_wait failed or !ok\n", - name.c_str())); - } + if (!ok) return false; - } } tp = queue.front(); @@ -217,12 +212,6 @@ public: ok = false; client_cond.broadcast(); } - -private: - bool IsOK() - { - return ok && n_threads > 0; - } }; #endif /* _WORKQUEUE_H_INCLUDED_ */ -- cgit v1.2.3