]> git.rkrishnan.org Git - pihpsdr.git/commitdiff
MacOS: include header file "MacOS.h" to implement clock_gettime and clock_nanosleep
authorc vw <dl1ycf@darc.de>
Fri, 12 Jun 2020 07:57:19 +0000 (09:57 +0200)
committerc vw <dl1ycf@darc.de>
Fri, 12 Jun 2020 07:57:19 +0000 (09:57 +0200)
MacOS.h [new file with mode: 0644]
hpsdrsim.c
iambic.c
midi2.c
new_protocol_programmer.c
newhpsdrsim.c

diff --git a/MacOS.h b/MacOS.h
new file mode 100644 (file)
index 0000000..ac745b2
--- /dev/null
+++ b/MacOS.h
@@ -0,0 +1,90 @@
+/*
+ *  Replace missing library functions with inline code
+ */
+
+#ifdef __APPLE__
+
+#include <time.h>
+
+#if !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC)
+//
+// MacOS < 10.12 does not have clock_gettime
+//
+// Contribution from github user "ra1nb0w"
+//
+
+#define CLOCK_REALTIME  0
+#define CLOCK_MONOTONIC 6
+typedef int clockid_t;
+
+#include <sys/time.h>
+#include <mach/mach_time.h>
+
+// here to avoid problem on linking
+static inline int clock_gettime( clockid_t clk_id, struct timespec *ts )
+{
+  int ret = -1;
+  if ( ts )
+  {
+    if      ( CLOCK_REALTIME == clk_id )
+    {
+      struct timeval tv;
+      ret = gettimeofday(&tv, NULL);
+      ts->tv_sec  = tv.tv_sec;
+      ts->tv_nsec = tv.tv_usec * 1000;
+    }
+    else if ( CLOCK_MONOTONIC == clk_id )
+    {
+      const uint64_t t = mach_absolute_time();
+      mach_timebase_info_data_t timebase;
+      mach_timebase_info(&timebase);
+      const uint64_t tdiff = t * timebase.numer / timebase.denom;
+      ts->tv_sec  = tdiff / 1000000000;
+      ts->tv_nsec = tdiff % 1000000000;
+      ret = 0;
+    }
+  }
+  return ret;
+}
+
+#endif // CLOCK_REALTIME and CLOCK_MONOTONIC
+
+//
+// MacOS does not have clock_nanosleep but it does have nanosleep
+// We ignores clock_id (assuming CLOCK_MONOTONIC)
+// but for the flags we allow TIMER_ABSTIME (sleep until a specific poin
+// in time), for all other value we sleep for a speficic period.
+//
+
+#if !defined(TIMER_ABSTIME)
+#define TIMER_ABSTIME 12345
+
+static inline int clock_nanosleep(clockid_t clock_id, int flags,
+                              const struct timespec *request,
+                              struct timespec *remain) {
+  struct timespec now;
+  int rc;
+
+  if (clock_id == TIMER_ABSTIME) {
+    //
+    // sleep until point in the future
+    //
+    clock_gettime(CLOCK_MONOTONIC, &now);
+    now.tv_sec = request->tv_sec  - now.tv_sec;
+    now.tv_nsec= request->tv_nsec - now.tv_nsec;
+    while (now.tv_nsec < 0) {
+      now.tv_nsec += 1000000000;
+      now.tv_sec--;
+    }
+    rc=nanosleep(&now, remain);
+  } else {
+    //
+    // sleep for the given period
+    //
+    rc=nanosleep(request, remain);
+  }
+  return rc;
+}
+#endif  // !defined(TIMER_ABSTIME)
+
+#endif // __APPLE__
index f3ef03dddc589818dea4c9a596796563663532a0..ccd8417b04908a6bde5dbfb15b8d59177c16c500 100644 (file)
@@ -64,6 +64,9 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
+#ifdef __APPLE__
+#include "MacOS.h"  // emulate clock_gettime on old MacOS systems
+#endif
 
 #define NEED_DUMMY_AUDIO 1
 
@@ -1158,9 +1161,6 @@ void *handler_ep6(void *arg)
         int16_t ssample;
 
         struct timespec delay;
-#ifdef __APPLE__
-       struct timespec now;
-#endif
         long wait;
         int noiseIQpt,toneIQpt,divpt,rxptr;
         double i1,q1,fac1,fac2,fac3,fac4;
@@ -1366,24 +1366,7 @@ void *handler_ep6(void *arg)
                   delay.tv_nsec -= 1000000000;
                   delay.tv_sec++;
                 }
