[v3,3/7] eal: add sleep API
Checks
Commit Message
POSIX sleep(3) is missing from Windows.
Add generic rte_thread_sleep() to suspend current OS thread.
Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
lib/librte_eal/common/eal_common_timer.c | 5 +++--
lib/librte_eal/include/rte_thread.h | 11 +++++++++++
lib/librte_eal/rte_eal_exports.def | 2 ++
lib/librte_eal/unix/rte_thread.c | 10 +++++++++-
lib/librte_eal/version.map | 3 +++
lib/librte_eal/windows/eal_thread.c | 9 ++++++++-
6 files changed, 36 insertions(+), 4 deletions(-)
Comments
Hi Dmitry,
> +void
> +rte_thread_sleep(unsigned int sec)
> +{
> + return Sleep(MS_PER_S * sec);
> +}
There's probably no benefit in returning the 'void' value - I'd suggest
just call Sleep().
Regards,
Nick
> POSIX sleep(3) is missing from Windows.
> Add generic rte_thread_sleep() to suspend current OS thread.
>
> Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
> ---
Acked-by: Khoa To <khot@microsoft.com>
@@ -16,6 +16,7 @@
#include <rte_cycles.h>
#include <rte_pause.h>
#include <rte_eal.h>
+#include <rte_thread.h>
#include "eal_private.h"
#include "eal_memcfg.h"
@@ -47,9 +48,9 @@ estimate_tsc_freq(void)
#define CYC_PER_10MHZ 1E7
RTE_LOG(WARNING, EAL, "WARNING: TSC frequency estimated roughly"
" - clock timings may be less accurate.\n");
- /* assume that the sleep(1) will sleep for 1 second */
+ /* assume that the rte_thread_sleep(1) will sleep for 1 second */
uint64_t start = rte_rdtsc();
- sleep(1);
+ rte_thread_sleep(1);
/* Round up to 10Mhz. 1E7 ~ 10Mhz */
return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);
}
@@ -106,6 +106,17 @@ int rte_thread_tls_value_set(rte_tls_key key, const void *value);
__rte_experimental
void *rte_thread_tls_value_get(rte_tls_key key);
+/**
+ * Suspend current OS thread for the specified time, yielding CPU to scheduler.
+ *
+ * @param sec
+ * Number of seconds to sleep. The system may return control later,
+ * but not earlier. Zero value always yields the CPU, but control may be
+ * returned immediately.
+ */
+__rte_experimental
+void rte_thread_sleep(unsigned int sec);
+
#ifdef __cplusplus
}
#endif
@@ -334,3 +334,5 @@ EXPORTS
rte_mem_map
rte_mem_page_size
rte_mem_unmap
+
+ rte_thread_sleep
@@ -3,10 +3,12 @@
*/
#include <errno.h>
-#include <pthread.h>
#include <stdlib.h>
#include <string.h>
+#include <pthread.h>
+#include <unistd.h>
+
#include <rte_common.h>
#include <rte_errno.h>
#include <rte_log.h>
@@ -84,3 +86,9 @@ rte_thread_tls_value_get(rte_tls_key key)
}
return pthread_getspecific(key->thread_index);
}
+
+void
+rte_thread_sleep(unsigned int sec)
+{
+ sleep(sec);
+}
@@ -412,6 +412,9 @@ EXPERIMENTAL {
rte_thread_tls_key_delete;
rte_thread_tls_value_get;
rte_thread_tls_value_set;
+
+ # added in 21.05
+ rte_thread_sleep;
};
INTERNAL {
@@ -11,9 +11,10 @@
#include <rte_per_lcore.h>
#include <rte_common.h>
#include <rte_memory.h>
-#include <eal_thread.h>
+#include <rte_thread.h>
#include "eal_private.h"
+#include "eal_thread.h"
#include "eal_windows.h"
/*
@@ -154,3 +155,9 @@ rte_thread_setname(__rte_unused pthread_t id, __rte_unused const char *name)
/* This is a stub, not the expected result */
return 0;
}
+
+void
+rte_thread_sleep(unsigned int sec)
+{
+ return Sleep(MS_PER_S * sec);
+}