O-RAN E Maintenance Release contribution for ODULOW
[o-du/phy.git] / fapi_5g / source / framework / workers / nr5g_fapi_urllc_thread.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2021 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18 #include "nr5g_fapi_std.h"
19 #include "nr5g_fapi_framework.h"
20 #include "nr5g_fapi_urllc_thread.h"
21 #include "nr5g_fapi_phy2mac_thread.h"
22 #include "nr5g_fapi_mac2phy_thread.h"
23 #include "nr5g_fapi_fapi2mac_api.h"
24 #include "nr5g_fapi_fapi2phy_api.h"
25
26 static nr5g_fapi_urllc_msg_dir_t urllc_msg_dir = NR5G_FAPI_URLLC_MSG_DIR_LAST;
27 static void *p_urllc_list_elem = NULL;
28 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
29
30 void nr5g_fapi_urllc_thread_callback(
31     nr5g_fapi_urllc_msg_dir_t msg_dir,
32     void *p_list_elem)
33 {
34     p_nr5g_fapi_phy_ctx_t p_phy_ctx = nr5g_fapi_get_nr5g_fapi_phy_ctx();
35     sem_wait(&p_phy_ctx->urllc_sem_done);
36     pthread_mutex_lock(&lock);
37     p_urllc_list_elem = p_list_elem;
38     urllc_msg_dir = msg_dir;
39     pthread_mutex_unlock(&lock);
40     sem_post(&p_phy_ctx->urllc_sem_process);
41 }
42
43 void *nr5g_fapi_urllc_thread_func(
44     void *config)
45 {
46     cpu_set_t cpuset;
47     pthread_t thread;
48     p_nr5g_fapi_phy_ctx_t p_phy_ctx = (p_nr5g_fapi_phy_ctx_t) config;
49     uint64_t start_tick;
50
51     NR5G_FAPI_LOG(INFO_LOG, ("[URLLC] Thread %s launched LWP:%ld on "
52             "Core: %d\n", __func__, pthread_self(),
53             p_phy_ctx->urllc_worker_core_id));
54
55     thread = p_phy_ctx->urllc_tid = pthread_self();
56
57     CPU_ZERO(&cpuset);
58     CPU_SET(p_phy_ctx->urllc_worker_core_id, &cpuset);
59     pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
60
61     usleep(1000);
62     while (!p_phy_ctx->process_exit) {
63         sem_wait(&p_phy_ctx->urllc_sem_process);
64         pthread_mutex_lock(&lock);
65         if (p_urllc_list_elem)
66         {
67             switch (urllc_msg_dir) {
68                 case NR5G_FAPI_URLLC_MSG_DIR_MAC2PHY:
69                     nr5g_fapi_mac2phy_api_recv_handler(true, config, (p_fapi_api_queue_elem_t) p_urllc_list_elem);
70                     start_tick = __rdtsc();
71                     NR5G_FAPI_LOG(TRACE_LOG, ("[MAC2PHY] Send to PHY urllc.."));
72                     nr5g_fapi_fapi2phy_send_api_list(true);
73                     tick_total_wls_send_per_tti_dl += __rdtsc() - start_tick;
74                     break;
75                 case NR5G_FAPI_URLLC_MSG_DIR_PHY2MAC:
76                     nr5g_fapi_phy2mac_api_recv_handler(true, config, (PMAC2PHY_QUEUE_EL) p_urllc_list_elem);
77                     nr5g_fapi_fapi2mac_send_api_list(true);
78                     break;
79                 default:
80                     NR5G_FAPI_LOG(ERROR_LOG, ("[URLLC]: Invalid URLLC message direction.\n"));
81                     break;
82             }
83
84             p_urllc_list_elem = NULL;
85             urllc_msg_dir = NR5G_FAPI_URLLC_MSG_DIR_LAST;
86         }
87         pthread_mutex_unlock(&lock);
88         sem_post(&p_phy_ctx->urllc_sem_done);
89     }
90  
91     pthread_exit(NULL);
92 }