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 #define NOMINMAX 1
38 
39 // windows - defined for both Win32 and Win64
40 #include <Windows.h>
41 #include <malloc.h>
42 #include <io.h>
43 #include <tchar.h>
44 #include <process.h>
45 
46 #else
47 
48 // linux/unix/posix
49 #include <stdlib.h>
50 #include <alloca.h>
51 #include <string.h>
52 #include <pthread.h>
53 #include <inttypes.h>
54 // OS for spinlock
55 #ifdef __APPLE__
56 #include <libkern/OSAtomic.h>
57 #include <sys/types.h>
58 #endif
59 #endif // defined(_WIN32)...
60 
61 // general includes
62 #include <stdio.h>
63 #include <math.h>
64 #include <assert.h>
65 #include <sys/time.h>
66 
67 // missing functions on Windows
68 #ifdef WINDOWS
69 #define snprintf sprintf_s
70 #define strtok_r strtok_s
71 typedef __int64 FilePos;
72 #define fseeko _fseeki64
73 #define ftello _ftelli64
74 
75 inline double log2(double x) { return log(x) * 1.4426950408889634; }
76 
77 typedef unsigned int uint32_t;
78 #define M_E (2.7182818284590452354)
79 #define M_PI (3.141592653589793238)
80 #define UINT32_MAX (0xffffffff)
81 #define UINT32_MIN (0)
82 #else
83 typedef off_t FilePos;
84 #endif
85 
86 namespace SeExpr2 {
87 #ifndef WINDOWS
88 
89 class Timer {
90 #ifdef __APPLE__
91  typedef struct timeval Time;
92 #else
93  typedef timespec Time;
94 #endif
96  bool started;
97 
98  public:
99  Timer() : started(false) {}
100 
101  void start() {
102  started = true;
103 #ifdef __APPLE__
104  gettimeofday(&startTime, 0);
105 #else
106  clock_gettime(CLOCK_MONOTONIC, &startTime);
107 #endif
108  }
109 
110  long elapsedTime() {
111  assert(started);
112 #ifdef __APPLE__
113  gettimeofday(&stopTime, 0);
114  long seconds = stopTime.tv_sec - startTime.tv_sec;
115  long useconds = stopTime.tv_usec - startTime.tv_usec;
116  long elapsedTime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
117 #else
118  clock_gettime(CLOCK_MONOTONIC, &stopTime);
119  long seconds = stopTime.tv_sec - startTime.tv_sec;
120  long nseconds = stopTime.tv_nsec - startTime.tv_nsec;
121  long elapsedTime = ((seconds) * 1000 + nseconds / 1000000.0) + 0.5;
122 #endif
123  return elapsedTime;
124  }
125 };
126 #else // Windows
127 class Timer {
128  public:
129  Timer() : started(false) {}
130 
131  void start() { std::cerr << "timer not implemented on Windows" << std::endl; }
132  long elapsedTime() { return 0; }
133 };
134 #endif
135 
136 class PrintTiming {
137  public:
138  PrintTiming(const std::string& s) : _s(s) { _timer.start(); }
139 
140  ~PrintTiming() { std::cout << _s << " (" << _timer.elapsedTime() << " ms)" << std::endl; }
141 
142  private:
144  const std::string _s;
145 };
146 }
147 
148 namespace SeExprInternal2 {
149 
150 /*
151  * Mutex/SpinLock classes
152  */
153 
154 #ifdef WINDOWS
155 
156 class _Mutex {
157  public:
158  _Mutex() { _mutex = CreateMutex(NULL, FALSE, NULL); }
159  ~_Mutex() { CloseHandle(_mutex); }
160  void lock() { WaitForSingleObject(_mutex, INFINITE); }
161  void unlock() { ReleaseMutex(_mutex); }
162 
163  private:
164  HANDLE _mutex;
165 };
166 
167 class _SpinLock {
168  public:
169  _SpinLock() { InitializeCriticalSection(&_spinlock); }
170  ~_SpinLock() { DeleteCriticalSection(&_spinlock); }
171  void lock() { EnterCriticalSection(&_spinlock); }
172  void unlock() { LeaveCriticalSection(&_spinlock); }
173 
174  private:
175  CRITICAL_SECTION _spinlock;
176 };
177 
178 #else
179 // assume linux/unix/posix
180 class _Mutex {
181  public:
182  _Mutex() { pthread_mutex_init(&_mutex, 0); }
183  ~_Mutex() { pthread_mutex_destroy(&_mutex); }
184  void lock() { pthread_mutex_lock(&_mutex); }
185  void unlock() { pthread_mutex_unlock(&_mutex); }
186 
187  private:
188  pthread_mutex_t _mutex;
189 };
190 
191 #ifdef __APPLE__
192 class _SpinLock {
193  public:
194  _SpinLock() { _spinlock = 0; }
195  ~_SpinLock() {}
196  void lock() { OSSpinLockLock(&_spinlock); }
197  void unlock() { OSSpinLockUnlock(&_spinlock); }
198 
199  private:
200  OSSpinLock _spinlock;
201 };
202 #else
203 class _SpinLock {
204  public:
205  _SpinLock() { pthread_spin_init(&_spinlock, PTHREAD_PROCESS_PRIVATE); }
206  ~_SpinLock() { pthread_spin_destroy(&_spinlock); }
207  void lock() { pthread_spin_lock(&_spinlock); }
208  void unlock() { pthread_spin_unlock(&_spinlock); }
209 
210  private:
211  pthread_spinlock_t _spinlock;
212 };
213 #endif // __APPLE__
214 #endif
215 }
216 
217 #endif // Platform_h
off_t FilePos
Definition: Platform.h:83
long elapsedTime()
Definition: Platform.h:110
Time stopTime
Definition: Platform.h:95
timespec Time
Definition: Platform.h:93
bool started
Definition: Platform.h:96
pthread_mutex_t _mutex
Definition: Platform.h:188
Time startTime
Definition: Platform.h:95
PrintTiming(const std::string &s)
Definition: Platform.h:138
</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
void start()
Definition: Platform.h:101
pthread_spinlock_t _spinlock
Definition: Platform.h:211
const std::string _s
Definition: Platform.h:144