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:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
7  * Authors:   Sean Kelly
8  * Source:    $(DRUNTIMESRC core/sync/_config.d)
9  */
10 
11 /*          Copyright Sean Kelly 2005 - 2009.
12  * Distributed under the Boost Software License, Version 1.0.
13  *    (See accompanying file LICENSE or copy at
14  *          http://www.boost.org/LICENSE_1_0.txt)
15  */
16 module core.sync.config;
17 
18 
19 version (Posix)
20 {
21     import core.sys.posix.pthread;
22     import core.sys.posix.time;
23     import core.sys.posix.sys.time;
24     import core.time;
25 
26 
27     void mktspec( ref timespec t ) nothrow @nogc
28     {
29         static if ( is (typeof ( pthread_condattr_setclock ) ) )
30         {
31             clock_gettime( CLOCK_MONOTONIC, &t );
32         }
33         else
34         {
35             timeval tv;
36 
37             gettimeofday( &tv, null );
38             (cast(byte*) &t)[0 .. t.sizeof] = 0;
39             t.tv_sec  = cast(typeof(t.tv_sec))  tv.tv_sec;
40             t.tv_nsec = cast(typeof(t.tv_nsec)) tv.tv_usec * 1_000;
41         }
42     }
43 
44 
45     void mktspec( ref timespec t, Duration delta ) nothrow @nogc
46     {
47         mktspec( t );
48         mvtspec( t, delta );
49     }
50 
51 
52     void mvtspec( ref timespec t, Duration delta ) nothrow @nogc
53     {
54         auto val  = delta;
55              val += dur!"seconds"( t.tv_sec );
56              val += dur!"nsecs"( t.tv_nsec );
57 
58         //auto val = delta + dur!"seconds"( t.tv_sec ) +
59         //                 + dur!"nsecs"( t.tv_nsec );
60 
61         if ( val.total!"seconds" > t.tv_sec.max )
62         {
63             t.tv_sec  = t.tv_sec.max;
64             t.tv_nsec = cast(typeof(t.tv_nsec)) val.split!("seconds", "nsecs")().nsecs;
65         }
66         else
67             val.split!("seconds", "nsecs")(t.tv_sec, t.tv_nsec);
68     }
69 }