X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fceph%2Fsrc%2Fdmclock%2Fsupport%2Fsrc%2Frun_every.h;fp=src%2Fceph%2Fsrc%2Fdmclock%2Fsupport%2Fsrc%2Frun_every.h;h=0d4c538577971f032f5d5575c9521fa963429b59;hb=812ff6ca9fcd3e629e49d4328905f33eee8ca3f5;hp=0000000000000000000000000000000000000000;hpb=15280273faafb77777eab341909a3f495cf248d9;p=stor4nfv.git diff --git a/src/ceph/src/dmclock/support/src/run_every.h b/src/ceph/src/dmclock/support/src/run_every.h new file mode 100644 index 0000000..0d4c538 --- /dev/null +++ b/src/ceph/src/dmclock/support/src/run_every.h @@ -0,0 +1,70 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Copyright (C) 2017 Red Hat Inc. + */ + + +#pragma once + +#include +#include +#include +#include +#include + +namespace crimson { + using std::chrono::duration_cast; + using std::chrono::milliseconds; + + // runs a given simple function object waiting wait_period + // milliseconds between; the destructor stops the other thread + // immediately + class RunEvery { + using Lock = std::unique_lock; + using Guard = std::lock_guard; + using TimePoint = std::chrono::steady_clock::time_point; + + bool finishing = false; + std::chrono::milliseconds wait_period; + std::function body; + std::mutex mtx; + std::condition_variable cv; + + // put threads last so all other variables are initialized first + + std::thread thd; + + public: + +#ifdef ADD_MOVE_SEMANTICS + RunEvery(); +#endif + + template + RunEvery(D _wait_period, + std::function _body) : + wait_period(duration_cast(_wait_period)), + body(_body) + { + thd = std::thread(&RunEvery::run, this); + } + + RunEvery(const RunEvery& other) = delete; + RunEvery& operator=(const RunEvery& other) = delete; + RunEvery(RunEvery&& other) = delete; +#ifdef ADD_MOVE_SEMANTICS + RunEvery& operator=(RunEvery&& other); +#else + RunEvery& operator=(RunEvery&& other) = delete; +#endif + + ~RunEvery(); + + void join(); + + protected: + + void run(); + }; +}