-#ifdef __APPLE__
-               //
-               // The (so-called) operating system for Mac does not have clock_nanosleep(),
-               // but is has clock_gettime as well as nanosleep.
-               // So, to circumvent this problem, we look at the watch and determine
-               // how long we should sleep now.
-               //
-               clock_gettime(CLOCK_MONOTONIC, &now);
-               now.tv_sec =delay.tv_sec  - now.tv_sec;
-               now.tv_nsec=delay.tv_nsec - now.tv_nsec;
-               while (now.tv_nsec < 0) {
-                   now.tv_nsec += 1000000000;
-                   now.tv_sec--;
-               }
-               nanosleep(&now, NULL);
-#else
                clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &delay, NULL);
-#endif
 
                if (sock_TCP_Client > -1)
                {
index 58a20cb14127a16ca9d37e098d28ab431e1a5c78..49196631c8d5133940437d3656be4acb555333d3 100644 (file)
--- a/iambic.c
+++ b/iambic.c
@@ -230,6 +230,10 @@ static sem_t cw_event;
 
 static int cwvox = 0;
 
+#ifdef __APPLE__
+#include "MacOS.h"  // emulate clock_gettime on old MacOS systems
+#endif
+
 #ifndef __APPLE__
 // using clock_nanosleep of librt
 extern int clock_nanosleep(clockid_t __clock_id, int __flags,
@@ -343,9 +347,6 @@ static void* keyer_thread(void *arg) {
     int kdelay;
     int old_volume;
     int txmode;
-#ifdef __APPLE__
-    struct timespec now;
-#endif
 
     fprintf(stderr,"keyer_thread  state running= %d\n", running);
     while(running) {
@@ -427,18 +428,7 @@ static void* keyer_thread(void *arg) {
                          loop_delay.tv_sec++;
                        }
                        if (!*kdash) break;
-#ifdef __APPLE__
-                       clock_gettime(CLOCK_MONOTONIC, &now);
-                       now.tv_sec =loop_delay.tv_sec  - now.tv_sec;
-                       now.tv_nsec=loop_delay.tv_nsec - now.tv_nsec;
-                       while (now.tv_nsec < 0) {
-                         now.tv_nsec += 1000000000;
-                         now.tv_sec--;
-                       }
-                       nanosleep(&now, NULL);
-#else
                        clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &loop_delay, NULL);
-#endif
                      }
                      // dash released.
                       set_keyer_out(0);
@@ -471,18 +461,7 @@ static void* keyer_thread(void *arg) {
                   loop_delay.tv_nsec -= NSEC_PER_SEC;
                   loop_delay.tv_sec++;
                }
-#ifdef __APPLE__
-                clock_gettime(CLOCK_MONOTONIC, &now);
-                now.tv_sec =loop_delay.tv_sec  - now.tv_sec;
-                now.tv_nsec=loop_delay.tv_nsec - now.tv_nsec;
-                while (now.tv_nsec < 0) {
-                  now.tv_nsec += 1000000000;
-                  now.tv_sec--;
-                }
-                nanosleep(&now, NULL);
-#else
                 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &loop_delay, NULL);
-#endif
                 set_keyer_out(0);
                 key_state = DOTDELAY;       // add inter-character spacing of one dot length
                kdelay=0;
@@ -533,18 +512,7 @@ static void* keyer_thread(void *arg) {
                   loop_delay.tv_nsec -= NSEC_PER_SEC;
                   loop_delay.tv_sec++;
                 }
-#ifdef __APPLE__
-                clock_gettime(CLOCK_MONOTONIC, &now);
-                now.tv_sec =loop_delay.tv_sec  - now.tv_sec;
-                now.tv_nsec=loop_delay.tv_nsec - now.tv_nsec;
-                while (now.tv_nsec < 0) {
-                  now.tv_nsec += 1000000000;
-                  now.tv_sec--;
-                }
-                nanosleep(&now, NULL);
-#else
                 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &loop_delay, NULL);
-#endif
                 set_keyer_out(0);
                 key_state = DASHDELAY;       // add inter-character spacing of one dot length
                kdelay=0;
@@ -602,18 +570,7 @@ static void* keyer_thread(void *arg) {
                 loop_delay.tv_nsec -= NSEC_PER_SEC;
                 loop_delay.tv_sec++;
             }
-#ifdef __APPLE__
-            clock_gettime(CLOCK_MONOTONIC, &now);
-            now.tv_sec =loop_delay.tv_sec  - now.tv_sec;
-            now.tv_nsec=loop_delay.tv_nsec - now.tv_nsec;
-            while (now.tv_nsec < 0) {
-              now.tv_nsec += 1000000000;
-              now.tv_sec--;
-            }
-            nanosleep(&now, NULL);
-#else
             clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &loop_delay, NULL);
