mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 16:46:47 +00:00
Tools: Add InstanceLimiter utility
This commit is contained in:
parent
44da054e78
commit
b5da31498d
|
@ -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
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
// IWYU pragma: begin_exports
|
||||
#include <fairmq/tools/CppSTL.h>
|
||||
#include <fairmq/tools/InstanceLimit.h>
|
||||
#include <fairmq/tools/Network.h>
|
||||
#include <fairmq/tools/Process.h>
|
||||
#include <fairmq/tools/RateLimit.h>
|
||||
|
|
53
fairmq/tools/InstanceLimit.h
Normal file
53
fairmq/tools/InstanceLimit.h
Normal file
|
@ -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<typename Tag, int Max>
|
||||
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<typename Tag, int Max>
|
||||
int InstanceLimiter<Tag, Max>::fCount(0);
|
||||
|
||||
} /* namespace tools */
|
||||
} /* namespace mq */
|
||||
} /* namespace fair */
|
||||
|
||||
#endif /* FAIR_MQ_TOOLS_INSTANCELIMIT_H */
|
Loading…
Reference in New Issue
Block a user