#include <stdio.h>
#include <stdlib.h>
-#include <stdint.h>
-#include <unistd.h>
#include <pthread.h>
-#include <signal.h>
#include <sched.h>
#include <assert.h>
#include <string.h>
RTE_PER_LCORE(_lcore_id) = LCORE_ID_ANY;
}
+enum __rte_ctrl_thread_status {
+ CTRL_THREAD_LAUNCHING, /* Yet to call pthread_create function */
+ CTRL_THREAD_RUNNING, /* Control thread is running successfully */
+ CTRL_THREAD_ERROR /* Control thread encountered an error */
+};
+
struct rte_thread_ctrl_params {
void *(*start_routine)(void *);
void *arg;
- pthread_barrier_t configured;
- unsigned int refcnt;
+ int ret;
+ /* Control thread status.
+ * If the status is CTRL_THREAD_ERROR, 'ret' has the error code.
+ */
+ enum __rte_ctrl_thread_status ctrl_thread_status;
};
-static void ctrl_params_free(struct rte_thread_ctrl_params *params)
-{
- if (__atomic_sub_fetch(¶ms->refcnt, 1, __ATOMIC_ACQ_REL) == 0) {
- (void)pthread_barrier_destroy(¶ms->configured);
- free(params);
- }
-}
-
static void *ctrl_thread_init(void *arg)
{
struct internal_config *internal_conf =
eal_get_internal_configuration();
rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset;
struct rte_thread_ctrl_params *params = arg;
- void *(*start_routine)(void *);
+ void *(*start_routine)(void *) = params->start_routine;
void *routine_arg = params->arg;
__rte_thread_init(rte_lcore_id(), cpuset);
-
- pthread_barrier_wait(¶ms->configured);
- start_routine = params->start_routine;
- ctrl_params_free(params);
-
- if (start_routine == NULL)
+ params->ret = pthread_setaffinity_np(pthread_self(), sizeof(*cpuset),
+ cpuset);
+ if (params->ret != 0) {
+ __atomic_store_n(¶ms->ctrl_thread_status,
+ CTRL_THREAD_ERROR, __ATOMIC_RELEASE);
return NULL;
+ }
+
+ __atomic_store_n(¶ms->ctrl_thread_status,
+ CTRL_THREAD_RUNNING, __ATOMIC_RELEASE);
return start_routine(routine_arg);
}
const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg)
{
- struct internal_config *internal_conf =
- eal_get_internal_configuration();
- rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset;
struct rte_thread_ctrl_params *params;
+ enum __rte_ctrl_thread_status ctrl_thread_status;
int ret;
params = malloc(sizeof(*params));
params->start_routine = start_routine;
params->arg = arg;
- params->refcnt = 2;
-
- ret = pthread_barrier_init(¶ms->configured, NULL, 2);
- if (ret != 0)
- goto fail_no_barrier;
+ params->ret = 0;
+ params->ctrl_thread_status = CTRL_THREAD_LAUNCHING;
ret = pthread_create(thread, attr, ctrl_thread_init, (void *)params);
- if (ret != 0)
- goto fail_with_barrier;
+ if (ret != 0) {
+ free(params);
+ return -ret;
+ }
if (name != NULL) {
ret = rte_thread_setname(*thread, name);
"Cannot set name for ctrl thread\n");
}
- ret = pthread_setaffinity_np(*thread, sizeof(*cpuset), cpuset);
- if (ret != 0)
- params->start_routine = NULL;
-
- pthread_barrier_wait(¶ms->configured);
- ctrl_params_free(params);
+ /* Wait for the control thread to initialize successfully */
+ while ((ctrl_thread_status =
+ __atomic_load_n(¶ms->ctrl_thread_status,
+ __ATOMIC_ACQUIRE)) == CTRL_THREAD_LAUNCHING) {
+ /* Yield the CPU. Using sched_yield call requires maintaining
+ * another implementation for Windows as sched_yield is not
+ * supported on Windows.
+ */
+ rte_delay_us_sleep(1);
+ }
- if (ret != 0)
- /* start_routine has been set to NULL above; */
- /* ctrl thread will exit immediately */
+ /* Check if the control thread encountered an error */
+ if (ctrl_thread_status == CTRL_THREAD_ERROR) {
+ /* ctrl thread is exiting */
pthread_join(*thread, NULL);
+ }
- return -ret;
-
-fail_with_barrier:
- (void)pthread_barrier_destroy(¶ms->configured);
-
-fail_no_barrier:
+ ret = params->ret;
free(params);
return -ret;