[niepce] Port to use C++11 concurrency. No more Glib::Threads. No more API breakage.



commit 915741f8f5b6e33f02c1a20b00a82f928025edb9
Author: Hubert Figuière <hub figuiere net>
Date:   Fri Aug 9 21:41:46 2013 -0400

    Port to use C++11 concurrency. No more Glib::Threads. No more API breakage.

 src/engine/db/library.cpp                |    2 +-
 src/fwk/toolkit/notification.hpp         |    7 ++---
 src/fwk/toolkit/notificationcenter.cpp   |    2 +-
 src/fwk/toolkit/notificationcenter.hpp   |    2 +
 src/fwk/utils/db/sqlite/sqlitecnxdrv.cpp |   33 +++++++++++++++--------------
 src/fwk/utils/db/sqlite/sqlitecnxdrv.hpp |    9 +++----
 src/fwk/utils/mtqueue.hpp                |   16 +++++++-------
 src/fwk/utils/thread.cpp                 |   16 ++++++++------
 src/fwk/utils/thread.hpp                 |   15 +++++--------
 src/fwk/utils/worker.hpp                 |   22 ++++++++++---------
 10 files changed, 63 insertions(+), 61 deletions(-)
---
diff --git a/src/engine/db/library.cpp b/src/engine/db/library.cpp
index dfa38a2..08a99e6 100644
--- a/src/engine/db/library.cpp
+++ b/src/engine/db/library.cpp
@@ -91,7 +91,7 @@ void Library::notify(NotifyType t, const boost::any & param)
         DBG_OUT("notif");
         // pass the notification
         fwk::Notification::Ptr n(new fwk::Notification(niepce::NOTIFICATION_LIB));
-        fwk::Notification::mutex_t::Lock lock(n->mutex());
+        std::lock_guard<fwk::Notification::mutex_t> lock(n->mutex());
         LibNotification ln;
         ln.type = t;
         ln.param = param;
diff --git a/src/fwk/toolkit/notification.hpp b/src/fwk/toolkit/notification.hpp
index 6c35932..d6017a8 100644
--- a/src/fwk/toolkit/notification.hpp
+++ b/src/fwk/toolkit/notification.hpp
@@ -25,10 +25,9 @@
 #define __FWK_NOTIFICATION_H__
 
 #include <memory>
+#include <mutex>
 #include <boost/any.hpp>
 
