Mercurial > projects > ldc
comparison druntime/src/common/core/sync/config.d @ 1458:e0b2d67cfe7c
Added druntime (this should be removed once it works).
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Tue, 02 Jun 2009 17:43:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1456:7b218ec1044f | 1458:e0b2d67cfe7c |
---|---|
1 /** | |
2 * The config module contains utility routines and configuration information | |
3 * specific to this package. | |
4 * | |
5 * Copyright: Copyright Sean Kelly 2005 - 2009. | |
6 * License: <a href="http://www.boost.org/LICENSE_1_0.txt>Boost License 1.0</a>. | |
7 * Authors: Sean Kelly | |
8 * | |
9 * Copyright Sean Kelly 2005 - 2009. | |
10 * Distributed under the Boost Software License, Version 1.0. | |
11 * (See accompanying file LICENSE_1_0.txt or copy at | |
12 * http://www.boost.org/LICENSE_1_0.txt) | |
13 */ | |
14 module core.sync.config; | |
15 | |
16 | |
17 version( Posix ) | |
18 { | |
19 private import core.sys.posix.time; | |
20 private import core.sys.posix.sys.time; | |
21 | |
22 | |
23 void mktspec( inout timespec t, long delta = 0 ) | |
24 { | |
25 static if( is( typeof( clock_gettime ) ) ) | |
26 { | |
27 clock_gettime( CLOCK_REALTIME, &t ); | |
28 } | |
29 else | |
30 { | |
31 timeval tv; | |
32 | |
33 gettimeofday( &tv, null ); | |
34 (cast(byte*) &t)[0 .. t.sizeof] = 0; | |
35 t.tv_sec = cast(typeof(t.tv_sec)) tv.tv_sec; | |
36 t.tv_nsec = cast(typeof(t.tv_nsec)) tv.tv_usec * 1_000; | |
37 } | |
38 mvtspec( t, delta ); | |
39 } | |
40 | |
41 | |
42 void mvtspec( inout timespec t, long delta ) | |
43 { | |
44 if( delta == 0 ) | |
45 return; | |
46 | |
47 enum : uint | |
48 { | |
49 NANOS_PER_TICK = 100, | |
50 TICKS_PER_SECOND = 10_000_000, | |
51 NANOS_PER_SECOND = NANOS_PER_TICK * TICKS_PER_SECOND, | |
52 } | |
53 | |
54 if( t.tv_sec.max - t.tv_sec < delta / TICKS_PER_SECOND ) | |
55 { | |
56 t.tv_sec = t.tv_sec.max; | |
57 t.tv_nsec = 0; | |
58 } | |
59 else | |
60 { | |
61 t.tv_sec += cast(typeof(t.tv_sec)) (delta / TICKS_PER_SECOND); | |
62 long ns = (delta % TICKS_PER_SECOND) * NANOS_PER_TICK; | |
63 if( NANOS_PER_SECOND - t.tv_nsec > ns ) | |
64 { | |
65 t.tv_nsec = cast(typeof(t.tv_nsec)) ns; | |
66 return; | |
67 } | |
68 t.tv_sec += 1; | |
69 t.tv_nsec += cast(typeof(t.tv_nsec)) (ns - NANOS_PER_SECOND); | |
70 } | |
71 } | |
72 } |