SeExpr
Platform.h
Go to the documentation of this file.
1/*
2 Copyright Disney Enterprises, Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License
6 and the following modification to it: Section 6 Trademarks.
7 deleted and replaced with:
8
9 6. Trademarks. This License does not grant permission to use the
10 trade names, trademarks, service marks, or product names of the
11 Licensor and its affiliates, except as required for reproducing
12 the content of the NOTICE file.
13
14 You may obtain a copy of the License at
15 http://www.apache.org/licenses/LICENSE-2.0
16*/
17#ifndef Platform_h
18#define Platform_h
19
23#include <iostream>
24
25#ifdef __APPLE__
26#include <Availability.h>
27#include <libgen.h>
28#endif
29
30// platform-specific includes
31#if defined(_WIN32) || defined(_WINDOWS) || defined(_MSC_VER)
32#ifndef WINDOWS
33#define WINDOWS
34#endif
35#define _CRT_NONSTDC_NO_DEPRECATE 1
36#define _CRT_SECURE_NO_DEPRECATE 1
37#if !defined(NOMINMAX)
38#define NOMINMAX 1
39#endif
40
41// note: because there are some conflicts preventing the use of
42// windows.h and COFF.h (one of LLVM include files) in the same
43// compilation unit (https://groups.google.com/forum/#!topic/llvm-dev/6n5Q0pFdaSA)
44// do NOT include windows.h here. The Windows implementation is
45// done on the Platform.cpp file, using opaque types.
46
47#include <malloc.h>
48#include <io.h>
49#include <tchar.h>
50#include <process.h>
51
52#else
53
54// linux/unix/posix
55#include <stdlib.h>
56#include <alloca.h>
57#include <string.h>
58#include <pthread.h>
59#include <inttypes.h>
60#include <sys/time.h>
61// OS for spinlock
62#ifdef __APPLE__
63#include <libkern/OSAtomic.h>
64#include <sys/types.h>
65#endif
66#endif // defined(_WIN32)...
67
68// general includes
69#include <stdio.h>
70#include <math.h>
71#include <assert.h>
72
73// missing functions on Windows
74#ifdef WINDOWS
75#define snprintf sprintf_s
76#define strtok_r strtok_s
77typedef __int64 FilePos;
78#define fseeko _fseeki64
79#define ftello _ftelli64
80
81inline double log2(double x) { return log(x) * 1.4426950408889634; }
82
83typedef unsigned int uint32_t;
84#define M_E (2.7182818284590452354)
85#define M_PI (3.141592653589793238)
86#if !defined(UINT32_MAX)
87#define UINT32_MAX (0xffffffff)
88#endif
89#if !defined(UINT32_MAX)
90#define UINT32_MIN (0)
91#endif
92#else
93typedef off_t FilePos;
94#endif
95
96namespace SeExpr2 {
97#ifndef WINDOWS
98
99class Timer {
100#ifdef __APPLE__
101 typedef struct timeval Time;
102#else
103 typedef timespec Time;
104#endif
107
108 public:
109 Timer() : started(false) {}
110
111 void start() {
112 started = true;
113#ifdef __APPLE__
114 gettimeofday(&startTime, 0);
115#else
116 clock_gettime(CLOCK_MONOTONIC, &startTime);
117#endif
118 }
119
120 long elapsedTime() {
121 assert(started);
122#ifdef __APPLE__
123 gettimeofday(&stopTime, 0);
124 long seconds = stopTime.tv_sec - startTime.tv_sec;
125 long useconds = stopTime.tv_usec - startTime.tv_usec;
126 long elapsedTime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
127#else
128 clock_gettime(CLOCK_MONOTONIC, &stopTime);
129 long seconds = stopTime.tv_sec - startTime.tv_sec;
130 long nseconds = stopTime.tv_nsec - startTime.tv_nsec;
131 long elapsedTime = ((seconds) * 1000 + nseconds / 1000000.0) + 0.5;
132#endif
133 return elapsedTime;
134 }
135};
136#else // Windows
137class Timer {
138 __int64 time();
139 __int64 ticksPerSeconds;
140 __int64 startTime, stopTime;
141 bool started;
142
143 public:
144 Timer();
145 void start();
146 long elapsedTime();
147};
148#endif
149
151 public:
152 PrintTiming(const std::string& s) : _s(s) { _timer.start(); }
153
154 ~PrintTiming() { std::cout << _s.c_str() << " (" << _timer.elapsedTime() << " ms)" << std::endl; }
155
156
157 private:
159 const std::string _s;
160};
161}
162
163namespace SeExprInternal2 {
164
165/*
166 * Mutex/SpinLock classes
167 */
168
169#ifdef WINDOWS
170
171class _Mutex {
172 public:
173 _Mutex();
174 ~_Mutex();
175 void lock();
176 void unlock();
177
178 private:
179 void* _mutex;
180};
181
182class _SpinLock {
183 public:
184 _SpinLock();
185 ~_SpinLock();
186 void lock();
187 void unlock();
188
189 private:
190 void* _spinlock;
191};
192
193#else
194// assume linux/unix/posix
195class _Mutex {
196 public:
197 _Mutex() { pthread_mutex_init(&_mutex, 0); }
198 ~_Mutex() { pthread_mutex_destroy(&_mutex); }
199 void lock() { pthread_mutex_lock(&_mutex); }
200 void unlock() { pthread_mutex_unlock(&_mutex); }
201
202 private:
203 pthread_mutex_t _mutex;
204};
205
206#ifdef __APPLE__
207class _SpinLock {
208 public:
209 _SpinLock() { _spinlock = 0; }
210 ~_SpinLock() {}
211 void lock() { OSSpinLockLock(&_spinlock); }
212 void unlock() { OSSpinLockUnlock(&_spinlock); }
213
214 private:
215 OSSpinLock _spinlock;
216};
217#else
219 public:
220 _SpinLock() { pthread_spin_init(&_spinlock, PTHREAD_PROCESS_PRIVATE); }
221 ~_SpinLock() { pthread_spin_destroy(&_spinlock); }
222 void lock() { pthread_spin_lock(&_spinlock); }
223 void unlock() { pthread_spin_unlock(&_spinlock); }
224
225 private:
226 pthread_spinlock_t _spinlock;
227};
228#endif // __APPLE__
229#endif
230}
231
232#endif // Platform_h
off_t FilePos
Definition: Platform.h:93
const std::string _s
Definition: Platform.h:159
PrintTiming(const std::string &s)
Definition: Platform.h:152
Time startTime
Definition: Platform.h:105
timespec Time
Definition: Platform.h:103
long elapsedTime()
Definition: Platform.h:120
void start()
Definition: Platform.h:111
pthread_mutex_t _mutex
Definition: Platform.h:203
pthread_spinlock_t _spinlock
Definition: Platform.h:226
</pre >< h3 > A simple variable reference</h3 > This is not a very interesting subclass of expression until we add some additional variables Variables on some applications may be very dynamic In this we only need x
Definition: tutorial.txt:108