forked from lightspark/lightspark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.cpp
275 lines (235 loc) · 6.12 KB
/
timer.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**************************************************************************
Lightspark, a free flash player implementation
Copyright (C) 2009,2010 Alessandro Pignotti ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#include "swf.h"
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include "timer.h"
#include "compat.h"
using namespace lightspark;
using namespace std;
uint64_t lightspark::timespecToMsecs(timespec t)
{
uint64_t ret=0;
ret+=(t.tv_sec*1000LL);
ret+=(t.tv_nsec/1000000LL);
return ret;
}
uint64_t lightspark::timespecToUsecs(timespec t)
{
uint64_t ret=0;
ret+=(t.tv_sec*1000000LL);
ret+=(t.tv_nsec/1000LL);
return ret;
}
timespec lightspark::msecsToTimespec(uint64_t time)
{
timespec ret;
ret.tv_sec=time/1000LL;
ret.tv_nsec=(time%1000LL)*1000000LL;
return ret;
}
TimerThread::TimerThread(SystemState* s):m_sys(s),currentJob(NULL),stopped(false)
{
sem_init(&mutex,0,1);
sem_init(&newEvent,0,0);
pthread_create(&t,NULL,(thread_worker)timer_worker,this);
}
void TimerThread::stop()
{
if(!stopped)
{
stopped=true;
sem_post(&newEvent);
}
}
void TimerThread::wait()
{
stop();
pthread_join(t,NULL);
}
TimerThread::~TimerThread()
{
stop();
list<TimingEvent*>::iterator it=pendingEvents.begin();
for(;it!=pendingEvents.end();++it)
delete *it;
sem_destroy(&mutex);
sem_destroy(&newEvent);
}
void TimerThread::insertNewEvent_nolock(TimingEvent* e)
{
list<TimingEvent*>::iterator it=pendingEvents.begin();
//If there are no events pending, or this is earlier than the first, signal newEvent
if(pendingEvents.empty() || (*it)->timing > e->timing)
{
pendingEvents.insert(it, e);
sem_post(&newEvent);
return;
}
++it;
for(;it!=pendingEvents.end();++it)
{
if((*it)->timing > e->timing)
{
pendingEvents.insert(it, e);
return;
}
}
//Event has to be inserted after all the others
pendingEvents.insert(pendingEvents.end(), e);
}
void TimerThread::insertNewEvent(TimingEvent* e)
{
sem_wait(&mutex);
insertNewEvent_nolock(e);
sem_post(&mutex);
}
//Unsafe debugging routine
void TimerThread::dumpJobs()
{
list<TimingEvent*>::iterator it=pendingEvents.begin();
//Find if the job is in the list
for(;it!=pendingEvents.end();++it)
cout << (*it)->job << endl;
}
void* TimerThread::timer_worker(TimerThread* th)
{
sys=th->m_sys;
while(1)
{
//Wait until a time expires
sem_wait(&th->mutex);
//Check if there is any event
while(th->pendingEvents.empty())
{
sem_post(&th->mutex);
sem_wait(&th->newEvent);
if(th->stopped)
pthread_exit(0);
sem_wait(&th->mutex);
}
//Get expiration of first event
uint64_t timing=th->pendingEvents.front()->timing;
//Wait for the absolute time, or a newEvent signal
timespec tmpt=msecsToTimespec(timing);
sem_post(&th->mutex);
int ret=sem_timedwait(&th->newEvent, &tmpt);
if(th->stopped)
pthread_exit(0);
if(ret==0)
continue;
//The first event has expired
int err=errno;
if(err!=ETIMEDOUT)
{
LOG(LOG_ERROR,_("Unexpected failure of sem_timedwait.. Trying to go on. errno=") << err);
continue;
}
//Note: it may happen that between the sem_timewait and this code another event gets inserted in the front. In this
//case it's not an error to execute it now, as it's expiration time is the first anyway and before the one expired
sem_wait(&th->mutex);
TimingEvent* e=th->pendingEvents.front();
th->pendingEvents.pop_front();
th->currentJob=e->job;
bool destroyEvent=true;
if(e->isTick) //Let's schedule the event again
{
e->timing+=e->tickTime;
th->insertNewEvent_nolock(e); //newEvent may be signaled, and will be waited by sem_timedwait
destroyEvent=false;
}
sem_post(&th->mutex);
//Now execute the job
//NOTE: jobs may be invalid if they has been cancelled in the meantime
assert(e->job || !e->isTick);
if(e->job)
e->job->tick();
th->currentJob=NULL;
//Cleanup
if(destroyEvent)
delete e;
}
}
void TimerThread::addTick(uint32_t tickTime, ITickJob* job)
{
TimingEvent* e=new TimingEvent;
e->isTick=true;
e->job=job;
e->tickTime=tickTime;
e->timing=compat_get_current_time_ms()+tickTime;
insertNewEvent(e);
}
void TimerThread::addWait(uint32_t waitTime, ITickJob* job)
{
TimingEvent* e=new TimingEvent;
e->isTick=false;
e->job=job;
e->tickTime=0;
e->timing=compat_get_current_time_ms()+waitTime;
insertNewEvent(e);
}
bool TimerThread::removeJob(ITickJob* job)
{
sem_wait(&mutex);
list<TimingEvent*>::iterator it=pendingEvents.begin();
bool first=true;
//Find if the job is in the list
for(;it!=pendingEvents.end();++it)
{
if((*it)->job==job)
break;
first=false;
}
//Spin wait until the job ends (per design should be very short)
//As we hold the mutex and currentJob is not NULL we are surely executing a job
while(currentJob==job);
//The job ended
if(it==pendingEvents.end())
{
sem_post(&mutex);
return false;
}
//If we are waiting of this event it's not safe to remove it
//so we flag it has invalid and the worker thread will remove it
//this is equivalent, as the event is not going to be fired
TimingEvent* e=*it;
if(first)
{
e->job=NULL;
e->isTick=false;
}
else
{
pendingEvents.erase(it);
delete e;
}
sem_post(&mutex);
return true;
}
Chronometer::Chronometer()
{
start = compat_get_thread_cputime_us();
}
uint32_t lightspark::Chronometer::checkpoint()
{
uint64_t newstart;
uint32_t ret;
newstart=compat_get_thread_cputime_us();
assert((newstart-start) < UINT32_MAX);
ret=newstart-start;
start=newstart;
return ret;
}