AGX Dynamics  2.37.2.0
Sleep.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are 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  void nanoSleep( long );
33 
34  inline void noopLoop(UInt64 numIterations)
35  {
36  for (UInt64 i = 0; i < numIterations; ++i)
37  {
38  #if defined(_MSC_VER)
39  #if defined(_WIN64)
40  __nop();
41  #else
42  __asm nop;
43  #endif
44  #else
45  asm volatile("nop\n\t");
46  // _mm_pause();
47  #endif
48  }
49  }
50 
54  inline void microSleep(Real microSeconds)
55  {
56 #if defined(__APPLE__) && defined(__aarch64__)
57  // getCpuFrequency is not supported on Apple Silicon yet.
58  nanoSleep(static_cast<long>(microSeconds * 1000.0));
59 #else
60  noopLoop((UInt64)(microSeconds * Real(SystemInformation::getCpuFrequency()))); // NOTE: getCpuFrequency returns MHz
61 #endif
62  }
63 
64 
65  inline void nanoSleep( long nanoSeconds )
66  {
67 #if defined(_MSC_VER)
68  agx::microSleep( nanoSeconds / agx::Real(1000.0) );
69 #else
70  timespec sleepTime;
71  sleepTime.tv_sec = nanoSeconds / 1000000000;
72  sleepTime.tv_nsec = nanoSeconds % 1000000000;
73 
74  nanosleep( &sleepTime, 0 );
75 #endif
76  }
77 }
78 
79 
80 #endif /* AGX_SLEEP_H */
void microSleep(Real microSeconds)
Sleep the current thread.
Definition: Sleep.h:54
void noopLoop(UInt64 numIterations)
Definition: Sleep.h:34
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint64_t UInt64
Definition: Integer.h:41
void nanoSleep(long)
Definition: Sleep.h:65
double Real
Definition: Real.h:42
static agx::UInt getCpuFrequency()