This commit is contained in:
Tony Kuker 2023-01-28 22:06:54 -06:00
parent 4ec6424446
commit 119a33fc61
2 changed files with 64 additions and 161 deletions

View File

@ -1,14 +1,52 @@
//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI for Raspberry Pi
//
// Copyright (C) 2023 akuker
//
//---------------------------------------------------------------------------
#include "shared_memory.h" #include "shared_memory.h"
#include "shared/log.h" #include "shared/log.h"
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
// template <class T> void SharedMemory<T>::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<int>(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() SharedMemory::~SharedMemory()
{ {
LOGTRACE("%s", __PRETTY_FUNCTION__); LOGTRACE("%s", __PRETTY_FUNCTION__);
if (m_mem != nullptr) { if (m_shared_mem != nullptr) {
if (munmap(m_mem, m_size) == 0) { if (munmap(m_shared_mem, sizeof(lockable_data_t)) == 0) {
LOGTRACE("munmap successful"); LOGTRACE("munmap successful");
} else { } else {
LOGWARN("munmap NOT successful ERROR!!!"); LOGWARN("munmap NOT successful ERROR!!!");
@ -21,84 +59,4 @@ SharedMemory::~SharedMemory()
} else { } else {
LOGWARN("shm_unlink failed!!!"); LOGWARN("shm_unlink failed!!!");
} }
} }
// template <class T> SharedMemory<T>::~SharedMemory()
// // SharedMemory::~SharedMemory()
// {
// // }
// }
// template <class T> SharedMemory<T>::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 <class T> SharedMemory<T>::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<int>(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 <class T > SharedMemory<T>::~SharedMemory() {
// // template <typename T>
// // shared_ptr<SharedMemory<T>> SharedMemory::Create(SharedMemory::Role role){
// // mem_ = mmap(start, size, prot, flags, fd, offset);
// // size_ = size;
// }

View File

@ -1,12 +1,10 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// //
// SCSI Target Emulator PiSCSI // SCSI Target Emulator PiSCSI for Raspberry Pi
// for Raspberry Pi
// //
// Copyright (C) 2023 akuker // Copyright (C) 2023 akuker
// Portions of this file Copyright (C) 2018 Intel Corporation //
// (MIT licensed) // Reference: https://en.cppreference.com/w/cpp/thread/shared_mutex
// https://github.com/nodejs/node/blob/main/src/large_pages/node_large_page.h
// //
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -20,98 +18,45 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <thread> #include <thread>
// template <class T> class SharedMemory
class SharedMemory class SharedMemory
{ {
public: public:
enum RoleEnum { Controller, User }; SharedMemory(std::string);
SharedMemory() = default;
// SharedMemory(RoleEnum, std::string);
SharedMemory(std::string);
// static shared_ptr<SharedMemory<T>> Create(RoleEnum);
~SharedMemory(); ~SharedMemory();
// // inline bool operator==(T* rhs) const inline uint32_t get() 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)
{ {
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) { if (value) {
// Set bit // Set bit
shared_mem->value_ |= (1 << bit_num); m_shared_mem->value_ |= (1 << bit_num);
} else { } else {
// Clear bit // 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: private:
typedef struct lockable_data { typedef struct lockable_data {
mutable std::shared_mutex mutex_; mutable std::shared_mutex mutex_;
// T value_;
uint32_t value_; uint32_t value_;
} lockable_data_t; } lockable_data_t;
// typedef lockable_data_t struct lockable_data_struct;
// explicit SharedMemory() lockable_data_t* m_shared_mem;
// {
// }
lockable_data_t* shared_mem; bool m_valid;
// SharedMemory::RoleEnum m_role;
size_t m_size = 0;
void* m_mem = nullptr;
// void* mem_;
sem_t* m_mutex_sem;
int m_fd_shared_mem; int m_fd_shared_mem;
std::string m_name; std::string m_name;
}; };