[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[XTF 4/6] time: Add helper functions and macros to time management



From: Paul Semel <phentex@xxxxxxxxx>

Add the following helper functions:
  - nspin_sleep()
  - spin_sleep()
  - mspin_sleep()

Add the following helper macros:
  - sleep()
  - msleep()
  - NOW()

Signed-off-by: Paul Semel <phentex@xxxxxxxxx>
---
 common/time.c      | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/xtf/time.h | 16 +++++++++++++++
 2 files changed, 74 insertions(+)

diff --git a/common/time.c b/common/time.c
index fffae1c..3db1f8f 100644
--- a/common/time.c
+++ b/common/time.c
@@ -104,6 +104,64 @@ int gettimeofday(struct timeval *tp)
     return 0;
 }
 
+#if defined(__i386__)
+static inline void nspin_sleep(uint32_t t)
+#else
+static inline void nspin_sleep(uint64_t t)
+#endif
+{
+    unsigned long end = since_boot_time() + t;
+
+    while ( since_boot_time() < end )
+        asm volatile ( "pause" );
+}
+
+#if defined(__i386__)
+static inline void spin_sleep(uint32_t t)
+#else
+static inline void spin_sleep(uint64_t t)
+#endif
+{
+#if defined(__i386__)
+    uint32_t nsec = t * 1000000000;
+#else
+    uint64_t nsec = t * 1000000000ul;
+#endif
+    nspin_sleep(nsec);
+}
+
+#if defined(__i386__)
+static inline void mspin_sleep(uint32_t t)
+#else
+static inline void mspin_sleep(uint64_t t)
+#endif
+{
+#if defined(__i386__)
+    uint32_t nsec = t * 1000000;
+#else
+    uint64_t nsec = t * 1000000ul;
+#endif
+    nspin_sleep(nsec);
+}
+
+#if defined(__i386__)
+void sleep(uint32_t t)
+#else
+void sleep(uint64_t t)
+#endif
+{
+    spin_sleep(t);
+}
+
+#if defined(__i386__)
+void msleep(uint32_t t)
+#else
+void msleep(uint64_t t)
+#endif
+{
+    mspin_sleep(t);
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/include/xtf/time.h b/include/xtf/time.h
index e312465..07fcae5 100644
--- a/include/xtf/time.h
+++ b/include/xtf/time.h
@@ -23,14 +23,30 @@ struct timeval {
 uint32_t since_boot_time(void);
 
 uint32_t current_time(void);
+
+/* This function takes seconds in parameter */
+void sleep(uint32_t f);
+
+/* Be careful, this function takes milliseconds in parameter,
+ * not microseconds !
+ */
+void msleep(uint32_t f);
 #else
 uint64_t since_boot_time(void);
 
 uint64_t current_time(void);
+
+void sleep(uint64_t f);
+
+void msleep(uint64_t f);
 #endif
 
 int gettimeofday(struct timeval *tp);
 
+
+/* This returns the current epoch time */
+#define NOW() current_time()
+
 #endif /* XTF_TIME_H */
 
 /*
-- 
2.16.6




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879






 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.