]> git.vomp.tv Git - vompclient.git/blob - timers.h
Sleep timer
[vompclient.git] / timers.h
1
2
3
4
5
6
7
8
9
10 /*
11
12
13 FYI
14
15 Fixed:
16
17 The main problem was that timers only seemed to launch messages which went into The Big Mutex.
18 This has been fixed a different way by implementing locking in BoxStack - meaning timercalls
19 can now draw and update their displays without locking The Big Mutex. Problem solved. I think
20 the whole program might move more towards classes keeping more mutexes and rely less on The
21 Big Mutex.
22
23
24
25 >      Timers ... deprecated. Maybe.
26
27 >      This whole thing will be replaced with a timed-message idea. It will be possible
28 >      to send yourself (or whoever) a message with a delay, or delivery time. The message
29 >      will be handed to Command as usual, but command will do the right thing. The messages
30 >      will be delivered to the recipient _with the gui mutex locked_, meaning updates can
31 >      be done there and then in the message handler.
32
33 >      Good points:
34 >      * Cuts down on code lines
35 >      * Most (all?) timercall()s eventually send a message to command in order to
36 >        do something within The Big Mutex. This makes it easier and simpler code wise
37 >      * Gets rid of Timers.
38 >      * Hopefully gets rid of most postMessageFromOuterSpace calls
39
40 >      Bad points:
41 >      * Timers become gui only features. Solve this with a MessageReceiver interface and
42 >        have command deliver messages straight to the recipients rather than through BoxStack.
43 >      * Timer delivery accuracy becomes dependant on everything that uses The Big Mutex.
44 >        It will become more important to not block The Big Mutex.
45 >      * Cancelling timers... hmm
46
47 >      If you have any comments about the new design, like, "It's just as flawed as the old one",
48 >      then I'd appreciate hearing it before I start writing it all :)
49
50
51 */
52
53
54
55
56
57 /*
58     Copyright 2004-2005 Chris Tallon
59
60     This file is part of VOMP.
61
62     VOMP is free software; you can redistribute it and/or modify
63     it under the terms of the GNU General Public License as published by
64     the Free Software Foundation; either version 2 of the License, or
65     (at your option) any later version.
66
67     VOMP is distributed in the hope that it will be useful,
68     but WITHOUT ANY WARRANTY; without even the implied warranty of
69     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
70     GNU General Public License for more details.
71
72     You should have received a copy of the GNU General Public License
73     along with VOMP; if not, write to the Free Software
74     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
75 */
76
77 #ifndef TIMERS_H
78 #define TIMERS_H
79
80 #include <stdio.h>
81 #include <list>
82
83 #ifndef WIN32
84 #include "threadp.h"
85 #else
86 #include "threadwin.h"
87 #endif
88
89 #include "defines.h"
90
91
92 class Log;
93 class TimerReceiver;
94
95 /*
96
97 Timers documentation
98
99 Call setTimer to set a timer.... cancelTimer to delete a running timer.
100 Derive your object from TimerReceiver (timerreceiver.h), implement timercall() in your class
101 and supply your 'this' pointer to setTimer.
102
103 Once a timer has fired it does not exist anymore, you have to keep creating them if you want
104 a constant pulse.
105
106 clientReference is any int of your choice. It will be supplied back to you in the timercall()
107 so you can identify which timer has fired if you have more than one.
108
109 You can reset a timer by calling setTimer again. This will not create 2 timers, it will overwrite the first one.
110
111 You must not allow a timer to fire on an object that has been deleted already, unless you want
112 segfaulty hell.
113
114 ??
115
116 You must call cancelTimer before deleting object. cancelTimer guarantees that timercall
117 will not be called again.
118
119 */
120
121 class TimerEvent : public Thread_TYPE
122 {
123   public:
124     TimerEvent();
125
126     virtual void run();
127     virtual void threadMethod();
128     virtual void threadPostStopCleanup() {};
129
130     TimerReceiver* client;
131     int clientReference;
132     struct timespec requestedTime;
133
134     bool running;
135     bool restartAfterFinish;
136 };
137
138
139 using namespace std;
140
141 typedef list<TimerEvent*> TimerList;
142
143 class Timers : public Thread_TYPE
144 {
145   public:
146     Timers();
147     virtual ~Timers();
148     static Timers* getInstance();
149
150     int init();
151     int shutdown();
152
153     bool setTimerT(TimerReceiver* client, int clientReference, long int requestedTime, long int requestedTimeNSEC=0);
154     bool setTimerD(TimerReceiver* client, int clientReference, long int requestedSecs, long int requestedNSecs=0);
155     bool cancelTimer(TimerReceiver* client, int clientReference);
156
157     // Thread stuff
158     virtual void threadMethod();
159     virtual void threadPostStopCleanup() {};
160
161     void timerEventFinished(TimerEvent* timerEvent); // internal use only, does not return
162
163   private:
164     static Timers* instance;
165     Log* logger;
166     bool initted;
167     TimerList timerList;
168     bool resetThreadFlag;
169 };
170
171 #endif