From 119a33fc61866ad85dba650f3c104d239ff6b455 Mon Sep 17 00:00:00 2001 From: Tony Kuker Date: Sat, 28 Jan 2023 22:06:54 -0600 Subject: [PATCH] Cleanup --- cpp/shared/shared_memory.cpp | 126 ++++++++++++----------------------- cpp/shared/shared_memory.h | 99 ++++++--------------------- 2 files changed, 64 insertions(+), 161 deletions(-) diff --git a/cpp/shared/shared_memory.cpp b/cpp/shared/shared_memory.cpp index 907270e3..c949658c 100644 --- a/cpp/shared/shared_memory.cpp +++ b/cpp/shared/shared_memory.cpp @@ -1,14 +1,52 @@ +//--------------------------------------------------------------------------- +// +// SCSI Target Emulator PiSCSI for Raspberry Pi +// +// Copyright (C) 2023 akuker +// +//--------------------------------------------------------------------------- #include "shared_memory.h" #include "shared/log.h" #include #include -// template void SharedMemory::teardown(){ +SharedMemory::SharedMemory(std::string region_name) : m_valid(true), m_name(region_name) +{ + LOGINFO("%s Opening shared memory %s", __PRETTY_FUNCTION__, region_name.c_str()) + // Get shared memory + int mode = S_IRWXU | S_IRWXG; + if ((m_fd_shared_mem = shm_open(region_name.c_str(), O_RDWR | O_CREAT | O_TRUNC, mode)) == -1) { + LOGERROR("Unable to open shared memory %s. Is scsisim already running?", region_name.c_str()); + m_valid = false; + return; + } + LOGTRACE("%s Successfully created shared memory %s", __PRETTY_FUNCTION__, region_name.c_str()) + + // Extend the shared memory, since its default size is zero + if (ftruncate(m_fd_shared_mem, sizeof(lockable_data_t)) == -1) { + LOGERROR("Unable to expand shared memory"); + m_valid = false; + shm_unlink(region_name.c_str()); + return; + } + LOGINFO("%s Shared memory region expanded to %d bytes", __PRETTY_FUNCTION__, static_cast(sizeof(uint32_t))) + + m_shared_mem = + (lockable_data_t*)mmap(NULL, sizeof(lockable_data_t), PROT_READ | PROT_WRITE, MAP_SHARED, m_fd_shared_mem, 0); + if (m_shared_mem == MAP_FAILED) { + LOGERROR("Unabled to map shared memory"); + m_valid = false; + shm_unlink(region_name.c_str()); + return; + } + LOGINFO("%s Shared memory region successfully memory mapped", __PRETTY_FUNCTION__) +} + SharedMemory::~SharedMemory() { LOGTRACE("%s", __PRETTY_FUNCTION__); - if (m_mem != nullptr) { - if (munmap(m_mem, m_size) == 0) { + if (m_shared_mem != nullptr) { + if (munmap(m_shared_mem, sizeof(lockable_data_t)) == 0) { LOGTRACE("munmap successful"); } else { LOGWARN("munmap NOT successful ERROR!!!"); @@ -21,84 +59,4 @@ SharedMemory::~SharedMemory() } else { LOGWARN("shm_unlink failed!!!"); } -} - -// template SharedMemory::~SharedMemory() -// // SharedMemory::~SharedMemory() -// { -// // } - -// } -// template SharedMemory::SharedMemory(SharedMemory::RoleEnum role, std::string region_name) : -// // m_role(role), m_size(sizeof(T)) -// SharedMemory::SharedMemory(SharedMemory::RoleEnum role, std::string region_name) -// : m_role(role), m_size(sizeof(uint32_t)) -// { -// (void)role; -// (void)region_name; -// } -// template SharedMemory::SharedMemory(SharedMemory::RoleEnum role, std::string region_name) : -// m_role(role), m_size(sizeof(T)) -SharedMemory::SharedMemory(std::string region_name) : m_size(sizeof(uint32_t)), m_name(region_name) -{ - // if(m_role == RoleEnum::Controller){ - // m_mutex_sem = sem_open(SHARED_MEM_MUTEX_NAME.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, 0); - // } - // else{ - // m_mutex_sem = sem_open(SHARED_MEM_MUTEX_NAME.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, 0); - - // } - // // Create a shared memory region that can be accessed as a virtual "SCSI bus" - // // mutual exclusion semaphore, mutex_sem with an initial value 0. - // if ( == SEM_FAILED) { - // LOGERROR("Unable to open shared memory semaphore %s. Is scsisim already running?", - // SHARED_MEM_MUTEX_NAME.c_str()); - // return 0; - // } - // LOGTRACE("%s Successfully created mutex %s", __PRETTY_FUNCTION__, SHARED_MEM_MUTEX_NAME.c_str()) - - // sem_post(mutex_sem); - - LOGINFO("%s Opening shared memory %s", __PRETTY_FUNCTION__, region_name.c_str()) - // Get shared memory - int mode = S_IRWXU | S_IRWXG; - if ((m_fd_shared_mem = shm_open(region_name.c_str(), O_RDWR | O_CREAT | O_TRUNC, mode)) == -1) { - LOGERROR("Unable to open shared memory %s. Is scsisim already running?", region_name.c_str()); - sem_close(m_mutex_sem); - } - LOGTRACE("%s Successfully created shared memory %s", __PRETTY_FUNCTION__, region_name.c_str()) - - // Extend the shared memory, since its default size is zero - if (ftruncate(m_fd_shared_mem, sizeof(lockable_data_t)) == -1) { - LOGERROR("Unable to expand shared memory"); - sem_close(m_mutex_sem); - shm_unlink(region_name.c_str()); - return; - } - LOGINFO("%s Shared memory region expanded to %d bytes", __PRETTY_FUNCTION__, static_cast(sizeof(uint32_t))) - - // signals.Reset(NULL, sizeof(uint32_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd_shm, 0); - - shared_mem = - (lockable_data_t*)mmap(NULL, sizeof(lockable_data_t), PROT_READ | PROT_WRITE, MAP_SHARED, m_fd_shared_mem, 0); - if (shared_mem == MAP_FAILED) { - LOGERROR("Unabled to map shared memory"); - sem_close(m_mutex_sem); - shm_unlink(region_name.c_str()); - return; - } - - LOGINFO("%s Shared memory region successfully memory mapped", __PRETTY_FUNCTION__) - - // *signals.mem() = 0; -} - -// template SharedMemory::~SharedMemory() { - -// // template -// // shared_ptr> SharedMemory::Create(SharedMemory::Role role){ - -// // mem_ = mmap(start, size, prot, flags, fd, offset); -// // size_ = size; - -// } \ No newline at end of file +} \ No newline at end of file diff --git a/cpp/shared/shared_memory.h b/cpp/shared/shared_memory.h index fe44d3e8..f4e3350d 100644 --- a/cpp/shared/shared_memory.h +++ b/cpp/shared/shared_memory.h @@ -1,12 +1,10 @@ //--------------------------------------------------------------------------- // -// SCSI Target Emulator PiSCSI -// for Raspberry Pi +// SCSI Target Emulator PiSCSI for Raspberry Pi // // Copyright (C) 2023 akuker -// Portions of this file Copyright (C) 2018 Intel Corporation -// (MIT licensed) -// https://github.com/nodejs/node/blob/main/src/large_pages/node_large_page.h +// +// Reference: https://en.cppreference.com/w/cpp/thread/shared_mutex // //--------------------------------------------------------------------------- @@ -20,98 +18,45 @@ #include #include -// template class SharedMemory class SharedMemory { public: - enum RoleEnum { Controller, User }; - SharedMemory() = default; - // SharedMemory(RoleEnum, std::string); - SharedMemory(std::string); - - // static shared_ptr> Create(RoleEnum); - + SharedMemory(std::string); ~SharedMemory(); - // // inline bool operator==(T* rhs) const - // inline bool operator==(uint32_t* rhs) const - // { - // return mem_ == (void*)rhs; - // } - // // inline T* mem() const - // inline uint32_t* mem() const - // { - // return (uint32_t*)mem_; - // } - // // inline T read() const - // inline uint32_t read() const - // { - // return *mem(); - // } - // SharedMemory(const SharedMemory&) = delete; - // SharedMemory(SharedMemory&&) = delete; - // void operator=(const SharedMemory&) = delete; - // void operator=(const SharedMemory&&) = delete; - - // inline bool IsValid() - // { - // return ((mem_ != nullptr) && (mem_ != MAP_FAILED)); - // } - - // void teardown(); - - // Multiple threads/readers can read the counter's value at the same time. - // T get() const { - uint32_t get() const{ - std::shared_lock lock(shared_mem->mutex_); - return shared_mem->value_; - } - - // Only one thread/writer can increment/write the counter's value. - // void set(T new_val) { - void set(uint32_t new_val) { - std::unique_lock lock(shared_mem->mutex_); - shared_mem->value_ = new_val; - } - - void set_bit(int bit_num, int value) + inline uint32_t get() const { - std::unique_lock lock(shared_mem->mutex_); + std::shared_lock lock(m_shared_mem->mutex_); + return m_shared_mem->value_; + } + + inline void set(uint32_t new_val) + { + std::unique_lock lock(m_shared_mem->mutex_); + m_shared_mem->value_ = new_val; + } + + inline void set_bit(int bit_num, int value) + { + std::unique_lock lock(m_shared_mem->mutex_); if (value) { // Set bit - shared_mem->value_ |= (1 << bit_num); + m_shared_mem->value_ |= (1 << bit_num); } else { // Clear bit - shared_mem->value_ &= ~(1 << bit_num); + m_shared_mem->value_ &= ~(1 << bit_num); } } - // Only one thread/writer can reset/write the counter's value. - void reset() - { - std::unique_lock lock(shared_mem->mutex_); - shared_mem->value_ = 0; - } - private: typedef struct lockable_data { mutable std::shared_mutex mutex_; - // T value_; uint32_t value_; } lockable_data_t; - // typedef lockable_data_t struct lockable_data_struct; - // explicit SharedMemory() - // { - // } + lockable_data_t* m_shared_mem; - lockable_data_t* shared_mem; - - // SharedMemory::RoleEnum m_role; - size_t m_size = 0; - void* m_mem = nullptr; - // void* mem_; - sem_t* m_mutex_sem; + bool m_valid; int m_fd_shared_mem; std::string m_name; };