egedit: do not save cursor movement in undo -- this is my stupid habit, and it comple...
[iv.d.git] / _obsolete_dont_use / ticks.d
blobe993977b5c0befa02a1dd6c84626b8df7dc9c49b
1 /*
2 * Simple timer
3 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
4 * Understanding is not required. Only obedience.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, version 3 of the License ONLY.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // ////////////////////////////////////////////////////////////////////////// //
19 // severely outdated, do not use!
20 module iv.ticks /*is aliced*/;
21 import iv.alice;
24 private import core.sys.posix.time;
25 // idiotic phobos forgets 'nothrow' at it
26 extern(C) private int clock_gettime (clockid_t, timespec*) @trusted nothrow @nogc;
29 version(linux) {
30 private enum CLOCK_MONOTONIC_RAW = 4;
31 private enum CLOCK_MONOTONIC_COARSE = 6;
33 shared static this () {
34 initializeClock();
38 private __gshared timespec videolib_clock_stt;
39 private __gshared int gtClockType = CLOCK_MONOTONIC_COARSE;
42 nothrow @trusted @nogc:
44 private void initializeClock () {
45 timespec cres = void;
46 bool inited = false;
47 if (clock_getres(gtClockType, &cres) == 0) {
48 if (cres.tv_sec == 0 && cres.tv_nsec <= cast(long)1000000*1 /*1 ms*/) inited = true;
50 if (!inited) {
51 gtClockType = CLOCK_MONOTONIC_RAW;
52 if (clock_getres(gtClockType, &cres) == 0) {
53 if (cres.tv_sec == 0 && cres.tv_nsec <= cast(long)1000000*1 /*1 ms*/) inited = true;
56 if (!inited) assert(0, "FATAL: can't initialize clock subsystem!");
57 if (clock_gettime(gtClockType, &videolib_clock_stt) != 0) {
58 assert(0, "FATAL: can't initialize clock subsystem!");
63 /** returns monitonically increasing time; starting value is UNDEFINED (i.e. can be any number); milliseconds */
64 ulong getTicks () {
65 static if (__VERSION__ > 2067) pragma(inline, true);
66 timespec ts = void;
67 if (clock_gettime(gtClockType, &ts) != 0) assert(0, "FATAL: can't get real-time clock value!\n");
68 // ah, ignore nanoseconds in videolib_clock_stt->stt here: we need only 'differential' time, and it can start with something weird
69 return (cast(ulong)(ts.tv_sec-videolib_clock_stt.tv_sec))*1000+ts.tv_nsec/1000000+1;
73 /** returns monitonically increasing time; starting value is UNDEFINED (i.e. can be any number); microseconds */
74 ulong getTicksMicro () {
75 static if (__VERSION__ > 2067) pragma(inline, true);
76 timespec ts = void;
77 if (clock_gettime(gtClockType, &ts) != 0) assert(0, "FATAL: can't get real-time clock value!\n");
78 // ah, ignore nanoseconds in videolib_clock_stt->stt here: we need only 'differential' time, and it can start with something weird
79 return (cast(ulong)(ts.tv_sec-videolib_clock_stt.tv_sec))*1000000+ts.tv_nsec/1000+1;
81 } else version(Windows) {
82 private import core.sys.windows.winbase : GetTickCount;
84 ulong getTicks () { return GetTickCount(); }
86 ulong getTicksMicro () {
87 static assert(0, "iv.ticks.getTicksMicro() is not implemented on windoze");
89 } else {
90 static assert(0, "sorry, only GNU/Linux for now; please, fix `getTicks()` and company for your OS!");