Add varying levels of precision to TimeTaker
[oweals/minetest.git] / src / porting.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /*
21         Random portability stuff
22 */
23
24 #ifndef PORTING_HEADER
25 #define PORTING_HEADER
26
27 #include <string>
28 #include "irrlichttypes.h" // u32
29 #include "debug.h"
30 #include "constants.h"
31 #include "gettime.h"
32
33 #ifdef _MSC_VER
34         #define SWPRINTF_CHARSTRING L"%S"
35 #else
36         #define SWPRINTF_CHARSTRING L"%s"
37 #endif
38
39 //currently not needed
40 //template<typename T> struct alignment_trick { char c; T member; };
41 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
42
43 #ifdef _WIN32
44         #ifndef _WIN32_WINNT
45                 #define _WIN32_WINNT 0x0500
46         #endif
47         #include <windows.h>
48         
49         #define sleep_ms(x) Sleep(x)
50 #else
51         #include <unistd.h>
52         #include <stdint.h> //for uintptr_t
53         
54         #if (defined(linux) || defined(__linux)) && !defined(_GNU_SOURCE)
55                 #define _GNU_SOURCE
56         #endif
57
58         #include <sched.h>
59
60         #ifdef __FreeBSD__
61                 #include <pthread_np.h>
62                 typedef cpuset_t cpu_set_t;
63         #elif defined(__sun) || defined(sun)
64                 #include <sys/types.h>
65                 #include <sys/processor.h>
66         #elif defined(_AIX)
67                 #include <sys/processor.h>
68         #elif __APPLE__
69                 #include <mach/mach_init.h>
70                 #include <mach/thread_policy.h>
71         #endif
72
73         #define sleep_ms(x) usleep(x*1000)
74         
75         #define THREAD_PRIORITY_LOWEST       0
76         #define THREAD_PRIORITY_BELOW_NORMAL 1
77         #define THREAD_PRIORITY_NORMAL       2
78         #define THREAD_PRIORITY_ABOVE_NORMAL 3
79         #define THREAD_PRIORITY_HIGHEST      4
80 #endif
81
82 #ifdef _MSC_VER
83         #define ALIGNOF(x) __alignof(x)
84         #define strtok_r(x, y, z) strtok_s(x, y, z)
85         #define strtof(x, y) (float)strtod(x, y)
86         #define strtoll(x, y, z) _strtoi64(x, y, z)
87         #define strtoull(x, y, z) _strtoui64(x, y, z)
88         #define strcasecmp(x, y) stricmp(x, y)
89 #else
90         #define ALIGNOF(x) __alignof__(x)
91 #endif
92
93 #ifdef __MINGW32__
94         #define strtok_r(x, y, z) mystrtok_r(x, y, z)
95 #endif
96
97 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
98
99 namespace porting
100 {
101
102 /*
103         Signal handler (grabs Ctrl-C on POSIX systems)
104 */
105
106 void signal_handler_init(void);
107 // Returns a pointer to a bool.
108 // When the bool is true, program should quit.
109 bool * signal_handler_killstatus(void);
110
111 /*
112         Path of static data directory.
113 */
114 extern std::string path_share;
115
116 /*
117         Directory for storing user data. Examples:
118         Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
119         Linux: "~/.<PROJECT_NAME>"
120         Mac: "~/Library/Application Support/<PROJECT_NAME>"
121 */
122 extern std::string path_user;
123
124 /*
125         Get full path of stuff in data directory.
126         Example: "stone.png" -> "../data/stone.png"
127 */
128 std::string getDataPath(const char *subpath);
129
130 /*
131         Initialize path_share and path_user.
132 */
133 void initializePaths();
134
135 /*
136         Get number of online processors in the system.
137 */
138 int getNumberOfProcessors();
139
140 /*
141         Set a thread's affinity to a particular processor.
142 */
143 bool threadBindToProcessor(threadid_t tid, int pnumber);
144
145 /*
146         Set a thread's priority.
147 */
148 bool threadSetPriority(threadid_t tid, int prio);
149
150 /*
151         Resolution is 10-20ms.
152         Remember to check for overflows.
153         Overflow can occur at any value higher than 10000000.
154 */
155 #ifdef _WIN32 // Windows
156         #include <windows.h>
157         
158         inline u32 getTimeS()
159         {
160                 return GetTickCount() / 1000;
161         }
162         
163         inline u32 getTimeMs()
164         {
165                 return GetTickCount();
166         }
167         
168         inline u32 getTimeUs()
169         {
170                 LARGE_INTEGER freq, t;
171                 QueryPerformanceFrequency(&freq);
172                 QueryPerformanceCounter(&t);
173                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
174         }
175         
176         inline u32 getTimeNs()
177         {
178                 LARGE_INTEGER freq, t;
179                 QueryPerformanceFrequency(&freq);
180                 QueryPerformanceCounter(&t);
181                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
182         }
183         
184 #else // Posix
185         #include <sys/time.h>
186         #include <time.h>
187         
188         inline u32 getTimeS()
189         {
190                 struct timeval tv;
191                 gettimeofday(&tv, NULL);
192                 return tv.tv_sec;
193         }
194         
195         inline u32 getTimeMs()
196         {
197                 struct timeval tv;
198                 gettimeofday(&tv, NULL);
199                 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
200         }
201         
202         inline u32 getTimeUs()
203         {
204                 struct timeval tv;
205                 gettimeofday(&tv, NULL);
206                 return tv.tv_sec * 1000000 + tv.tv_usec;
207         }
208         
209         inline u32 getTimeNs()
210         {
211                 struct timespec ts;
212                 clock_gettime(CLOCK_REALTIME, &ts);
213                 return ts.tv_sec * 1000000000 + ts.tv_nsec;
214         }
215         
216         /*#include <sys/timeb.h>
217         inline u32 getTimeMs()
218         {
219                 struct timeb tb;
220                 ftime(&tb);
221                 return tb.time * 1000 + tb.millitm;
222         }*/
223 #endif
224
225 inline u32 getTime(TimePrecision prec)
226 {
227         switch (prec) {
228                 case PRECISION_SECONDS:
229                         return getTimeS();
230                 case PRECISION_MILLI:
231                         return getTimeMs();
232                 case PRECISION_MICRO:
233                         return getTimeUs();
234                 case PRECISION_NANO:
235                         return getTimeNs();
236         }
237         return 0;
238 }
239
240
241 } // namespace porting
242
243 #endif // PORTING_HEADER
244