Changeset 149 in flair-src for trunk/lib/FlairCore/src
- Timestamp:
- Mar 1, 2017, 2:36:10 PM (8 years ago)
- Location:
- trunk/lib/FlairCore/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/FlairCore/src/SharedMem.cpp
r15 r149 24 24 namespace core { 25 25 26 SharedMem::SharedMem(const Object *parent, string name, size_t size )27 : Object(parent, name, "shmem") {28 pimpl_ = new SharedMem_impl(this, name, size );26 SharedMem::SharedMem(const Object *parent, string name, size_t size, Type type) 27 : Object(parent, name, "shmem"), type(type) { 28 pimpl_ = new SharedMem_impl(this, name, size, type); 29 29 } 30 30 … … 35 35 } 36 36 37 void SharedMem::Read(char *buf, size_t size) const { pimpl_->Read(buf, size); } 37 void SharedMem::Read(char *buf, size_t size) const { 38 pimpl_->Read(buf, size); 39 } 40 41 void SharedMem::ReaderReady() { 42 if (type==Type::producerConsumer) pimpl_->ReaderReady(); 43 else Warn("Function called for a non producerConsumer\n"); 44 } 38 45 39 46 } // end namespace core -
trunk/lib/FlairCore/src/SharedMem.h
r15 r149 32 32 class SharedMem : public Object { 33 33 public: 34 enum class Type { mutex, producerConsumer }; 34 35 /*! 35 36 * \brief Constructor … … 40 41 * \param name name 41 42 * \param size size of the shared memory 43 * \param blockOnRead if true reading will block if nothing written 42 44 */ 43 SharedMem(const Object *parent, std::string name, size_t size );45 SharedMem(const Object *parent, std::string name, size_t size, Type type=Type::mutex); 44 46 45 47 /*! … … 65 67 void Read(char *buf, size_t size) const; 66 68 69 70 /*! 71 * \brief This function should be called when reader starts (in case of a SharedMem of type producerConsumer) 72 */ 73 void ReaderReady(); 67 74 private: 68 75 SharedMem_impl *pimpl_; 76 Type type; 69 77 }; 70 78 -
trunk/lib/FlairCore/src/SharedMem_impl.cpp
r133 r149 27 27 28 28 SharedMem_impl::SharedMem_impl(const SharedMem *self, string name, 29 size_t size ) {29 size_t size, SharedMem::Type &type):type(type) { 30 30 this->size = size; 31 31 this->self = self; … … 72 72 ftruncate(fd, size); 73 73 74 sem_name = "/" + name; 75 sem = sem_open(sem_name.c_str(), O_CREAT, 0666, 1); 76 if (sem == SEM_FAILED) { 77 self->Err("Error creating semaphore\n"); 74 if (type==SharedMem::Type::mutex) { 75 sem_name="/" + name + "_mutex"; 76 sem=sem_open(sem_name.c_str(), O_CREAT, 0666, 1); 77 if (sem==SEM_FAILED) { 78 self->Err("Error creating mutex semaphore\n"); 79 } 80 } else if (type==SharedMem::Type::producerConsumer) { 81 sem_name_producer="/" + name + "_producer"; 82 sem_producer=sem_open(sem_name_producer.c_str(), O_CREAT, 0666, 0); 83 if (sem==SEM_FAILED) { 84 self->Err("Error creating producer semaphore\n"); 85 } 86 Printf("semaphore %s opened\n",sem_name_producer.c_str()); 87 sem_name_consumer="/" + name + "_consumer"; 88 sem_consumer=sem_open(sem_name_consumer.c_str(), O_CREAT, 0666, 0); 89 if (sem==SEM_FAILED) { 90 self->Err("Error creating consumer semaphore\n"); 91 } 92 Printf("semaphore %s opened\n",sem_name_consumer.c_str()); 78 93 } 79 94 … … 123 138 } 124 139 125 // do not check erro s as it can be done by another process140 // do not check errors as it can be done by another process 126 141 status = shm_unlink(shm_name.c_str()); /* 127 142 if(status!=0) … … 130 145 } 131 146 */ 132 // do not check erros as it can be done by another process 133 status = sem_unlink(sem_name.c_str()); /* 134 if(status!=0) 135 { 136 self->Err("Failed to unlink semaphore (%s)\n",strerror_r(-status, errorMsg, sizeof(errorMsg))); 137 }*/ 138 139 status = sem_close(sem); 140 if (status != 0) { 141 self->Err("Failed to close semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg))); 142 } 147 // do not check errors as it can be done by another process 148 if (type==SharedMem::Type::mutex) { 149 status = sem_unlink(sem_name.c_str()); 150 status = sem_close(sem); 151 if (status != 0) { 152 self->Err("Failed to close mutex semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg))); 153 } 154 } else if (type==SharedMem::Type::producerConsumer) { 155 status = sem_unlink(sem_name_producer.c_str()); 156 status = sem_close(sem_producer); 157 if (status != 0) { 158 self->Err("Failed to close producer semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg))); 159 } 160 status = sem_unlink(sem_name_consumer.c_str()); 161 status = sem_close(sem_consumer); 162 if (status != 0) { 163 self->Err("Failed to close consumer semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg))); 164 } 165 } 166 #endif 167 } 168 169 void SharedMem_impl::ReaderReady() { 170 #ifdef __XENO__ 171 //TODO 172 #else 173 sem_post(sem_consumer); 143 174 #endif 144 175 } … … 158 189 } 159 190 #else 160 sem_wait(sem); 161 memcpy(mem_segment, buf, size); 162 sem_post(sem); 191 if (type==SharedMem::Type::mutex) { 192 sem_wait(sem); 193 memcpy(mem_segment, buf, size); 194 sem_post(sem); 195 } else if (type==SharedMem::Type::producerConsumer) { 196 //wait until consumer took the data away before writting a new one 197 //but should not block if nobody's there to read 198 if (sem_trywait(sem_consumer)==0) { 199 memcpy(mem_segment, buf, size); 200 sem_post(sem_producer); 201 } 202 } 163 203 #endif 164 204 } … … 178 218 } 179 219 #else 180 sem_wait(sem); 181 memcpy(buf, mem_segment, size); 182 sem_post(sem); 183 #endif 184 } 220 if (type==SharedMem::Type::mutex) { 221 sem_wait(sem); 222 memcpy(buf, mem_segment, size); 223 sem_post(sem); 224 } else if (type==SharedMem::Type::producerConsumer) { 225 sem_wait(sem_producer); //wait until some data is available 226 memcpy(buf, mem_segment, size); 227 sem_post(sem_consumer); 228 } 229 #endif 230 } -
trunk/lib/FlairCore/src/unexported/SharedMem_impl.h
r15 r149 23 23 #endif 24 24 25 namespace flair { 26 namespace core { 27 class SharedMem; 28 } 29 } 25 #include <SharedMem.h> 30 26 31 27 /*! \class SharedMem_impl … … 37 33 public: 38 34 SharedMem_impl(const flair::core::SharedMem *self, std::string name, 39 size_t size );35 size_t size, flair::core::SharedMem::Type &type); 40 36 ~SharedMem_impl(); 41 37 42 38 void Write(const char *buf, size_t size); 43 39 void Read(char *buf, size_t size); 40 void ReaderReady(); 44 41 45 42 private: 46 43 const flair::core::SharedMem *self; 44 flair::core::SharedMem::Type type; 47 45 size_t size; 48 46 char *mem_segment; … … 54 52 #else 55 53 int fd; 56 sem_t *sem ;54 sem_t *sem,*sem_producer,*sem_consumer; 57 55 std::string sem_name, shm_name; 56 std::string sem_name_producer,sem_name_consumer; 58 57 #endif 59 58 };
Note:
See TracChangeset
for help on using the changeset viewer.