timeout_info.h 2.0 KB
Newer Older
羽飞's avatar
羽飞 已提交
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
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2010
//

#ifndef __COMMON_TIME_TIMEOUT_INFO_H__
#define __COMMON_TIME_TIMEOUT_INFO_H__

#include <time.h>

#include "common/lang/mutex.h"
namespace common {

/**
 * Timeout info class used to judge if a certain deadline_ has reached or not.
 * It's good to use handle-body to automate the reference count
 * increase/decrease. However, explicit attach/detach interfaces
 * are used here to simplify the implementation.
 */

class TimeoutInfo {
31
public:
羽飞's avatar
羽飞 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  /**
   * Constructor
   * @param[in] deadline_  deadline_ of this timeout
   */
  TimeoutInfo(time_t deadline_);

  // Increase ref count
  void attach();

  // Decrease ref count
  void detach();

  // Check if it has timed out
  bool has_timed_out();

47
private:
羽飞's avatar
羽飞 已提交
48 49 50 51 52 53 54 55
  // Forbid copy ctor and =() to support ref count

  // Copy constructor.
  TimeoutInfo(const TimeoutInfo &ti);

  // Assignment operator.
  TimeoutInfo &operator=(const TimeoutInfo &ti);

56
protected:
羽飞's avatar
羽飞 已提交
57 58 59 60 61 62
  // Avoid heap-based \c TimeoutInfo
  // so it can easily associated with \c StageEvent

  // Destructor.
  ~TimeoutInfo();

63 64
private:
  time_t deadline_;  // when should this be timed out
羽飞's avatar
羽飞 已提交
65 66 67 68

  // used to predict timeout if now + reservedTime > deadline_
  // time_t reservedTime;

69
  bool is_timed_out_;  // timeout flag
羽飞's avatar
羽飞 已提交
70 71

  int ref_cnt_;            // reference count of this object
72
  pthread_mutex_t mutex_;  // mutex_ to protect ref_cnt_ and flag
羽飞's avatar
羽飞 已提交
73 74
};

75 76
}  // namespace common
#endif  // __COMMON_TIME_TIMEOUT_INFO_H__