summaryrefslogtreecommitdiffstats
path: root/src/lib/testutils/threaded_test.cc
blob: 88cb846e9f951eee802adc2816acf8ce4232c509 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <testutils/threaded_test.h>

namespace isc {
namespace test {

ThreadedTest::ThreadedTest()
    : thread_(), condvar_(), ready_(false), stopping_(false),
      stopped_(false) {
}

void
ThreadedTest::doSignal(bool& flag) {
    {
        std::lock_guard<std::mutex> lock(mutex_);
        flag = true;
    }
    condvar_.notify_one();
}

void
ThreadedTest::signalReady() {
    doSignal(ready_);
}

void
ThreadedTest::signalStopping() {
    doSignal(stopping_);
}

void
ThreadedTest::signalStopped() {
    doSignal(stopped_);
}

void
ThreadedTest::doWait(bool& flag) {
    std::unique_lock<std::mutex> lock(mutex_);
    while (!flag) {
        condvar_.wait(lock);
    }
}

void
ThreadedTest::waitReady() {
    doWait(ready_);
}

void
ThreadedTest::waitStopping() {
    doWait(stopping_);
}

void
ThreadedTest::waitStopped() {
    doWait(stopped_);
}

bool
ThreadedTest::isStopping() {
    std::lock_guard<std::mutex> lock(mutex_);
    return (stopping_);
}

} // end of namespace isc::test
} // end of namespace isc