Skip to content

Commit

Permalink
Use fixed typed and aligned DbId for thread safe access and copletely…
Browse files Browse the repository at this point in the history
… inlined
  • Loading branch information
daschuer committed Sep 23, 2023
1 parent 40a0c16 commit 24aa5cf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 55 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,6 @@ add_library(mixxx-lib STATIC EXCLUDE_FROM_ALL
src/util/db/dbconnectionpool.cpp
src/util/db/dbconnectionpooled.cpp
src/util/db/dbconnectionpooler.cpp
src/util/db/dbid.cpp
src/util/db/fwdsqlquery.cpp
src/util/db/fwdsqlqueryselectresult.cpp
src/util/db/sqlite.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/track/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ void Track::setBitrate(int iBitrate) {
}

TrackId Track::getId() const {
static_assert(sizeof(TrackId) <= sizeof(void*)); // no locking required
// TrackId is an aligned integer, no locking required
return m_record.getId();
}

Expand Down
28 changes: 0 additions & 28 deletions src/util/db/dbid.cpp

This file was deleted.

57 changes: 32 additions & 25 deletions src/util/db/dbid.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,28 @@
class DbId {
protected:
public:
// Alias for the corresponding native type. It keeps the
// implementation of this class flexible if we ever gonna
// need to change it from 'int' to 'long' or any other type.
typedef int value_type;

DbId()
: m_value(kInvalidValue) {
DEBUG_ASSERT(!isValid());
}
explicit DbId(value_type value)
: m_value(value) {
DEBUG_ASSERT(isValid() || (kInvalidValue == m_value));
}
explicit DbId(QVariant variant)
: DbId(valueOf(std::move(variant))) {
}
constexpr DbId()
: m_value(kInvalidValue) {
DEBUG_ASSERT(!isValid());
}
explicit DbId(int value)
: m_value(value) {
DEBUG_ASSERT(isValid() || (kInvalidValue == m_value));
}
explicit DbId(const QVariant& variant)
: DbId(valueOf(variant)) {
}

bool isValid() const {
return isValidValue(m_value);
}

value_type value() const {
int value() const {
return m_value;
}

std::size_t hash() const {
return std::hash<value_type>()(m_value);
return std::hash<int>()(m_value);
}

typedef std::function<std::size_t (const DbId& dbid)> hash_fun_t;
Expand Down Expand Up @@ -107,21 +102,33 @@ class DbId {
}

private:
static constexpr value_type kInvalidValue = -1;
static constexpr int kInvalidValue = -1;

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
static const QMetaType kVariantType;
#else
static const QVariant::Type kVariantType;
#endif

static bool isValidValue(value_type value) {
static bool isValidValue(int value) {
return 0 <= value;
}

static value_type valueOf(QVariant /*pass-by-value*/ variant);

value_type m_value;
}

static int valueOf(const QVariant& variant) {
bool ok;
int value = variant.toInt(&ok);
if (ok) {
return value;
}
qCritical() << "Invalid database identifier value:"
<< variant;
return kInvalidValue;
}

// This value can be safely read from different threads but
// does not use any memory barriers or function calls for a
// guaranteed access order like std::atomic_int;
alignas(int) int m_value;
};

Q_DECLARE_TYPEINFO(DbId, Q_MOVABLE_TYPE);
Expand Down

0 comments on commit 24aa5cf

Please sign in to comment.