all repos — openbox @ 8f8acc24933830d4f5784616b9b0c5896bde0b93

openbox fork - make it a bit more like ryudo

otk/timerqueuemanager.cc (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-

#ifdef    HAVE_CONFIG_H
#  include "../config.h"
#endif // HAVE_CONFIG_H

#include "timerqueuemanager.hh"
#include "display.hh"

namespace otk {

void TimerQueueManager::fire(bool wait)
{
  fd_set rfds;
  timeval now, tm, *timeout = (timeval *) 0;

  const int xfd = ConnectionNumber(Display::display);
  
  FD_ZERO(&rfds);
  FD_SET(xfd, &rfds); // break on any x events

  if (wait) {
    if (! timerList.empty()) {
      const Timer* const timer = timerList.top();
      
      gettimeofday(&now, 0);
      tm = timer->remainingTime(now);
      
      timeout = &tm;
    }

    select(xfd + 1, &rfds, 0, 0, timeout);
  }

  // check for timer timeout
  gettimeofday(&now, 0);

  // there is a small chance for deadlock here:
  // *IF* the timer list keeps getting refreshed *AND* the time between
  // timer->start() and timer->shouldFire() is within the timer's period
  // then the timer will keep firing.  This should be VERY near impossible.
  while (! timerList.empty()) {
    Timer *timer = timerList.top();
    if (! timer->shouldFire(now))
      break;

    timerList.pop();

    timer->fire();
    if (timer->recurring())
      timer->start();
  }
}


void TimerQueueManager::addTimer(Timer *timer)
{
  assert(timer);
  timerList.push(timer);
}

void TimerQueueManager::removeTimer(Timer* timer)
{
  assert(timer);
  timerList.release(timer);
}

}