Revision e386e4be src/timer.cpp

View differences:

src/timer.cpp
42 42
  struct timeval now;
43 43
  gettimeofday(&now, 0);
44 44

  
45
  std::cout << "Calculating time difference between " << future.tv_sec << ", " 
46
	    << future.tv_usec << " and now " << now.tv_sec << ", " << now.tv_usec << std::endl;
47

  
45 48
  interval.tv_sec = future.tv_sec - now.tv_sec;
46 49
  interval.tv_usec = future.tv_usec - now.tv_usec;
47 50

  
......
91 94
  it.it_interval.tv_usec = 0;
92 95
  it.it_value = interval;
93 96
    
94
  std::cout << "Setting up itimer\n";
97
  std::cout << "Setting up itimer for interval " 
98
	    << it.it_value.tv_sec << ", " << it.it_value.tv_usec << std::endl;
95 99
  setitimer(ITIMER_REAL, &it, 0);
96 100
}
97 101

  
98 102
static void handleAlarmSig(int sig)
99 103
{
100
  std::cout << "Got alarm signal\n";
104
  std::cout << "Got alarm signal" << std::endl;
101 105
  // Alarm signal is masked already
102 106
  fireTimers();
103 107
  if (!timerQueue.empty())
......
107 111
static struct SetupSignalHandler {
108 112
  SetupSignalHandler()
109 113
  {
110
    std::cout << "Setting up signal handler\n";
114
    std::cout << "Setting up signal handler" << std::endl;
111 115
    struct sigaction act;
112 116
    act.sa_handler = handleAlarmSig;
113 117
    sigemptyset(&act.sa_mask);
......
120 124

  
121 125
void Timer::start()
122 126
{
127
  stop();
128

  
123 129
  // Calculate expiry time
124 130
  gettimeofday(&expiry, 0);
125 131
  expiry.tv_usec += delayMs * 1000;
126
  expiry.tv_sec += expiry.tv_usec % 1000000;
132
  expiry.tv_sec += expiry.tv_usec / 1000000;
127 133
  expiry.tv_usec %= 1000000;
128 134
  
129 135
  // Add the timer to the queue
......
140 146

  
141 147
void Timer::stop()
142 148
{
143
  maskTimer();
144
  TimerQueue copy = timerQueue;
149
  if (started) {
150
    started = false;
151
    maskTimer();
152
    TimerQueue copy = timerQueue;
145 153

  
146
  while (!timerQueue.empty())
147
    timerQueue.pop();
154
    while (!timerQueue.empty())
155
      timerQueue.pop();
148 156

  
149
  while (!copy.empty()) {
150
    Timer *top = copy.top();
151
    copy.pop();
152
    if (top != this)
153
      timerQueue.push(top);
157
    while (!copy.empty()) {
158
      Timer *top = copy.top();
159
      copy.pop();
160
      if (top != this)
161
	timerQueue.push(top);
162
    }
163
    unmaskTimer();
154 164
  }
155
  unmaskTimer();
156 165
}

Also available in: Unified diff