Skip to content

Commit 0610fdc

Browse files
committed
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 <[email protected]>
1 parent 7faa52c commit 0610fdc

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

opal/class/opal_lifo.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
* $HEADER$
2020
*/
2121

22+
/* needed for nanosleep() */
23+
#define _POSIX_C_SOURCE 200809L
24+
2225
#include "opal_config.h"
26+
#include <time.h>
2327
#include "opal/class/opal_lifo.h"
2428

2529
static void opal_lifo_construct(opal_lifo_t *lifo)
@@ -31,3 +35,16 @@ static void opal_lifo_construct(opal_lifo_t *lifo)
3135
}
3236

3337
OBJ_CLASS_INSTANCE(opal_lifo_t, opal_object_t, opal_lifo_construct, NULL);
38+
39+
40+
void opal_lifo_release_cpu(void)
41+
{
42+
/* NTH: there are many ways to cause the current thread to be suspended. This one
43+
* should work well in most cases. Another approach would be to use poll (NULL, 0, ) but
44+
* the interval will be forced to be in ms (instead of ns or us). Note that there
45+
* is a performance improvement for the lifo test when this call is made on detection
46+
* of contention but it may not translate into actually MPI or application performance
47+
* improvements. */
48+
static struct timespec interval = {.tv_sec = 0, .tv_nsec = 100};
49+
nanosleep(&interval, NULL);
50+
}

opal/class/opal_lifo.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
#include "opal_config.h"
3232
#include "opal/class/opal_list.h"
33-
#include <time.h>
3433

3534
#include "opal/mca/threads/mutex.h"
3635
#include "opal/sys/atomic.h"
@@ -92,17 +91,7 @@ opal_read_counted_pointer(volatile opal_counted_pointer_t *volatile addr,
9291
/**
9392
* @brief Helper function for lifo/fifo to sleep this thread if excessive contention is detected
9493
*/
95-
static inline void _opal_lifo_release_cpu(void)
96-
{
97-
/* NTH: there are many ways to cause the current thread to be suspended. This one
98-
* should work well in most cases. Another approach would be to use poll (NULL, 0, ) but
99-
* the interval will be forced to be in ms (instead of ns or us). Note that there
100-
* is a performance improvement for the lifo test when this call is made on detection
101-
* of contention but it may not translate into actually MPI or application performance
102-
* improvements. */
103-
static struct timespec interval = {.tv_sec = 0, .tv_nsec = 100};
104-
nanosleep(&interval, NULL);
105-
}
94+
void opal_lifo_release_cpu(void);
10695

10796
/* Atomic Last In First Out lists. If we are in a multi-threaded environment then the
10897
* 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)
225214
if (++attempt == 5) {
226215
/* deliberately suspend this thread to allow other threads to run. this should
227216
* only occur during periods of contention on the lifo. */
228-
_opal_lifo_release_cpu();
217+
opal_lifo_release_cpu();
229218
attempt = 0;
230219
}
231220

opal/mca/threads/pthreads/threads_pthreads_yield.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* $HEADER$
1111
*/
1212

13+
/* needed for nanosleep() */
14+
#define _POSIX_C_SOURCE 200809L
15+
1316
#include "opal_config.h"
1417
#include <time.h>
1518
#ifdef HAVE_SCHED_H

0 commit comments

Comments
 (0)