~ubuntu-branches/ubuntu/trusty/lcd4linux/trusty-proposed

« back to all changes in this revision

Viewing changes to timer.c

  • Committer: Package Import Robot
  • Author(s): Jonathan McCrohan
  • Date: 2012-05-19 15:10:03 UTC
  • mfrom: (1.1.8)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20120519151003-is09w5xk0122vnuc
Tags: 0.11.0~svn1189-1
* New upstream snapshot
  - Adds Hex support to the evaluator
  - Fixes -Wimplicit-function-declaration build warnings
  - Fixes -Werror=format-security build errors
  - Fixes plugin_file syslog flood if file does not exist
* d/rules: Add get-orig-source target
* Bump dh to v9
* Switch to dh-style debian/rules
* Add Build-Depends on libftdi-dev, libgphoto2-2-dev, libmpdclient-dev and
  libvncserver-dev to enable new drivers

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: timer.c 1143 2011-02-12 22:46:19Z mzuther $
 
1
/* $Id: timer.c 1185 2012-03-26 13:24:37Z mjona $
2
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/timer.c $
3
3
 *
4
4
 * Generic timer handling.
126
126
    struct timeval diff;
127
127
    timersub(now, &Timers[timer].when, &diff);
128
128
 
129
 
    /* convert this time difference to fractional seconds */
130
 
    float time_difference = diff.tv_sec + diff.tv_usec / 1000000.0f;
131
 
 
132
 
    /* convert time difference to fractional milliseconds */
133
 
    time_difference = time_difference * 1000.0f;
 
129
    /* convert this time difference to fractional milliseconds */
 
130
    float time_difference = (diff.tv_sec * 1000.0f) + (diff.tv_usec / 1000.0f);
134
131
 
135
132
    /* calculate the number of timer intervals that have passed since
136
133
       the last timer the given timer has been processed -- value is
184
181
       timer slot by looking for its settings */
185
182
    for (timer = 0; timer < nTimers; timer++) {
186
183
        /* skip inactive (i.e. deleted) timers */
187
 
        if (Timers[timer].active == 0)
 
184
        if (Timers[timer].active == TIMER_INACTIVE)
188
185
            continue;
189
186
 
190
187
        if (Timers[timer].callback == callback && Timers[timer].data == data) {
191
188
            /* we have found the timer slot, so mark it as being inactive;
192
189
               we will not actually delete the slot, so its allocated
193
190
               memory may be re-used */
194
 
            Timers[timer].active = 0;
 
191
            Timers[timer].active = TIMER_INACTIVE;
195
192
 
196
193
            /* signal successful timer removal */
197
194
            return 0;
232
229
    /* try to minimize memory usage by looping through the timer slots
233
230
       and looking for an inactive timer */
234
231
    for (timer = 0; timer < nTimers; timer++) {
235
 
        if (Timers[timer].active == 0) {
 
232
        if (Timers[timer].active == TIMER_INACTIVE) {
236
233
            /* we've just found one, so let's reuse it ("timer" holds its
237
234
               ID) by breaking the loop */
238
235
            break;
241
238
 
242
239
    /* no inactive timers (or none at all) found, so we have to add a
243
240
       new timer slot */
244
 
    if (timer >= nTimers) {
245
 
        /* increment number of timers and (re-)allocate memory used for
246
 
           storing the timer slots */
247
 
        nTimers++;
248
 
        Timers = realloc(Timers, nTimers * sizeof(*Timers));
249
 
 
250
 
        /* make sure "timer" points to valid memory */
251
 
        timer = nTimers - 1;
252
 
 
253
 
        /* realloc() has failed */
254
 
        if (Timers == NULL) {
255
 
            /* restore old number of timers */
256
 
            nTimers--;
257
 
 
 
241
    if (timer == nTimers) {
 
242
        TIMER *tmp;
 
243
 
 
244
        if ((tmp = realloc(Timers, (nTimers + 1) * sizeof(*Timers))) == NULL) {
258
245
            /* signal unsuccessful timer creation */
259
246
            return -1;
260
247
        }
 
248
        Timers = tmp;
 
249
        nTimers++;
261
250
    }
262
251
 
263
252
    /* get current time so the timer triggers immediately */
272
261
 
273
262
    /* set timer to active so that it is processed and not overwritten
274
263
       by the memory optimization routine above */
275
 
    Timers[timer].active = 1;
 
264
    Timers[timer].active = TIMER_ACTIVE;
276
265
 
277
266
    /* one-shot timers should NOT fire immediately, so delay them by a
278
267
       single timer interval */
323
312
       by looking for its settings */
324
313
    for (timer = 0; timer < nTimers; timer++) {
325
314
        /* skip inactive (i.e. deleted) timers */
326
 
        if (Timers[timer].active == 0)
 
315
        if (Timers[timer].active == TIMER_INACTIVE)
327
316
            continue;
328
317
 
329
318
        if (Timers[timer].callback == callback && Timers[timer].data == data && Timers[timer].interval == interval) {
372
361
    /* process all expired timers */
373
362
    for (timer = 0; timer < nTimers; timer++) {
374
363
        /* skip inactive (i.e. deleted) timers */
375
 
        if (Timers[timer].active == 0)
 
364
        if (Timers[timer].active == TIMER_INACTIVE)
376
365
            continue;
377
366
 
378
367
        /* check whether current timer needs to be processed, i.e. the
391
380
            if (Timers[timer].one_shot) {
392
381
                /* mark one-shot timer as inactive (which means the timer has
393
382
                   been deleted and its allocated memory may be re-used) */
394
 
                Timers[timer].active = 0;
 
383
                Timers[timer].active = TIMER_INACTIVE;
395
384
            } else {
396
385
                /* otherwise, re-spawn timer by adding one triggering interval
397
386
                   to its triggering time */
406
395
       timer */
407
396
    for (timer = 0; timer < nTimers; timer++) {
408
397
        /* skip inactive (i.e. deleted) timers */
409
 
        if (Timers[timer].active == 0)
 
398
        if (Timers[timer].active == TIMER_INACTIVE)
410
399
            continue;
411
400
 
412
401
        /* if this is the first timer that we check, mark it as the next
473
462
            /* process all timers */
474
463
            for (timer = 0; timer < nTimers; timer++) {
475
464
                /* skip inactive (i.e. deleted) timers */
476
 
                if (Timers[timer].active == 0)
 
465
                if (Timers[timer].active == TIMER_INACTIVE)
477
466
                    continue;
478
467
 
479
468
                /* correct timer's time stamp by clock skew */