00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __PANALIX_TIMER_HPP
00012 #define __PANALIX_TIMER_HPP
00013
00014 #include "src/collection/avltree.hpp"
00015
00016
00017 namespace Arch { namespace x86 { namespace RTC { extern volatile uint64 up_time; } } }
00018 using Arch::x86::RTC::up_time;
00019
00020
00021 namespace Thread {
00022 namespace Timer {
00023 struct timer_handle {
00024 uint32 d;
00025 timer_handle(uint32 d) : d(d) { }
00026 timer_handle() : d(0) { }
00027 };
00028
00029 typedef int (*timer_proc)(int,int);
00030
00031 void initTimerThread();
00032
00033 struct timer_handle add(timer_proc proc, uint32 interval, int arg1, int arg2);
00034 bool remove(struct timer_handle &hndl);
00035
00036
00037 using namespace Collection;
00038
00043 template <typename T,int (*cmp)(T,T)>
00044 bool avltree_insert_with_timer(uint32 ticks, struct AvlTree<T,cmp> *tr, T v);
00045
00046
00047
00048 template<typename T, int (*cmp)(T,T)>
00049 int avltree_remove_timer(struct AvlTree<T,cmp> *tr, T *v) {
00050 tr->remove(*v);
00051 kfree(v);
00052 return 0;
00053 }
00054
00056 template <typename T,int (*cmp)(T,T)>
00057 bool avltree_insert_with_timer(uint32 ticks, struct AvlTree<T,cmp> *tr, T v) {
00058 T *vptr = static_cast<T*>(kmalloc(sizeof(T),"avl timer helper object"));
00059 memmove(vptr, &v, sizeof(T));
00060 bool ret = tr->insert(v);
00061 int (*f)(struct AvlTree<T,cmp>*,T*)= &avltree_remove_timer<T,cmp>;
00062 add(
00063
00064 reinterpret_cast<int(*)(int,int)>(f),
00065 ticks,
00066 reinterpret_cast<int>(tr),
00067 reinterpret_cast<int>(vptr));
00068 return ret;
00069 }
00070
00072 #if 0
00073 template<typename T, int (*cmp)(T*,T*)>
00074 bool avltree_insert_with_timer(struct AvlTree<T*,cmp> *tr, T *v, uint32 ticks);
00075
00076 template<>
00077 bool avltree_insert_with_timer(struct AvlTree<uint32,generic_cmp<uint32> > *tr, uint32 v, uint32 ticks);
00078
00079
00080 template<typename T, int (*cmp)(T*,T*)>
00081 int avltree_remove_timer(struct AvlTree<T,cmp> *tr, T *v) {
00082 tr->remove(v);
00083 return 0;
00084 }
00085
00086 template <typename T, int (*cmp)(T*,T*)>
00087 bool avltree_insert_with_timer(struct AvlTree<T*,cmp> *tr, T *v, uint32 ticks) {
00088 bool ret = tr->insert(v);
00089 add(reinterpret_cast<int(*)(int,int)>(avltree_remove_timer<T,cmp>), ticks, reinterpret_cast<int>(tr), reinterpret_cast<int>(v));
00090 return ret;
00091 }
00092
00093 template< >
00094 int avltree_remove_timer<uint32, generic_cmp<uint32> >(struct AvlTree<uint32,generic_cmp<uint32> > *tr, uint32 v) {
00095 tr->remove(v);
00096 return 0;
00097 }
00098
00099 template<>
00100 bool avltree_insert_with_timer(struct AvlTree<uint32,generic_cmp<uint32> > *tr, uint32 v, uint32 ticks) {
00101 bool ret = tr->insert(v);
00102 add(reinterpret_cast<int(*)(int,int)>(avltree_remove_timer<uint32,generic_cmp<uint32> >), ticks, reinterpret_cast<int>(tr), static_cast<int>(v));
00103 return ret;
00104 }
00105 #endif
00106 }
00107 }
00108
00109
00110 #endif
00111