OpenDaVINCI  4.15.3
POSIXConcurrencyFactoryWorker.h
Go to the documentation of this file.
1 
20 #ifndef OPENDAVINCI_CORE_WRAPPER_POSIX_POSIXCONCURRENCYFACTORYWORKER_H_
21 #define OPENDAVINCI_CORE_WRAPPER_POSIX_POSIXCONCURRENCYFACTORYWORKER_H_
22 
23 #include <sys/time.h>
24 
26 
29 
31 
32 namespace odcore {
33  namespace wrapper {
34 
35  using namespace std;
36 
38  public:
39  static Thread* createThread(Runnable &runnable) {
40  return new odcore::wrapper::POSIX::POSIXThread(runnable);
41  };
42 
43  static void usleepFor(const long &microseconds) {
44  struct timespec delay;
45 
46  delay.tv_sec = 0;
47  delay.tv_nsec = 0;
48 
49  const long NANOSECONDS_PER_SECOND = 1000 * 1000 * 1000;
50  long nanoseconds = microseconds * 1000;
51  while (nanoseconds >= NANOSECONDS_PER_SECOND) {
52  nanoseconds -= NANOSECONDS_PER_SECOND;
53  delay.tv_sec++;
54  }
55  // Add remaining nanoseconds.
56  delay.tv_nsec += nanoseconds;
57 
58 #if defined(__FreeBSD__) || (__NetBSD__) || (__OpenBSD__) || (__APPLE__) || (__DragonFly__)
59  nanosleep(&delay, NULL);
60 #else
61  // Same call as nanosleep(&delay, NULL);
62  clock_nanosleep(CLOCK_REALTIME, 0, &delay, NULL);
63 #endif
64  };
65 
66  static void usleepUntil(const long &seconds, const long &partialMicroseconds) {
67 #if defined(__FreeBSD__) || (__NetBSD__) || (__OpenBSD__) || (__APPLE__) || (__DragonFly__)
68  struct timeval now;
69  gettimeofday(&now, NULL);
70 
71  long waitSec = seconds - now.tv_sec;
72  long waitPartialMicrosec = partialMicroseconds - now.tv_usec;
73 
74  // Check for partial microseconds being negative (waiting not a full second but just a remainder).
75  if (waitPartialMicrosec < 0) {
76  waitSec--;
77  waitPartialMicrosec += (1000 * 1000);
78  }
79 
80  if ((waitSec > 0) && (waitPartialMicrosec > 0)) {
81  struct timespec delay;
82  delay.tv_sec = waitSec;
83  delay.tv_nsec = waitPartialMicrosec;
84 
85  const long NANOSECONDS_PER_SECOND = 1000 * 1000 * 1000;
86  long nanoseconds = delay.tv_nsec * 1000;
87  while (nanoseconds >= NANOSECONDS_PER_SECOND) {
88  nanoseconds -= NANOSECONDS_PER_SECOND;
89  delay.tv_sec++;
90  }
91  // Add remaining nanoseconds.
92  delay.tv_nsec += nanoseconds;
93 
94  // Use regular nanosleep.
95  nanosleep(&delay, NULL);
96  }
97 #else
98  struct timespec timePoint;
99  timePoint.tv_sec = seconds;
100  timePoint.tv_nsec = 0;
101 
102  const long NANOSECONDS_PER_SECOND = 1000 * 1000 * 1000;
103  long nanoseconds = partialMicroseconds * 1000;
104  while (nanoseconds >= NANOSECONDS_PER_SECOND) {
105  nanoseconds -= NANOSECONDS_PER_SECOND;
106  timePoint.tv_sec++;
107  }
108  // Add remaining nanoseconds.
109  timePoint.tv_nsec += nanoseconds;
110 
111  clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &timePoint, NULL);
112 #endif
113  };
114  };
115  }
116 } // odcore::wrapper::POSIX
117 
118 #endif /*OPENDAVINCI_CORE_WRAPPER_POSIX_POSIXCONCURRENCYFACTORY_H_*/
static Thread * createThread(Runnable &runnable)
Definition: POSIXConcurrencyFactoryWorker.h:39
Definition: ConcurrencyFactoryWorker.h:42
#define OPENDAVINCI_API
Definition: opendavinci.h:67
Definition: Runnable.h:35
static void usleepFor(const long &microseconds)
Definition: POSIXConcurrencyFactoryWorker.h:43
static void usleepUntil(const long &seconds, const long &partialMicroseconds)
Definition: POSIXConcurrencyFactoryWorker.h:66
Definition: POSIXThread.h:45
Definition: base/Thread.h:34
Definition: SystemLibraryProducts.h:28