comparison deps/Platinum/ThirdParty/Neptune/Source/System/Posix/NptPosixSystem.cpp @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /*****************************************************************
2 |
3 | Neptune - System :: Posix Implementation
4 |
5 | (c) 2001-2003 Gilles Boccon-Gibod
6 | Author: Gilles Boccon-Gibod (bok@bok.net)
7 |
8 ****************************************************************/
9
10 /*----------------------------------------------------------------------
11 | includes
12 +---------------------------------------------------------------------*/
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <time.h>
17 #include <sys/time.h>
18 #include <pthread.h>
19 #include <unistd.h>
20
21 #include "NptConfig.h"
22 #include "NptTypes.h"
23 #include "NptSystem.h"
24 #include "NptResults.h"
25 #include "NptDebug.h"
26
27 /*----------------------------------------------------------------------
28 | NPT_PosixSystem
29 +---------------------------------------------------------------------*/
30 class NPT_PosixSystem
31 {
32 public:
33 // class variables
34 static NPT_PosixSystem System;
35
36 // methods
37 NPT_PosixSystem();
38 ~NPT_PosixSystem();
39
40 // members
41 pthread_mutex_t m_SleepMutex;
42 pthread_cond_t m_SleepCondition;
43 };
44 NPT_PosixSystem NPT_PosixSystem::System;
45
46 /*----------------------------------------------------------------------
47 | NPT_PosixSystem::NPT_PosixSystem
48 +---------------------------------------------------------------------*/
49 NPT_PosixSystem::NPT_PosixSystem()
50 {
51 pthread_mutex_init(&m_SleepMutex, NULL);
52 pthread_cond_init(&m_SleepCondition, NULL);
53 }
54
55 /*----------------------------------------------------------------------
56 | NPT_PosixSystem::~NPT_PosixSystem
57 +---------------------------------------------------------------------*/
58 NPT_PosixSystem::~NPT_PosixSystem()
59 {
60 pthread_cond_destroy(&m_SleepCondition);
61 pthread_mutex_destroy(&m_SleepMutex);
62 }
63
64 /*----------------------------------------------------------------------
65 | NPT_System::GetProcessId
66 +---------------------------------------------------------------------*/
67 NPT_Result
68 NPT_System::GetProcessId(NPT_UInt32& id)
69 {
70 id = getpid();
71 return NPT_SUCCESS;
72 }
73
74 /*----------------------------------------------------------------------
75 | NPT_System::GetCurrentTimeStamp
76 +---------------------------------------------------------------------*/
77 NPT_Result
78 NPT_System::GetCurrentTimeStamp(NPT_TimeStamp& now)
79 {
80 struct timeval now_tv;
81
82 // get current time from system
83 if (gettimeofday(&now_tv, NULL)) {
84 now.m_Seconds = 0;
85 now.m_NanoSeconds = 0;
86 return NPT_FAILURE;
87 }
88
89 // convert format
90 now.m_Seconds = now_tv.tv_sec;
91 now.m_NanoSeconds = now_tv.tv_usec * 1000;
92
93 return NPT_SUCCESS;
94 }
95
96 /*----------------------------------------------------------------------
97 | NPT_System::Sleep
98 +---------------------------------------------------------------------*/
99 NPT_Result
100 NPT_System::Sleep(const NPT_TimeInterval& duration)
101 {
102 struct timespec time_req;
103 struct timespec time_rem;
104 int result;
105
106 // setup the time value
107 time_req.tv_sec = duration.m_Seconds;
108 time_req.tv_nsec = duration.m_NanoSeconds;
109
110 // sleep
111 do {
112 result = nanosleep(&time_req, &time_rem);
113 time_req = time_rem;
114 } while (result == -1 && errno == EINTR);
115
116 return NPT_SUCCESS;
117 }
118
119 /*----------------------------------------------------------------------
120 | NPT_System::SleepUntil
121 +---------------------------------------------------------------------*/
122 NPT_Result
123 NPT_System::SleepUntil(const NPT_TimeStamp& when)
124 {
125 struct timespec timeout;
126 struct timeval now;
127 int result;
128
129 // get current time from system
130 if (gettimeofday(&now, NULL)) {
131 return NPT_FAILURE;
132 }
133
134 // setup timeout
135 timeout.tv_sec = now.tv_sec + when.m_Seconds;
136 timeout.tv_nsec = now.tv_usec * 1000 + when.m_NanoSeconds;
137
138 if (timeout.tv_nsec >= 1000000000) {
139 timeout.tv_sec += timeout.tv_nsec / 1000000000;
140 timeout.tv_nsec %= 1000000000;
141 }
142
143 // sleep
144 do {
145 result = pthread_cond_timedwait(&NPT_PosixSystem::System.m_SleepCondition,
146 &NPT_PosixSystem::System.m_SleepMutex,
147 &timeout);
148 if (result == ETIMEDOUT) {
149 return NPT_SUCCESS;
150 }
151 } while (result == EINTR);
152
153 return NPT_FAILURE;
154 }
155
156 /*----------------------------------------------------------------------
157 | NPT_System::SetRandomSeed
158 +---------------------------------------------------------------------*/
159 NPT_Result
160 NPT_System::SetRandomSeed(unsigned int seed)
161 {
162 srand(seed);
163 return NPT_SUCCESS;
164 }
165
166 /*----------------------------------------------------------------------
167 | NPT_System::GetRandomInteger
168 +---------------------------------------------------------------------*/
169 NPT_UInt32
170 NPT_System::GetRandomInteger()
171 {
172 static bool seeded = false;
173 if (seeded == false) {
174 NPT_TimeStamp now;
175 GetCurrentTimeStamp(now);
176 SetRandomSeed(now.m_NanoSeconds);
177 seeded = true;
178 }
179
180 return rand();
181 }