← Back to team overview

kicad-developers team mailing list archive

[PATCH] Replace remaining Boost Mutexes with std::mutex

 

This patch series removes the last uses of MUTEX and MUTLOCK from the code
base, so now everything is using std::mutex and std::lock_guard instead of
the Boost provided classes. I have also removed the ki_mutex.h header file
since this is no longer needed (and found an extraneous inclusion in the
process).

This was made against the master branch. It appears the kicad_curl.cpp file
has diverged due to openssl changes between 5.1 and master, which makes
this not apply cleanly to 5.1.

Thanks,
-Ian
From 34e1977ef038d55baf03e73eda772f79d2f4fe9e Mon Sep 17 00:00:00 2001
From: Ian McInerney <Ian.S.McInerney@xxxxxxxx>
Date: Fri, 3 May 2019 18:28:11 +0100
Subject: [PATCH 1/2] Replace remaining Boost mutexs with std::mutex

CHANGED: Replaced all MUTEX types with std::mutex
         Replaced all MUTLOCK types with std::lock_guard
---
 common/common.cpp                  |  6 +++---
 common/kicad_curl/kicad_curl.cpp   | 18 +++++++++---------
 include/gl_context_mgr.h           |  4 ++--
 pcbnew/footprint_preview_panel.cpp | 24 ++++++++++++------------
 4 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/common/common.cpp b/common/common.cpp
index 8f27b8032..0316f28dd 100644
--- a/common/common.cpp
+++ b/common/common.cpp
@@ -34,6 +34,7 @@
 #include <macros.h>
 #include <base_units.h>
 #include <reporter.h>
+#include <mutex>
 
 #include <wx/process.h>
 #include <wx/config.h>
@@ -454,14 +455,13 @@ wxString KIwxExpandEnvVars(const wxString& str)
 }
 
 
