From f07107a2e26dfce755caef05487a32adc4fae42a Mon Sep 17 00:00:00 2001 From: Sylvain Thery Date: Fri, 4 Mar 2011 11:21:02 +0100 Subject: [PATCH] Memoire partagee interapplication --- include/Utils/shared_mem.h | 117 +++++++++++++++++++++++++++++++++ include/Utils/shared_mem.hpp | 121 +++++++++++++++++++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 include/Utils/shared_mem.h create mode 100644 include/Utils/shared_mem.hpp diff --git a/include/Utils/shared_mem.h b/include/Utils/shared_mem.h new file mode 100644 index 000000000..6a7f3f5e8 --- /dev/null +++ b/include/Utils/shared_mem.h @@ -0,0 +1,117 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg * +* * +* This library is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by the * +* Free Software Foundation; either version 2.1 of the License, or (at your * +* option) any later version. * +* * +* This library is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this library; if not, write to the Free Software Foundation, * +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * +* * +* Web site: https://iggservis.u-strasbg.fr/CGoGN/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + +#ifndef __CGoGN_MEM_SHARED_ +#define __CGoGN_MEM_SHARED_ + + +#include +#include +#include +#include +#include + + +namespace CGoGN +{ + +namespace Utils +{ +/** +* Shared Memory Segment management +* Example: +* in master: +* SharedMem shm; +* shm.initMaster(4321); +* ... +* *(shm.lockForWrite()) = localData; +* shm.release(); +* ... +* in slave +* SharedMem shm; +* shm.initSlace(4321); +* ... +* MY_DATA localData = *(shm.dataRead()); +* ... +*/ +template +class SharedMemSeg +{ +protected: + int m_mem_id; + int* m_ptr; + DATA* m_ptr1; + DATA* m_ptr2; +public: + + /** + * Constructor + */ + SharedMemSeg(); + + /** + * Destructor + */ + ~SharedMemSeg(); + + /** + * Initialization for master + * @param key key of shared mem zone + */ + bool initMaster(int key); + + /** + * Initialization for master + * @param key key of shared mem zone + */ + bool initSlave(int key); + + /** + * read data + * @return a pointer on data (for copy or read) + */ + DATA* dataRead(); + + /** + * lock data for writing. + * @return a pointer on data (back buffer) to copy data in + */ + DATA* lockForWrite(); + + /** + * release the data ( switch ptr from back to front) + */ + void release(); + +}; + +} +} + +#include "Utils/shared_mem.hpp" + +#endif + + + diff --git a/include/Utils/shared_mem.hpp b/include/Utils/shared_mem.hpp new file mode 100644 index 000000000..8ad5a5aeb --- /dev/null +++ b/include/Utils/shared_mem.hpp @@ -0,0 +1,121 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg * +* * +* This library is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by the * +* Free Software Foundation; either version 2.1 of the License, or (at your * +* option) any later version. * +* * +* This library is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this library; if not, write to the Free Software Foundation, * +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * +* * +* Web site: https://iggservis.u-strasbg.fr/CGoGN/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + + +namespace CGoGN +{ + +namespace Utils +{ + + +template +SharedMemSeg::SharedMemSeg(): +m_ptr(NULL) +{ } + +template +SharedMemSeg::~SharedMemSeg() +{ + if (m_ptr !=NULL) + { + struct shmid_ds buf; + shmctl(m_mem_id,IPC_RMID, &buf); + shmdt(m_ptr); + } +} + +template +bool SharedMemSeg::initMaster(int key) +{ + m_mem_id = shmget(key, 2*sizeof(DATA)+2*sizeof(int), IPC_CREAT|0666); + if (m_mem_id < 0) + { + std::cerr << "Error shmget"<(shmat(m_mem_id, NULL, 0)); + if (m_ptr == (int*)-1) + { + std::cerr << "Error shmat"<(m_ptr + 2); + m_ptr2 = m_ptr1+1; + *m_ptr = 0; +} + +template +bool SharedMemSeg::initSlave(int key) +{ + m_mem_id = shmget(key, 2*sizeof(DATA)+2*sizeof(int), 0444); + if (m_mem_id < 0) + { + std::cerr <<"Shared Memory "<< key << " does not exist can not read"<< std::endl; + return false; + } + + m_ptr = reinterpret_cast(shmat(m_mem_id, NULL, 0)); + if (m_ptr == (int*)-1) + { + std::cerr <<"Problem getting shared memory ptr for "<< key << std::endl; + return false; + } + + m_ptr1 = reinterpret_cast(m_ptr + 2); + m_ptr2 = m_ptr1+1; + *(int*)m_ptr = 0; +} + + +template +DATA* SharedMemSeg::dataRead() +{ + if (*m_ptr == 0) + return m_ptr1; + return m_ptr2; +} + + +template +DATA* SharedMemSeg::lockForWrite() +{ + if (*m_ptr == 0) + return m_ptr2; + return m_ptr1; +} + +template +void SharedMemSeg::release() +{ + *m_ptr = 1 - *m_ptr; //invert id +} + + +} +} + + -- GitLab