From e132ccd0ddfabb82641459b36910e79b14f6df88 Mon Sep 17 00:00:00 2001 From: Joseph Schuchart Date: Wed, 10 Jul 2024 18:34:22 -0400 Subject: [PATCH] Enable nanosleep with _POSIX_C_SOURCE Move calls to nanosleep out of headers and add the feature test macro _POSIX_C_SOURCE to enable its use. This should not cause any significant overheads. Signed-off-by: Joseph Schuchart --- opal/class/opal_fifo.h | 2 +- opal/class/opal_lifo.c | 17 +++++++++++++++++ opal/class/opal_lifo.h | 15 ++------------- .../threads/pthreads/threads_pthreads_yield.c | 3 +++ 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/opal/class/opal_fifo.h b/opal/class/opal_fifo.h index 3f04f18ea71..816b2fb0ecd 100644 --- a/opal/class/opal_fifo.h +++ b/opal/class/opal_fifo.h @@ -224,7 +224,7 @@ static inline opal_list_item_t *opal_fifo_pop_atomic(opal_fifo_t *fifo) if (++attempt == 5) { /* deliberately suspend this thread to allow other threads to run. this should * only occur during periods of contention on the lifo. */ - _opal_lifo_release_cpu(); + opal_lifo_release_cpu(); attempt = 0; } diff --git a/opal/class/opal_lifo.c b/opal/class/opal_lifo.c index e4bcce794d8..0af332e3e3a 100644 --- a/opal/class/opal_lifo.c +++ b/opal/class/opal_lifo.c @@ -19,7 +19,11 @@ * $HEADER$ */ +/* needed for nanosleep() */ +#define _POSIX_C_SOURCE 200809L + #include "opal_config.h" +#include #include "opal/class/opal_lifo.h" static void opal_lifo_construct(opal_lifo_t *lifo) @@ -31,3 +35,16 @@ static void opal_lifo_construct(opal_lifo_t *lifo) } OBJ_CLASS_INSTANCE(opal_lifo_t, opal_object_t, opal_lifo_construct, NULL); + + +void opal_lifo_release_cpu(void) +{ + /* NTH: there are many ways to cause the current thread to be suspended. This one + * should work well in most cases. Another approach would be to use poll (NULL, 0, ) but + * the interval will be forced to be in ms (instead of ns or us). Note that there + * is a performance improvement for the lifo test when this call is made on detection + * of contention but it may not translate into actually MPI or application performance + * improvements. */ + static struct timespec interval = {.tv_sec = 0, .tv_nsec = 100}; + nanosleep(&interval, NULL); +} diff --git a/opal/class/opal_lifo.h b/opal/class/opal_lifo.h index d659ca2a9c7..72cc531e3b8 100644 --- a/opal/class/opal_lifo.h +++ b/opal/class/opal_lifo.h @@ -30,7 +30,6 @@ #include "opal_config.h" #include "opal/class/opal_list.h" -#include #include "opal/mca/threads/mutex.h" #include "opal/sys/atomic.h" @@ -92,17 +91,7 @@ opal_read_counted_pointer(volatile opal_counted_pointer_t *volatile addr, /** * @brief Helper function for lifo/fifo to sleep this thread if excessive contention is detected */ -static inline void _opal_lifo_release_cpu(void) -{ - /* NTH: there are many ways to cause the current thread to be suspended. This one - * should work well in most cases. Another approach would be to use poll (NULL, 0, ) but - * the interval will be forced to be in ms (instead of ns or us). Note that there - * is a performance improvement for the lifo test when this call is made on detection - * of contention but it may not translate into actually MPI or application performance - * improvements. */ - static struct timespec interval = {.tv_sec = 0, .tv_nsec = 100}; - nanosleep(&interval, NULL); -} +void opal_lifo_release_cpu(void); /* Atomic Last In First Out lists. If we are in a multi-threaded environment then the * atomicity is insured via the compare-and-swap operation, if not we simply do a read @@ -225,7 +214,7 @@ static inline opal_list_item_t *opal_lifo_pop_atomic(opal_lifo_t *lifo) if (++attempt == 5) { /* deliberately suspend this thread to allow other threads to run. this should * only occur during periods of contention on the lifo. */ - _opal_lifo_release_cpu(); + opal_lifo_release_cpu(); attempt = 0; } diff --git a/opal/mca/threads/pthreads/threads_pthreads_yield.c b/opal/mca/threads/pthreads/threads_pthreads_yield.c index 3ae102e43e0..4ab5caa85ec 100644 --- a/opal/mca/threads/pthreads/threads_pthreads_yield.c +++ b/opal/mca/threads/pthreads/threads_pthreads_yield.c @@ -10,6 +10,9 @@ * $HEADER$ */ +/* needed for nanosleep() */ +#define _POSIX_C_SOURCE 200809L + #include "opal_config.h" #include #ifdef HAVE_SCHED_H