summaryrefslogtreecommitdiffstats
path: root/src/lib/util/watched_thread.cc
blob: 6cd86ab8b9b3f4e9bb601ee1886a788c816d9660 (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
74
75
76
77
78
79
80
81
82
83
84
// 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 <util/watched_thread.h>

namespace isc {
namespace util {

void
WatchedThread::start(const boost::function<void()>& thread_main) {
    clearReady(ERROR);
    clearReady(READY);
    clearReady(TERMINATE);
    setErrorInternal("no error");
    thread_.reset(new std::thread(thread_main));
}

int
WatchedThread::getWatchFd(WatchType watch_type) {
    return(sockets_[watch_type].getSelectFd());
}

void
WatchedThread::markReady(WatchType watch_type)  {
    sockets_[watch_type].markReady();
}

bool
WatchedThread::isReady(WatchType watch_type) {
    return (sockets_[watch_type].isReady());
}

void
WatchedThread::clearReady(WatchType watch_type) {
    sockets_[watch_type].clearReady();
}

bool
WatchedThread::shouldTerminate() {
    if (sockets_[TERMINATE].isReady()) {
        clearReady(TERMINATE);
        return (true);
    }

    return (false);
}

void
WatchedThread::stop() {
    if (thread_) {
        markReady(TERMINATE);
        thread_->join();
        thread_.reset();
    }

    clearReady(ERROR);
    clearReady(READY);
    setErrorInternal("thread stopped");
}

void
WatchedThread::setErrorInternal(const std::string& error_msg) {
    std::lock_guard<std::mutex> lock(mutex_);
    last_error_ = error_msg;
}

void
WatchedThread::setError(const std::string& error_msg) {
    setErrorInternal(error_msg);
    markReady(ERROR);
}

std::string
WatchedThread::getLastError() {
    std::lock_guard<std::mutex> lock(mutex_);
    return (last_error_);
}

}  // namespace util
}  // namespace isc