|
| 1 | +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright (c) 2020 High Performance Computing Center Stuttgart, |
| 4 | + * University of Stuttgart. All rights reserved. |
| 5 | + * |
| 6 | + * $COPYRIGHT$ |
| 7 | + * |
| 8 | + * Additional copyrights may follow |
| 9 | + * |
| 10 | + * $HEADER$ |
| 11 | + */ |
| 12 | + |
| 13 | +#include "opal_config.h" |
| 14 | +#include <time.h> |
| 15 | +#ifdef HAVE_SCHED_H |
| 16 | +#include <sched.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +#include "opal/constants.h" |
| 20 | +#include "opal/mca/threads/thread.h" |
| 21 | +#include "opal/mca/threads/pthreads/threads_pthreads.h" |
| 22 | + |
| 23 | +static void opal_thread_pthreads_yield_sched_yield(void); |
| 24 | +static void opal_thread_pthreads_yield_nanosleep(void); |
| 25 | + |
| 26 | +typedef enum { |
| 27 | + OPAL_PTHREADS_YIELD_SCHED_YIELD = 0, |
| 28 | + OPAL_PTHREADS_YIELD_NANOSLEEP |
| 29 | +} opal_threads_pthreads_yield_strategy_t; |
| 30 | + |
| 31 | +static mca_base_var_enum_value_t yield_strategy_values[] = { |
| 32 | + {OPAL_PTHREADS_YIELD_SCHED_YIELD, "sched_yield"}, |
| 33 | + {OPAL_PTHREADS_YIELD_NANOSLEEP, "nanosleep"}, |
| 34 | + {0, NULL}}; |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +/* Number of nanoseconds to nanosleep, if enabled */ |
| 39 | +static uint64_t yield_nsleep_nanosecs; |
| 40 | +/* The time to nanosleep, if enabled */ |
| 41 | +static struct timespec yield_nsleep_time = {.tv_sec = 0, .tv_nsec = 1}; |
| 42 | +static opal_threads_pthreads_yield_strategy_t yield_strategy = OPAL_PTHREADS_YIELD_SCHED_YIELD; |
| 43 | + |
| 44 | +opal_threads_pthreads_yield_fn_t *opal_threads_pthreads_yield_fn = &opal_thread_pthreads_yield_sched_yield; |
| 45 | + |
| 46 | +int opal_threads_pthreads_yield_init(const mca_base_component_t *component) |
| 47 | +{ |
| 48 | + mca_base_var_enum_t *yield_strategy_enumerator; |
| 49 | + mca_base_var_enum_create("pthread_yield_strategies", yield_strategy_values, &yield_strategy_enumerator); |
| 50 | + |
| 51 | + (void) mca_base_component_var_register(component, "yield_strategy", |
| 52 | + "Pthread yield strategy to use", |
| 53 | + MCA_BASE_VAR_TYPE_INT, yield_strategy_enumerator, 0, 0, OPAL_INFO_LVL_3, |
| 54 | + MCA_BASE_VAR_SCOPE_LOCAL, &yield_strategy); |
| 55 | + switch(yield_strategy) { |
| 56 | + case OPAL_PTHREADS_YIELD_NANOSLEEP: |
| 57 | + opal_threads_pthreads_yield_fn = &opal_thread_pthreads_yield_nanosleep; |
| 58 | + break; |
| 59 | + default: |
| 60 | + /* use initial value */ |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + OBJ_RELEASE(yield_strategy_enumerator); |
| 65 | + |
| 66 | + yield_nsleep_nanosecs = (yield_nsleep_time.tv_sec * 1E9) + yield_nsleep_time.tv_nsec; |
| 67 | + (void) mca_base_component_var_register(component, "nanosleep_time", |
| 68 | + "Number of nanoseconds to sleep when using nanosleep as the pthread yield strategy", |
| 69 | + MCA_BASE_VAR_TYPE_UINT64_T, NULL, 0, 0, OPAL_INFO_LVL_3, |
| 70 | + MCA_BASE_VAR_SCOPE_LOCAL, &yield_nsleep_nanosecs); |
| 71 | + yield_nsleep_time.tv_sec = yield_nsleep_nanosecs / 1E9; |
| 72 | + yield_nsleep_time.tv_nsec = yield_nsleep_nanosecs - (uint64_t)(yield_nsleep_time.tv_sec * 1E9); |
| 73 | + |
| 74 | + return OPAL_SUCCESS; |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +void opal_thread_pthreads_yield_sched_yield(void) |
| 79 | +{ |
| 80 | +#ifdef HAVE_SCHED_H |
| 81 | + sched_yield(); |
| 82 | +#endif |
| 83 | +} |
| 84 | + |
| 85 | +void opal_thread_pthreads_yield_nanosleep(void) |
| 86 | +{ |
| 87 | + nanosleep(&yield_nsleep_time, NULL); |
| 88 | +} |
| 89 | + |
0 commit comments