00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include "timers.hh"
00047
00048
00049
00050
00051
00052 TimerManager::TimerManager()
00053 {
00054 struct timeval tv;
00055
00056
00057 next_handle_ = 1;
00058 GetTime(&tv);
00059 SetSeed(&tv);
00060
00061
00062 #ifdef NS_DIFFUSION
00063 eq_ = new DiffEventQueue(this);
00064 #else
00065 eq_ = new EventQueue;
00066 #endif // NS_DIFFUSION
00067
00068 #ifdef USE_THREADS
00069 queue_mtx_ = new pthread_mutex_t;
00070 pthread_mutex_init(queue_mtx_, NULL);
00071 #endif // USE_THREADS
00072 }
00073
00074
00075
00076
00077
00078 handle TimerManager::addTimer(int timeout, TimerCallback *cb)
00079 {
00080 TimerEntry *entry;
00081
00082 #ifdef USE_THREADS
00083 pthread_mutex_lock(queue_mtx_);
00084 #endif // USE_THREADS
00085 entry = new TimerEntry(next_handle_, timeout, cb);
00086 eq_->eqAddAfter(next_handle_, entry, timeout);
00087 next_handle_++;
00088
00089 #ifdef USE_THREADS
00090 pthread_mutex_unlock(queue_mtx_);
00091 #endif // USE_THREADS
00092 return entry->hdl_;
00093 }
00094
00095
00096
00097
00098
00099 bool TimerManager::removeTimer(handle hdl)
00100 {
00101 #ifdef NS_DIFFUSION
00102 if (eq_->eqRemove(hdl) == false){
00103 fprintf(stderr, "Error: Can't remove event from queue !\n");
00104 return false;
00105 }
00106 return true;
00107 #else
00108 QueueEvent *e = NULL;
00109 TimerEntry *entry;
00110
00111 #ifdef USE_THREADS
00112 pthread_mutex_lock(queue_mtx_);
00113 #endif // USE_THREADS
00114
00115
00116 e = eq_->eqFindEvent(hdl);
00117
00118
00119 if (e){
00120 entry = (TimerEntry *) e->payload_;
00121 if (eq_->eqRemove(hdl) == false){
00122 fprintf(stderr, "Error: Can't remove event from queue !\n");
00123 exit(-1);
00124 }
00125
00126
00127
00128 delete entry;
00129 delete e;
00130 }
00131 else{
00132 #ifdef USE_THREADS
00133 pthread_mutex_unlock(queue_mtx_);
00134 #endif // USE_THREADS
00135 return false;
00136 }
00137
00138 #ifdef USE_THREADS
00139 pthread_mutex_unlock(queue_mtx_);
00140 #endif // USE_THREADS
00141 return true;
00142 #endif // NS_DIFFUSION
00143 }
00144
00145
00146 void TimerManager::nextTimerTime(struct timeval *tv)
00147 {
00148 #ifdef USE_THREADS
00149 pthread_mutex_lock(queue_mtx_);
00150 #endif // USE_THREADS
00151 eq_->eqNextTimer(tv);
00152 #ifdef USE_THREADS
00153 pthread_mutex_unlock(queue_mtx_);
00154 #endif // USE_THREADS
00155 }
00156
00157
00158 #ifdef NS_DIFFUSION
00159 void TimerManager::diffTimeout(DiffEvent *e)
00160 {
00161 TimerEntry *entry = (TimerEntry *) e->payload();
00162
00163
00164 int new_timeout = entry->cb_->expire();
00165
00166 if (new_timeout >= 0){
00167 if (new_timeout > 0){
00168
00169 entry->timeout_ = new_timeout;
00170 }
00171 eq_->eqAddAfter(entry->hdl_, (TimerEntry *) entry, entry->timeout_);
00172 }
00173 else{
00174 delete entry;
00175 }
00176 delete e;
00177 }
00178 #else
00179 void TimerManager::executeNextTimer()
00180 {
00181 #ifdef USE_THREADS
00182 pthread_mutex_lock(queue_mtx_);
00183 #endif // USE_THREADS
00184 QueueEvent *e = eq_->eqPop();
00185 TimerEntry *entry = (TimerEntry *) e->payload_;
00186
00187 #ifdef USE_THREADS
00188 pthread_mutex_unlock(queue_mtx_);
00189 #endif // USE_THREADS
00190
00191 int new_timeout = entry->cb_->expire();
00192
00193 if (new_timeout >= 0){
00194 if (new_timeout > 0){
00195
00196 entry->timeout_ = new_timeout;
00197 }
00198 #ifdef USE_THREADS
00199 pthread_mutex_lock(queue_mtx_);
00200 #endif // USE_THREADS
00201 eq_->eqAddAfter(entry->hdl_, (TimerEntry *) entry, entry->timeout_);
00202 #ifdef USE_THREADS
00203 pthread_mutex_unlock(queue_mtx_);
00204 #endif // USE_THREADS
00205 }
00206 else{
00207 delete entry;
00208 }
00209 delete e;
00210 }
00211
00212 void TimerManager::executeAllExpiredTimers()
00213 {
00214 struct timeval tv;
00215
00216
00217 nextTimerTime(&tv);
00218
00219
00220
00221 while (tv.tv_sec == 0 && tv.tv_usec == 0){
00222
00223 executeNextTimer();
00224 nextTimerTime(&tv);
00225 }
00226 }
00227 #endif // NS_DIFFUSION