Changeset 15 in flair-src for trunk/lib/FlairCore/src/SharedMem_impl.cpp
- Timestamp:
- 04/08/16 15:40:57 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/FlairCore/src/SharedMem_impl.cpp
r2 r15 26 26 using namespace flair::core; 27 27 28 SharedMem_impl::SharedMem_impl(const SharedMem * self,string name,size_t size)29 {30 this->size=size;31 this->self=self;28 SharedMem_impl::SharedMem_impl(const SharedMem *self, string name, 29 size_t size) { 30 this->size = size; 31 this->self = self; 32 32 33 33 #ifdef __XENO__ 34 heap_binded=false; 35 int status=rt_heap_create(&heap,name.c_str(),size,H_SHARED|H_FIFO|H_NONCACHED); 36 if(status==-EEXIST) 37 { 38 heap_binded=true; 39 status=rt_heap_bind(&heap,name.c_str(),TM_INFINITE); 40 } 41 if(status!=0) 42 { 43 self->Err("rt_heap_create error (%s)\n",strerror(-status)); 44 return; 45 } 34 heap_binded = false; 35 int status = rt_heap_create(&heap, name.c_str(), size, 36 H_SHARED | H_FIFO | H_NONCACHED); 37 if (status == -EEXIST) { 38 heap_binded = true; 39 status = rt_heap_bind(&heap, name.c_str(), TM_INFINITE); 40 } 41 if (status != 0) { 42 self->Err("rt_heap_create error (%s)\n", strerror(-status)); 43 return; 44 } 46 45 47 void *ptr; 48 status=rt_heap_alloc(&heap,0,TM_NONBLOCK ,&ptr); 49 if(status!=0) 50 { 51 self->Err("rt_heap_alloc error (%s)\n",strerror(-status)); 52 } 53 mem_segment =(char*)ptr; 46 void *ptr; 47 status = rt_heap_alloc(&heap, 0, TM_NONBLOCK, &ptr); 48 if (status != 0) { 49 self->Err("rt_heap_alloc error (%s)\n", strerror(-status)); 50 } 51 mem_segment = (char *)ptr; 54 52 55 mutex_binded=false; 56 string mutex_name="mutex_"+ name; 57 status=rt_mutex_create(&mutex,mutex_name.c_str()); 58 if(status==-EEXIST) 59 { 60 mutex_binded=true; 61 status=rt_mutex_bind(&mutex,mutex_name.c_str(),TM_INFINITE); 62 } 63 if(status!=0) 64 { 65 self->Err("rt_mutex_create error (%s)\n",strerror(-status)); 66 return; 67 } 53 mutex_binded = false; 54 string mutex_name = "mutex_" + name; 55 status = rt_mutex_create(&mutex, mutex_name.c_str()); 56 if (status == -EEXIST) { 57 mutex_binded = true; 58 status = rt_mutex_bind(&mutex, mutex_name.c_str(), TM_INFINITE); 59 } 60 if (status != 0) { 61 self->Err("rt_mutex_create error (%s)\n", strerror(-status)); 62 return; 63 } 68 64 69 65 #else 70 shm_name="/" + name; 71 fd = shm_open(shm_name.c_str(), O_RDWR | O_CREAT, 0666); 72 if (fd == -1) 73 { 74 self->Err("Error creating shared memory\n"); 75 } 76 ftruncate(fd, size); 66 shm_name = "/" + name; 67 fd = shm_open(shm_name.c_str(), O_RDWR | O_CREAT, 0666); 68 if (fd == -1) { 69 self->Err("Error creating shared memory\n"); 70 } 71 ftruncate(fd, size); 77 72 78 sem_name="/" +name; 79 sem = sem_open(sem_name.c_str(), O_CREAT, 0666, 1); 80 if (sem == SEM_FAILED) 81 { 82 self->Err("Error creating semaphore\n"); 83 } 73 sem_name = "/" + name; 74 sem = sem_open(sem_name.c_str(), O_CREAT, 0666, 1); 75 if (sem == SEM_FAILED) { 76 self->Err("Error creating semaphore\n"); 77 } 84 78 85 mem_segment =(char*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);86 if (mem_segment == MAP_FAILED)87 88 89 79 mem_segment = 80 (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 81 if (mem_segment == MAP_FAILED) { 82 self->Err("Failed to map memory\n"); 83 } 90 84 #endif 91 85 } 92 86 93 SharedMem_impl::~SharedMem_impl() 94 { 95 int status; 87 SharedMem_impl::~SharedMem_impl() { 88 int status; 96 89 #ifdef __XENO__ 97 /* unnecessary because heap is opened in H_SINGLE mode 98 status=rt_heap_free(&heap,mem_segment); 99 if(status!=0) 100 { 101 self->Err("rt_heap_free error (%s)\n",strerror(-status)); 90 /* unnecessary because heap is opened in H_SINGLE mode 91 status=rt_heap_free(&heap,mem_segment); 92 if(status!=0) 93 { 94 self->Err("rt_heap_free error (%s)\n",strerror(-status)); 95 } 96 */ 97 98 if (heap_binded == false) { 99 status = rt_heap_delete(&heap); 100 if (status != 0) { 101 self->Err("rt_heap_delete error (%s)\n", strerror(-status)); 102 102 } 103 */103 } 104 104 105 if(heap_binded==false) 106 { 107 status=rt_heap_delete(&heap); 108 if(status!=0) 109 { 110 self->Err("rt_heap_delete error (%s)\n",strerror(-status)); 111 } 105 if (mutex_binded == false) { 106 status = rt_mutex_delete(&mutex); 107 if (status != 0) { 108 self->Err("error destroying mutex (%s)\n", strerror(-status)); 112 109 } 110 } 111 #else 112 status = munmap(mem_segment, size); 113 if (status != 0) { 114 self->Err("Failed to unmap memory (%s)\n", strerror(-status)); 115 } 113 116 114 if(mutex_binded==false) 115 { 116 status=rt_mutex_delete(&mutex); 117 if(status!=0) 118 { 119 self->Err("error destroying mutex (%s)\n",strerror(-status)); 120 } 121 } 122 #else 123 status = munmap(mem_segment, size); 124 if(status!=0) 125 { 126 self->Err("Failed to unmap memory (%s)\n",strerror(-status)); 127 } 117 status = close(fd); 118 if (status != 0) { 119 self->Err("Failed to close file (%s)\n", strerror(-status)); 120 } 128 121 129 status = close(fd); 130 if(status!=0) 131 { 132 self->Err("Failed to close file (%s)\n",strerror(-status)); 133 } 122 // do not check erros as it can be done by another process 123 status = shm_unlink(shm_name.c_str()); /* 124 if(status!=0) 125 { 126 self->Err("Failed to unlink memory (%s)\n",strerror(-status)); 127 } 128 */ 129 // do not check erros as it can be done by another process 130 status = sem_unlink(sem_name.c_str()); /* 131 if(status!=0) 132 { 133 self->Err("Failed to unlink semaphore (%s)\n",strerror(-status)); 134 }*/ 134 135 135 //do not check erros as it can be done by another process 136 status = shm_unlink(shm_name.c_str());/* 137 if(status!=0) 138 { 139 self->Err("Failed to unlink memory (%s)\n",strerror(-status)); 140 } 141 */ 142 //do not check erros as it can be done by another process 143 status = sem_unlink(sem_name.c_str());/* 144 if(status!=0) 145 { 146 self->Err("Failed to unlink semaphore (%s)\n",strerror(-status)); 147 }*/ 148 149 status = sem_close(sem); 150 if(status!=0) 151 { 152 self->Err("Failed to close semaphore (%s)\n",strerror(-status)); 153 } 136 status = sem_close(sem); 137 if (status != 0) { 138 self->Err("Failed to close semaphore (%s)\n", strerror(-status)); 139 } 154 140 #endif 155 141 } 156 142 157 158 void SharedMem_impl::Write(const char* buf,size_t size) 159 { 143 void SharedMem_impl::Write(const char *buf, size_t size) { 160 144 #ifdef __XENO__ 161 int status=rt_mutex_acquire(&mutex,TM_INFINITE); 162 if(status!=0) self->Err("error (%s)\n",strerror(-status)); 163 memcpy(mem_segment,buf,size); 164 status=rt_mutex_release(&mutex); 165 if(status!=0) self->Err("error (%s)\n",strerror(-status)); 145 int status = rt_mutex_acquire(&mutex, TM_INFINITE); 146 if (status != 0) 147 self->Err("error (%s)\n", strerror(-status)); 148 memcpy(mem_segment, buf, size); 149 status = rt_mutex_release(&mutex); 150 if (status != 0) 151 self->Err("error (%s)\n", strerror(-status)); 166 152 #else 167 168 memcpy(mem_segment,buf,size);169 153 sem_wait(sem); 154 memcpy(mem_segment, buf, size); 155 sem_post(sem); 170 156 #endif 171 157 } 172 158 173 void SharedMem_impl::Read(char* buf,size_t size) 174 { 159 void SharedMem_impl::Read(char *buf, size_t size) { 175 160 #ifdef __XENO__ 176 int status=rt_mutex_acquire(&mutex,TM_INFINITE); 177 if(status!=0) self->Err("error (%s)\n",strerror(-status)); 178 memcpy(buf,mem_segment,size); 179 status=rt_mutex_release(&mutex); 180 if(status!=0) self->Err("error (%s)\n",strerror(-status)); 161 int status = rt_mutex_acquire(&mutex, TM_INFINITE); 162 if (status != 0) 163 self->Err("error (%s)\n", strerror(-status)); 164 memcpy(buf, mem_segment, size); 165 status = rt_mutex_release(&mutex); 166 if (status != 0) 167 self->Err("error (%s)\n", strerror(-status)); 181 168 #else 182 183 memcpy(buf,mem_segment,size);184 169 sem_wait(sem); 170 memcpy(buf, mem_segment, size); 171 sem_post(sem); 185 172 #endif 186 173 }
Note:
See TracChangeset
for help on using the changeset viewer.