summaryrefslogtreecommitdiffstats
path: root/vbench/src/vbench/core/handler_thread.h
blob: 24753730b11fc2071247a03223e10aa0192cecc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "handler.h"
#include <vespa/vespalib/util/sync.h>
#include <vespa/vespalib/util/arrayqueue.hpp>
#include <vespa/vespalib/util/thread.h>
#include <vespa/vespalib/util/runnable.h>
#include <vespa/vespalib/util/joinable.h>

namespace vbench {

/**
 * A Handler that will forward incoming objects to another handler in
 * a separate thread. All objects are forwarded using the same thread,
 * reducing the need for synchronization in the handler forwarded
 * to. A call to join will wait until all queued object have been
 * forwarded. Object obtained after join is invoked will be discarded.
 **/
template <typename T>
class HandlerThread : public Handler<T>,
                      public vespalib::Runnable,
                      public vespalib::Joinable
{
private:
    vespalib::Monitor                          _monitor;
    vespalib::ArrayQueue<std::unique_ptr<T> >  _queue;
    Handler<T>                                &_next;
    vespalib::Thread                           _thread;
    bool                                       _done;

    void run() override;

public:
    HandlerThread(Handler<T> &next);
    ~HandlerThread();
    void handle(std::unique_ptr<T> obj) override;
    void join() override;
};

} // namespace vbench

#include "handler_thread.hpp"