AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
Sleep.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_SLEEP_H
18#define AGX_SLEEP_H
19
21#include <time.h>
22
23//#include <xmmintrin.h>
24
25#ifdef _WIN64
26#include <intrin.h>
27#endif
28// #include <emmintrin.h>
29
30namespace 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 */
static agx::UInt getCpuFrequency()
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint64_t UInt64
Definition Integer.h:33
void microSleep(Real microSeconds)
Sleep the current thread.
Definition Sleep.h:54
void noopLoop(UInt64 numIterations)
Definition Sleep.h:34
double Real
Definition Real.h:42
void nanoSleep(long)
Definition Sleep.h:65