mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
Tools: Introduce semaphore
This commit is contained in:
committed by
Dennis Klein
parent
a98965031f
commit
99ed61a58b
50
fairmq/tools/Semaphore.cxx
Normal file
50
fairmq/tools/Semaphore.cxx
Normal file
@@ -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<std::mutex> lk(fMutex);
|
||||
if (fCount > 0) {
|
||||
--fCount;
|
||||
} else {
|
||||
fCv.wait(lk, [this] { return fCount > 0; });
|
||||
--fCount;
|
||||
}
|
||||
}
|
||||
|
||||
auto Semaphore::Signal() -> void
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(fMutex);
|
||||
++fCount;
|
||||
lk.unlock();
|
||||
fCv.notify_one();
|
||||
}
|
||||
|
||||
auto Semaphore::GetCount() -> std::size_t
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(fMutex);
|
||||
return fCount;
|
||||
}
|
||||
|
||||
} /* namespace tools */
|
||||
} /* namespace mq */
|
||||
} /* namespace fair */
|
44
fairmq/tools/Semaphore.h
Normal file
44
fairmq/tools/Semaphore.h
Normal file
@@ -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 <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
|
||||
namespace fair {
|
||||
namespace mq {
|
||||
namespace tools {
|
||||
|
||||
/**
|
||||
* @struct Semaphore Semaphore.h <fairmq/tools/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 */
|
Reference in New Issue
Block a user