Fawkes API  Fawkes Development Version
qa_timebug.cpp
1 
2 /***************************************************************************
3  * qa_timebug.cpp - QA app to find a potential bug related to the Time class
4  *
5  * Created: Tue Dec 18 10:38:30 2007
6  * Copyright 2007 Tim Niemueller
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 /// @cond QA
25 
26 #include <core/threading/thread.h>
27 #include <utils/system/signal.h>
28 #include <utils/time/clock.h>
29 #include <utils/time/time.h>
30 #include <utils/time/wait.h>
31 
32 #include <iostream>
33 #include <unistd.h>
34 
35 using namespace std;
36 using namespace fawkes;
37 
38 class QaTestWait
39 {
40 public:
41  QaTestWait()
42  {
43  clock_ = Clock::instance();
44  until_ = new Time();
45  }
46 
47  void
48  mark_start()
49  {
50  clock_->get_time(until_);
51  *until_ += (long int)30000;
52  }
53 
54  void
55  wait()
56  {
57  Time now;
58  printf("Now at %p\n", &now);
59  clock_->get_time(&now);
60  usleep(0);
61  long int remaining_usec = (*until_ - now).in_usec();
62  while (remaining_usec > 0) {
63  usleep(remaining_usec);
64  clock_->get_time(&now);
65  remaining_usec = (*until_ - now).in_usec();
66  //remaining_usec = 0;
67  }
68  }
69 
70  Clock *clock_;
71  Time * until_;
72 };
73 
74 class QaSignalHandler : public SignalHandler
75 {
76 public:
77  QaSignalHandler(Thread *thread)
78  {
79  this->thread = thread;
80  }
81 
82  virtual void
83  handle_signal(int signum)
84  {
85  thread->cancel();
86  }
87 
88  Thread *thread;
89 };
90 
91 class QaTestThread : public Thread
92 {
93 public:
94  QaTestThread() : Thread("QaTestThread")
95  {
96  timewait = new TimeWait(Clock::instance(), 30000);
97  testwait = new QaTestWait();
98  }
99 
100  virtual void
101  loop()
102  {
103  printf("Loop running\n");
104  timewait->mark_start();
105  timewait->wait();
106  //testwait->mark_start();
107  //testwait->wait();
108  }
109 
110  QaTestWait *testwait;
111  TimeWait * timewait;
112 };
113 
114 int
115 main(int argc, char **argv)
116 {
117  QaTestThread t;
118  t.start();
119 
120  QaSignalHandler h(&t);
121  SignalManager::register_handler(SIGINT, &h);
122 
123  t.join();
124 
125  return 0;
126 }
127 
128 /// @endcond
This is supposed to be the central clock in Fawkes.
Definition: clock.h:35
Interface for signal handling.
Definition: signal.h:36
Thread class encapsulation of pthreads.
Definition: thread.h:46
Time wait utility.
Definition: wait.h:33
A class for handling time.
Definition: time.h:93
Fawkes library namespace.