Skip to content

Commit

Permalink
Merge pull request #19 from toppers/mmap
Browse files Browse the repository at this point in the history
fix #14
  • Loading branch information
tmori authored Feb 11, 2024
2 parents e96cb76 + 422add8 commit 4b99a0d
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_library(
#hako/utils/hako_share/impl/${OS_TYPE}/hako_flock.cpp

#for sem && shared memory
hako/utils/hako_share/impl/hako_shared_memory_factory.cpp
hako/utils/hako_share/impl/posix/hako_shared_memory_shm.cpp
hako/utils/hako_share/impl/posix/hako_sem.cpp

Expand Down
22 changes: 16 additions & 6 deletions src/hako/data/hako_master_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define _HAKO_MASTER_DATA_HPP_

#include "data/hako_base_data.hpp"
#include "utils/hako_share/hako_shared_memory.hpp"
#include "utils/hako_share/hako_shared_memory_factory.hpp"
#include "utils/hako_string.hpp"
#include "utils/hako_assert.hpp"
#include "utils/hako_clock.hpp"
Expand Down Expand Up @@ -36,13 +36,22 @@ namespace hako::data {
public:
HakoMasterData()
{
this->shm_type_ = "shm";
}
virtual ~HakoMasterData()
{
}
void init()
std::string get_shm_type()
{
this->shmp_ = std::make_shared<hako::utils::HakoSharedMemory>();
return this->shm_type_;
}
void init(const std::string& type="shm")
{
if (type != "shm")
{
this->shm_type_ = type;
}
this->shmp_ = hako::utils::hako_shared_memory_create(this->shm_type_);
auto shmid = this->shmp_->create_memory(HAKO_SHARED_MEMORY_ID_0, sizeof(HakoMasterDataType));
HAKO_ASSERT(shmid >= 0);
void *datap = this->shmp_->lock_memory(HAKO_SHARED_MEMORY_ID_0);
Expand All @@ -53,7 +62,7 @@ namespace hako::data {
this->master_datap_->master_pid = context.get_pid();
}
this->shmp_->unlock_memory(HAKO_SHARED_MEMORY_ID_0);
this->pdu_datap_ = std::make_shared<HakoPduData>(&this->master_datap_->pdu_meta_data, this->shmp_);
this->pdu_datap_ = std::make_shared<HakoPduData>(&this->master_datap_->pdu_meta_data, this->shmp_, this->get_shm_type());
}
pid_t get_master_pid()
{
Expand All @@ -69,15 +78,15 @@ namespace hako::data {
if (this->shmp_ != nullptr) {
return true;
}
this->shmp_ = std::make_shared<hako::utils::HakoSharedMemory>();
this->shmp_ = hako::utils::hako_shared_memory_create(this->get_shm_type());
void *datap = this->shmp_->load_memory(HAKO_SHARED_MEMORY_ID_0, sizeof(HakoMasterDataType));
if (datap == nullptr) {
return false;
}
//printf("master load:addr=%p\n", datap);
this->master_datap_ = static_cast<HakoMasterDataType*>(datap);
HAKO_ASSERT((this->shmp_ != nullptr) && (this->master_datap_ != nullptr));
this->pdu_datap_ = std::make_shared<HakoPduData>(&this->master_datap_->pdu_meta_data, this->shmp_);
this->pdu_datap_ = std::make_shared<HakoPduData>(&this->master_datap_->pdu_meta_data, this->shmp_, this->get_shm_type());
return true;
}

Expand Down Expand Up @@ -395,6 +404,7 @@ namespace hako::data {
std::shared_ptr<hako::utils::HakoSharedMemory> shmp_;
HakoMasterDataType *master_datap_ = nullptr;
std::shared_ptr<HakoPduData> pdu_datap_ = nullptr;
std::string shm_type_;
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/hako/data/hako_pdu_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
#define _HAKO_PDU_DATA_HPP_

#include "data/hako_base_data.hpp"
#include "utils/hako_share/hako_shared_memory.hpp"
#include "utils/hako_share/hako_shared_memory_factory.hpp"
#include <string.h>
#include <iostream>
#include "hako_log.hpp"

namespace hako::data {
class HakoPduData {
public:
HakoPduData(HakoPduMetaDataType *pdu_meta_data, std::shared_ptr<hako::utils::HakoSharedMemory> master_shmp)
HakoPduData(HakoPduMetaDataType *pdu_meta_data, std::shared_ptr<hako::utils::HakoSharedMemory> master_shmp, const std::string& shm_type)
{
this->pdu_meta_data_ = pdu_meta_data;
this->pdu_ = nullptr;
this->master_shmp_ = master_shmp;
this->asset_shmp_ = std::make_shared<hako::utils::HakoSharedMemory>();
this->asset_shmp_ = hako::utils::hako_shared_memory_create(shm_type);
}
virtual ~HakoPduData()
{
Expand Down
17 changes: 6 additions & 11 deletions src/hako/utils/hako_share/hako_shared_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,17 @@ namespace hako::utils {

class HakoSharedMemory {
public:
HakoSharedMemory() {}
virtual ~HakoSharedMemory() {}

int32_t create_memory(int32_t key, int32_t size);
void* load_memory(int32_t key, int32_t size);
virtual int32_t create_memory(int32_t key, int32_t size) = 0;
virtual void* load_memory(int32_t key, int32_t size) = 0;

void* lock_memory(int32_t key);
void unlock_memory(int32_t key);
void destroy_memory(int32_t key);
virtual void* lock_memory(int32_t key) = 0;
virtual void unlock_memory(int32_t key) = 0;
virtual void destroy_memory(int32_t key) = 0;

int32_t get_shmid();
int32_t get_semid(int32_t key);
virtual int32_t get_semid(int32_t key) = 0;

private:
void* load_memory_shmid(int32_t key, int32_t shmid);
std::map<int32_t, SharedMemoryInfoType> shared_memory_map_;
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/hako/utils/hako_share/hako_shared_memory_factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _HAKO_SHARED_MEMORY_FACTORY_HPP_
#define _HAKO_SHARED_MEMORY_FACTORY_HPP_

#include "hako_shared_memory.hpp"

namespace hako::utils {
std::shared_ptr<hako::utils::HakoSharedMemory> hako_shared_memory_create(const std::string& type);
}

#endif /* _HAKO_SHARED_MEMORY_FACTORY_HPP_ */
31 changes: 31 additions & 0 deletions src/hako/utils/hako_share/hako_shared_memory_shm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _HAKO_SHARED_MEMORY_SHM_HPP_
#define _HAKO_SHARED_MEMORY_SHM_HPP_

#include "types/hako_types.hpp"
#include "utils/hako_share/hako_shared_memory.hpp"
#include <map>

namespace hako::utils {

class HakoSharedMemoryShm : public HakoSharedMemory {
public:
HakoSharedMemoryShm() {}
virtual ~HakoSharedMemoryShm() {}

virtual int32_t create_memory(int32_t key, int32_t size) override;
virtual void* load_memory(int32_t key, int32_t size) override;

virtual void* lock_memory(int32_t key) override;
virtual void unlock_memory(int32_t key) override;
virtual void destroy_memory(int32_t key) override;

virtual int32_t get_semid(int32_t key) override;

private:
void* load_memory_shmid(int32_t key, int32_t shmid);
std::map<int32_t, SharedMemoryInfoType> shared_memory_map_;
};
}


#endif /* _HAKO_SHARED_MEMORY_SHM_HPP_ */
13 changes: 13 additions & 0 deletions src/hako/utils/hako_share/impl/hako_shared_memory_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "utils/hako_share/hako_shared_memory_factory.hpp"
#include "utils/hako_share/hako_shared_memory_shm.hpp"

std::shared_ptr<hako::utils::HakoSharedMemory> hako::utils::hako_shared_memory_create(const std::string& type)
{
if (type == "shm") {
return std::make_shared<hako::utils::HakoSharedMemoryShm>();
}
else
{
return nullptr;
}
}
16 changes: 8 additions & 8 deletions src/hako/utils/hako_share/impl/posix/hako_shared_memory_shm.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "types/hako_types.hpp"

#include "utils/hako_share/hako_shared_memory.hpp"
#include "utils/hako_share/hako_shared_memory_shm.hpp"
#include "utils/hako_share/hako_sem.hpp"

int32_t hako::utils::HakoSharedMemory::create_memory(int32_t key, int32_t size)
int32_t hako::utils::HakoSharedMemoryShm::create_memory(int32_t key, int32_t size)
{
int32_t total_size = size + sizeof(SharedMemoryMetaDataType);
int32_t shmid = shmget(key, total_size, IPC_CREAT | S_IRUSR | S_IWUSR);
Expand Down Expand Up @@ -37,7 +37,7 @@ int32_t hako::utils::HakoSharedMemory::create_memory(int32_t key, int32_t size)
this->shared_memory_map_.insert(std::make_pair(key, info));
return shmid;
}
void* hako::utils::HakoSharedMemory::load_memory(int32_t key, int32_t size)
void* hako::utils::HakoSharedMemoryShm::load_memory(int32_t key, int32_t size)
{
int32_t total_size = size + sizeof(SharedMemoryMetaDataType);
int32_t shmid = shmget(key, total_size, S_IRUSR | S_IWUSR);
Expand All @@ -48,7 +48,7 @@ void* hako::utils::HakoSharedMemory::load_memory(int32_t key, int32_t size)
return this->load_memory_shmid(key, shmid);
}

void* hako::utils::HakoSharedMemory::load_memory_shmid(int32_t key, int32_t shmid)
void* hako::utils::HakoSharedMemoryShm::load_memory_shmid(int32_t key, int32_t shmid)
{
void *shared_memory = shmat(shmid, 0, 0);
if (shared_memory == ((void*)-1)) {
Expand All @@ -63,7 +63,7 @@ void* hako::utils::HakoSharedMemory::load_memory_shmid(int32_t key, int32_t shmi
return &this->shared_memory_map_[key].addr->data[0];
}

void* hako::utils::HakoSharedMemory::lock_memory(int32_t key)
void* hako::utils::HakoSharedMemoryShm::lock_memory(int32_t key)
{
#if 1
hako::utils::sem::master_lock(this->shared_memory_map_[key].sem_id);
Expand All @@ -80,7 +80,7 @@ void* hako::utils::HakoSharedMemory::lock_memory(int32_t key)
return &this->shared_memory_map_[key].addr->data[0];
}

void hako::utils::HakoSharedMemory::unlock_memory(int32_t key)
void hako::utils::HakoSharedMemoryShm::unlock_memory(int32_t key)
{
#if 1
hako::utils::sem::master_unlock(this->shared_memory_map_[key].sem_id);
Expand All @@ -96,12 +96,12 @@ void hako::utils::HakoSharedMemory::unlock_memory(int32_t key)
#endif
return;
}
int32_t hako::utils::HakoSharedMemory::get_semid(int32_t key)
int32_t hako::utils::HakoSharedMemoryShm::get_semid(int32_t key)
{
return this->shared_memory_map_[key].sem_id;
}

void hako::utils::HakoSharedMemory::destroy_memory(int32_t key)
void hako::utils::HakoSharedMemoryShm::destroy_memory(int32_t key)
{
void *addr = this->shared_memory_map_[key].addr;
if (addr != nullptr) {
Expand Down
4 changes: 2 additions & 2 deletions test/hako/hako_shared_memory_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <gtest/gtest.h>
#include "utils/hako_share/hako_shared_memory.hpp"
#include "utils/hako_share/hako_shared_memory_factory.hpp"

class HakoSharedMemoryTest : public ::testing::Test {
protected:
Expand All @@ -20,7 +20,7 @@ class HakoSharedMemoryTest : public ::testing::Test {

TEST_F(HakoSharedMemoryTest, HakoSharedMemory_01)
{
std::shared_ptr<hako::utils::HakoSharedMemory> shm = std::make_shared<hako::utils::HakoSharedMemory>();
std::shared_ptr<hako::utils::HakoSharedMemory> shm = hako::utils::hako_shared_memory_create("shm");
EXPECT_TRUE(shm.get() != nullptr);

int32_t shmid = shm->create_memory(HAKO_SHARED_MEMORY_ID_0, 1024);
Expand Down
4 changes: 2 additions & 2 deletions test/hako/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include <gtest/gtest.h>

#include "hako.hpp"
#include "utils/hako_share/hako_shared_memory.hpp"
#include "utils/hako_share/hako_shared_memory_factory.hpp"
#include <unistd.h>

void do_shared_memory_multi_proc_test()
{
std::shared_ptr<hako::utils::HakoSharedMemory> shm = std::make_shared<hako::utils::HakoSharedMemory>();
std::shared_ptr<hako::utils::HakoSharedMemory> shm = hako::utils::hako_shared_memory_create("shm");
int32_t shmid = shm->create_memory(HAKO_SHARED_MEMORY_ID_0, 1024);
EXPECT_TRUE(shmid > 0);

Expand Down

0 comments on commit 4b99a0d

Please sign in to comment.