Revert "Add daemon support for linux like operating systems"
[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 #ifdef _WIN32
28         #ifdef _WIN32_WINNT
29                 #undef _WIN32_WINNT
30         #endif
31         #define _WIN32_WINNT 0x0501 // We need to do this before any other headers
32                 // because those might include sdkddkver.h which defines _WIN32_WINNT if not already set
33 #endif
34
35 #include <string>
36 #include "irrlicht.h"
37 #include "irrlichttypes.h" // u32
38 #include "irrlichttypes_extrabloated.h"
39 #include "debug.h"
40 #include "constants.h"
41 #include "gettime.h"
42 #include "threads.h"
43
44 #ifdef _MSC_VER
45         #define SWPRINTF_CHARSTRING L"%S"
46 #else
47         #define SWPRINTF_CHARSTRING L"%s"
48 #endif
49
50 //currently not needed
51 //template<typename T> struct alignment_trick { char c; T member; };
52 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
53
54 #ifdef _WIN32
55         #include <windows.h>
56         
57         #define sleep_ms(x) Sleep(x)
58 #else
59         #include <unistd.h>
60         #include <stdint.h> //for uintptr_t
61         
62         #if (defined(linux) || defined(__linux)) && !defined(_GNU_SOURCE)
63                 #define _GNU_SOURCE
64         #endif
65
66         #include <sched.h>
67
68         #ifdef __FreeBSD__
69                 #include <pthread_np.h>
70                 typedef cpuset_t cpu_set_t;
71         #elif defined(__sun) || defined(sun)
72                 #include <sys/types.h>
73                 #include <sys/processor.h>
74         #elif defined(_AIX)
75                 #include <sys/processor.h>
76         #elif __APPLE__
77                 #include <mach/mach_init.h>
78                 #include <mach/thread_policy.h>
79         #endif
80
81         #define sleep_ms(x) usleep(x*1000)
82         
83         #define THREAD_PRIORITY_LOWEST       0
84         #define THREAD_PRIORITY_BELOW_NORMAL 1
85         #define THREAD_PRIORITY_NORMAL       2
86         #define THREAD_PRIORITY_ABOVE_NORMAL 3
87         #define THREAD_PRIORITY_HIGHEST      4
88 #endif
89
90 #ifdef _MSC_VER
91         #define ALIGNOF(x) __alignof(x)
92         #define strtok_r(x, y, z) strtok_s(x, y, z)
93         #define strtof(x, y) (float)strtod(x, y)
94         #define strtoll(x, y, z) _strtoi64(x, y, z)
95         #define strtoull(x, y, z) _strtoui64(x, y, z)
96         #define strcasecmp(x, y) stricmp(x, y)
97         #define strncasecmp(x, y, n) strnicmp(x, y, n)
98 #else
99         #define ALIGNOF(x) __alignof__(x)
100 #endif
101
102 #ifdef __MINGW32__
103         #define strtok_r(x, y, z) mystrtok_r(x, y, z)
104 #endif
105
106 // strlcpy is missing from glibc.  thanks a lot, drepper.
107 // strlcpy is also missing from AIX and HP-UX because they aim to be weird.
108 // We can't simply alias strlcpy to MSVC's strcpy_s, since strcpy_s by
109 // default raises an assertion error and aborts the program if the buffer is
110 // too small.
111 #if defined(__FreeBSD__) || defined(__NetBSD__)    || \
112         defined(__OpenBSD__) || defined(__DragonFly__) || \
113         defined(__APPLE__)   ||                           \
114         defined(__sun)       || defined(sun)           || \
115         defined(__QNX__)     || defined(__QNXNTO__)
116         #define HAVE_STRLCPY
117 #endif
118
119 // So we need to define our own.
120 #ifndef HAVE_STRLCPY
121         #define strlcpy(d, s, n) mystrlcpy(d, s, n)
122 #endif
123
124 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
125
126 namespace porting
127 {
128
129 /*
130         Signal handler (grabs Ctrl-C on POSIX systems)
131 */
132
133 void signal_handler_init(void);
134 // Returns a pointer to a bool.
135 // When the bool is true, program should quit.
136 bool * signal_handler_killstatus(void);
137
138 /*
139         Path of static data directory.
140 */
141 extern std::string path_share;
142
143 /*
144         Directory for storing user data. Examples:
145         Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
146         Linux: "~/.<PROJECT_NAME>"
147         Mac: "~/Library/Application Support/<PROJECT_NAME>"
148 */
149 extern std::string path_user;
150
151 /*
152         Get full path of stuff in data directory.
153         Example: "stone.png" -> "../data/stone.png"
154 */
155 std::string getDataPath(const char *subpath);
156
157 /*
158         Initialize path_share and path_user.
159 */
160 void initializePaths();
161
162 /*
163         Get number of online processors in the system.
164 */
165 int getNumberOfProcessors();
166
167 /*
168         Set a thread's affinity to a particular processor.
169 */
170 bool threadBindToProcessor(threadid_t tid, int pnumber);
171
172 /*
173         Set a thread's priority.
174 */
175 bool threadSetPriority(threadid_t tid, int prio);
176
177 /*
178         Return system information
179         e.g. "Linux/3.12.7 x86_64"
180 */
181 std::string get_sysinfo();
182
183 void initIrrlicht(irr::IrrlichtDevice * );
184
185 /*
186         Resolution is 10-20ms.
187         Remember to check for overflows.
188         Overflow can occur at any value higher than 10000000.
189 */
190 #ifdef _WIN32 // Windows
191 #ifndef _WIN32_WINNT
192         #define _WIN32_WINNT 0x0501
193 #endif
194         #include <windows.h>
195         
196         inline u32 getTimeS()
197         {
198                 return GetTickCount() / 1000;
199         }
200         
201         inline u32 getTimeMs()
202         {
203                 return GetTickCount();
204         }
205         
206         inline u32 getTimeUs()
207         {
208                 LARGE_INTEGER freq, t;
209                 QueryPerformanceFrequency(&freq);
210                 QueryPerformanceCounter(&t);
211                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
212         }
213         
214         inline u32 getTimeNs()
215         {
216                 LARGE_INTEGER freq, t;
217                 QueryPerformanceFrequency(&freq);
218                 QueryPerformanceCounter(&t);
219                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
220         }
221         
222 #else // Posix
223         #include <sys/time.h>
224         #include <time.h>
225         
226         inline u32 getTimeS()
227         {
228                 struct timeval tv;
229                 gettimeofday(&tv, NULL);
230                 return tv.tv_sec;
231         }
232         
233         inline u32 getTimeMs()
234         {
235                 struct timeval tv;
236                 gettimeofday(&tv, NULL);
237                 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
238         }
239         
240         inline u32 getTimeUs()
241         {
242                 struct timeval tv;
243                 gettimeofday(&tv, NULL);
244                 return tv.tv_sec * 1000000 + tv.tv_usec;
245         }
246         
247         inline u32 getTimeNs()
248         {
249                 struct timespec ts;
250                 clock_gettime(CLOCK_REALTIME, &ts);
251                 return ts.tv_sec * 1000000000 + ts.tv_nsec;
252         }
253         
254         /*#include <sys/timeb.h>
255         inline u32 getTimeMs()
256         {
257                 struct timeb tb;
258                 ftime(&tb);
259                 return tb.time * 1000 + tb.millitm;
260         }*/
261 #endif
262
263 inline u32 getTime(TimePrecision prec)
264 {
265         switch (prec) {
266                 case PRECISION_SECONDS:
267                         return getTimeS();
268                 case PRECISION_MILLI:
269                         return getTimeMs();
270                 case PRECISION_MICRO:
271                         return getTimeUs();
272                 case PRECISION_NANO:
273                         return getTimeNs();
274         }
275         return 0;
276 }
277
278 #if defined(linux) || defined(__linux)
279         #include <sys/prctl.h>
280
281         inline void setThreadName(const char *name) {
282                 /* It would be cleaner to do this with pthread_setname_np,
283                  * which was added to glibc in version 2.12, but some major
284                  * distributions are still runing 2.11 and previous versions.
285                  */
286                 prctl(PR_SET_NAME, name);
287         }
288 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
289         #include <pthread.h>
290         #include <pthread_np.h>
291
292         inline void setThreadName(const char *name) {
293                 pthread_set_name_np(pthread_self(), name);
294         }
295 #elif defined(__NetBSD__)
296         #include <pthread.h>
297
298         inline void setThreadName(const char *name) {
299                 pthread_setname_np(pthread_self(), name);
300         }
301 #elif defined(_MSC_VER)
302         typedef struct tagTHREADNAME_INFO {
303                 DWORD dwType; // must be 0x1000
304                 LPCSTR szName; // pointer to name (in user addr space)
305                 DWORD dwThreadID; // thread ID (-1=caller thread)
306                 DWORD dwFlags; // reserved for future use, must be zero
307         } THREADNAME_INFO;
308
309         inline void setThreadName(const char *name) {
310                 THREADNAME_INFO info;
311                 info.dwType = 0x1000;
312                 info.szName = name;
313                 info.dwThreadID = -1;
314                 info.dwFlags = 0;
315                 __try {
316                         RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD), (ULONG_PTR *) &info);
317                 } __except (EXCEPTION_CONTINUE_EXECUTION) {}
318         }
319 #elif defined(__APPLE__)
320         #include <pthread.h>
321
322         inline void setThreadName(const char *name) {
323                 pthread_setname_np(name);
324         }
325 #elif defined(_WIN32)
326         inline void setThreadName(const char* name) {}
327 #else
328         #warning "Unrecognized platform, thread names will not be available."
329         inline void setThreadName(const char* name) {}
330 #endif
331
332 #ifndef SERVER
333 float getDisplayDensity();
334
335 v2u32 getDisplaySize();
336 v2u32 getWindowSize();
337 #endif
338
339 } // namespace porting
340
341 #endif // PORTING_HEADER
342