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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
// Copyright (C) 2018-2021 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>
#include <signal.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace util {

void
WatchedThread::start(const std::function<void()>& thread_main) {
    clearReady(ERROR);
    clearReady(READY);
    clearReady(TERMINATE);
    setErrorInternal("no error");
    // Protect us against signals
    sigset_t sset;
    sigset_t osset;
    sigemptyset(&sset);
    sigaddset(&sset, SIGCHLD);
    sigaddset(&sset, SIGINT);
    sigaddset(&sset, SIGHUP);
    sigaddset(&sset, SIGTERM);
    pthread_sigmask(SIG_BLOCK, &sset, &osset);
    try {
        thread_.reset(new std::thread(thread_main));
    } catch (...) {
        // Restore signal mask.
        pthread_sigmask(SIG_SETMASK, &osset, 0);
        throw;
    }
    // Restore signal mask.
    pthread_sigmask(SIG_SETMASK, &osset, 0);
}

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