From: Arvin Schnell Date: Fri, 20 Jul 2012 15:18:56 +0000 (+0200) Subject: - work on dbus interface X-Git-Tag: v0.1.3~200 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=31c2c9d82277bddac3e9aaf076ea2c6dd21986ae;p=thirdparty%2Fsnapper.git - work on dbus interface --- diff --git a/server/Client.cc b/server/Client.cc index 6a4ba94c..f02dd542 100644 --- a/server/Client.cc +++ b/server/Client.cc @@ -32,16 +32,20 @@ bool contains(const ListType& l, const Type& value) Client::Client(const string& name) - : name(name) + : name(name), stop(false) { - c = new boost::condition_variable; - t = NULL; } Client::~Client() { - // TODO + boost::unique_lock lock(mutex); + stop = true; + lock.unlock(); + + condition.notify_all(); + + thread.join(); // TODO this can block } @@ -96,32 +100,34 @@ Client::has_lock(const string& config_name) const void Client::add_task(DBus::Connection& conn, DBus::Message& msg) { - if (!t) - t = new boost::thread(boost::bind(&Client::worker, this)); + if (thread.get_id() == boost::thread::id()) + thread = boost::thread(boost::bind(&Client::worker, this)); - boost::unique_lock l(m); + boost::unique_lock lock(mutex); tasks.push(Task(conn, msg)); - l.unlock(); + lock.unlock(); - c->notify_one(); + condition.notify_one(); } - void Client::worker() { while (true) { - boost::unique_lock l(m); + boost::unique_lock lock(mutex); + + while (tasks.empty() && !stop) + condition.wait(lock); - while (tasks.empty()) - c->wait(l); + if (stop) + break; - Task task = tasks.front(); - tasks.pop(); + Task task = tasks.front(); + tasks.pop(); - l.unlock(); + lock.unlock(); dispatch(task.conn, task.msg); } diff --git a/server/Client.h b/server/Client.h index 2f171a10..149ac733 100644 --- a/server/Client.h +++ b/server/Client.h @@ -120,10 +120,10 @@ public: DBus::Message msg; }; - boost::condition_variable* c; - boost::mutex m; - boost::thread* t; - + boost::condition_variable condition; + boost::mutex mutex; + boost::thread thread; + bool stop; queue tasks; void add_task(DBus::Connection& conn, DBus::Message& msg);