AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Sleep.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, is copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_SLEEP_H
18 #define AGX_SLEEP_H
19 
20 #include <agx/SystemInformation.h>
21 #include <time.h>
22 
23 //#include <xmmintrin.h>
24 
25 #ifdef _WIN64
26 #include <intrin.h>
27 #endif
28 // #include <emmintrin.h>
29 
30 namespace agx
31 {
32  inline void noopLoop(UInt64 numIterations)
33  {
34  for (UInt64 i = 0; i < numIterations; ++i)
35  {
36  #if defined(_MSC_VER)
37  #if defined(_WIN64)
38  __nop();
39  #else
40  __asm nop;
41  #endif
42  #else
43  asm volatile("nop\n\t");
44  // _mm_pause();
45  #endif
46  }
47  }
48 
52  inline void microSleep(Real microSeconds)
53  {
54  noopLoop((UInt64)(microSeconds * Real(SystemInformation::getCpuFrequency()))); // NOTE: getCpuFrequency returns MHz
55  }
56 
57 
58  inline void nanoSleep( long nanoSeconds )
59  {
60 #if defined(_MSC_VER)
61  agx::microSleep( nanoSeconds / agx::Real(1000.0) );
62 #else
63  timespec sleepTime;
64  sleepTime.tv_sec = nanoSeconds / 1000000000;
65  sleepTime.tv_nsec = nanoSeconds % 1000000000;
66 
67  nanosleep( &sleepTime, 0 );
68 #endif
69  }
70 }
71 
72 
73 #endif /* AGX_SLEEP_H */
void microSleep(Real microSeconds)
Sleep the current thread.
Definition: Sleep.h:52
void noopLoop(UInt64 numIterations)
Definition: Sleep.h:32
void nanoSleep(long nanoSeconds)
Definition: Sleep.h:58
uint64_t UInt64
Definition: Integer.h:41
double Real
Definition: Real.h:42
static agx::UInt getCpuFrequency()