From 24d8f16ce9c4ba5f2c09f68fff66e0f46383b6c1 Mon Sep 17 00:00:00 2001 From: Conlain Kelly Date: Wed, 18 Jul 2018 13:44:53 -0400 Subject: Cleanup, add some cmake support --- src/3rd_party/CMakeLists.txt | 4 +- .../connection_handler/connection_handler_impl.h | 2 +- .../connection_handler/src/connection.cc | 2 +- .../src/connection_handler_impl.cc | 2 +- src/components/include/utils/lock.h | 3 + src/components/include/utils/lock_posix.h | 184 --------------------- src/components/utils/CMakeLists.txt | 4 +- src/components/utils/src/lock_posix.cc | 149 ----------------- src/components/utils/test/lock_posix_test.cc | 123 -------------- 9 files changed, 11 insertions(+), 462 deletions(-) delete mode 100644 src/components/include/utils/lock_posix.h delete mode 100644 src/components/utils/src/lock_posix.cc delete mode 100644 src/components/utils/test/lock_posix_test.cc diff --git a/src/3rd_party/CMakeLists.txt b/src/3rd_party/CMakeLists.txt index ae9267e5bb..b72d86f902 100644 --- a/src/3rd_party/CMakeLists.txt +++ b/src/3rd_party/CMakeLists.txt @@ -227,9 +227,9 @@ if (HMIADAPTER STREQUAL "messagebroker") URL https://dl.bintray.com/boostorg/release/1.66.0/source/boost_1_66_0.tar.gz DOWNLOAD_DIR ${BOOST_LIB_SOURCE_DIRECTORY} SOURCE_DIR ${BOOST_LIB_SOURCE_DIRECTORY} - CONFIGURE_COMMAND ./bootstrap.sh --with-libraries=system --prefix=${3RD_PARTY_INSTALL_PREFIX} + CONFIGURE_COMMAND ./bootstrap.sh --with-libraries=system,thread --prefix=${3RD_PARTY_INSTALL_PREFIX} BUILD_COMMAND ./b2 - INSTALL_COMMAND ${BOOST_INSTALL_COMMAND} --with-system --prefix=${3RD_PARTY_INSTALL_PREFIX} > boost_install.log + INSTALL_COMMAND ${BOOST_INSTALL_COMMAND} --with-system --with-thread --prefix=${3RD_PARTY_INSTALL_PREFIX} > boost_install.log INSTALL_DIR ${3RD_PARTY_INSTALL_PREFIX} BUILD_IN_SOURCE true ) diff --git a/src/components/connection_handler/include/connection_handler/connection_handler_impl.h b/src/components/connection_handler/include/connection_handler/connection_handler_impl.h index 121d5ed08c..730d7ebe32 100644 --- a/src/components/connection_handler/include/connection_handler/connection_handler_impl.h +++ b/src/components/connection_handler/include/connection_handler/connection_handler_impl.h @@ -640,7 +640,7 @@ class ConnectionHandlerImpl * @brief session/connection map */ SessionConnectionMap session_connection_map_; - mutable std::shared_ptr + mutable std::shared_ptr session_connection_map_lock_ptr_; /** diff --git a/src/components/connection_handler/src/connection.cc b/src/components/connection_handler/src/connection.cc index b8399b2c9b..08b37b9026 100644 --- a/src/components/connection_handler/src/connection.cc +++ b/src/components/connection_handler/src/connection.cc @@ -81,7 +81,7 @@ Connection::Connection(ConnectionHandle connection_handle, : connection_handler_(connection_handler) , connection_handle_(connection_handle) , connection_device_handle_(connection_device_handle) - , session_map_lock_(true) + , session_map_lock_() , primary_connection_handle_(0) , heartbeat_timeout_(heartbeat_timeout) { LOG4CXX_AUTO_TRACE(logger_); diff --git a/src/components/connection_handler/src/connection_handler_impl.cc b/src/components/connection_handler/src/connection_handler_impl.cc index 270b38d724..9aa84989d9 100644 --- a/src/components/connection_handler/src/connection_handler_impl.cc +++ b/src/components/connection_handler/src/connection_handler_impl.cc @@ -69,7 +69,7 @@ ConnectionHandlerImpl::ConnectionHandlerImpl( , transport_manager_(tm) , protocol_handler_(NULL) , session_connection_map_lock_ptr_( - std::make_shared(true)) + std::make_shared()) , connection_list_lock_() , connection_handler_observer_lock_() , connection_list_deleter_(&connection_list_) diff --git a/src/components/include/utils/lock.h b/src/components/include/utils/lock.h index 1b27a4e915..bfa1ef1770 100644 --- a/src/components/include/utils/lock.h +++ b/src/components/include/utils/lock.h @@ -159,6 +159,9 @@ class RecursiveLock : public BaseLock { // This class is used to automatically acquire and release the a lock class AutoLock { public: + explicit AutoLock(const std::shared_ptr& lock) : lock_(*lock) { + lock_.Acquire(); + } explicit AutoLock(BaseLock& lock) : lock_(lock) { // std::cerr << "lock is at " << &lock << std::endl; lock_.Acquire(); diff --git a/src/components/include/utils/lock_posix.h b/src/components/include/utils/lock_posix.h deleted file mode 100644 index ae8b64959f..0000000000 --- a/src/components/include/utils/lock_posix.h +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright (c) 2013, Ford Motor Company - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * Neither the name of the Ford Motor Company nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef SRC_COMPONENTS_INCLUDE_UTILS_LOCK_H_ -#define SRC_COMPONENTS_INCLUDE_UTILS_LOCK_H_ - -#if defined(OS_POSIX) -#include -#include -#else -#error Please implement lock for your OS -#endif -#include -#include "utils/macro.h" -#include "utils/atomic.h" -#include "utils/memory_barrier.h" - -namespace sync_primitives_posix { - -namespace impl { -#if defined(OS_POSIX) -typedef pthread_mutex_t PlatformMutex; -#endif -} // namespace impl - -class SpinMutex { - public: - SpinMutex() : state_(0) {} - void Lock() { - // Comment below add exception for lint error - // Reason: FlexeLint doesn't know about compiler's built-in instructions - /*lint -e1055*/ - if (atomic_post_set(&state_) == 0) { - return; - } - for (;;) { - sched_yield(); - /*lint -e1055*/ - if (state_ == 0 && atomic_post_set(&state_) == 0) { - return; - } - } - } - void Unlock() { - state_ = 0; - } - ~SpinMutex() {} - - private: - volatile unsigned int state_; -}; - -/* Platform-indepenednt NON-RECURSIVE lock (mutex) wrapper - Please use AutoLock to ackquire and (automatically) release it - It eases balancing of multple lock taking/releasing and makes it - Impossible to forget to release the lock: - ... - ConcurentlyAccessedData data_; - sync_primitives::Lock data_lock_; - ... - { - sync_primitives::AutoLock auto_lock(data_lock_); - data_.ReadOrWriteData(); - } // lock is automatically released here -*/ -class Lock { - public: - Lock(); - Lock(bool is_recursive); - ~Lock(); - - // Ackquire the lock. Must be called only once on a thread. - // Please consider using AutoLock to capture it. - void Acquire(); - // Release the lock. Must be called only once on a thread after lock. - // was acquired. Please consider using AutoLock to automatically release - // the lock - void Release(); - // Try if lock can be captured and lock it if it was possible. - // If it captured, lock must be manually released calling to Release - // when protected resource access was finished. - // @returns wether lock was captured. - bool Try(); - - private: - impl::PlatformMutex mutex_; - -#ifndef NDEBUG - /** - * @brief Basic debugging aid, a flag that signals wether this lock is - * currently taken - * Allows detection of abandoned and recursively captured mutexes - */ - uint32_t lock_taken_; - - /** - * @brief Describe if mutex is recurcive or not - */ - bool is_mutex_recursive_; - - void AssertFreeAndMarkTaken(); - void AssertTakenAndMarkFree(); -#else - void AssertFreeAndMarkTaken() {} - void AssertTakenAndMarkFree() {} -#endif - - void Init(bool is_recursive); - - friend class ConditionalVariable; - DISALLOW_COPY_AND_ASSIGN(Lock); -}; - -// This class is used to automatically acquire and release the a lock -class AutoLock { - public: - explicit AutoLock(Lock& lock) : lock_(lock) { - lock_.Acquire(); - } - ~AutoLock() { - lock_.Release(); - } - - private: - Lock& GetLock() { - return lock_; - } - Lock& lock_; - - private: - friend class AutoUnlock; - friend class ConditionalVariable; - DISALLOW_COPY_AND_ASSIGN(AutoLock); -}; - -// This class is used to temporarly unlock autolocked lock -class AutoUnlock { - public: - explicit AutoUnlock(Lock& lock) : lock_(lock) { - lock_.Release(); - } - explicit AutoUnlock(AutoLock& lock) : lock_(lock.GetLock()) { - lock_.Release(); - } - ~AutoUnlock() { - lock_.Acquire(); - } - - private: - Lock& lock_; - - private: - DISALLOW_COPY_AND_ASSIGN(AutoUnlock); -}; -} // namespace sync_primitives -#endif // SRC_COMPONENTS_INCLUDE_UTILS_LOCK_H_ diff --git a/src/components/utils/CMakeLists.txt b/src/components/utils/CMakeLists.txt index 6449f123a4..0eb64ae8c2 100644 --- a/src/components/utils/CMakeLists.txt +++ b/src/components/utils/CMakeLists.txt @@ -114,8 +114,10 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") endif() add_library("Utils" ${SOURCES}) -list(APPEND LIBRARIES -lboost_system -lboost_filesystem -lboost_thread) +list(APPEND LIBRARIES -lboost_system -lboost_thread) target_link_libraries("Utils" ${LIBRARIES}) +add_dependencies("Utils" Boost) + if(ENABLE_LOG) add_dependencies("Utils" install-3rd_party_logger) diff --git a/src/components/utils/src/lock_posix.cc b/src/components/utils/src/lock_posix.cc deleted file mode 100644 index 2807de1d0a..0000000000 --- a/src/components/utils/src/lock_posix.cc +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (c) 2013, Ford Motor Company - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * Neither the name of the Ford Motor Company nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "utils/lock_posix.h" -#include -#include -#include -#include -#include -#include "utils/logger.h" - -namespace sync_primitives_posix { - -CREATE_LOGGERPTR_GLOBAL(logger_, "Utils") - -Lock::Lock() -#ifndef NDEBUG - : lock_taken_(0) - , is_mutex_recursive_(false) -#endif // NDEBUG -{ - Init(false); -} - -Lock::Lock(bool is_recursive) -#ifndef NDEBUG - : lock_taken_(0) - , is_mutex_recursive_(is_recursive) -#endif // NDEBUG -{ - Init(is_recursive); -} - -Lock::~Lock() { -#ifndef NDEBUG - if (lock_taken_ > 0) { - LOG4CXX_FATAL(logger_, "Destroying non-released mutex " << &mutex_); - NOTREACHED(); - } -#endif - int32_t status = pthread_mutex_destroy(&mutex_); - if (status != 0) { - LOG4CXX_FATAL(logger_, - "Failed to destroy mutex " << &mutex_ << ": " - << strerror(status)); - NOTREACHED(); - } -} - -void Lock::Acquire() { - const int32_t status = pthread_mutex_lock(&mutex_); - if (status != 0) { - LOG4CXX_FATAL(logger_, - "Failed to acquire mutex " << &mutex_ << ": " - << strerror(status)); - NOTREACHED(); - } else { - AssertFreeAndMarkTaken(); - } -} - -void Lock::Release() { - AssertTakenAndMarkFree(); - const int32_t status = pthread_mutex_unlock(&mutex_); - if (status != 0) { - LOG4CXX_FATAL(logger_, - "Failed to unlock mutex" << &mutex_ << ": " - << strerror(status)); - NOTREACHED(); - } -} - -bool Lock::Try() { - const int32_t status = pthread_mutex_trylock(&mutex_); - if (status == 0) { -#ifndef NDEBUG - lock_taken_++; -#endif - return true; - } - return false; -} - -#ifndef NDEBUG -void Lock::AssertFreeAndMarkTaken() { - if ((lock_taken_ > 0) && !is_mutex_recursive_) { - LOG4CXX_FATAL(logger_, "Locking already taken not recursive mutex"); - NOTREACHED(); - } - lock_taken_++; -} -void Lock::AssertTakenAndMarkFree() { - if (lock_taken_ == 0) { - LOG4CXX_FATAL(logger_, "Unlocking a mutex that is not taken"); - NOTREACHED(); - } - lock_taken_--; -} -#endif - -void Lock::Init(bool is_recursive) { - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - - const int32_t mutex_type = - is_recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_ERRORCHECK; - - pthread_mutexattr_settype(&attr, mutex_type); - const int32_t status = pthread_mutex_init(&mutex_, &attr); - - pthread_mutexattr_destroy(&attr); - - if (status != 0) { - LOG4CXX_FATAL(logger_, - "Failed to initialize mutex. " << std::strerror(status)); - DCHECK(status != 0); - } -} - -} // namespace sync_primitives diff --git a/src/components/utils/test/lock_posix_test.cc b/src/components/utils/test/lock_posix_test.cc deleted file mode 100644 index 04f2f5fa34..0000000000 --- a/src/components/utils/test/lock_posix_test.cc +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2015, Ford Motor Company - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * Neither the name of the Ford Motor Company nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "gtest/gtest.h" -#include "utils/lock_posix.h" - -namespace test { -namespace components { -namespace utils_test { - -using sync_primitives_posix::Lock; - -TEST(LockPosixTest, DefaultCtorTest_ExpectNonRecursiveMutexCreated) { - // Create Lock object - Lock test_mutex; - // Lock mutex - test_mutex.Acquire(); - // Check if created mutex is non-recursive - EXPECT_FALSE(test_mutex.Try()); - // Release mutex before destroy - test_mutex.Release(); -} - -TEST(LockPosixTest, CtorTestWithFalseArgument_ExpectNonRecursiveMutexCreated) { - // Create Lock object - Lock test_mutex(false); - // Lock mutex - test_mutex.Acquire(); - // Check if created mutex is non-recursive - EXPECT_FALSE(test_mutex.Try()); - // Release mutex before destroy - test_mutex.Release(); -} - -TEST(LockPosixTest, CtorTestWithTrueArgument_ExpectRecursiveMutexCreated) { - // Create Lock object - Lock test_mutex(true); - // Lock mutex - test_mutex.Acquire(); - // Check if created mutex is recursive - EXPECT_TRUE(test_mutex.Try()); - // Release mutex before destroy - test_mutex.Release(); - test_mutex.Release(); -} - -TEST(LockPosixTest, AcquireMutex_ExpectMutexLocked) { - // Create Lock object (non-recursive mutex) - Lock test_mutex; - // Lock mutex - test_mutex.Acquire(); - // Try to lock it again. If locked expect false - EXPECT_FALSE(test_mutex.Try()); - test_mutex.Release(); -} - -TEST(LockPosixTest, ReleaseMutex_ExpectMutexReleased) { - // Create Lock object (non-recursive mutex) - Lock test_mutex; - // Lock mutex - test_mutex.Acquire(); - // Release mutex - test_mutex.Release(); - // Try to lock it again. If released expect true - EXPECT_TRUE(test_mutex.Try()); - test_mutex.Release(); -} - -TEST(LockPosixTest, TryLockNonRecursiveMutex_ExpectMutexNotLockedTwice) { - // Create Lock object (non-recursive mutex) - Lock test_mutex; - // Lock mutex - test_mutex.Try(); - // Try to lock it again. If locked expect false - EXPECT_FALSE(test_mutex.Try()); - test_mutex.Release(); -} - -TEST(LockPosixTest, TryLockRecursiveMutex_ExpectMutexLockedTwice) { - // Create Lock object (recursive mutex) - Lock test_mutex(true); - // Lock mutex - test_mutex.Try(); - // Try to lock it again. Expect true and internal counter increase - EXPECT_TRUE(test_mutex.Try()); - // Release mutex twice as was locked twice. - // Every Release() will decrement internal counter - test_mutex.Release(); - test_mutex.Release(); -} - -} // namespace utils_test -} // namespace components -} // namespace test -- cgit v1.2.1