-#include <glibmm/threads.h>
-
 namespace fwk {
 
        /** A notification to post to the notification center */
@@ -36,13 +35,13 @@ namespace fwk {
        {
        public:
                typedef std::shared_ptr<Notification> Ptr;
-         typedef Glib::Threads::RecMutex mutex_t;
+               typedef std::recursive_mutex mutex_t;
 
                Notification(int _type)
                        : m_type(_type)
                        {}
                ~Notification()
-                       { mutex_t::Lock lock(m_mutex); }
+                       { std::lock_guard<mutex_t> lock(m_mutex); }
                mutex_t & mutex() const
                        { return m_mutex; }
                int type() const
diff --git a/src/fwk/toolkit/notificationcenter.cpp b/src/fwk/toolkit/notificationcenter.cpp
index 978e7ae..4ebe47e 100644
--- a/src/fwk/toolkit/notificationcenter.cpp
+++ b/src/fwk/toolkit/notificationcenter.cpp
@@ -74,7 +74,7 @@ void NotificationCenter::_dispatch(void)
     /* this is not pop() like in STL. */
     Notification::Ptr notif( p->m_notificationQueue.pop() );
 
-    Notification::mutex_t::Lock lock(notif->mutex());
+    std::lock_guard<Notification::mutex_t> lock(notif->mutex());
     p->m_subscribers[notif->type()](notif);
 }
 
diff --git a/src/fwk/toolkit/notificationcenter.hpp b/src/fwk/toolkit/notificationcenter.hpp
index 865cd85..be2116d 100644
--- a/src/fwk/toolkit/notificationcenter.hpp
+++ b/src/fwk/toolkit/notificationcenter.hpp
@@ -22,6 +22,8 @@
 #define __FWK_NOTIFICATIONCENTER_H__
 
 #include <memory>
+#include <sigc++/trackable.h>
+#include <sigc++/signal.h>
 
 #include "fwk/toolkit/notification.hpp"
 
diff --git a/src/fwk/utils/db/sqlite/sqlitecnxdrv.cpp b/src/fwk/utils/db/sqlite/sqlitecnxdrv.cpp
index ca47263..2564fac 100644
--- a/src/fwk/utils/db/sqlite/sqlitecnxdrv.cpp
+++ b/src/fwk/utils/db/sqlite/sqlitecnxdrv.cpp
@@ -32,6 +32,7 @@
 #include <string>
 #include <list>
 #include <functional>
+#include <mutex>
 
 #include <sqlite3.h>
 #include "fwk/utils/exception.hpp"
@@ -210,7 +211,7 @@ namespace db { namespace sqlite {
 
                SqliteCnxDrv::SqliteCnxDrv (sqlite3 *a_sqlite_handle)
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (a_sqlite_handle) ;
                        m_priv.reset (new Priv) ;
                        m_priv->sqlite.reset (a_sqlite_handle) ;
@@ -218,7 +219,7 @@ namespace db { namespace sqlite {
 
                SqliteCnxDrv::~SqliteCnxDrv ()
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        close () ;
                        m_priv.reset(NULL);
                }
@@ -234,7 +235,7 @@ namespace db { namespace sqlite {
                const char*
                SqliteCnxDrv::get_last_error () const
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        if (m_priv && m_priv->sqlite) {
                                return sqlite3_errmsg (m_priv->sqlite.get ());
                        }
@@ -265,7 +266,7 @@ namespace db { namespace sqlite {
                bool
                SqliteCnxDrv::execute_statement (const SQLStatement &a_statement)
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+      std::lock_guard<std::recursive_mutex> lock(m_mutex);
 
                  THROW_IF_FAIL (m_priv && m_priv->sqlite) ;
                  DBG_OUT("sql string: %s", a_statement.to_string().c_str()) ;
@@ -369,7 +370,7 @@ namespace db { namespace sqlite {
                bool
                SqliteCnxDrv::should_have_data () const
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
 
                        if (get_number_of_columns () > 0)
@@ -380,7 +381,7 @@ namespace db { namespace sqlite {
                bool
                SqliteCnxDrv::read_next_row ()
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
                        if (m_priv->cur_stmt) {
                                if (m_priv->last_execution_result == SQLITE_DONE) {
@@ -403,7 +404,7 @@ namespace db { namespace sqlite {
                unsigned int
                SqliteCnxDrv::get_number_of_columns () const
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
                        if (!m_priv->cur_stmt)
                                return 0 ;
@@ -414,7 +415,7 @@ namespace db { namespace sqlite {
                SqliteCnxDrv::get_column_content (uint32_t a_offset,
                                                                                  fwk::Buffer 
&a_column_content) const
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
 
                        RETURN_VAL_IF_FAIL (m_priv->check_offset (a_offset), false) ;
@@ -428,7 +429,7 @@ namespace db { namespace sqlite {
                SqliteCnxDrv::get_column_content (uint32_t a_offset,
                                                                                  int32_t &a_column_content) 
const
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
 
                        RETURN_VAL_IF_FAIL (m_priv->check_offset (a_offset), false) ;
@@ -446,7 +447,7 @@ namespace db { namespace sqlite {
                SqliteCnxDrv::get_column_content (uint32_t a_offset,
                                                                                  int64_t &a_column_content) 
const
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
 
                        RETURN_VAL_IF_FAIL (m_priv->check_offset (a_offset), false) ;
@@ -465,7 +466,7 @@ namespace db { namespace sqlite {
                SqliteCnxDrv::get_column_content (uint32_t a_offset,
                                                                                  double& a_column_content) 
const
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
                        RETURN_VAL_IF_FAIL (m_priv->check_offset (a_offset), false) ;
                        int type = sqlite3_column_type (m_priv->cur_stmt, a_offset) ;
@@ -481,7 +482,7 @@ namespace db { namespace sqlite {
                SqliteCnxDrv::get_column_content (uint32_t a_offset,
                                                                                  std::string& 
a_column_content) const
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
 
                        RETURN_VAL_IF_FAIL (m_priv->check_offset (a_offset), false) ;
@@ -505,7 +506,7 @@ namespace db { namespace sqlite {
                SqliteCnxDrv::get_column_type (uint32_t a_offset,
                                                                           enum ColumnType &a_type) const
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
                        RETURN_VAL_IF_FAIL (m_priv->check_offset (a_offset), false) ;
                        int type = sqlite3_column_type (m_priv->cur_stmt, a_offset) ;
@@ -537,7 +538,7 @@ namespace db { namespace sqlite {
                bool
                SqliteCnxDrv::get_column_name (uint32_t a_offset, fwk::Buffer &a_name) const
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
                        RETURN_VAL_IF_FAIL (m_priv->check_offset (a_offset), false) ;
                        const char* name = sqlite3_column_name (m_priv->cur_stmt, a_offset) ;
@@ -550,7 +551,7 @@ namespace db { namespace sqlite {
                void
                SqliteCnxDrv::close ()
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv) ;
 
                        if (m_priv->sqlite) {
@@ -564,7 +565,7 @@ namespace db { namespace sqlite {
                int64_t
                SqliteCnxDrv::last_row_id()
                {
-      Glib::Threads::RecMutex::Lock lock(m_mutex);
+                    std::lock_guard<std::recursive_mutex> lock(m_mutex);
                        THROW_IF_FAIL (m_priv);
                        if (m_priv->sqlite) {
                                return sqlite3_last_insert_rowid(m_priv->sqlite.get());
diff --git a/src/fwk/utils/db/sqlite/sqlitecnxdrv.hpp b/src/fwk/utils/db/sqlite/sqlitecnxdrv.hpp
index 8d01e61..b4d453f 100644
--- a/src/fwk/utils/db/sqlite/sqlitecnxdrv.hpp
+++ b/src/fwk/utils/db/sqlite/sqlitecnxdrv.hpp
@@ -27,9 +27,8 @@
 #ifndef __NEMIVER_SQLITE_CNX_DRV_H__
 #define __NEMIVER_SQLITE_CNX_DRV_H__
 
-#include <boost/scoped_ptr.hpp>
-
-#include <glibmm/threads.h>
+#include <mutex>
+#include <memory>
 
 #include "fwk/utils/db/iconnectiondriver.hpp"
 
@@ -43,8 +42,8 @@ namespace sqlite {
 class SqliteCnxDrv: public db::IConnectionDriver {
     struct Priv ;
     friend class SqliteCnxMgrDrv ;
-    boost::scoped_ptr<Priv> m_priv ;
-    mutable Glib::Threads::RecMutex m_mutex;
+    std::unique_ptr<Priv> m_priv ;
+    mutable std::recursive_mutex m_mutex;
 
     //forbid copy
     SqliteCnxDrv (const SqliteCnxDrv &) ;
diff --git a/src/fwk/utils/mtqueue.hpp b/src/fwk/utils/mtqueue.hpp
index 2a38116..68c9920 100644
--- a/src/fwk/utils/mtqueue.hpp
+++ b/src/fwk/utils/mtqueue.hpp
@@ -23,7 +23,7 @@
 #define __FWK_UTILS_MTQUEUE_H__
 
 #include <deque>
-#include <glibmm/threads.h>
+#include <mutex>
 
 namespace fwk {
 
@@ -36,7 +36,7 @@ namespace fwk {
        class MtQueue
        {
        public:
-         typedef Glib::Threads::RecMutex mutex_t;
+               typedef std::recursive_mutex mutex_t;
                typedef T              value_type;
 
                MtQueue();
@@ -65,13 +65,13 @@ namespace fwk {
        template < class T >
        MtQueue<T>::~MtQueue()
        {
-               mutex_t::Lock lock(m_mutex);            
+               std::lock_guard<mutex_t> lock(m_mutex);
        }
 
        template < class T > void
        MtQueue<T>::add(const T &op)
        {
-               mutex_t::Lock lock(m_mutex);
+               std::lock_guard<mutex_t> lock(m_mutex);
                m_queue.push_back(op);
        }
 
@@ -79,7 +79,7 @@ namespace fwk {
        template < class T > void
        MtQueue<T>::add(T &op)
        {
-               mutex_t::Lock lock(m_mutex);
+               std::lock_guard<mutex_t> lock(m_mutex);
                m_queue.push_back(std::move(op));
        }
 
@@ -87,7 +87,7 @@ namespace fwk {
        T MtQueue<T>::pop()
        {
                T elem;
-               mutex_t::Lock lock(m_mutex);
+               std::lock_guard<mutex_t> lock(m_mutex);
                elem = std::move(m_queue.front());
                m_queue.pop_front();
                return elem;
@@ -96,14 +96,14 @@ namespace fwk {
        template < class T >
        bool MtQueue<T>::empty() const
        {
-               mutex_t::Lock lock(m_mutex);
+               std::lock_guard<mutex_t> lock(m_mutex);
                return m_queue.empty();
        }
 
        template < class T >
        void MtQueue<T>::clear() 
        {
-               mutex_t::Lock lock(m_mutex);
+               std::lock_guard<mutex_t> lock(m_mutex);
                m_queue.clear();
        }
 }
diff --git a/src/fwk/utils/thread.cpp b/src/fwk/utils/thread.cpp
index c39c7b6..c64344e 100644
--- a/src/fwk/utils/thread.cpp
+++ b/src/fwk/utils/thread.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/utils/thread.cpp
  *
- * Copyright (C) 2007-2009 Hubert Figuiere
+ * Copyright (C) 2007-2013 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,28 +17,30 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-
+#include "fwk/base/debug.hpp"
 #include "thread.hpp"
 
 namespace fwk {
 
-
 Thread::Thread()
     : m_terminated(true),
-      m_thrd(NULL)
+      m_thrd(nullptr)
 {
 }
 
-
 Thread::~Thread()
 {
     terminate();
+    delete m_thrd;
 }
 
-
 void Thread::start()
 {
-    m_thrd = Glib::Threads::Thread::create(sigc::mem_fun(*this, &Thread::main));
+    DBG_ASSERT(!m_thrd, "Thread already start. Will cancel.");
+    if(m_thrd) {
+        delete m_thrd;
+    }
+    m_thrd = new std::thread(&Thread::main, this);
 // TODO add this thread to a manager for task management.
 //             thrd->join();
 }
diff --git a/src/fwk/utils/thread.hpp b/src/fwk/utils/thread.hpp
index 0067894..7473820 100644
--- a/src/fwk/utils/thread.hpp
+++ b/src/fwk/utils/thread.hpp
@@ -1,7 +1,7 @@
 /*
- * niepce - fwk/utils/thread.h
+ * niepce - fwk/utils/thread.hpp
  *
- * Copyright (C) 2007-2009 Hubert Figuiere
+ * Copyright (C) 2007-2013 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -23,9 +23,7 @@
 
 
 #include <string>
-
-#include <glibmm/threads.h>
-
+#include <thread>
 
 namespace fwk {
 
@@ -33,20 +31,19 @@ namespace fwk {
 class Thread
 {
 public:
-    /** create the worker for the library whose dir is specified */
     Thread();
     virtual ~Thread();
-    
+
     void terminate()
         { m_terminated = true; }
 protected:
     void start();
     virtual void main() = 0;
     volatile bool m_terminated;
-    Glib::Threads::Thread * thread() const
+    std::thread * thread() const
         { return m_thrd; }
 private:
-    Glib::Threads::Thread *       m_thrd;
+    std::thread *       m_thrd;
 };
 
 }
diff --git a/src/fwk/utils/worker.hpp b/src/fwk/utils/worker.hpp
index dd0196a..fd1cd66 100644
--- a/src/fwk/utils/worker.hpp
+++ b/src/fwk/utils/worker.hpp
@@ -23,6 +23,8 @@
 
 #include <memory>
 #include <string>
+#include <mutex>
+#include <condition_variable>
 
 #include "fwk/utils/thread.hpp"
 #include "fwk/utils/mtqueue.hpp"
@@ -54,8 +56,8 @@ protected:
     queue_t      m_tasks;
 private:
     virtual void execute(const ptr_t & _op) = 0;
-    Glib::Threads::Mutex m_q_mutex;
-    Glib::Threads::Cond m_wait_cond;
+    std::mutex m_q_mutex;
+    std::condition_variable m_wait_cond;
 };
 
 template <class T>
@@ -70,9 +72,9 @@ Worker<T>::~Worker()
 {
     m_tasks.clear();
     {
-        Glib::Threads::Mutex::Lock lock(m_q_mutex);
+        std::lock_guard<std::mutex> lock(m_q_mutex);
         m_terminated = true;
-        m_wait_cond.broadcast();
+        m_wait_cond.notify_all();
     }
     thread()->join();
 }
@@ -88,7 +90,7 @@ void Worker<T>::main()
         {
             // make sure we terminate the thread before we unlock
             // the task queue.
-            Glib::Threads::Mutex::Lock lock(m_q_mutex);
+            std::lock_guard<std::mutex> lock(m_q_mutex);
             if(!m_tasks.empty()) {
                 op = m_tasks.pop();
             }
@@ -98,9 +100,9 @@ void Worker<T>::main()
             execute(op);
         }
 
-        Glib::Threads::Mutex::Lock lock(m_q_mutex);
+        std::unique_lock<std::mutex> lock(m_q_mutex);
         if(m_tasks.empty()) {
-            m_wait_cond.wait(m_q_mutex);
+            m_wait_cond.wait(lock);
         }
     } while(!m_terminated);
 }
@@ -108,15 +110,15 @@ void Worker<T>::main()
 template <class T>
 void Worker<T>::schedule(ptr_t & _op)
 {
-    Glib::Threads::Mutex::Lock lock(m_q_mutex);
+    std::lock_guard<std::mutex> lock(m_q_mutex);
     m_tasks.add(_op);
-    m_wait_cond.broadcast();
+    m_wait_cond.notify_all();
 }
 
 template <class T>
 void Worker<T>::clear()
 {
-    Glib::Threads::Mutex::Lock lock(m_q_mutex);
+    std::lock_guard<std::mutex> lock(m_q_mutex);
     m_tasks.clear();
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]