From 6e721cec84bfa9eb165cb0a0d75bac839a1e70f3 Mon Sep 17 00:00:00 2001 From: Michael Cook Date: Sat, 13 Jun 2020 10:22:22 -0400 Subject: [PATCH] Move queue_t into it own .h, .c --- cmake_targets/CMakeLists.txt | 1 + openair2/PHY_INTERFACE/phy_stub_UE.c | 63 ++++++++-------------------- openair2/PHY_INTERFACE/phy_stub_UE.h | 16 +------ openair2/PHY_INTERFACE/queue.c | 48 +++++++++++++++++++++ openair2/PHY_INTERFACE/queue.h | 45 ++++++++++++++++++++ 5 files changed, 112 insertions(+), 61 deletions(-) create mode 100644 openair2/PHY_INTERFACE/queue.c create mode 100644 openair2/PHY_INTERFACE/queue.h diff --git a/cmake_targets/CMakeLists.txt b/cmake_targets/CMakeLists.txt index eea22d129f..38b4c4e247 100644 --- a/cmake_targets/CMakeLists.txt +++ b/cmake_targets/CMakeLists.txt @@ -1805,6 +1805,7 @@ set (MAC_NR_SRC set (MAC_SRC_UE ${PHY_INTERFACE_DIR}/phy_stub_UE.c + ${PHY_INTERFACE_DIR}/queue.c ${MAC_DIR}/main_ue.c ${MAC_DIR}/ue_procedures.c ${MAC_DIR}/ra_procedures.c diff --git a/openair2/PHY_INTERFACE/phy_stub_UE.c b/openair2/PHY_INTERFACE/phy_stub_UE.c index 1955956452..309401ba20 100644 --- a/openair2/PHY_INTERFACE/phy_stub_UE.c +++ b/openair2/PHY_INTERFACE/phy_stub_UE.c @@ -55,48 +55,6 @@ int frame_sfn = 0; static int ue_sock_descriptor = -1; -void init_queue(queue_t *q) { - memset(q, 0, sizeof(*q)); - pthread_mutex_init(&q->mutex, NULL); -} - -void put_queue(queue_t *q, void *item) { - - if (pthread_mutex_lock(&q->mutex) != 0) { - LOG_E(MAC, "put_queue mutex_lock failed\n"); - return; - } - - if (q->num_items >= MAX_QUEUE_SIZE) { - LOG_E(MAC, "Queue is full in put_queue\n"); - } else { - q->items[q->write_index] = item; - q->write_index = (q->write_index + 1) % MAX_QUEUE_SIZE; - q->num_items++; - } - - pthread_mutex_unlock(&q->mutex); - -} - -void *get_queue(queue_t *q) { - - void *item = NULL; - if (pthread_mutex_lock(&q->mutex) != 0) { - LOG_E(MAC, "get_queue mutex_lock failed\n"); - return NULL; - } - - if (q->num_items > 0) { - item = q->items[q->read_index]; - q->read_index = (q->read_index + 1) % MAX_QUEUE_SIZE; - q->num_items--; - } - - pthread_mutex_unlock(&q->mutex); - return item; -} - extern nfapi_tx_request_pdu_t* tx_request_pdu[1023][10][10]; extern int timer_subframe; extern int timer_frame; @@ -971,7 +929,9 @@ int memcpy_dl_config_req(L1_rxtx_proc_t *proc, req->dl_config_request_body.dl_config_pdu_list[i]; } - put_queue(&dl_config_req_queue, p); + if (!put_queue(&dl_config_req_queue, p)) { + free(p); + } return 0; } @@ -998,7 +958,11 @@ int memcpy_ul_config_req (L1_rxtx_proc_t *proc, nfapi_pnf_p7_config_t* pnf_p7, n req->ul_config_request_body.ul_config_pdu_list[i]; } - // put_queue(&ul_config_req_queue, (void *)p); +#if 0 // TODO not using queue here yet + if (!put_queue(&ul_config_req_queue, p)) { + free(p); + } +#endif ul_config_req = p; return 0; } @@ -1021,7 +985,9 @@ int memcpy_tx_req(nfapi_pnf_p7_config_t *pnf_p7, nfapi_tx_request_t *req) { } } } - put_queue(&tx_req_pdu_queue, p); + if (!put_queue(&tx_req_pdu_queue, p)) { + free(p); + } return 0; } @@ -1058,7 +1024,12 @@ int memcpy_hi_dci0_req (L1_rxtx_proc_t *proc, // \n",req->hi_dci0_request_body.hi_dci0_pdu_list[i].pdu_type, // UE_mac_inst[Mod_id].p->hi_dci0_request_body.hi_dci0_pdu_list[i].pdu_type); } - // put_queue(&hi_dci0_req_queue, (void *)p); + +#if 0 // TODO not using queue here yet + if (!put_queue(&hi_dci0_req_queue, p)) { + free(p); + } +#endif hi_dci0_req = p; return 0; diff --git a/openair2/PHY_INTERFACE/phy_stub_UE.h b/openair2/PHY_INTERFACE/phy_stub_UE.h index 9393ae0781..488c196c83 100644 --- a/openair2/PHY_INTERFACE/phy_stub_UE.h +++ b/openair2/PHY_INTERFACE/phy_stub_UE.h @@ -18,6 +18,7 @@ //#include "openair1/PHY/LTE_TRANSPORT/defs.h" //#include "openair1/PHY/defs.h" //#include "openair1/PHY/LTE_TRANSPORT/defs.h" +#include "queue.h" // this mutex is used to set multiple UE's UL value in L2 FAPI simulator. FILL_UL_INFO_MUTEX_t fill_ul_mutex; @@ -134,26 +135,11 @@ void ue_init_standalone_socket(const char *addr, int port); // This function is used to read from standalone pnf socket call corresponding memcpy functions void *ue_standalone_pnf_task(void *context); -#define MAX_QUEUE_SIZE 512 - -typedef struct queue_t { - void *items[MAX_QUEUE_SIZE]; - size_t read_index, write_index; - size_t num_items; - pthread_mutex_t mutex; -} queue_t; - - -void init_queue(queue_t *q); -void put_queue(queue_t *q, void *item); -void *get_queue(queue_t *q); - extern queue_t dl_config_req_queue; extern queue_t tx_req_pdu_queue; extern queue_t ul_config_req_queue; extern queue_t hi_dci0_req_queue; - extern nfapi_ul_config_request_t* ul_config_req; extern nfapi_hi_dci0_request_t* hi_dci0_req; diff --git a/openair2/PHY_INTERFACE/queue.c b/openair2/PHY_INTERFACE/queue.c new file mode 100644 index 0000000000..3bd35bb356 --- /dev/null +++ b/openair2/PHY_INTERFACE/queue.c @@ -0,0 +1,48 @@ +#include "queue.h" +#include "common/utils/LOG/log.h" +#include + +void init_queue(queue_t *q) { + memset(q, 0, sizeof(*q)); + pthread_mutex_init(&q->mutex, NULL); +} + +bool put_queue(queue_t *q, void *item) { + + if (pthread_mutex_lock(&q->mutex) != 0) { + LOG_E(PHY, "put_queue mutex_lock failed\n"); + return false; + } + + bool queued; + if (q->num_items >= MAX_QUEUE_SIZE) { + LOG_E(PHY, "Queue is full in put_queue\n"); + queued = false; + } else { + q->items[q->write_index] = item; + q->write_index = (q->write_index + 1) % MAX_QUEUE_SIZE; + q->num_items++; + queued = true; + } + + pthread_mutex_unlock(&q->mutex); + return queued; +} + +void *get_queue(queue_t *q) { + + void *item = NULL; + if (pthread_mutex_lock(&q->mutex) != 0) { + LOG_E(PHY, "get_queue mutex_lock failed\n"); + return NULL; + } + + if (q->num_items > 0) { + item = q->items[q->read_index]; + q->read_index = (q->read_index + 1) % MAX_QUEUE_SIZE; + q->num_items--; + } + + pthread_mutex_unlock(&q->mutex); + return item; +} diff --git a/openair2/PHY_INTERFACE/queue.h b/openair2/PHY_INTERFACE/queue.h new file mode 100644 index 0000000000..83de583ace --- /dev/null +++ b/openair2/PHY_INTERFACE/queue.h @@ -0,0 +1,45 @@ +/* + queue_t is a basic thread-safe non-blocking fixed-size queue. + + put_queue returns false if the queue is full. + get_queue returns NULL if the queue is empty. + + Example usage: + + // Initialization: + queue_t fooq; + init_queue(&fooq); + + // Producer: + foo_t *item = new_foo(); + if (!put_queue(&fooq, item)) + delete_foo(item); + + // Consumer: + foo_t *item = get_queue(&fooq); + if (item) + { + do_something_with_foo(item); + delete_foo(item); + } +*/ + +#pragma once + +#include +#include +#include + +#define MAX_QUEUE_SIZE 512 + +typedef struct queue_t { + void *items[MAX_QUEUE_SIZE]; + size_t read_index, write_index; + size_t num_items; + pthread_mutex_t mutex; +} queue_t; + + +void init_queue(queue_t *q); +bool put_queue(queue_t *q, void *item); +void *get_queue(queue_t *q);