1 2 /** 3 * Tencent is pleased to support the open source community by making MSEC available. 4 * 5 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. 6 * 7 * Licensed under the GNU General Public License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. You may 9 * obtain a copy of the License at 10 * 11 * https://opensource.org/licenses/GPL-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software distributed under the 14 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 15 * either express or implied. See the License for the specific language governing permissions 16 * and limitations under the License. 17 */ 18 19 20 /** 21 * @file heap_timer.h 22 */ 23 24 #ifndef _MICRO_THREAD_TIMER_H_ 25 #define _MICRO_THREAD_TIMER_H_ 26 27 #include <stdint.h> 28 #include "heap.h" 29 30 namespace NS_MICRO_THREAD 31 { 32 33 /** 34 * @brief ��ʱ��������� 35 */ 36 class CTimerNotify : public HeapEntry 37 { 38 public: 39 40 /** 41 * @brief ��ʱ֪ͨ����, ����ʵ���� 42 */ 43 virtual void timer_notify() { return;}; 44 45 /** 46 * @brief ��Ԫ��ȡֵ����, ���ڷ���ֵ�Ƚ�, ���Ӻ���ʵ��, ����Ĭ������ 47 * @return ��Ԫ��ӳ���ֵ 48 */ 49 virtual unsigned long long HeapValue() { 50 return (unsigned long long)_time_expired; 51 }; 52 53 /** 54 * @brief ���캯�� 55 */ 56 CTimerNotify() : _time_expired(0) {}; 57 58 /** 59 * @brief ���������� 60 */ 61 virtual ~CTimerNotify(){}; 62 63 /** 64 * @brief ���þ��Գ�ʱʱ��, ��λms 65 * @param expired ���Գ�ʱʱ�� ms��λ 66 */ 67 void set_expired_time(uint64_t expired) { 68 _time_expired = expired; 69 }; 70 71 /** 72 * @brief ��ȡ���Գ�ʱʱ��, ��λms 73 * @return ���Գ�ʱʱ�� ms��λ 74 */ 75 uint64_t get_expired_time() { 76 return _time_expired; 77 }; 78 79 private: 80 81 uint64_t _time_expired; // ���Եij�ʱʱ��ms��λ 82 }; 83 84 85 /** 86 * @brief ��ʱ���������� 87 */ 88 class CTimerMng 89 { 90 public: 91 92 93 /** 94 * @brief ���캯�� 95 * @param max_item ���ɹ���Ķ�ʱ��������Ŀ(ָ����Ŀ) 96 */ 97 explicit CTimerMng(uint32_t max_item = 100000); 98 99 /** 100 * @brief �������� 101 */ 102 ~CTimerMng(); 103 104 /** 105 * @brief ��ʱ�����ú��� 106 * @param timerable ��ʱ������ 107 * @param interval ��ʱ�ļ�� ms��λ 108 * @return �ɹ�����true, ����ʧ�� 109 */ 110 bool start_timer(CTimerNotify* timerable, uint32_t interval); 111 112 /** 113 * @brief ��ʱ��ֹͣ�ӿں��� 114 * @param timerable ��ʱ������ 115 */ 116 void stop_timer(CTimerNotify* timerable); 117 118 /** 119 * @brief ��ʱ����ʱ��⺯�� 120 */ 121 void check_expired(); 122 123 private: 124 125 HeapList* _heap; // ��С��ָ�� 126 }; 127 128 } 129 130 #endif 131 132