patch 4.0
This commit is contained in:
115
deps/oblib/src/lib/task/ob_timer.h
vendored
115
deps/oblib/src/lib/task/ob_timer.h
vendored
@ -15,30 +15,35 @@
|
||||
|
||||
#include <pthread.h>
|
||||
#include <typeinfo>
|
||||
#include "lib/time/Time.h"
|
||||
#include "lib/lock/mutex.h"
|
||||
#include "lib/lock/ob_monitor.h"
|
||||
#include "lib/ob_define.h"
|
||||
#include "lib/utility/ob_print_utils.h"
|
||||
#include "lib/thread/thread_pool.h"
|
||||
|
||||
namespace oceanbase {
|
||||
namespace tests {
|
||||
namespace blocksstable {
|
||||
namespace oceanbase
|
||||
{
|
||||
namespace tests
|
||||
{
|
||||
namespace blocksstable
|
||||
{
|
||||
class FakeTabletManager;
|
||||
}
|
||||
} // namespace tests
|
||||
}
|
||||
|
||||
namespace common {
|
||||
class ObTimerTask {
|
||||
namespace common
|
||||
{
|
||||
class ObTimer;
|
||||
|
||||
class ObTimerTask
|
||||
{
|
||||
friend class ObTimer;
|
||||
public:
|
||||
ObTimerTask() : timeout_check_(true)
|
||||
{}
|
||||
virtual ~ObTimerTask(){};
|
||||
virtual void cancelCallBack()
|
||||
{}
|
||||
ObTimerTask() : timeout_check_(true), timer_(nullptr) {}
|
||||
virtual ~ObTimerTask() { abort_unless(OB_ISNULL(ATOMIC_LOAD(&timer_))); }
|
||||
virtual void cancelCallBack() {}
|
||||
virtual void runTimerTask() = 0;
|
||||
virtual int64_t to_string(char* buf, const int64_t buf_len) const
|
||||
virtual int64_t to_string(char *buf, const int64_t buf_len) const
|
||||
{
|
||||
int64_t pos = 0;
|
||||
if (NULL != buf && buf_len > 0) {
|
||||
@ -47,84 +52,60 @@ public:
|
||||
return pos;
|
||||
}
|
||||
|
||||
inline void enable_timeout_check()
|
||||
{
|
||||
timeout_check_ = true;
|
||||
}
|
||||
inline void disable_timeout_check()
|
||||
{
|
||||
timeout_check_ = false;
|
||||
}
|
||||
inline bool timeout_check()
|
||||
{
|
||||
return timeout_check_;
|
||||
}
|
||||
virtual bool need_retry()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void enable_timeout_check() { timeout_check_ = true; }
|
||||
inline void disable_timeout_check() { timeout_check_ = false; }
|
||||
inline bool timeout_check() { return timeout_check_; }
|
||||
virtual bool need_retry() { return true; }
|
||||
private:
|
||||
bool timeout_check_;
|
||||
ObTimer* timer_;
|
||||
};
|
||||
|
||||
class ObTimer : public lib::ThreadPool {
|
||||
class ObTimer
|
||||
: public lib::ThreadPool
|
||||
{
|
||||
public:
|
||||
friend class oceanbase::tests::blocksstable::FakeTabletManager;
|
||||
ObTimer()
|
||||
: tasks_num_(0),
|
||||
wakeup_time_(0),
|
||||
is_inited_(false),
|
||||
is_stopped_(false),
|
||||
is_destroyed_(false),
|
||||
has_running_task_(false),
|
||||
has_running_repeat_task_(false),
|
||||
thread_id_(-1),
|
||||
thread_name_(nullptr)
|
||||
{}
|
||||
ObTimer(): tasks_num_(0), wakeup_time_(0), is_inited_(false), is_stopped_(false),
|
||||
is_destroyed_(false), has_running_task_(false), has_running_repeat_task_(false),
|
||||
thread_id_(-1), thread_name_(nullptr) {}
|
||||
~ObTimer();
|
||||
int init(const char* thread_name = nullptr);
|
||||
bool inited() const;
|
||||
int create(); // create new timer thread and start
|
||||
int start(); // only start
|
||||
int create(); // create new timer thread and start
|
||||
int start(); // only start
|
||||
void stop(); // only stop
|
||||
void wait(); // wait all running task finish
|
||||
void destroy();
|
||||
|
||||
public:
|
||||
int schedule(ObTimerTask& task, const int64_t delay, bool repeate = false);
|
||||
int schedule_repeate_task_immediately(ObTimerTask& task, const int64_t delay);
|
||||
bool task_exist(const common::ObTimerTask& task);
|
||||
int task_exist(const common::ObTimerTask& task, bool& exist)
|
||||
int schedule(ObTimerTask &task, const int64_t delay, bool repeate = false);
|
||||
int schedule_repeate_task_immediately(ObTimerTask &task, const int64_t delay);
|
||||
bool task_exist(const common::ObTimerTask &task);
|
||||
int task_exist(const common::ObTimerTask &task, bool &exist)
|
||||
{
|
||||
exist = task_exist(task);
|
||||
return OB_SUCCESS;
|
||||
}
|
||||
int cancel(const ObTimerTask& task);
|
||||
int cancel(const ObTimerTask &task);
|
||||
void cancel_all();
|
||||
int32_t get_tasks_num() const
|
||||
{
|
||||
return tasks_num_;
|
||||
}
|
||||
int32_t get_tasks_num() const { return tasks_num_; }
|
||||
void dump() const;
|
||||
|
||||
private:
|
||||
struct Token {
|
||||
Token() : scheduled_time(0), delay(0), task(NULL)
|
||||
{}
|
||||
Token(const int64_t st, const int64_t dt, ObTimerTask* task) : scheduled_time(st), delay(dt), task(task)
|
||||
{}
|
||||
struct Token
|
||||
{
|
||||
Token(): scheduled_time(0), delay(0), task(NULL) {}
|
||||
Token(const int64_t st, const int64_t dt, ObTimerTask *task)
|
||||
: scheduled_time(st), delay(dt), task(task) {}
|
||||
TO_STRING_KV(K(scheduled_time), K(delay), KP(task), KPC(task));
|
||||
int64_t scheduled_time;
|
||||
int64_t delay;
|
||||
ObTimerTask* task;
|
||||
ObTimerTask *task;
|
||||
};
|
||||
static const int32_t MAX_TASK_NUM = 32;
|
||||
int insert_token(const Token& token);
|
||||
int insert_token(const Token &token);
|
||||
void run1() final;
|
||||
int schedule_task(ObTimerTask& task, const int64_t delay, const bool repeate, const bool is_scheduled_immediately);
|
||||
int schedule_task(ObTimerTask &task, const int64_t delay, const bool repeate, const bool is_scheduled_immediately);
|
||||
DISALLOW_COPY_AND_ASSIGN(ObTimer);
|
||||
|
||||
private:
|
||||
int32_t tasks_num_;
|
||||
int64_t wakeup_time_;
|
||||
@ -139,7 +120,7 @@ private:
|
||||
const char* thread_name_;
|
||||
};
|
||||
|
||||
} // namespace common
|
||||
} // namespace oceanbase
|
||||
} /* common */
|
||||
} /* oceanbase */
|
||||
|
||||
#endif // OCEANBASE_COMMON_OB_TIMER_
|
||||
#endif // OCEANBASE_COMMON_OB_TIMER_
|
||||
|
||||
Reference in New Issue
Block a user