YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/background_task.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include "yb/util/background_task.h"
15
16
#include "yb/util/status.h"
17
#include "yb/util/status_log.h"
18
#include "yb/util/thread.h"
19
20
namespace yb {
21
22
BackgroundTask::BackgroundTask(
23
    std::function<void()> task, std::string category, const std::string& name,
24
    std::chrono::milliseconds interval_msec)
25
    : task_(std::move(task)),
26
      category_(category),
27
      name_(std::move(name)),
28
17.2k
      interval_(interval_msec) {}
29
30
250
BackgroundTask::~BackgroundTask() {
31
250
}
32
33
16.6k
Status BackgroundTask::Init() {
34
16.6k
  RETURN_NOT_OK(Thread::Create(category_, name_, &BackgroundTask::Run, this, &thread_));
35
16.6k
  return Status::OK();
36
16.6k
}
37
38
// Wait for pending tasks and shut down
39
322
void BackgroundTask::Shutdown() {
40
322
  {
41
322
    std::unique_lock<std::mutex> lock(mutex_);
42
322
    if (closing_) {
43
0
      VLOG(2) << "BackgroundTask already shut down";
44
0
      return;
45
0
    }
46
322
    closing_ = true;
47
322
  }
48
0
  cond_.notify_one();
49
322
  CHECK_OK(ThreadJoiner(thread_.get()).Join());
50
322
}
51
52
229k
Status BackgroundTask::Wake() {
53
229k
  {
54
229k
    std::lock_guard<std::mutex> lock(mutex_);
55
229k
    if (closing_) {
56
0
      return STATUS(ShutdownInProgress, "Task is shutting down.");
57
0
    }
58
229k
    have_job_ = true;
59
229k
  }
60
0
  cond_.notify_one();
61
229k
  return Status::OK();
62
229k
}
63
64
16.6k
void BackgroundTask::Run() {
65
19.4k
  while (WaitForJob()) {
66
2.72k
    task_();
67
2.72k
  }
68
16.6k
  VLOG
(1) << "BackgroundTask thread shutting down"16.3k
;
69
16.6k
}
70
71
19.4k
bool BackgroundTask::WaitForJob() {
72
19.4k
  std::unique_lock<std::mutex> lock(mutex_);
73
38.5k
  while(true) {
74
22.1k
    if (closing_) {
75
322
      return false;
76
322
    }
77
21.8k
    if (have_job_) {
78
2.72k
      have_job_ = false;
79
2.72k
      return true;
80
2.72k
    }
81
82
    // Wait
83
19.0k
    if (interval_ != std::chrono::milliseconds::zero()) {
84
1.04k
      cond_.wait_for(lock, interval_);
85
      // If we wake here from the interval_ timeout, then we should behave as if we have a job. If
86
      // we wake from an explicit notify from a Wake() call, we should still behave as if we have
87
      // a job.
88
1.04k
      have_job_ = true;
89
18.0k
    } else {
90
18.0k
      cond_.wait(lock);
91
18.0k
    }
92
19.0k
  }
93
19.4k
}
94
95
}  // namespace yb