1 /*
2 * Copyright (c) 2000 Sasha Vasko <sasha at aftercode.net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include <unistd.h>
20 #include <sys/time.h>
21 #include <sys/times.h>
22
23 #if defined ___AIX || defined _AIX || defined __QNX__ || defined ___AIXV3 || defined AIXV3 || defined _SEQUENT_
24 #include <sys/select.h>
25 #endif
26
27 #include "config.h"
28 #include "astypes.h"
29 #include "sleep.h"
30
31 /**************************************************************************
32 * Sleep for n microseconds
33 *************************************************************************/
34 void
sleep_a_little(int n)35 sleep_a_little (int n)
36 {
37 struct timeval value;
38
39 if (n <= 0)
40 return;
41
42 value.tv_usec = n % 1000000;
43 value.tv_sec = n / 1000000;
44
45 PORTABLE_SELECT (1, 0, 0, 0, &value);
46 }
47
48
49 static clock_t _as_ticker_last_tick = 0;
50 static clock_t _as_ticker_tick_size = 1;
51 static clock_t _as_ticker_tick_time = 0;
52
53 void
start_ticker(unsigned int size)54 start_ticker (unsigned int size)
55 {
56 struct tms t;
57
58 _as_ticker_last_tick = times (&t); /* in system ticks */
59 if (_as_ticker_tick_time == 0)
60 {
61 register clock_t delta = _as_ticker_last_tick;
62 /* calibrating clock - how many ms per cpu tick ? */
63 sleep_a_little (100);
64 _as_ticker_last_tick = times (&t);
65 delta = _as_ticker_last_tick - delta ;
66 if( delta <= 0 )
67 _as_ticker_tick_time = 100;
68 else
69 _as_ticker_tick_time = 101 / delta;
70 }
71 _as_ticker_tick_size = size; /* in ms */
72 }
73
74 void
wait_tick()75 wait_tick ()
76 {
77 struct tms t;
78 register clock_t curr = (times (&t) - _as_ticker_last_tick) * _as_ticker_tick_time;
79
80 if (curr < _as_ticker_tick_size)
81 sleep_a_little (_as_ticker_tick_size - curr);
82
83 _as_ticker_last_tick = times (&t);
84 }
85
86 int
is_tick()87 is_tick ()
88 {
89 struct tms t;
90 register clock_t curr = (times (&t) - _as_ticker_last_tick) * _as_ticker_tick_time;
91
92 return (curr >= _as_ticker_tick_size) ;
93 }
94