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/time/clock.h>
28 #include <utils/time/time.h>
29 #include <utils/time/wait.h>
30 #include <utils/system/signal.h>
31 
32 #include <unistd.h>
33 
34 #include <iostream>
35 
36 using namespace std;
37 using namespace fawkes;
38 
39 
40 class QaTestWait
41 {
42 public:
43  QaTestWait()
44  {
45  __clock = Clock::instance();
46  __until = new Time();
47  }
48 
49 
50  void mark_start()
51  {
52  __clock->get_time(__until);
53  *__until += (long int)30000;
54  }
55 
56  void wait()
57  {
58  Time now;
59  printf("Now at %p\n", &now);
60  __clock->get_time(&now);
61  usleep(0);
62  long int remaining_usec = (*__until - now).in_usec();
63  while ( remaining_usec > 0 ) {
64  usleep(remaining_usec);
65  __clock->get_time(&now);
66  remaining_usec = (*__until - now).in_usec();
67  //remaining_usec = 0;
68  }
69  }
70 
71  Clock *__clock;
72  Time *__until;
73 };
74 
75 class QaSignalHandler : public SignalHandler
76 {
77 public:
78  QaSignalHandler(Thread *thread)
79  {
80  this->thread = thread;
81  }
82 
83  virtual void 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 loop()
101  {
102  printf("Loop running\n");
103  timewait->mark_start();
104  timewait->wait();
105  //testwait->mark_start();
106  //testwait->wait();
107  }
108 
109  QaTestWait *testwait;
110  TimeWait *timewait;
111 };
112 
113 int main(int argc, char** argv)
114 {
115  QaTestThread t;
116  t.start();
117 
118  QaSignalHandler h(&t);
119  SignalManager::register_handler(SIGINT, &h);
120 
121  t.join();
122 
123  return 0;
124 }
125 
126 /// @endcond
Fawkes library namespace.
This is supposed to be the central clock in Fawkes.
Definition: clock.h:34
STL namespace.
Interface for signal handling.
Definition: signal.h:35
A class for handling time.
Definition: time.h:91
Thread class encapsulation of pthreads.
Definition: thread.h:42
Time wait utility.
Definition: wait.h:32