-#endif
         }
        //
        // If we have reduced the side tone volume, restore it!
diff --git a/midi2.c b/midi2.c
index 78cfaff985c8407e17566c0f82ee86659c0e111a..871559c6e598be21e808af2e26a71ab6bce762a2 100644 (file)
--- a/midi2.c
+++ b/midi2.c
 #include <string.h>
 #include <stdlib.h>
 #include <time.h>
+#ifdef __APPLE__
+#include "MacOS.h"  // emulate clock_gettime on old MacOS systems
+#endif
+
 #include "midi.h"
 
 void NewMidiEvent(enum MIDIevent event, int channel, int note, int val) {
index 31d40327d9ac18990391fb84f9876dd4383d352b..aba35b3f32ff0bf02251abf4d5ad5115104ecce0 100644 (file)
@@ -36,6 +36,9 @@
 #include <time.h>
 #include <math.h>
 #include <errno.h>
+#ifdef __APPLE__
+#include "MacOS.h"  // emulate clock_gettime on old MacOS systems
+#endif
 
 #include <gtk/gtk.h>
 
index b7b2f2a25e33129a9364652a1f4a666449e7ec6c..48b49c85c4abe6fd119245fc61877ab6099af8ae 100644 (file)
@@ -9,6 +9,9 @@
 #include <unistd.h>
 #include <arpa/inet.h>
 #include <math.h>
+#ifdef __APPLE__
+#include "MacOS.h"  // emulate clock_gettime on old MacOS systems
+#endif
 
 #define EXTERN extern
 #include "hpsdrsim.h"
@@ -754,9 +757,6 @@ void *rx_thread(void *data) {
   unsigned int seed;
   
   struct timespec delay;
-#ifdef __APPLE__
-  struct timespec now;
-#endif
 
   myddc=(int) (uintptr_t) data;
   if (myddc < 0 || myddc >= NUMRECEIVERS) return NULL;
@@ -926,24 +926,7 @@ void *rx_thread(void *data) {
           delay.tv_nsec -= 1000000000;
           delay.tv_sec++;
        }
-#ifdef __APPLE__
-        //
-        // The (so-called) operating system for Mac does not have clock_nanosleep(),
-        // but is has clock_gettime as well as nanosleep.
-        // So, to circumvent this problem, we look at the watch and determine
-        // how long we should sleep now.
-        //
-        clock_gettime(CLOCK_MONOTONIC, &now);
-        now.tv_sec =delay.tv_sec  - now.tv_sec;
-        now.tv_nsec=delay.tv_nsec - now.tv_nsec;
-        while (now.tv_nsec < 0) {
-         now.tv_nsec += 1000000000;
-         now.tv_sec--;
-        }
-        nanosleep(&now, NULL);
-#else
         clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &delay, NULL);
-#endif
         if (sendto(sock, buffer, 1444, 0, (struct sockaddr*)&addr_new, sizeof(addr_new)) < 0) {
           perror("***** ERROR: RX thread sendto");
           break;
@@ -1200,9 +1183,6 @@ void *mic_thread(void *data) {
   int rc;
   int i;
   struct timespec delay;
-#ifdef __APPLE__
-  struct timespec now;
-#endif
 
 
   sock=socket(AF_INET, SOCK_DGRAM, 0);
@@ -1242,24 +1222,7 @@ void *mic_thread(void *data) {
       delay.tv_nsec -= 1000000000;
       delay.tv_sec++;
     }
-#ifdef __APPLE__
-    //
-    // The (so-called) operating system for Mac does not have clock_nanosleep(),
-    // but is has clock_gettime as well as nanosleep.
-    // So, to circumvent this problem, we look at the watch and determine
-    // how long we should sleep now.
-    //
-    clock_gettime(CLOCK_MONOTONIC, &now);
-    now.tv_sec =delay.tv_sec  - now.tv_sec;
-    now.tv_nsec=delay.tv_nsec - now.tv_nsec;
-    while (now.tv_nsec < 0) {
-     now.tv_nsec += 1000000000;
-     now.tv_sec--;
-    }
-    nanosleep(&now, NULL);
-#else
     clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &delay, NULL);
-#endif
     if (sendto(sock, buffer, 132, 0, (struct sockaddr*)&addr_new, sizeof(addr_new)) < 0) {
       perror("***** ERROR: Mic thread sendto");
       break;