diff --git a/fairmq/CMakeLists.txt b/fairmq/CMakeLists.txt index 91c094e7..0d41cef2 100644 --- a/fairmq/CMakeLists.txt +++ b/fairmq/CMakeLists.txt @@ -38,6 +38,7 @@ if(BUILD_FAIRMQ OR BUILD_SDK) tools/Network.h tools/Process.h tools/RateLimit.h + tools/Semaphore.h tools/Strings.h tools/Unique.h tools/Version.h @@ -47,6 +48,7 @@ if(BUILD_FAIRMQ OR BUILD_SDK) set(TOOLS_SOURCE_FILES tools/Network.cxx tools/Process.cxx + tools/Semaphore.cxx tools/Unique.cxx ) diff --git a/fairmq/Tools.h b/fairmq/Tools.h index d2ad6d34..f0057eb7 100644 --- a/fairmq/Tools.h +++ b/fairmq/Tools.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include diff --git a/fairmq/tools/Semaphore.cxx b/fairmq/tools/Semaphore.cxx new file mode 100644 index 00000000..d60f4fe1 --- /dev/null +++ b/fairmq/tools/Semaphore.cxx @@ -0,0 +1,50 @@ +/******************************************************************************** + * 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" * + ********************************************************************************/ + +#include "Semaphore.h" + +namespace fair { +namespace mq { +namespace tools { + +Semaphore::Semaphore() + : Semaphore(0) +{} + +Semaphore::Semaphore(std::size_t initial_count) + : fCount(initial_count) +{} + +auto Semaphore::Wait() -> void +{ + std::unique_lock lk(fMutex); + if (fCount > 0) { + --fCount; + } else { + fCv.wait(lk, [this] { return fCount > 0; }); + --fCount; + } +} + +auto Semaphore::Signal() -> void +{ + std::unique_lock lk(fMutex); + ++fCount; + lk.unlock(); + fCv.notify_one(); +} + +auto Semaphore::GetCount() -> std::size_t +{ + std::unique_lock lk(fMutex); + return fCount; +} + +} /* namespace tools */ +} /* namespace mq */ +} /* namespace fair */ diff --git a/fairmq/tools/Semaphore.h b/fairmq/tools/Semaphore.h new file mode 100644 index 00000000..09f97412 --- /dev/null +++ b/fairmq/tools/Semaphore.h @@ -0,0 +1,44 @@ +/******************************************************************************** + * 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_SEMAPHORE_H +#define FAIR_MQ_TOOLS_SEMAPHORE_H + +#include +#include +#include +#include + +namespace fair { +namespace mq { +namespace tools { + +/** + * @struct Semaphore Semaphore.h + * @brief A simple blocking semaphore. + */ +struct Semaphore +{ + explicit Semaphore(); + explicit Semaphore(std::size_t initial_count); + + auto Wait() -> void; + auto Signal() -> void; + auto GetCount() -> std::size_t; + +private: + std::size_t fCount; + std::mutex fMutex; + std::condition_variable fCv; +}; + +} /* namespace tools */ +} /* namespace mq */ +} /* namespace fair */ + +#endif /* FAIR_MQ_TOOLS_SEMAPHORE_H */