Commit 73899149 authored by Max Kellermann's avatar Max Kellermann

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

parent ee4c3ff1
...@@ -75,7 +75,7 @@ public: ...@@ -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: ...@@ -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: ...@@ -110,6 +111,8 @@ public:
return false; return false;
} }
} }
ok = true;
return true; return true;
} }
...@@ -121,10 +124,10 @@ public: ...@@ -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: ...@@ -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,22 +177,15 @@ public: ...@@ -175,22 +177,15 @@ 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();
...@@ -217,12 +212,6 @@ public: ...@@ -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_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment