File: timer.cpp

package info (click to toggle)
mona 1.4-7-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,976 kB
  • ctags: 3,713
  • sloc: ansic: 14,363; cpp: 12,610; sh: 1,076; yacc: 493; lex: 358; makefile: 154; lisp: 53
file content (97 lines) | stat: -rw-r--r-- 2,040 bytes parent folder | download | duplicates (2)
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
/*
 * MONA
 * Copyright (C) 1997-2004 BRICS.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the  Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 */

#define _LANGUAGE_C_PLUS_PLUS

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include "timer.h"

const unsigned long hour = 360000;
const unsigned long min  = 6000;
const unsigned long sec  = 100;

const unsigned long step = 2000; // sec. pr. step

static unsigned long prev, clocks, steps; // CPU time (user+system)

void
refresh(int)
{
  unsigned long t = (unsigned long) clock();
  clocks += t - prev;
  prev = t;
  while (clocks >= step*CLOCKS_PER_SEC) {
    steps++;
    clocks -= step*CLOCKS_PER_SEC;
  }
  signal(SIGALRM, &refresh);
  alarm(step);
}

void
initTimer()
{
  clocks = steps = 0;
  refresh(0);
}

void
Timer::start()
{
  alarm(0);
  refresh(0);
  tsteps = steps;
  tclocks = clocks;
}

void
Timer::stop()
{
  alarm(0);
  refresh(0);
  unsigned long ttsteps = steps;
  unsigned long ttclocks = clocks;

  if (ttclocks < tclocks) {
    ttclocks += step*CLOCKS_PER_SEC;
    ttsteps--;
  }
  res += (ttsteps - tsteps)*step*100 + (ttclocks - tclocks)/(CLOCKS_PER_SEC/100);
}

void
Timer::print()
{
  unsigned long t = res, hours, mins, secs;

  hours = t / hour;
  t -= hours * hour;

  mins = t / min;
  t -= mins * min;

  secs = t / sec;
  t -= secs * sec;

  printf("%02lu:%02lu:%02lu.%02lu\n", hours, mins, secs, t);
}