From b39b4dcf481e618030852c3c66840f23c4b36eb5 Mon Sep 17 00:00:00 2001 From: silverqx Date: Fri, 12 Nov 2021 12:27:03 +0100 Subject: [PATCH] fixed errors for Linux clang/gcc --- cmake/Modules/TinySources.cmake | 1 + include/include.pri | 1 + include/orm/configuration.hpp | 9 +++++---- include/orm/macros/threadlocal.hpp | 16 ++++++++++++++++ include/orm/support/databaseconnectionsmap.hpp | 3 ++- include/orm/tiny/concerns/guardsattributes.hpp | 9 +++++---- include/orm/tiny/concerns/hasrelationstore.hpp | 2 +- .../orm/tiny/concerns/queriesrelationships.hpp | 4 ++-- include/orm/tiny/model.hpp | 11 ++++++----- include/orm/tiny/relations/basepivot.hpp | 2 +- include/orm/tiny/relations/belongsto.hpp | 2 +- .../relations/concerns/supportsdefaultmodels.hpp | 3 ++- include/orm/tiny/relations/hasoneormany.hpp | 2 +- include/orm/tiny/relations/relation.hpp | 3 ++- include/orm/utils/thread.hpp | 8 ++++++-- src/orm/query/grammars/mysqlgrammar.cpp | 7 ++++--- src/orm/query/grammars/postgresgrammar.cpp | 7 ++++--- src/orm/query/grammars/sqlitegrammar.cpp | 7 ++++--- src/orm/utils/thread.cpp | 8 +++++++- .../orm/databasemanager/tst_databasemanager.cpp | 2 +- 20 files changed, 72 insertions(+), 35 deletions(-) create mode 100644 include/orm/macros/threadlocal.hpp diff --git a/cmake/Modules/TinySources.cmake b/cmake/Modules/TinySources.cmake index 10fcb5692..1cf7e81a7 100644 --- a/cmake/Modules/TinySources.cmake +++ b/cmake/Modules/TinySources.cmake @@ -44,6 +44,7 @@ function(tiny_sources out_headers out_sources) macros/export.hpp macros/export_common.hpp macros/systemheader.hpp + macros/threadlocal.hpp mysqlconnection.hpp ormtypes.hpp postgresconnection.hpp diff --git a/include/include.pri b/include/include.pri index 2fd661f88..1a3592d38 100644 --- a/include/include.pri +++ b/include/include.pri @@ -41,6 +41,7 @@ HEADERS += \ $$PWD/orm/macros/export.hpp \ $$PWD/orm/macros/export_common.hpp \ $$PWD/orm/macros/systemheader.hpp \ + $$PWD/orm/macros/threadlocal.hpp \ $$PWD/orm/mysqlconnection.hpp \ $$PWD/orm/ormtypes.hpp \ $$PWD/orm/postgresconnection.hpp \ diff --git a/include/orm/configuration.hpp b/include/orm/configuration.hpp index 91bf54125..f53f15a98 100644 --- a/include/orm/configuration.hpp +++ b/include/orm/configuration.hpp @@ -8,6 +8,7 @@ TINY_SYSTEM_HEADER #include #include "orm/macros/commonnamespace.hpp" +#include "orm/macros/threadlocal.hpp" TINYORM_BEGIN_COMMON_NAMESPACE @@ -29,16 +30,16 @@ namespace Orm::Support /*! Default Database Connection Name, used as default value in method declarations only. */ - thread_local + T_THREAD_LOCAL inline static QString defaultConnectionName = QStringLiteral("tinyorm_default"); // CUR better naming silverqx /*! Currently set Default Database Connection Name in a current thread. */ - thread_local + T_THREAD_LOCAL inline static QString defaultConnection; /*! Default namespace prefix for MySQL savepoints in a current thread. */ - thread_local + T_THREAD_LOCAL inline static QString defaultSavepointNamespace = QStringLiteral("tinyorm_savepoint"); @@ -53,7 +54,7 @@ namespace Orm::Support private: /*! Database connection configurations. */ - thread_local + T_THREAD_LOCAL inline static ConfigurationsType m_configurations; }; diff --git a/include/orm/macros/threadlocal.hpp b/include/orm/macros/threadlocal.hpp new file mode 100644 index 000000000..d2216c4c7 --- /dev/null +++ b/include/orm/macros/threadlocal.hpp @@ -0,0 +1,16 @@ +#pragma once +#ifndef ORM_MACROS_THREADLOCAL_HPP +#define ORM_MACROS_THREADLOCAL_HPP + +#include "orm/macros/systemheader.hpp" +TINY_SYSTEM_HEADER + +#if !defined(__clang__) +# define T_THREAD_LOCAL thread_local +#endif + +#if !defined(T_THREAD_LOCAL) +# define T_THREAD_LOCAL +#endif + +#endif // ORM_MACROS_THREADLOCAL_HPP diff --git a/include/orm/support/databaseconnectionsmap.hpp b/include/orm/support/databaseconnectionsmap.hpp index 3e9dd24a5..26a99c9d1 100644 --- a/include/orm/support/databaseconnectionsmap.hpp +++ b/include/orm/support/databaseconnectionsmap.hpp @@ -9,6 +9,7 @@ TINY_SYSTEM_HEADER #include "orm/databaseconnection.hpp" #include "orm/macros/commonnamespace.hpp" +#include "orm/macros/threadlocal.hpp" TINYORM_BEGIN_COMMON_NAMESPACE @@ -39,7 +40,7 @@ namespace Orm::Support private: /*! Database connections for the current thread. */ - thread_local + T_THREAD_LOCAL inline static ConnectionsType m_connections; }; diff --git a/include/orm/tiny/concerns/guardsattributes.hpp b/include/orm/tiny/concerns/guardsattributes.hpp index cb322b1d5..c95d53a29 100644 --- a/include/orm/tiny/concerns/guardsattributes.hpp +++ b/include/orm/tiny/concerns/guardsattributes.hpp @@ -6,6 +6,7 @@ TINY_SYSTEM_HEADER #include "orm/concepts.hpp" +#include "orm/macros/threadlocal.hpp" #include "orm/ormtypes.hpp" #include "orm/utils/type.hpp" @@ -80,16 +81,16 @@ namespace Concerns fillableFromArray(QVector &&attributes) const; /*! The attributes that are mass assignable. */ - thread_local + T_THREAD_LOCAL inline static QStringList u_fillable; /*! The attributes that aren't mass assignable. */ - thread_local + T_THREAD_LOCAL inline static QStringList u_guarded {ASTERISK}; // NOLINT(cppcoreguidelines-interfaces-global-init) /*! Indicates if all mass assignment is enabled. */ - thread_local + T_THREAD_LOCAL inline static bool m_unguarded = false; /*! The actual columns that exist on the database and can be guarded. */ - thread_local + T_THREAD_LOCAL inline static QHash m_guardableColumns; private: diff --git a/include/orm/tiny/concerns/hasrelationstore.hpp b/include/orm/tiny/concerns/hasrelationstore.hpp index 692800d87..99aa56f68 100644 --- a/include/orm/tiny/concerns/hasrelationstore.hpp +++ b/include/orm/tiny/concerns/hasrelationstore.hpp @@ -246,7 +246,7 @@ namespace Concerns constexpr static RelationStoreType initStoreType(); /*! Served store type, this class can handle two store types. */ - thread_local + T_THREAD_LOCAL constexpr static RelationStoreType STORE_TYPE = initStoreType(); private: diff --git a/include/orm/tiny/concerns/queriesrelationships.hpp b/include/orm/tiny/concerns/queriesrelationships.hpp index c4472b66b..3ee62f2d4 100644 --- a/include/orm/tiny/concerns/queriesrelationships.hpp +++ b/include/orm/tiny/concerns/queriesrelationships.hpp @@ -50,13 +50,13 @@ namespace Private // BUG clang and thread_local, gcc on MinGW doesn't work too silverqx /*! Data member for nested stores. */ template - thread_local + T_THREAD_LOCAL inline static std::stack>> STORE; /*! Stored Related type is used to avoid a cryptic message when a bad type-id was passed to the has() nested method. */ - thread_local + T_THREAD_LOCAL inline static std::stack STORE_TYPEID; diff --git a/include/orm/tiny/model.hpp b/include/orm/tiny/model.hpp index d0292e7b1..7dbface3a 100644 --- a/include/orm/tiny/model.hpp +++ b/include/orm/tiny/model.hpp @@ -46,7 +46,7 @@ namespace Relations { { /*! The connection to use in the Model, this data member is picked up in the Model::getConnectionName(). */ - thread_local + T_THREAD_LOCAL inline static QString connection; }; #endif @@ -131,6 +131,7 @@ namespace Relations { // CUR study how to use acquire/release memory order for m_queryLogId atomic silverqx // CUR u_guarded in Pivot/BasePivot and TLS silverqx // CUR rename m_db to m_dm in tinyplay silverqx + // CUR move T_LIKELY to likely.hpp from macros.hpp silverqx /*! Base model class. */ template class Model : @@ -705,7 +706,7 @@ namespace Relations { /* HasAttributes */ /*! The model's default values for attributes. */ - thread_local + T_THREAD_LOCAL inline static QVector u_attributes; /*! The model's attributes (insert order). */ QVector m_attributes; @@ -727,10 +728,10 @@ namespace Relations { // TODO add support for 'U' like in PHP to support unix timestamp, I will have to manually check if u_dateFormat contains 'U' and use QDateTime::fromSecsSinceEpoch() silverqx /*! The storage format of the model's date columns. */ - thread_local + T_THREAD_LOCAL inline static QString u_dateFormat; /*! The attributes that should be mutated to dates. @deprecated */ - thread_local + T_THREAD_LOCAL inline static QStringList u_dates; /* HasRelationships */ @@ -2439,7 +2440,7 @@ namespace Relations { const QStringList & Model::getDates() const { - thread_local + T_THREAD_LOCAL static const QStringList &dates = getDatesInternal(); return dates; diff --git a/include/orm/tiny/relations/basepivot.hpp b/include/orm/tiny/relations/basepivot.hpp index 0f06f55b6..1f4df3c3a 100644 --- a/include/orm/tiny/relations/basepivot.hpp +++ b/include/orm/tiny/relations/basepivot.hpp @@ -84,7 +84,7 @@ namespace Orm::Tiny::Relations /*! Indicates if the ID is auto-incrementing. */ bool u_incrementing = false; /*! The attributes that aren't mass assignable. */ - thread_local + T_THREAD_LOCAL inline static QStringList u_guarded; /* AsPivot */ diff --git a/include/orm/tiny/relations/belongsto.hpp b/include/orm/tiny/relations/belongsto.hpp index 60bad13d7..754a471a9 100644 --- a/include/orm/tiny/relations/belongsto.hpp +++ b/include/orm/tiny/relations/belongsto.hpp @@ -104,7 +104,7 @@ namespace Orm::Tiny::Relations /*! The name of the relationship. */ QString m_relationName; /*! The count of self joins. */ - thread_local + T_THREAD_LOCAL inline static int selfJoinCount = 0; }; diff --git a/include/orm/tiny/relations/concerns/supportsdefaultmodels.hpp b/include/orm/tiny/relations/concerns/supportsdefaultmodels.hpp index 33582ecba..352ddd9d1 100644 --- a/include/orm/tiny/relations/concerns/supportsdefaultmodels.hpp +++ b/include/orm/tiny/relations/concerns/supportsdefaultmodels.hpp @@ -5,6 +5,7 @@ #include "orm/macros/systemheader.hpp" TINY_SYSTEM_HEADER +#include "orm/macros/threadlocal.hpp" // FEATURE orm types, only AttributeItem used silverqx #include "orm/ormtypes.hpp" @@ -127,7 +128,7 @@ namespace Concerns { inline Relation & SupportsDefaultModels::relation() { - thread_local + T_THREAD_LOCAL static auto &cached = dynamic_cast &>(*this); return cached; diff --git a/include/orm/tiny/relations/hasoneormany.hpp b/include/orm/tiny/relations/hasoneormany.hpp index 53fee6302..9985b6e95 100644 --- a/include/orm/tiny/relations/hasoneormany.hpp +++ b/include/orm/tiny/relations/hasoneormany.hpp @@ -115,7 +115,7 @@ namespace Orm::Tiny::Relations /*! The local key of the parent model. */ QString m_localKey; /*! The count of self joins. */ - thread_local + T_THREAD_LOCAL inline static int selfJoinCount = 0; }; diff --git a/include/orm/tiny/relations/relation.hpp b/include/orm/tiny/relations/relation.hpp index b32f9c537..10500b0f5 100644 --- a/include/orm/tiny/relations/relation.hpp +++ b/include/orm/tiny/relations/relation.hpp @@ -13,6 +13,7 @@ TINY_SYSTEM_HEADER #include #include "orm/exceptions/runtimeerror.hpp" +#include "orm/macros/threadlocal.hpp" #include "orm/tiny/relations/relationproxies.hpp" TINYORM_BEGIN_COMMON_NAMESPACE @@ -175,7 +176,7 @@ namespace Relations /*! The TinyORM TinyBuilder instance. */ std::unique_ptr> m_query; /*! Indicates if the relation is adding constraints. */ - thread_local + T_THREAD_LOCAL inline static bool constraints = true; }; diff --git a/include/orm/utils/thread.hpp b/include/orm/utils/thread.hpp index e8d6168c6..6a8ce6d4a 100644 --- a/include/orm/utils/thread.hpp +++ b/include/orm/utils/thread.hpp @@ -5,6 +5,8 @@ #include "orm/macros/systemheader.hpp" TINY_SYSTEM_HEADER +#include + #include "orm/macros/commonnamespace.hpp" #include "orm/macros/export.hpp" @@ -26,10 +28,12 @@ namespace Orm::Utils /*! Set thread name for debugger, -1 for a current thread. */ static void nameThreadForDebugging( - const QString &threadName, quint64 threadId = -1); + const QString &threadName, + quint64 threadId = static_cast(-1)); /*! Set thread name for debugger, -1 for a current thread. */ static void nameThreadForDebugging( - const char *threadName, quint64 threadId = -1); + const char *threadName, + quint64 threadId = static_cast(-1)); }; } // namespace Orm::Utils diff --git a/src/orm/query/grammars/mysqlgrammar.cpp b/src/orm/query/grammars/mysqlgrammar.cpp index 7ffe56868..61b45eaa5 100644 --- a/src/orm/query/grammars/mysqlgrammar.cpp +++ b/src/orm/query/grammars/mysqlgrammar.cpp @@ -1,5 +1,6 @@ #include "orm/query/grammars/mysqlgrammar.hpp" +#include "orm/macros/threadlocal.hpp" #include "orm/query/querybuilder.hpp" TINYORM_BEGIN_COMMON_NAMESPACE @@ -106,7 +107,7 @@ MySqlGrammar::getCompileMap() const }; // Pointers to a where member methods by whereType, yes yes c++ 😂 - thread_local + T_THREAD_LOCAL static const QMap cached { {SelectComponentType::AGGREGATE, {bind(&MySqlGrammar::compileAggregate), [this] @@ -161,7 +162,7 @@ MySqlGrammar::getWhereMethod(const WhereType whereType) const // Pointers to a where member methods by whereType, yes yes c++ 😂 // An order has to be the same as in enum struct WhereType // FUTURE QHash would has faster lookup, I should choose QHash, fix also another Grammars silverx - thread_local + T_THREAD_LOCAL static const QVector> cached { bind(&MySqlGrammar::whereBasic), bind(&MySqlGrammar::whereNested), @@ -175,7 +176,7 @@ MySqlGrammar::getWhereMethod(const WhereType whereType) const bind(&MySqlGrammar::whereNotExists), }; - thread_local + T_THREAD_LOCAL static const auto size = cached.size(); // Check if whereType is in the range, just for sure 😏 diff --git a/src/orm/query/grammars/postgresgrammar.cpp b/src/orm/query/grammars/postgresgrammar.cpp index 42aac4d3e..9d0e06949 100644 --- a/src/orm/query/grammars/postgresgrammar.cpp +++ b/src/orm/query/grammars/postgresgrammar.cpp @@ -1,5 +1,6 @@ #include "orm/query/grammars/postgresgrammar.hpp" +#include "orm/macros/threadlocal.hpp" #include "orm/query/querybuilder.hpp" TINYORM_BEGIN_COMMON_NAMESPACE @@ -112,7 +113,7 @@ PostgresGrammar::getCompileMap() const }; // Pointers to a where member methods by whereType, yes yes c++ 😂 - thread_local + T_THREAD_LOCAL static const QMap cached { {SelectComponentType::AGGREGATE, {bind(&PostgresGrammar::compileAggregate), [this] @@ -166,7 +167,7 @@ PostgresGrammar::getWhereMethod(const WhereType whereType) const // Pointers to a where member methods by whereType, yes yes c++ 😂 // An order has to be the same as in enum struct WhereType // FUTURE QHash would has faster lookup, I should choose QHash, fix also another Grammars silverx - thread_local + T_THREAD_LOCAL static const QVector> cached { bind(&PostgresGrammar::whereBasic), bind(&PostgresGrammar::whereNested), @@ -180,7 +181,7 @@ PostgresGrammar::getWhereMethod(const WhereType whereType) const bind(&PostgresGrammar::whereNotExists), }; - thread_local + T_THREAD_LOCAL static const auto size = cached.size(); // Check if whereType is in the range, just for sure 😏 diff --git a/src/orm/query/grammars/sqlitegrammar.cpp b/src/orm/query/grammars/sqlitegrammar.cpp index 4285a993d..e65079863 100644 --- a/src/orm/query/grammars/sqlitegrammar.cpp +++ b/src/orm/query/grammars/sqlitegrammar.cpp @@ -1,5 +1,6 @@ #include "orm/query/grammars/sqlitegrammar.hpp" +#include "orm/macros/threadlocal.hpp" #include "orm/query/querybuilder.hpp" TINYORM_BEGIN_COMMON_NAMESPACE @@ -89,7 +90,7 @@ SQLiteGrammar::getCompileMap() const }; // Pointers to a where member methods by whereType, yes yes c++ 😂 - thread_local + T_THREAD_LOCAL static const QMap cached { {SelectComponentType::AGGREGATE, {bind(&SQLiteGrammar::compileAggregate), [this] @@ -142,7 +143,7 @@ SQLiteGrammar::getWhereMethod(const WhereType whereType) const // Pointers to a where member methods by whereType, yes yes c++ 😂 // An order has to be the same as in enum struct WhereType - thread_local + T_THREAD_LOCAL static const QVector> cached { bind(&SQLiteGrammar::whereBasic), bind(&SQLiteGrammar::whereNested), @@ -156,7 +157,7 @@ SQLiteGrammar::getWhereMethod(const WhereType whereType) const bind(&SQLiteGrammar::whereNotExists), }; - thread_local + T_THREAD_LOCAL static const auto size = cached.size(); // Check if whereType is in the range, just for sure 😏 diff --git a/src/orm/utils/thread.cpp b/src/orm/utils/thread.cpp index d319207b7..49b5623ac 100644 --- a/src/orm/utils/thread.cpp +++ b/src/orm/utils/thread.cpp @@ -1,10 +1,16 @@ #include "orm/utils/thread.hpp" +#include + #if !defined(__clang__) && \ !defined(TINYORM_NO_DEBUG) && defined(_MSC_VER) && !defined(Q_OS_WINRT) #include #endif +#if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) +#include +#endif + // CUR test on gcc/clang and add needed includes silverqx TINYORM_BEGIN_COMMON_NAMESPACE @@ -55,7 +61,7 @@ namespace void setCurrentThreadName(const char *name) { # if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) - prctl(PR_SET_NAME, (unsigned long) name, 0, 0, 0); + prctl(PR_SET_NAME, reinterpret_cast(name), 0, 0, 0); # elif defined(Q_OS_MAC) pthread_setname_np(name); # elif defined(Q_OS_QNX) diff --git a/tests/auto/functional/orm/databasemanager/tst_databasemanager.cpp b/tests/auto/functional/orm/databasemanager/tst_databasemanager.cpp index e65bf5f39..f77330424 100644 --- a/tests/auto/functional/orm/databasemanager/tst_databasemanager.cpp +++ b/tests/auto/functional/orm/databasemanager/tst_databasemanager.cpp @@ -33,7 +33,7 @@ private slots: // NOLINTNEXTLINE(readability-redundant-access-specifiers) private: /*! The Database Manager used in this test case. */ - std::unique_ptr m_dm; + std::unique_ptr m_dm {}; }; void tst_DatabaseManager::initTestCase()