Skip to content

Commit

Permalink
Enable nanosleep with _POSIX_C_SOURCE
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
devreal committed Jul 10, 2024
1 parent 7530ec7 commit 9337fd5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
17 changes: 17 additions & 0 deletions opal/class/opal_lifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
* $HEADER$
*/

/* needed for nanosleep() */
#define _POSIX_C_SOURCE 200809L

#include "opal_config.h"
#include <time.h>
#include "opal/class/opal_lifo.h"

static void opal_lifo_construct(opal_lifo_t *lifo)
Expand All @@ -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);
}
15 changes: 2 additions & 13 deletions opal/class/opal_lifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

#include "opal_config.h"
#include "opal/class/opal_list.h"
#include <time.h>

#include "opal/mca/threads/mutex.h"
#include "opal/sys/atomic.h"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions opal/mca/threads/pthreads/threads_pthreads_yield.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* $HEADER$
*/

/* needed for nanosleep() */
#define _POSIX_C_SOURCE 200809L

#include "opal_config.h"
#include <time.h>
#ifdef HAVE_SCHED_H
Expand Down

0 comments on commit 9337fd5

Please sign in to comment.