Skip to content
Snippets Groups Projects
Commit eb756de4 authored by Florian Fischer's avatar Florian Fischer
Browse files

Merge branch 'linux-version-construct-on-first-use' into 'master'

[LinuxVersion] Use "Construct on first use" idiom and add mutex

See merge request !348
parents 9f405aa4 18174680
No related branches found
No related tags found
1 merge request!348[LinuxVersion] Use "Construct on first use" idiom and add mutex
Pipeline #77775 passed
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2021 Florian Fischer
// Copyright © 2021-2022 Florian Fischer, Florian Schmaus
#include "lib/LinuxVersion.hpp"
#include <sys/utsname.h>
#include <cerrno>
#include <cstdlib>
#include <mutex>
#include "Common.hpp"
......@@ -35,19 +36,30 @@ static auto checked_strtol(const std::string& s) -> long {
namespace emper::lib {
std::string LinuxVersion::globalVersion;
LinuxVersion::LinuxVersion() {
if (globalVersion.empty()) {
struct utsname buf;
if (uname(&buf)) {
DIE_MSG_ERRNO("Failed to invoke uname()");
auto LinuxVersion::getUtsRelease() -> const std::string& {
// Use "construct (members) on first use idiom" to prevent the "static initialization order
// fiasco/problem".
static std::string utsRelease;
// This mutex will become necessary once we call getUtsRelease(),
// and by implication LinuxVersion's constructor, concurrently. So
// it can not hurt to have it right now.
static std::mutex utsReleaseMutex;
{
const std::lock_guard<std::mutex> lock(utsReleaseMutex);
if (utsRelease.empty()) {
struct utsname buf;
if (uname(&buf)) {
DIE_MSG_ERRNO("Failed to invoke uname()");
}
utsRelease = std::string(buf.release);
}
globalVersion = std::string(buf.release);
}
version = globalVersion;
return utsRelease;
}
LinuxVersion::LinuxVersion() { version = getUtsRelease(); }
// Returns 1 if s is smaller, -1 if this is smaller, 0 if equal
auto LinuxVersion::compare(const std::string& s) -> int {
size_t dot_pos, dot_pos2;
......
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2021 Florian Fischer
// Copyright © 2021-2022 Florian Fischer, Florian Schmaus
#pragma once
#include <string>
......@@ -9,7 +9,7 @@ class TestLinuxVersion;
namespace emper::lib {
class LinuxVersion {
friend class ::TestLinuxVersion;
static std::string globalVersion;
static auto getUtsRelease() -> const std::string&;
std::string version;
auto compare(const std::string& s) -> int;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment