diff options
Diffstat (limited to 'vbench/src/vbench/core/time_queue.h')
-rw-r--r-- | vbench/src/vbench/core/time_queue.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/vbench/src/vbench/core/time_queue.h b/vbench/src/vbench/core/time_queue.h new file mode 100644 index 00000000000..b7c287dbdc4 --- /dev/null +++ b/vbench/src/vbench/core/time_queue.h @@ -0,0 +1,55 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <vespa/vespalib/util/priority_queue.h> +#include <vespa/vespalib/util/sync.h> +#include <memory> + +#include "closeable.h" + +namespace vbench { + +/** + * A thread-safe priority queue keeping track of objects queued + * according to an abstract time line. After a time queue is closed, + * all incoming objects will be deleted. + **/ +template <typename T> +class TimeQueue : public Closeable +{ +private: + struct Entry { + std::unique_ptr<T> object; + double time; + Entry(std::unique_ptr<T> obj, double t) : object(std::move(obj)), time(t) {} + Entry(Entry &&rhs) : object(std::move(rhs.object)), time(rhs.time) {} + Entry &operator=(Entry &&rhs) { + object = std::move(rhs.object); + time = rhs.time; + return *this; + } + bool operator<(const Entry &rhs) const { + return (time < rhs.time); + } + }; + + vespalib::Monitor _monitor; + double _time; + double _window; + double _tick; + vespalib::PriorityQueue<Entry> _queue; + bool _closed; + +public: + TimeQueue(double window, double tick); + virtual void close(); + void discard(); + void insert(std::unique_ptr<T> obj, double time); + bool extract(double time, std::vector<std::unique_ptr<T> > &list, double &delay); +}; + +} // namespace vbench + +#include "time_queue.hpp" + |