1 /*****************************************************************
2 |
3 |   Neptune - System :: Win32 Implementation
4 |
5 |   (c) 2001-2006 Gilles Boccon-Gibod
6 |   Author: Gilles Boccon-Gibod (bok@bok.net)
7 |
8  ****************************************************************/
9 
10 /*----------------------------------------------------------------------
11 |   includes
12 +---------------------------------------------------------------------*/
13 #include "e32cmn.h"
14 #include "e32math.h"
15 #include "sys/time.h"
16 
17 #include "NptConfig.h"
18 #include "NptTypes.h"
19 #include "NptSystem.h"
20 #include "NptResults.h"
21 #include "NptDebug.h"
22 
23 
24 /*----------------------------------------------------------------------
25 |   globals
26 +---------------------------------------------------------------------*/
27 static TInt64 NPT_System_RandomGeneratorSeed = 0;
28 
29 
30 /*----------------------------------------------------------------------
31 |   NPT_System::GetProcessId
32 +---------------------------------------------------------------------*/
33 NPT_Result
GetProcessId(NPT_UInt32 & id)34 NPT_System::GetProcessId(NPT_UInt32& id)
35 {
36     //id = getpid();
37     id = 0;
38     return NPT_SUCCESS;
39 }
40 
41 /*----------------------------------------------------------------------
42 |   NPT_System::GetCurrentTimeStamp
43 +---------------------------------------------------------------------*/
44 NPT_Result
GetCurrentTimeStamp(NPT_TimeStamp & now)45 NPT_System::GetCurrentTimeStamp(NPT_TimeStamp& now)
46 {
47     struct timeval now_tv;
48 
49     /* get current time from system */
50     if (gettimeofday(&now_tv, NULL)) {
51         now.m_Seconds     = 0;
52         now.m_NanoSeconds = 0;
53         return NPT_FAILURE;
54     }
55 
56     /* convert format */
57     now.m_Seconds     = now_tv.tv_sec;
58     now.m_NanoSeconds = now_tv.tv_usec * 1000;
59 
60     return NPT_SUCCESS;
61 }
62 
63 /*----------------------------------------------------------------------
64 |   NPT_System::Sleep
65 +---------------------------------------------------------------------*/
66 NPT_Result
Sleep(const NPT_TimeInterval & duration)67 NPT_System::Sleep(const NPT_TimeInterval& duration)
68 {
69     TTimeIntervalMicroSeconds32  milliseconds = 1000*duration.m_Seconds + duration.m_NanoSeconds/1000000;
70     User::After(milliseconds); /* FIXME: this doesn't behave like a normal sleep() where the processor idles. Need to use CTimer much more complicated logic. */
71 
72     return NPT_SUCCESS;
73 }
74 
75 /*----------------------------------------------------------------------
76 |   NPT_System::SleepUntil
77 +---------------------------------------------------------------------*/
78 NPT_Result
SleepUntil(const NPT_TimeStamp & when)79 NPT_System::SleepUntil(const NPT_TimeStamp& when)
80 {
81     NPT_TimeStamp now;
82     GetCurrentTimeStamp(now);
83     if (when > now) {
84         NPT_TimeInterval duration = when-now;
85         return Sleep(duration);
86     } else {
87         return NPT_SUCCESS;
88     }
89 }
90 
91 /*----------------------------------------------------------------------
92 |   NPT_System::SetRandomSeed
93 +---------------------------------------------------------------------*/
94 NPT_Result
SetRandomSeed(unsigned int seed)95 NPT_System::SetRandomSeed(unsigned int seed)
96 {
97     NPT_System_RandomGeneratorSeed = seed;
98     return NPT_SUCCESS;
99 }
100 
101 /*----------------------------------------------------------------------
102 |   NPT_System::NPT_System
103 +---------------------------------------------------------------------*/
104 NPT_UInt32
GetRandomInteger()105 NPT_System::GetRandomInteger()
106 {
107     if (!NPT_System_RandomGeneratorSeed) {
108         TTime time;
109         time.HomeTime();
110 
111         NPT_System::SetRandomSeed(time.Int64());
112     }
113 
114     return Math::Rand(NPT_System_RandomGeneratorSeed);
115 }
116 
117