From b5da31498d3f606be55d9387e075b8df6366f859 Mon Sep 17 00:00:00 2001 From: Dennis Klein Date: Fri, 19 Jul 2019 19:48:47 +0200 Subject: [PATCH] Tools: Add InstanceLimiter utility --- fairmq/CMakeLists.txt | 1 + fairmq/Tools.h | 1 + fairmq/tools/InstanceLimit.h | 53 ++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 fairmq/tools/InstanceLimit.h diff --git a/fairmq/CMakeLists.txt b/fairmq/CMakeLists.txt index 0d41cef2..26a2f860 100644 --- a/fairmq/CMakeLists.txt +++ b/fairmq/CMakeLists.txt @@ -35,6 +35,7 @@ if(BUILD_FAIRMQ OR BUILD_SDK) set(TOOLS_PUBLIC_HEADER_FILES tools/CppSTL.h + tools/InstanceLimit.h tools/Network.h tools/Process.h tools/RateLimit.h diff --git a/fairmq/Tools.h b/fairmq/Tools.h index f0057eb7..3ec4aff9 100644 --- a/fairmq/Tools.h +++ b/fairmq/Tools.h @@ -11,6 +11,7 @@ // IWYU pragma: begin_exports #include +#include #include #include #include diff --git a/fairmq/tools/InstanceLimit.h b/fairmq/tools/InstanceLimit.h new file mode 100644 index 00000000..ffa1ec43 --- /dev/null +++ b/fairmq/tools/InstanceLimit.h @@ -0,0 +1,53 @@ +/******************************************************************************** + * Copyright (C) 2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ + +#ifndef FAIR_MQ_TOOLS_INSTANCELIMIT_H +#define FAIR_MQ_TOOLS_INSTANCELIMIT_H + +#include "Strings.h" + +namespace fair { +namespace mq { +namespace tools { + +template +struct InstanceLimiter +{ + InstanceLimiter() { Increment(); } + ~InstanceLimiter() { Decrement(); } + auto GetCount() -> int { return fCount; } + + private: + auto Increment() -> void + { + if (fCount < Max) { + ++fCount; + } else { + throw std::runtime_error( + ToString("More than ", Max, " instances of ", Tag(), " in parallel not supported")); + } + } + + auto Decrement() -> void + { + if (fCount > 0) { + --fCount; + } + } + + static int fCount; +}; + +template +int InstanceLimiter::fCount(0); + +} /* namespace tools */ +} /* namespace mq */ +} /* namespace fair */ + +#endif /* FAIR_MQ_TOOLS_INSTANCELIMIT_H */