Skip to content

Commit

Permalink
fix #24
Browse files Browse the repository at this point in the history
  • Loading branch information
tmori committed Feb 11, 2024
1 parent 71b1cdc commit d81b012
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 48 deletions.
7 changes: 5 additions & 2 deletions src/hako/core/rpc/hako_internal_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void hako::core::rpc::HakoInternalRpc::stop()
}
void hako::core::rpc::HakoInternalRpc::proxy_thread()
{
//hako::utils::logger::get("core")->info("HakoInternalRpc: monitor_thread start: asset[{0}]", this->asset_id_);
HAKO_LOG_INFO("HakoInternalRpc Thread: Start");
while (true) {
asset_down(this->master_data_->get_semid(), this->asset_id_);
auto* asset = this->master_data_->get_asset_event_nolock(this->asset_id_);
Expand All @@ -52,7 +52,7 @@ void hako::core::rpc::HakoInternalRpc::proxy_thread()
hako::data::HakoAssetEventType event_id = asset->event;
this->map_[event_id]();
}
//hako::utils::logger::get("core")->info("HakoInternalRpc: monitor_thread stop: asset[{0}]", this->asset_id_);
HAKO_LOG_INFO("HakoInternalRpc Thread: End");
return;
}

Expand All @@ -73,12 +73,15 @@ void hako::core::rpc::notify(std::shared_ptr<data::HakoMasterData> master_data,
}
switch (event_id) {
case hako::data::HakoAssetEvent_Start:
HAKO_LOG_INFO("Notify Event: Start asset=%s", asset->name);
asset->callback.start();
break;
case hako::data::HakoAssetEvent_Stop:
HAKO_LOG_INFO("Notify Event: Stop asset=%s", asset->name);
asset->callback.stop();
break;
case hako::data::HakoAssetEvent_Reset:
HAKO_LOG_INFO("Notify Event: Reset asset=%s", asset->name);
asset->callback.reset();
break;
default:
Expand Down
7 changes: 7 additions & 0 deletions src/hako/data/hako_master_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,18 @@ namespace hako::data {
{
HAKO_ASSERT((this->shmp_ != nullptr) && (this->master_datap_ != nullptr));
(void)this->shmp_->lock_memory(HAKO_SHARED_MEMORY_ID_0);
this->is_master_locked = true;
}
void unlock()
{
HAKO_ASSERT((this->shmp_ != nullptr) && (this->master_datap_ != nullptr));
this->is_master_locked = false;
(void)this->shmp_->unlock_memory(HAKO_SHARED_MEMORY_ID_0);
}
bool is_locked()
{
return this->is_master_locked;
}
/*
* Time APIs
*/
Expand Down Expand Up @@ -413,6 +419,7 @@ namespace hako::data {
HakoMasterDataType *master_datap_ = nullptr;
std::shared_ptr<HakoPduData> pdu_datap_ = nullptr;
std::string shm_type_;
bool is_master_locked = false;
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/hako/data/hako_pdu_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ namespace hako::data {
bool ret = true;
if (robo_name.length() >= HAKO_FIXED_STRLEN_MAX) {
printf("ERROR: robo_name length(%ld) is over max(%d)\n", robo_name.length(), HAKO_FIXED_STRLEN_MAX);
HAKO_LOG_ERROR("ERROR: robo_name length(%ld) is over max(%d)\n", robo_name.length(), HAKO_FIXED_STRLEN_MAX);
return false;
}
this->master_shmp_->lock_memory(HAKO_SHARED_MEMORY_ID_0);
auto new_channel_id = this->pdu_meta_data_->channel_num;
if (new_channel_id >= HAKO_PDU_CHANNEL_MAX) {
printf("ERROR: pdu chanel is full\n");
HAKO_LOG_ERROR("ERROR: pdu chanel is full\n");
ret = false;
}
else if (this->pdu_meta_data_->channel[new_channel_id].size == 0) {
Expand Down
17 changes: 11 additions & 6 deletions src/hako/hako_asset_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ bool hako::HakoAssetControllerImpl::asset_register(const std::string & name, Ass
hako::core::context::HakoContext context;
auto id = this->master_data_->alloc_asset(name, hako::data::HakoAssetType::HakoAsset_Inside, &callbacks);
if (id < 0) {
//hako::utils::logger::get("core")->error("can not registered: asset[{0}]", name);
HAKO_LOG_ERROR("can not registered: asset: %s", name.c_str());
return false;
}
else {
//hako::utils::logger::get("core")->info("Registered: asset[{0}]", name);
HAKO_LOG_INFO("registered: asset: %s", name.c_str());
}
this->rpc_ = std::make_shared<hako::core::rpc::HakoInternalRpc>(id, this->master_data_);
this->rpc_->register_callback(hako::data::HakoAssetEvent_Start, callbacks.start);
Expand All @@ -25,11 +25,11 @@ bool hako::HakoAssetControllerImpl::asset_register_polling(const std::string & n
hako::core::context::HakoContext context;
auto id = this->master_data_->alloc_asset(name, hako::data::HakoAssetType::HakoAsset_Inside, nullptr);
if (id < 0) {
//hako::utils::logger::get("core")->error("can not registered: polling asset[{0}]", name);
HAKO_LOG_ERROR("can not registered: asset: %s", name.c_str());
return false;
}
else {
//hako::utils::logger::get("core")->info("Registered: polling asset[{0}]", name);
HAKO_LOG_INFO("registered: asset: %s", name.c_str());
}
return true;
}
Expand All @@ -51,10 +51,10 @@ bool hako::HakoAssetControllerImpl::asset_unregister(const std::string & name)
{
auto ret = this->master_data_->free_asset(name);
if (ret) {
//hako::utils::logger::get("core")->info("Unregistered: asset[{0}]", name);
HAKO_LOG_ERROR("can not unregistered: asset: %s", name.c_str());
}
else {
//hako::utils::logger::get("core")->error("can not unregistered: asset[{0}]", name);
HAKO_LOG_INFO("unregistered: asset: %s", name.c_str());
}
if (this->rpc_ != nullptr) {
this->rpc_->stop();
Expand Down Expand Up @@ -113,22 +113,27 @@ bool hako::HakoAssetControllerImpl::feedback(const std::string& asset_name, bool

bool hako::HakoAssetControllerImpl::start_feedback(const std::string& asset_name, bool isOk)
{
HAKO_LOG_INFO("start feedbak: asset: %s %d", asset_name.c_str(), isOk);
return this->feedback(asset_name, isOk, HakoSim_Runnable);
}
bool hako::HakoAssetControllerImpl::stop_feedback(const std::string& asset_name, bool isOk)
{
HAKO_LOG_INFO("stop feedbak: asset: %s %d", asset_name.c_str(), isOk);
return this->feedback(asset_name, isOk, HakoSim_Stopping);
}
bool hako::HakoAssetControllerImpl::reset_feedback(const std::string& asset_name, bool isOk)
{
HAKO_LOG_INFO("reset feedbak: asset: %s %d", asset_name.c_str(), isOk);
return this->feedback(asset_name, isOk, HakoSim_Resetting);
}
bool hako::HakoAssetControllerImpl::create_pdu_channel(HakoPduChannelIdType channel_id, size_t pdu_size)
{
HAKO_LOG_INFO("create pdu channel: channel_id= %d pdu_size=%ld", channel_id, pdu_size);
return this->master_data_->get_pdu_data()->create_channel(channel_id, pdu_size);
}
bool hako::HakoAssetControllerImpl::create_pdu_lchannel(const std::string& robo_name, HakoPduChannelIdType channel_id, size_t pdu_size)
{
HAKO_LOG_INFO("create pdu lchannel: robo_name=%s channel_id= %d pdu_size=%ld", robo_name.c_str(), channel_id, pdu_size);
return this->master_data_->get_pdu_data()->create_lchannel(robo_name, channel_id, pdu_size);
}
HakoPduChannelIdType hako::HakoAssetControllerImpl::get_pdu_channel(const std::string& robo_name, HakoPduChannelIdType channel_id)
Expand Down
6 changes: 5 additions & 1 deletion src/hako/hako_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ void hako::log::add(const char* level, const char* file, int line, const char* f
{
if (!master_data_ptr) {
return;
}
}
if (master_data_ptr->is_locked()) {
std::cout << "WARN: hako log is discarded because of master lock is locked...." << std::endl;
return;
}
va_list args;
va_start(args, format);
master_data_ptr->add_log_internal(level, file, line, function, format, args);
Expand Down
5 changes: 4 additions & 1 deletion src/hako/hako_simevent_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "hako_simevent_impl.hpp"
//#include "utils/hako_logger.hpp"
#include "hako_log.hpp"
#include "core/rpc/hako_internal_rpc.hpp"

HakoSimulationStateType hako::HakoSimulationEventController::state()
Expand Down Expand Up @@ -34,18 +34,21 @@ bool hako::HakoSimulationEventController::trigger_event(HakoSimulationStateType

bool hako::HakoSimulationEventController::start()
{
HAKO_LOG_INFO("Event: Start");
return this->trigger_event(HakoSim_Stopped, HakoSim_Runnable, hako::data::HakoAssetEvent_Start);
}



bool hako::HakoSimulationEventController::stop()
{
HAKO_LOG_INFO("Event: Stop");
return this->trigger_event(HakoSim_Running, HakoSim_Stopping, hako::data::HakoAssetEvent_Stop);
}

bool hako::HakoSimulationEventController::reset()
{
HAKO_LOG_INFO("Event: Reset");
bool ret = false;
auto& state = this->master_data_->ref_state_nolock();
if (state == HakoSim_Stopped) {
Expand Down
9 changes: 3 additions & 6 deletions src/hako/utils/hako_share/impl/hako_sem_flock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "utils/hako_config_loader.hpp"
//#include "utils/hako_logger.hpp"
#include "utils/hako_assert.hpp"
#include <stdlib.h>

#define HAKO_SEM_INX_MASTER 0
#define HAKO_SEM_INX_ASSETS 1

Expand Down Expand Up @@ -57,12 +59,7 @@ static void hako_sem_down(int index)
}
hako_flock_release(flock_handle);
//printf("sem_down[%d][%lld]: tmp release\n", index, pid);
#ifdef WIN32
//https://learn.microsoft.com/ja-jp/windows/win32/api/synchapi/nf-synchapi-sleep
Sleep(500);
#else
usleep(500*1000);
#endif
usleep(20*1000);
//printf("sem_down[%d][%lld]: tmp acquire\n", index, pid);
hako_flock_acquire(flock_handle);
}
Expand Down
2 changes: 2 additions & 0 deletions src/hako/utils/hako_share/impl/hako_shared_memory_factory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "utils/hako_share/hako_shared_memory_factory.hpp"
#include "utils/hako_share/hako_shared_memory_shm.hpp"
#include "utils/hako_share/hako_shared_memory_mmap.hpp"
#include <iostream>

std::shared_ptr<hako::utils::HakoSharedMemory> hako::utils::hako_shared_memory_create(const std::string& type)
{
Expand All @@ -14,6 +15,7 @@ std::shared_ptr<hako::utils::HakoSharedMemory> hako::utils::hako_shared_memory_c
#endif
else
{
std::cout << "Error: Invalid shared memory type: " << type << std::endl;
return nullptr;
}
}
1 change: 1 addition & 0 deletions src/hako/utils/hako_share/impl/posix/hako_flock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ HakoFlockObjectType* hako_flock_open(std::string &filepath)
handle->flock_obj.obj = handle;
handle->fd = open(filepath.c_str(), O_RDWR);
if (handle->fd < 0) {
printf("ERROR: can not open flock file:%s\n", filepath.c_str());
hako_flock_destroy(&handle->flock_obj);
return nullptr;
}
Expand Down
13 changes: 7 additions & 6 deletions src/hako/utils/hako_share/impl/posix/hako_sem.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "utils/hako_share/hako_sem.hpp"
#include "hako_log.hpp"
//#include "utils/hako_logger.hpp"
#define HAKO_SEM_INX_MASTER 0
#define HAKO_SEM_INX_ASSETS 1
Expand All @@ -17,7 +18,7 @@ int32_t hako::utils::sem::create(int32_t key)
{
int32_t sem_id = semget(key, (1 + HAKO_DATA_MAX_ASSET_NUM), 0666 | IPC_CREAT);
if (sem_id < 0) {
//hako::utils::logger::get("core")->error("semget() key={0} error={1}", key, errno);
HAKO_LOG_ERROR("semget() error: key=%d errno=%d", key, errno);
return -1;
}

Expand All @@ -30,7 +31,7 @@ int32_t hako::utils::sem::create(int32_t key)
argument.array = values;
int err = semctl(sem_id, 0, SETALL, argument);
if (err < 0) {
//hako::utils::logger::get("core")->error("semctl() error = {0} sem_id={1}", errno, sem_id);
HAKO_LOG_ERROR("semctl() error: key=%d errno=%d", key, errno);
hako::utils::sem::destroy(sem_id);
return -1;
}
Expand All @@ -51,7 +52,7 @@ void hako::utils::sem::asset_down(int32_t sem_id, int32_t asset_id)
sop.sem_flg = 0; // Operation flag
int32_t err = semop(sem_id, &sop, 1);
if (err < 0) {
//hako::utils::logger::get("core")->error("asset_down() error = {0} sem_id={1} inx={2}", errno, sem_id, asset_id);
HAKO_LOG_ERROR("asset_down: semop() error: sem_id=%d asset_id=%d errno=%d", sem_id, asset_id, errno);
}
return;
}
Expand All @@ -63,7 +64,7 @@ void hako::utils::sem::asset_up(int32_t sem_id, int32_t asset_id)
sop.sem_flg = 0; // Operation flag
int32_t err = semop(sem_id, &sop, 1);
if (err < 0) {
//hako::utils::logger::get("core")->error("asset_up() error = {0} sem_id={1} inx={2}", errno, sem_id, asset_id);
HAKO_LOG_ERROR("asset_up: semop() error: sem_id=%d asset_id=%d errno=%d", sem_id, asset_id, errno);
}
return;
}
Expand All @@ -75,7 +76,7 @@ void hako::utils::sem::master_lock(int32_t sem_id)
sop.sem_flg = 0; // Operation flag
int32_t err = semop(sem_id, &sop, 1);
if (err < 0) {
//hako::utils::logger::get("core")->error("master_lock() error = {0} sem_id={1} inx={2}", errno, sem_id, 0);
HAKO_LOG_ERROR("master_lock: semop() error: sem_id=%d errno=%d", sem_id, errno);
}
return;
}
Expand All @@ -87,7 +88,7 @@ void hako::utils::sem::master_unlock(int32_t sem_id)
sop.sem_flg = 0; // Operation flag
int32_t err = semop(sem_id, &sop, 1);
if (err < 0) {
//hako::utils::logger::get("core")->error("master_unlock() error = {0} sem_id={1} inx={2}", errno, sem_id, 0);
HAKO_LOG_ERROR("master_unlock: semop() error: sem_id=%d errno=%d", sem_id, errno);
}
return;
}
28 changes: 2 additions & 26 deletions src/hako/utils/hako_share/impl/posix/hako_shared_memory_shm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ int32_t hako::utils::HakoSharedMemoryShm::create_memory(int32_t key, int32_t siz

int32_t sem_id = hako::utils::sem::create(key);
if (sem_id < 0) {
printf("ERROR: hako::utils::sem::create() id=%d size=%d error=%d\n", key, size, errno);
(void)shmdt(shared_memory);
(void)shmctl (shmid, IPC_RMID, 0);
return -1;
Expand Down Expand Up @@ -52,6 +53,7 @@ void* hako::utils::HakoSharedMemoryShm::load_memory_shmid(int32_t key, int32_t s
{
void *shared_memory = shmat(shmid, 0, 0);
if (shared_memory == ((void*)-1)) {
printf("ERROR: shmat() key=%d shmid=%d error=%d\n", key, shmid, errno);
return nullptr;
}
SharedMemoryMetaDataType *metap = static_cast<SharedMemoryMetaDataType*>(shared_memory);
Expand All @@ -65,35 +67,13 @@ void* hako::utils::HakoSharedMemoryShm::load_memory_shmid(int32_t key, int32_t s

void* hako::utils::HakoSharedMemoryShm::lock_memory(int32_t key)
{
#if 1
hako::utils::sem::master_lock(this->shared_memory_map_[key].sem_id);
#else
struct sembuf sop;
sop.sem_num = 0; // Semaphore number
sop.sem_op = -1; // Semaphore operation is Lock
sop.sem_flg = 0; // Operation flag
int32_t err = semop(this->shared_memory_map_[key].sem_id, &sop, 1);
if (err < 0) {
printf("ERROR: unlock_memory() semop() error=%d key=%d\n", errno, key);
}
#endif
return &this->shared_memory_map_[key].addr->data[0];
}

void hako::utils::HakoSharedMemoryShm::unlock_memory(int32_t key)
{
#if 1
hako::utils::sem::master_unlock(this->shared_memory_map_[key].sem_id);
#else
struct sembuf sop;
sop.sem_num = 0; // Semaphore number
sop.sem_op = 1; // Semaphore operation is Lock
sop.sem_flg = 0; // Operation flag
int32_t err = semop(this->shared_memory_map_[key].sem_id, &sop, 1);
if (err < 0) {
printf("ERROR: unlock_memory() semop() error=%d key=%d\n", errno, key);
}
#endif
return;
}
int32_t hako::utils::HakoSharedMemoryShm::get_semid(int32_t key)
Expand All @@ -107,11 +87,7 @@ void hako::utils::HakoSharedMemoryShm::destroy_memory(int32_t key)
if (addr != nullptr) {
(void)shmdt(addr);
(void)shmctl (this->shared_memory_map_[key].shm_id, IPC_RMID, 0);
#if 1
hako::utils::sem::destroy(this->shared_memory_map_[key].sem_id);
#else
(void)semctl(this->shared_memory_map_[key].sem_id, 1, IPC_RMID, NULL);
#endif
this->shared_memory_map_.erase(key);
}
return;
Expand Down

0 comments on commit d81b012

Please sign in to comment.