diff options
author | Hanoh Haim <hhaim@cisco.com> | 2015-06-24 14:03:29 +0300 |
---|---|---|
committer | Hanoh Haim <hhaim@cisco.com> | 2015-06-24 14:03:29 +0300 |
commit | 8b52a31ed2c299b759f330c4f976b9c70f5765f4 (patch) | |
tree | 9d6da5438b5b56b1d2d57e6c13494b4e65d000e7 /src/timer_wheel_pq.h |
first version
Diffstat (limited to 'src/timer_wheel_pq.h')
-rwxr-xr-x | src/timer_wheel_pq.h | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/timer_wheel_pq.h b/src/timer_wheel_pq.h new file mode 100755 index 00000000..cca5c07b --- /dev/null +++ b/src/timer_wheel_pq.h @@ -0,0 +1,134 @@ +/* + Hanoh Haim + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#ifndef TW_WHEEL_PQ +#define TW_WHEEL_PQ + +#include <stdint.h> +#include <stdio.h> +#include <string> +#include <vector> +#include <map> +#include <algorithm> +#include <queue> +#include <assert.h> + + +class CFlowTimerHandle; + +struct CFlowTimer { +public: + CFlowTimer(){ + m_updated_time = -1.0; + + } + /* C1 */ + /* time to expire */ + double m_time; + /* time when timer was updated */ + double m_updated_time; + + CFlowTimerHandle * m_flow; /* back pointer to the flow */ + + bool is_valid(){ + return (m_flow ? true : false); + } + +public: + bool operator <(const CFlowTimer * rsh) const { + return (m_time<rsh->m_time); + } + bool operator ==(const CFlowTimer * rsh) const { + return (m_time == rsh->m_time); + } + bool operator >(const CFlowTimer * rsh) const { + return (m_time>rsh->m_time); + } + +}; + +struct CFlowTimerCompare +{ + bool operator() (const CFlowTimer * lhs, const CFlowTimer * rhs) + { + return lhs->m_time > rhs->m_time; + } +}; + +class CFlowTimerHandle; +typedef void(*CallbackType_t)(CFlowTimerHandle * timer_handle); + +class CFlowTimerHandle { +public: + CFlowTimerHandle(){ + m_timer = 0; + m_object = 0; + m_object1=0; + m_callback = 0; + m_id = 0; + } + CFlowTimer * m_timer; + void * m_object; + void * m_object1; + CallbackType_t m_callback; + uint32_t m_id; +}; + +typedef CFlowTimer * timer_handle_t; + +typedef std::priority_queue<CFlowTimer *, std::vector<CFlowTimer *>, CFlowTimerCompare> tw_pqueue_t; + +class CTimerWheel { + +public: + CTimerWheel(){ + m_st_alloc=0; + m_st_free=0; + m_st_start=0; + m_st_stop=0; + m_st_handle=0; + } +public: + void restart_timer(CFlowTimerHandle * timer,double new_time); + void stop_timer(CFlowTimerHandle * timer); + bool peek_top_time(double & time); + void try_handle_events(double now); + void drain_all(); + + bool handle(); +public: + void Dump(FILE *fd); + void dump_json(std::string & json ); + +private: + tw_pqueue_t m_pq; +public: + + uint32_t m_st_alloc; + uint32_t m_st_free; + uint32_t m_st_start; + uint32_t m_st_stop; + uint32_t m_st_handle; + +}; + + +#endif |