From b6c88d52a54e5d66aa714b8b34d41e49a3a34c8c Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sat, 2 Mar 2013 02:11:48 +0100 Subject: ucoolib/arch: add yield to save CPU cycles on host --- ucoolib/arch/arch.host.cc | 13 +++++++++++++ ucoolib/arch/arch_common.arm.hh | 6 ++++++ ucoolib/arch/arch_common.hh | 8 ++++++++ 3 files changed, 27 insertions(+) (limited to 'ucoolib') diff --git a/ucoolib/arch/arch.host.cc b/ucoolib/arch/arch.host.cc index 1e24254..ed8c503 100644 --- a/ucoolib/arch/arch.host.cc +++ b/ucoolib/arch/arch.host.cc @@ -26,6 +26,8 @@ #include #include +#include +#include namespace ucoo { @@ -60,4 +62,15 @@ halt_perror () abort (); } +void +yield () +{ + // Do a small delay. + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 100 * 1000 * 1000; + while (nanosleep (&ts, &ts) == -1 && errno == EINTR) + ; +} + } // namespace ucoo diff --git a/ucoolib/arch/arch_common.arm.hh b/ucoolib/arch/arch_common.arm.hh index 4cfeebe..6fff9a9 100644 --- a/ucoolib/arch/arch_common.arm.hh +++ b/ucoolib/arch/arch_common.arm.hh @@ -48,6 +48,12 @@ irq_restore (irq_flags_t flags) : "memory", "cc"); } +inline void +yield () +{ + // Nothing, the CPU is ours! +} + } // namespace ucoo #endif // ucoolib_arch_arch_common_arm_hh diff --git a/ucoolib/arch/arch_common.hh b/ucoolib/arch/arch_common.hh index 2b5b71c..3a3b89a 100644 --- a/ucoolib/arch/arch_common.hh +++ b/ucoolib/arch/arch_common.hh @@ -32,4 +32,12 @@ # error "not implemented for this target" #endif +namespace ucoo { + +/// Give some time to other tasks when running in a tight loop. +void +yield (); + +} // namespace ucoo + #endif // ucoolib_arch_arch_common_hh -- cgit v1.2.3