]> rtime.felk.cvut.cz Git - frescor/fosa.git/blobdiff - src_marte/frsh_fosa.c
Creation of frsh_eat() to be used in FOSA tests
[frescor/fosa.git] / src_marte / frsh_fosa.c
index 094f26c82d87a21fff4d79fd10c2fc98a03622c0..66c64e9a353f31fa03343ceab3c0954bf271c50a 100644 (file)
@@ -66,6 +66,9 @@
 #include "frsh_fosa.h"
 #include <pthread.h>
 #include <stdio.h>
+#include <time.h>
+
+#include <timespec_operations.h> /* for incr_timespec */
 
 #include <misc/error_checks.h>
 
@@ -160,3 +163,37 @@ int frsh_thread_attr_get_stacksize
 {
   return pthread_attr_getstacksize(attr,stacksize);
 }
+
+
+/**
+ * frsh_eat()
+ *
+ * Eat some time using system clock facilities
+ **/
+#ifdef VIRTUAL_TIME
+    #include <fosa_vt.h>
+#endif
+
+void inline frsh_eat(const struct timespec *cpu_time)
+{
+#ifdef VIRTUAL_TIME
+    vt_time_t vt_clock;
+    timespec_2_vtime(cpu_time, vt_clock);
+    vt_use_time((unsigned long long)vt_clock);
+#else
+    int err;
+    clockid_t clock_id;
+    struct timespec current_time, time_to_go;
+
+    // NOTE: there should be a constant for the cpu_clock_id of the caller
+    // to avoid calling 'fosa_thread_get_cputime_clock'
+    err = pthread_getcpuclockid(pthread_self(), &clock_id);
+
+    err = clock_gettime(clock_id, &current_time);
+    add_timespec(&time_to_go, &current_time, cpu_time);
+
+    while (smaller_timespec(&current_time, &time_to_go)) {
+        err = clock_gettime(clock_id, &current_time);
+    }
+#endif
+}