-#include <ki_mutex.h>
 const wxString ExpandEnvVarSubstitutions( const wxString& aString )
 {
     // wxGetenv( wchar_t* ) is not re-entrant on linux.
     // Put a lock on multithreaded use of wxGetenv( wchar_t* ), called from wxEpandEnvVars(),
-    static MUTEX    getenv_mutex;
+    static std::mutex getenv_mutex;
 
-    MUTLOCK lock( getenv_mutex );
+    std::lock_guard<std::mutex> lock( getenv_mutex );
 
     // We reserve the right to do this another way, by providing our own member function.
     return KIwxExpandEnvVars( aString );
diff --git a/common/kicad_curl/kicad_curl.cpp b/common/kicad_curl/kicad_curl.cpp
index ea32691b2..2290cb763 100644
--- a/common/kicad_curl/kicad_curl.cpp
+++ b/common/kicad_curl/kicad_curl.cpp
@@ -27,7 +27,7 @@
 // conflicts for some defines, at least on Windows
 #include <kicad_curl/kicad_curl.h>
 
-#include <ki_mutex.h>       // MUTEX and MUTLOCK
+#include <mutex>
 #include <ki_exception.h>   // THROW_IO_ERROR
 
 
@@ -37,14 +37,14 @@
 // client (API) header file.
 static volatile bool s_initialized;
 
-static MUTEX s_lock;        // for s_initialized
+static std::mutex s_lock;        // for s_initialized
 
 // Assume that on these platforms libcurl uses OpenSSL
 #if defined(__linux__) || defined(__MINGW32__)
 
 #include <openssl/crypto.h>
 
-static MUTEX* s_crypto_locks;
+static std::mutex* s_crypto_locks;
 
 /*
  * From OpenSSL v1.1.0, the CRYPTO_set_locking_callback macro is a no-op.
@@ -83,7 +83,7 @@ static void lock_callback( int mode, int type, const char* file, int line )
 
 static void init_locks()
 {
-    s_crypto_locks = new MUTEX[ CRYPTO_num_locks() ];
+    s_crypto_locks = new std::mutex[ CRYPTO_num_locks() ];
 
     // From http://linux.die.net/man/3/crypto_set_id_callback:
 
@@ -154,7 +154,7 @@ void KICAD_CURL::Init()
     // will not need to lock.
     if( !s_initialized )
     {
-        MUTLOCK lock( s_lock );
+        std::lock_guard<std::mutex> lock( s_lock );
 
         if( !s_initialized )
         {
@@ -177,22 +177,22 @@ void KICAD_CURL::Cleanup()
 {
     /*
 
-    Calling MUTLOCK() from a static destructor will typically be bad, since the
+    Calling lock_guard() from a static destructor will typically be bad, since the
     s_lock may already have been statically destroyed itself leading to a boost
     exception. (Remember C++ does not provide certain sequencing of static
     destructor invocation.)
 
-    To prevent this we test s_initialized twice, which ensures that the MUTLOCK
+    To prevent this we test s_initialized twice, which ensures that the lock_guard
     is only instantiated on the first call, which should be from
     PGM_BASE::destroy() which is first called earlier than static destruction.
     Then when called again from the actual PGM_BASE::~PGM_BASE() function,
-    MUTLOCK will not be instantiated because s_initialized will be false.
+    lock_guard will not be instantiated because s_initialized will be false.
 
     */
 
     if( s_initialized )
     {
-        MUTLOCK lock( s_lock );
+        std::lock_guard<std::mutex> lock( s_lock );
 
         if( s_initialized )
         {
diff --git a/include/gl_context_mgr.h b/include/gl_context_mgr.h
index 1d74fefc2..b5812d263 100644
--- a/include/gl_context_mgr.h
+++ b/include/gl_context_mgr.h
@@ -26,7 +26,7 @@
 #define GL_CONTEXT_MANAGER_H
 
 #include <wx/glcanvas.h>
-#include <ki_mutex.h>
+#include <mutex>
 #include <map>
 
 class GL_CONTEXT_MANAGER
@@ -89,7 +89,7 @@ private:
     wxGLContext* m_glCtx;
 
     ///> Lock to prevent unexpected GL context switching.
-    MUTEX m_glCtxMutex;
+    std::mutex m_glCtxMutex;
 
     // Singleton
     GL_CONTEXT_MANAGER();
diff --git a/pcbnew/footprint_preview_panel.cpp b/pcbnew/footprint_preview_panel.cpp
index 5052e81bc..ad3416ff5 100644
--- a/pcbnew/footprint_preview_panel.cpp
+++ b/pcbnew/footprint_preview_panel.cpp
@@ -29,7 +29,7 @@
 #include <math/box2.h>
 #include <class_module.h>
 #include <class_board.h>
-#include <ki_mutex.h>
+#include <mutex>
 #include <draw_frame.h>
 #include <boost/bind.hpp>
 #include <utility>
@@ -51,7 +51,7 @@ class FP_THREAD_IFACE
         /// Retrieve a cache entry by LIB_ID
         OPT<CACHE_ENTRY> GetFromCache( LIB_ID const & aFPID )
         {
-            MUTLOCK lock( m_lock );
+            std::lock_guard<std::mutex> lock( m_lock );
             auto it = m_cachedFootprints.find( aFPID );
 
             if( it != m_cachedFootprints.end() )
@@ -66,7 +66,7 @@ class FP_THREAD_IFACE
          */
         CACHE_ENTRY AddToQueue( LIB_ID const & aEntry )
         {
-            MUTLOCK lock( m_lock );
+            std::lock_guard<std::mutex> lock( m_lock );
 
             CACHE_ENTRY ent = { aEntry, NULL, FPS_LOADING };
             m_cachedFootprints[aEntry] = ent;
@@ -78,7 +78,7 @@ class FP_THREAD_IFACE
         /// Pop an entry from the queue, or empty option if none is available.
         OPT<CACHE_ENTRY> PopFromQueue()
         {
-            MUTLOCK lock( m_lock );
+            std::lock_guard<std::mutex> lock( m_lock );
 
             if( m_loaderQueue.empty() )
             {
@@ -95,7 +95,7 @@ class FP_THREAD_IFACE
         /// Add an entry to the cache.
         void AddToCache( CACHE_ENTRY const & aEntry )
         {
-            MUTLOCK lock( m_lock );
+            std::lock_guard<std::mutex> lock( m_lock );
             m_cachedFootprints[aEntry.fpid] = aEntry;
         }
 
@@ -104,7 +104,7 @@ class FP_THREAD_IFACE
          */
         void SetCurrentFootprint( LIB_ID aFp )
         {
-            MUTLOCK lock( m_lock );
+            std::lock_guard<std::mutex> lock( m_lock );
             m_current_fp = std::move( aFp );
         }
 
@@ -113,7 +113,7 @@ class FP_THREAD_IFACE
          */
         LIB_ID GetCurrentFootprint()
         {
-            MUTLOCK lock( m_lock );
+            std::lock_guard<std::mutex> lock( m_lock );
             return m_current_fp;
         }
 
@@ -122,7 +122,7 @@ class FP_THREAD_IFACE
          */
         void SetPanel( FOOTPRINT_PREVIEW_PANEL* aPanel )
         {
-            MUTLOCK lock( m_lock );
+            std::lock_guard<std::mutex> lock( m_lock );
             m_panel = aPanel;
         }
 
@@ -131,7 +131,7 @@ class FP_THREAD_IFACE
          */
         FOOTPRINT_PREVIEW_PANEL* GetPanel()
         {
-            MUTLOCK lock( m_lock );
+            std::lock_guard<std::mutex> lock( m_lock );
             return m_panel;
         }
 
@@ -141,7 +141,7 @@ class FP_THREAD_IFACE
          */
         bool QueueEvent( wxEvent const& aEvent )
         {
-            MUTLOCK lock( m_lock );
+            std::lock_guard<std::mutex> lock( m_lock );
 
             if( m_panel )
             {
@@ -159,7 +159,7 @@ class FP_THREAD_IFACE
          */
         FP_LIB_TABLE* GetTable()
         {
-            MUTLOCK locK( m_lock );
+            std::lock_guard<std::mutex> locK( m_lock );
             return m_panel ? m_panel->Prj().PcbFootprintLibs() : nullptr;
         }
 
@@ -168,7 +168,7 @@ class FP_THREAD_IFACE
         std::map<LIB_ID, CACHE_ENTRY> m_cachedFootprints;
         LIB_ID m_current_fp;
         FOOTPRINT_PREVIEW_PANEL* m_panel = nullptr;
-        MUTEX m_lock;
+        std::mutex m_lock;
 };
 
 
-- 
2.17.1

From 7c5d8907c98906f0e292c32cceb697fbc4e3466e Mon Sep 17 00:00:00 2001
From: Ian McInerney <Ian.S.McInerney@xxxxxxxx>
Date: Fri, 3 May 2019 18:39:40 +0100
Subject: [PATCH 2/2] Remove ki_mutex.h and associated includes

REMOVED: ki_mutex.h since all mutexes now use std::mutex
---
 Documentation/development/technical_todo.md |  5 +-
 include/footprint_info.h                    |  1 -
 include/ki_mutex.h                          | 56 ---------------------
 3 files changed, 1 insertion(+), 61 deletions(-)
 delete mode 100644 include/ki_mutex.h

diff --git a/Documentation/development/technical_todo.md b/Documentation/development/technical_todo.md
index 9b70ab25b..da5d0cade 100644
--- a/Documentation/development/technical_todo.md
+++ b/Documentation/development/technical_todo.md
@@ -23,9 +23,6 @@ Newer C++ versions include features that make some of our current code unnecessa
 This C++ standard version is already used by KiCad, but much code that pre-dates
 the version switch exists and can be tidied.
 
-* `MUTEX` can use [`std::mutex`](https://en.cppreference.com/w/cpp/thread/mutex)
-  and remove the Boost mutex dependency and the whole `ki_mutex.h` header in
-  favour of `<mutex>`.
 * [`std::auto_ptr`](https://en.cppreference.com/w/cpp/memory/auto_ptr)
   should be changed to [`std::unique_ptr`](https://en.cppreference.com/w/cpp/memory/unique_ptr).
   `auto_ptr` is removed entirely in C++17.
@@ -169,4 +166,4 @@ Most of these will not be available in general distributions until v3.2.
 
 
 [Boost test]: https://github.com/boostorg/test
-[GCC 7]: https://gcc.gnu.org/gcc-7/changes.html
\ No newline at end of file
+[GCC 7]: https://gcc.gnu.org/gcc-7/changes.html
diff --git a/include/footprint_info.h b/include/footprint_info.h
index 0925dbd08..19f133829 100644
--- a/include/footprint_info.h
+++ b/include/footprint_info.h
@@ -34,7 +34,6 @@
 
 #include <import_export.h>
 #include <ki_exception.h>
-#include <ki_mutex.h>
 #include <kicad_string.h>
 #include <sync_queue.h>
 #include <lib_tree_item.h>
diff --git a/include/ki_mutex.h b/include/ki_mutex.h
deleted file mode 100644
index 3eab58e1e..000000000
--- a/include/ki_mutex.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * This program source code file is part of KiCad, a free EDA CAD application.
- *
- * Copyright (C) 2013-2014 SoftPLC Corporation, Dick Hollenbeck <dick@xxxxxxxxxxx>
- * Copyright (C) 1992-2014 KiCad Developers, see CHANGELOG.TXT for contributors.
- *
- * 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 the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, you may find one here:
- * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
- * or you may search the http://www.gnu.org website for the version 2 license,
- * or you may write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
- */
-
-#ifndef KI_MUTEX_H_
-#define KI_MUTEX_H_
-
-
-/// Establish KiCad MUTEX choices here in this file:
-///    typedef MUTEX and typedef MUTLOCK.
-///
-/// Using an unnamed resource is easier, providing a textual name for a
-/// constructor is cumbersome, so we make choice on that criteria mostly:
-
-#if 1
-
-// This is a fine choice between the two, but requires linking to ${Boost_LIBRARIES}
-
-#include <boost/interprocess/sync/interprocess_mutex.hpp>
-#include <boost/interprocess/sync/scoped_lock.hpp>
-
-typedef boost::interprocess::interprocess_mutex     MUTEX;
-typedef boost::interprocess::scoped_lock<MUTEX>     MUTLOCK;
-
-#else
-
-// This choice also works.
-
-#include <wx/thread.h>
-
-typedef wxMutex             MUTEX;
-typedef wxMutexLocker       MUTLOCK;
-
-#endif
-
-#endif  // KI_MUTEX_H_
-- 
2.17.1


Follow ups