oceanbase
80 строк · 2.1 Кб
1/**
2* Copyright (c) 2021 OceanBase
3* OceanBase CE is licensed under Mulan PubL v2.
4* You can use this software according to the terms and conditions of the Mulan PubL v2.
5* You may obtain a copy of Mulan PubL v2 at:
6* http://license.coscl.org.cn/MulanPubL-2.0
7* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10* See the Mulan PubL v2 for more details.
11*/
12
13#define USING_LOG_PREFIX RS14
15#include "ob_thread_idling.h"16#include "share/ob_define.h"17#include "lib/time/ob_time_utility.h"18#include "lib/thread/thread.h"19
20namespace oceanbase21{
22namespace rootserver23{
24using namespace common;25
26ObThreadIdling::ObThreadIdling(volatile bool &stop)27: cond_(), stop_(stop), wakeup_cnt_(0)28{
29cond_.init(ObWaitEventIds::THREAD_IDLING_COND_WAIT);30}
31
32void ObThreadIdling::wakeup()33{
34ObThreadCondGuard guard(cond_);35wakeup_cnt_++;36int tmp_ret = cond_.broadcast();37if (OB_SUCCESS != tmp_ret) {38LOG_WARN_RET(tmp_ret, "condition broadcast fail", K(tmp_ret));39}40}
41
42int ObThreadIdling::idle()43{
44int ret = OB_SUCCESS;45const int64_t max_time_us = INT64_MAX;46if (OB_FAIL(idle(max_time_us))) {47LOG_WARN("idle failed", K(ret));48}49return ret;50}
51
52int ObThreadIdling::idle(const int64_t max_idle_time_us)53{
54ObThreadCondGuard guard(cond_);55int ret = OB_SUCCESS;56const int64_t begin_time_ms = ObTimeUtility::current_time() / 1000;57while (!stop_ && wakeup_cnt_ == 0 && OB_SUCC(ret)) {58const int64_t now_ms = ObTimeUtility::current_time() / 1000;59const int64_t idle_time_ms = std::min(max_idle_time_us, get_idle_interval_us()) / 1000;60int64_t wait_time_ms = begin_time_ms + idle_time_ms - now_ms;61IGNORE_RETURN lib::Thread::update_loop_ts();62if (wait_time_ms <= 0) {63break;64}65wait_time_ms = std::min(1000l, wait_time_ms);66if (OB_FAIL(cond_.wait(static_cast<int>(wait_time_ms)))) {67ret = OB_SUCCESS;68} else {69break;70}71}72if (stop_) {73ret = OB_CANCELED;74}75wakeup_cnt_ = 0;76return ret;77}
78
79} // end namespace rootserver80} // end namespace oceanbase81