From 5d87aece0f3e677b0f77fb9aa3928ee15aad67fe Mon Sep 17 00:00:00 2001 From: Xavier Arteaga Date: Tue, 11 Jun 2019 00:55:39 +0200 Subject: [PATCH] SRSUE: mac.h/.cc mutexes changed to C++11 and set initial values --- srsue/hdr/stack/mac/mac.h | 25 +++++++++++++----------- srsue/src/stack/mac/mac.cc | 39 +++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/srsue/hdr/stack/mac/mac.h b/srsue/hdr/stack/mac/mac.h index 925120416..33fa702a9 100644 --- a/srsue/hdr/stack/mac/mac.h +++ b/srsue/hdr/stack/mac/mac.h @@ -37,6 +37,8 @@ #include "srslte/common/tti_sync_cv.h" #include "srslte/interfaces/ue_interfaces.h" #include "ul_harq.h" +#include +#include namespace srsue { @@ -168,24 +170,25 @@ private: srslte::timers timers; // pointer to MAC PCAP object - srslte::mac_pcap* pcap; - bool is_first_ul_grant; + srslte::mac_pcap* pcap = nullptr; + bool is_first_ul_grant = false; - mac_metrics_t metrics[SRSLTE_MAX_CARRIERS]; + mac_metrics_t metrics[SRSLTE_MAX_CARRIERS] = {}; /* Class to process MAC PDUs from DEMUX unit */ class pdu_process : public thread { - public: - pdu_process(demux *demux_unit); + public: + explicit pdu_process(demux* demux_unit); + ~pdu_process(); void notify(); void stop(); private: - void run_thread(); - bool running; - bool have_data; - pthread_mutex_t mutex; - pthread_cond_t cvar; - demux* demux_unit; + void run_thread() final; + bool running = false; + bool have_data = false; + std::mutex mutex; + std::condition_variable cvar; + demux* demux_unit = nullptr; }; pdu_process pdu_process_thread; }; diff --git a/srsue/src/stack/mac/mac.cc b/srsue/src/stack/mac/mac.cc index 9675c0991..2b84a2e22 100644 --- a/srsue/src/stack/mac/mac.cc +++ b/srsue/src/stack/mac/mac.cc @@ -651,44 +651,45 @@ uint32_t mac::timer_get_unique_id() * DEMUX unit * *******************************************************/ -mac::pdu_process::pdu_process(demux* demux_unit_) : thread("MAC_PDU_PROCESS") +mac::pdu_process::pdu_process(demux* demux_unit_) : thread("MAC_PDU_PROCESS"), demux_unit(demux_unit_) { - demux_unit = demux_unit_; - pthread_mutex_init(&mutex, NULL); - pthread_cond_init(&cvar, NULL); - have_data = false; - start(MAC_PDU_THREAD_PRIO); + running = true; + start(MAC_PDU_THREAD_PRIO); +} + +mac::pdu_process::~pdu_process() +{ + if (running) { + stop(); + } } void mac::pdu_process::stop() { - pthread_mutex_lock(&mutex); - running = false; - pthread_cond_signal(&cvar); - pthread_mutex_unlock(&mutex); - + mutex.lock(); + running = false; + cvar.notify_all(); + mutex.unlock(); + wait_thread_finish(); } void mac::pdu_process::notify() { - pthread_mutex_lock(&mutex); - have_data = true; - pthread_cond_signal(&cvar); - pthread_mutex_unlock(&mutex); + std::unique_lock ul(mutex); + have_data = true; + cvar.notify_all(); } void mac::pdu_process::run_thread() { - running = true; while(running) { have_data = demux_unit->process_pdus(); if (!have_data) { - pthread_mutex_lock(&mutex); + std::unique_lock ul(mutex); while(!have_data && running) { - pthread_cond_wait(&cvar, &mutex); + cvar.wait(ul); } - pthread_mutex_unlock(&mutex); } } }