My Project
ParaUtils.hpp
1 #pragma once
2 //-----------------------------------------------------------------------------
3 // Class: Header only utility functions
4 // Authors: LiXizhi
5 // Emails: LiXizhi@yeah.net
6 // Company: ParaEngine Co.
7 // Date: 2010.4.21
8 // Desc: Some util functions that can be used in plugins without the need to link with the core library.
9 //-----------------------------------------------------------------------------
10 #include "PEtypes.h"
11 
12 #ifdef WIN32
13 #include <time.h>
14 #else
15 // linux implementation
16 #include <sys/time.h>
17 #include <unistd.h>
18 #endif
19 
20 namespace ParaEngine
21 {
25  class CParaUtils
26  {
27  public:
28  static int64 GetTimeMS()
29  {
30  return (GetTimeUS()/1000);
31  }
32 
34  static int64 GetTimeUS()
35  {
36  static bool initialized=false;
37  static int queryCount=0;
38 #ifdef WIN32
39  // win32 implementation
40  static DWORD g_ProcMask;
41  static DWORD g_SysMask;
42  static HANDLE g_Thread;
43  static LARGE_INTEGER g_yo;
44 
45  // Win32
46  if ( initialized == false)
47  {
48  initialized = true;
49  // Save the current process
50  HANDLE mProc = GetCurrentProcess();
51 
52  // Get the current Affinity
53 #if _MSC_VER >= 1400 && defined (_M_X64)
54  GetProcessAffinityMask(mProc, (PDWORD_PTR)&g_ProcMask, (PDWORD_PTR)&g_SysMask);
55 #else
56  GetProcessAffinityMask(mProc, &g_ProcMask, &g_SysMask);
57 #endif
58  g_Thread = GetCurrentThread();
59 
60  QueryPerformanceFrequency( &g_yo );
61  }
62 
63  int64 curTime;
64  static int64 lastQueryVal=(int64)0;
65  // static unsigned long lastTickCountVal = GetTickCount();
66 
67  LARGE_INTEGER PerfVal;
68 
69  // Set affinity to the first core
70  SetThreadAffinityMask(g_Thread, 1);
71 
72  // Docs: On a multiprocessor computer, it should not matter which processor is called.
73  // However, g_you can get different results on different processors due to bugs in the basic input/output system (BIOS) or the hardware abstraction layer (HAL). To specify processor affinity for a thread, use the SetThreadAffinityMask function.
74  // Query the timer
75  QueryPerformanceCounter( &PerfVal );
76 
77  // Reset affinity
78  SetThreadAffinityMask(g_Thread, g_ProcMask);
79 
80 
81  int64 quotient, remainder;
82  quotient=((PerfVal.QuadPart) / g_yo.QuadPart);
83  remainder=((PerfVal.QuadPart) % g_yo.QuadPart);
84  curTime = (int64) quotient*(int64)1000000 + (remainder*(int64)1000000 / g_yo.QuadPart);
85 
86  // 08/26/08 - With the below workaround, the time seems to jump forward regardless.
87  // Just make sure the time doesn't go backwards
88  if (curTime < lastQueryVal)
89  return lastQueryVal;
90  lastQueryVal=curTime;
91 
92  return curTime;
93 #else
94  static timeval tp;
95  static int64 initialTime;
96 
97  // Linux implementation
98  if ( initialized == false)
99  {
100  gettimeofday( &tp, 0 );
101  initialized=true;
102  // I do this because otherwise RakNetTime in milliseconds won't work as it will underflow when dividing by 1000 to do the conversion
103  initialTime = ( tp.tv_sec ) * (int64) 1000000 + ( tp.tv_usec );
104  }
105 
106  // GCC
107  int64 curTime;
108  gettimeofday( &tp, 0 );
109 
110  curTime = ( tp.tv_sec ) * (int64) 1000000 + ( tp.tv_usec );
111  // Subtract from initialTime so the millisecond conversion does not underflow
112  return curTime - initialTime;
113 #endif
114  }
115  };
116 }
different physics engine has different winding order.
Definition: EventBinding.h:32
Main class.
Definition: ParaUtils.hpp:25
static int64 GetTimeUS()
get time in nano seconds.
Definition: ParaUtils.hpp:34
Definition: curl_setup_once.h:110