db/upnp/WorkQueue: initialize "ok" to false, eliminate redundant checks

This commit is contained in:
Max Kellermann
2014-01-14 11:45:49 +01:00
parent ee4c3ff1b8
commit 738991494a

View File

@@ -75,7 +75,7 @@ public:
WorkQueue(const char *_name, size_t hi = 0, size_t lo = 1) WorkQueue(const char *_name, size_t hi = 0, size_t lo = 1)
:name(_name), high(hi), low(lo), :name(_name), high(hi), low(lo),
n_workers_exited(0), n_workers_exited(0),
ok(true), ok(false),
n_threads(0), threads(nullptr) n_threads(0), threads(nullptr)
{ {
} }
@@ -97,6 +97,7 @@ public:
const ScopeLock protect(mutex); const ScopeLock protect(mutex);
assert(nworkers > 0); assert(nworkers > 0);
assert(!ok);
assert(n_threads == 0); assert(n_threads == 0);
assert(threads == nullptr); assert(threads == nullptr);
@@ -110,6 +111,8 @@ public:
return false; return false;
} }
} }
ok = true;
return true; return true;
} }
@@ -121,10 +124,10 @@ public:
{ {
const ScopeLock protect(mutex); const ScopeLock protect(mutex);
while (IsOK() && high > 0 && queue.size() >= high) { while (ok && high > 0 && queue.size() >= high) {
// Keep the order: we test IsOK() AFTER the sleep... // Keep the order: we test ok AFTER the sleep...
client_cond.wait(mutex); client_cond.wait(mutex);
if (!IsOK()) if (!ok)
return false; return false;
} }
@@ -163,7 +166,6 @@ public:
// Reset to start state. // Reset to start state.
n_workers_exited = 0; n_workers_exited = 0;
ok = true;
} }
/** Take task from queue. Called from worker. /** Take task from queue. Called from worker.
@@ -175,23 +177,16 @@ public:
{ {
const ScopeLock protect(mutex); const ScopeLock protect(mutex);
if (!IsOK()) { if (!ok)
return false; return false;
}
while (IsOK() && queue.size() < low) { while (ok && queue.size() < low) {
if (queue.empty()) if (queue.empty())
client_cond.broadcast(); client_cond.broadcast();
worker_cond.wait(mutex); worker_cond.wait(mutex);
if (!IsOK()) { if (!ok)
// !ok is a normal condition when shutting down
if (IsOK()) {
LOGERR(("WorkQueue::take:%s: cond_wait failed or !ok\n",
name.c_str()));
}
return false; return false;
} }
}
tp = queue.front(); tp = queue.front();
queue.pop(); queue.pop();
@@ -217,12 +212,6 @@ public:
ok = false; ok = false;
client_cond.broadcast(); client_cond.broadcast();
} }
private:
bool IsOK()
{
return ok && n_threads > 0;
}
}; };
#endif /* _WORKQUEUE_H_INCLUDED_ */ #endif /* _WORKQUEUE_H_INCLUDED_ */