// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #pragma once #include "invokeservice.h" #include "time.h" #include #include #include #include namespace vespalib { /** * An invoke service what will invoke the given function with at specified frequency. */ class InvokeServiceImpl : public InvokeService { public: InvokeServiceImpl(duration napTime); InvokeServiceImpl(const InvokeServiceImpl &) = delete; InvokeServiceImpl & operator=(const InvokeServiceImpl &) = delete; ~InvokeServiceImpl() override; std::unique_ptr registerInvoke(InvokeFunc func) override; const std::atomic & nowRef() const { return _now; } private: using IdAndFunc = std::pair; class Registration; void unregister(uint64_t id); void runLoop(); duration _naptime; std::atomic _now; std::mutex _lock; std::condition_variable _cond; uint64_t _currId; bool _closed; std::vector _toInvoke; std::unique_ptr _thread; }; }