test/mem: disable ASan when accessing unallocated memory
[dpdk.git] / lib / eal / common / eal_common_thread.c
index 1a52f42..962b7e9 100644 (file)
@@ -4,14 +4,12 @@
 
 #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>
 
+#include <rte_eal_trace.h>
 #include <rte_errno.h>
 #include <rte_lcore.h>
 #include <rte_log.h>
@@ -166,38 +164,104 @@ __rte_thread_uninit(void)
        RTE_PER_LCORE(_lcore_id) = LCORE_ID_ANY;
 }
 
+/* main loop of threads */
+__rte_noreturn void *
+eal_thread_loop(void *arg)
+{
+       unsigned int lcore_id = (uintptr_t)arg;
+       char cpuset[RTE_CPU_AFFINITY_STR_LEN];
+       int ret;
+
+       __rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
+
+       ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
+       RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
+               lcore_id, (uintptr_t)pthread_self(), cpuset,
+               ret == 0 ? "" : "...");
+
+       rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
+
+       /* read on our pipe to get commands */
+       while (1) {
+               lcore_function_t *f;
+               void *fct_arg;
+
+               eal_thread_wait_command();
+
+               /* Set the state to 'RUNNING'. Use release order
+                * since 'state' variable is used as the guard variable.
+                */
+               __atomic_store_n(&lcore_config[lcore_id].state, RUNNING,
+                       __ATOMIC_RELEASE);
+
+               eal_thread_ack_command();
+
+               /* Load 'f' with acquire order to ensure that
+                * the memory operations from the main thread
+                * are accessed only after update to 'f' is visible.
+                * Wait till the update to 'f' is visible to the worker.
+                */
+               while ((f = __atomic_load_n(&lcore_config[lcore_id].f,
+                               __ATOMIC_ACQUIRE)) == NULL)
+                       rte_pause();
+
+               /* call the function and store the return value */
+               fct_arg = lcore_config[lcore_id].arg;
+               ret = f(fct_arg);
+               lcore_config[lcore_id].ret = ret;
+               lcore_config[lcore_id].f = NULL;
+               lcore_config[lcore_id].arg = NULL;
+
+               /* Store the state with release order to ensure that
+                * the memory operations from the worker thread
+                * are completed before the state is updated.
+                * Use 'state' as the guard variable.
+                */
+               __atomic_store_n(&lcore_config[lcore_id].state, WAIT,
+                       __ATOMIC_RELEASE);
+       }
+
+       /* never reached */
+       /* pthread_exit(NULL); */
+       /* return NULL; */
+}
+
+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(&params->refcnt, 1, __ATOMIC_ACQ_REL) == 0) {
-               (void)pthread_barrier_destroy(&params->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(&params->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(&params->ctrl_thread_status,
+                       CTRL_THREAD_ERROR, __ATOMIC_RELEASE);
                return NULL;
+       }
+
+       __atomic_store_n(&params->ctrl_thread_status,
+               CTRL_THREAD_RUNNING, __ATOMIC_RELEASE);
 
        return start_routine(routine_arg);
 }
@@ -207,10 +271,8 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name,
                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));
@@ -219,15 +281,14 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name,
 
        params->start_routine = start_routine;
        params->arg = arg;
-       params->refcnt = 2;
-
-       ret = pthread_barrier_init(&params->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);
@@ -236,24 +297,24 @@ rte_ctrl_thread_create(pthread_t *thread, const char *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(&params->configured);
-       ctrl_params_free(params);
+       /* Wait for the control thread to initialize successfully */
+       while ((ctrl_thread_status =
+                       __atomic_load_n(&params->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(&params->configured);
-
-fail_no_barrier:
+       ret = params->ret;
        free(params);
 
        return -ret;