oceanbase

Форк
0
/
ob_thread_idling.cpp 
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 RS
14

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

20
namespace oceanbase
21
{
22
namespace rootserver
23
{
24
using namespace common;
25

26
ObThreadIdling::ObThreadIdling(volatile bool &stop)
27
    : cond_(), stop_(stop), wakeup_cnt_(0)
28
{
29
  cond_.init(ObWaitEventIds::THREAD_IDLING_COND_WAIT);
30
}
31

32
void ObThreadIdling::wakeup()
33
{
34
  ObThreadCondGuard guard(cond_);
35
  wakeup_cnt_++;
36
  int tmp_ret = cond_.broadcast();
37
  if (OB_SUCCESS != tmp_ret) {
38
    LOG_WARN_RET(tmp_ret, "condition broadcast fail", K(tmp_ret));
39
  }
40
}
41

42
int ObThreadIdling::idle()
43
{
44
  int ret = OB_SUCCESS;
45
  const int64_t max_time_us = INT64_MAX;
46
  if (OB_FAIL(idle(max_time_us))) {
47
    LOG_WARN("idle failed", K(ret));
48
  }
49
  return ret;
50
}
51

52
int ObThreadIdling::idle(const int64_t max_idle_time_us)
53
{
54
  ObThreadCondGuard guard(cond_);
55
  int ret = OB_SUCCESS;
56
  const int64_t begin_time_ms = ObTimeUtility::current_time() / 1000;
57
  while (!stop_ && wakeup_cnt_ == 0 && OB_SUCC(ret)) {
58
    const int64_t now_ms = ObTimeUtility::current_time() / 1000;
59
    const int64_t idle_time_ms = std::min(max_idle_time_us, get_idle_interval_us()) / 1000;
60
    int64_t wait_time_ms = begin_time_ms + idle_time_ms - now_ms;
61
    IGNORE_RETURN lib::Thread::update_loop_ts();
62
    if (wait_time_ms <= 0) {
63
      break;
64
    }
65
    wait_time_ms = std::min(1000l, wait_time_ms);
66
    if (OB_FAIL(cond_.wait(static_cast<int>(wait_time_ms)))) {
67
      ret = OB_SUCCESS;
68
    } else {
69
      break;
70
    }
71
  }
72
  if (stop_) {
73
    ret = OB_CANCELED;
74
  }
75
  wakeup_cnt_ = 0;
76
  return ret;
77
}
78

79
} // end namespace rootserver
80
} // end namespace oceanbase
81

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.