Added nanosleep with float argument
authorArjen Baart <arjen@andromeda.nl>
Tue, 3 Sep 2019 15:08:37 +0000 (17:08 +0200)
committerArjen Baart <arjen@andromeda.nl>
Tue, 3 Sep 2019 15:08:37 +0000 (17:08 +0200)
.gitignore [new file with mode: 0644]
doc/design.xml
src/Tachyon.cpp
src/Tachyon.h
test/Makefile.am
test/accellerate_float [new file with mode: 0755]
test/sleep_float.cpp [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..b4dc9a3
--- /dev/null
@@ -0,0 +1,2 @@
+*.[oa]
+Makefile.in
index ab2ba75..ee4c1c0 100644 (file)
@@ -1,6 +1,10 @@
 <?xml version='1.0'?>
 <doc  style="main.css">
 <book>
+  <titlepage>
+      <title>Tachyon</title>
+   </titlepage>
+
 <chapter>
 <heading>Introduction</heading>
 
@@ -127,6 +131,21 @@ Return the virtual in second since the Epoch. See also time(2).
 
 <item tag='int nanosleep(struct timespec req)'>
 Suspend execution until the time in req is passed. That time is divided by the accelleration set in the Tachyon object. So, if a waiting time of 60 seconds is requested and the accelleration is 10, execution will be actually be suspended for 6 seconds.
+The time is specified with nanosecond precision in the <code>struct timespec</code>. It is defined as follows:
+<verbatim>
+           struct timespec
+           {
+               time_t tv_sec;        /* seconds */
+               long   tv_nsec;       /* nanoseconds */
+           };
+
+
+</verbatim>
+</item>
+
+<item tag='int nanosleep(float req)'>
+Suspend execution until the time in req is passed. That time is divided by the accelleration set in the Tachyon object. So, if a waiting time of 60 seconds is requested and the accelleration is 10, execution will be actually be suspended for 6 seconds.
+The time is specified with nanosecond precision in a floating point number.
 </item>
 
 <item tag='settime (time_t sec)'>
index 3144a8c..58ef397 100644 (file)
@@ -105,6 +105,21 @@ int Tachyon::nanosleep(struct timespec req)
    ::nanosleep(&req, NULL);
 }
 
+int Tachyon::nanosleep(float req)
+{
+   double seconds;
+   double nanoseconds;
+   struct timespec req_t;
+
+   seconds     = req / accelleration;
+   nanoseconds = (seconds - trunc(seconds)) * 1.0e9;
+
+   req_t.tv_sec  = trunc(seconds);
+   req_t.tv_nsec = trunc(nanoseconds);
+
+   ::nanosleep(&req_t, NULL);
+}
+
 void Tachyon::settime(time_t sec)
 {
 }
index 9605aee..04eb354 100644 (file)
@@ -27,6 +27,7 @@ public:
 
    time_t time(void);
    int nanosleep(struct timespec req);
+   int nanosleep(float req);
 
    void settime(time_t sec);
    void accellerate(double factor);
index c49ca7a..d5e36d4 100644 (file)
@@ -1,13 +1,14 @@
-TESTS = create sleep accellerate
+TESTS = create sleep accellerate sleep_float accellerate_float
 
 AM_CPPFLAGS = -I../src
 LDADD = ../src/.libs/libTachyon.la -lrt -lm
 
-check_PROGRAMS = create sleep
+check_PROGRAMS = create sleep sleep_float
 
 
-create_SOURCES = create.cpp
-sleep_SOURCES  = sleep.cpp
+create_SOURCES       = create.cpp
+sleep_SOURCES        = sleep.cpp
+sleep_float_SOURCES  = sleep_float.cpp
 
 clean-local:
        rm -f sleep.tmp
diff --git a/test/accellerate_float b/test/accellerate_float
new file mode 100755 (executable)
index 0000000..f41e818
--- /dev/null
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+set -m
+
+PATH=../src:$PATH
+
+START_TIME=`date +%s`
+./sleep_float >sleep.tmp &
+sleep 1
+read a b c TACHYON_NAME <sleep.tmp
+tachyon -a 20 $TACHYON_NAME
+fg
+END_TIME=`date +%s`
+duration=$(($END_TIME - $START_TIME))
+echo "Elapsed time is $duration"
+if [[ $duration -eq 6 ]] || [[ $duration -eq 5 ]]
+then
+   echo "Elapsed time within 7 seconds"
+   exit 0
+fi
+echo "Elapsed time $duration seconds is unexpected."
+exit 1
diff --git a/test/sleep_float.cpp b/test/sleep_float.cpp
new file mode 100644 (file)
index 0000000..afbcc42
--- /dev/null
@@ -0,0 +1,19 @@
+#include <iostream>
+#include "Tachyon.h"
+
+int main()
+{
+
+   Tachyon timebase;
+   float interval;
+
+   interval = 3.33;
+
+   std::cout << "Tachyon name is " << timebase.name() << "\n";
+   std::cout.flush();
+   for (int i = 0; i < 10; i++)
+   {
+      timebase.nanosleep(interval);
+      std::cout << "3.3 seconds.\n";
+   }
+}