aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/attribute/runnable.h
blob: b95d7d3843fadf7b607bf479761b60a8aada8cb5 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/util/sync.h>
#include <vespa/fastos/thread.h>

namespace search {

class Runnable : public FastOS_Runnable
{
protected:
    uint32_t _id;
    vespalib::Monitor _cond;
    bool _done;
    bool _stopped;

public:
    Runnable(uint32_t id) :
        _id(id), _cond(), _done(false), _stopped(false)
    { }
    void Run(FastOS_ThreadInterface *, void *) override {
        doRun();

        vespalib::MonitorGuard guard(_cond);
        _stopped = true;
        guard.broadcast();
    }
    virtual void doRun() = 0;
    void stop() {
        vespalib::MonitorGuard guard(_cond);
        _done = true;
    }
    void join() {
        vespalib::MonitorGuard guard(_cond);
        while (!_stopped) {
            guard.wait();
        }
    }
};

} // search