source: flair-src/trunk/lib/FlairCore/src/SharedMem_impl.cpp@ 133

Last change on this file since 133 was 133, checked in by Sanahuja Guillaume, 5 years ago

modif sterror

File size: 4.9 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2014/02/10
6// filename: SharedMem_impl.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Class defining a shared memory
14//
15//
16/*********************************************************************/
17
18#include "SharedMem_impl.h"
19#include "SharedMem.h"
20#include <fcntl.h>
21#include <unistd.h>
22#include <sys/mman.h>
23#include <string.h>
24
25using std::string;
26using namespace flair::core;
27
28SharedMem_impl::SharedMem_impl(const SharedMem *self, string name,
29 size_t size) {
30 this->size = size;
31 this->self = self;
32 char errorMsg[256];
33
34#ifdef __XENO__
35 heap_binded = false;
36 int status = rt_heap_create(&heap, name.c_str(), size,
37 H_SHARED | H_FIFO | H_NONCACHED);
38 if (status == -EEXIST) {
39 heap_binded = true;
40 status = rt_heap_bind(&heap, name.c_str(), TM_INFINITE);
41 }
42 if (status != 0) {
43 self->Err("rt_heap_create error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
44 return;
45 }
46
47 void *ptr;
48 status = rt_heap_alloc(&heap, 0, TM_NONBLOCK, &ptr);
49 if (status != 0) {
50 self->Err("rt_heap_alloc error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
51 }
52 mem_segment = (char *)ptr;
53
54 mutex_binded = false;
55 string mutex_name = "mutex_" + name;
56 status = rt_mutex_create(&mutex, mutex_name.c_str());
57 if (status == -EEXIST) {
58 mutex_binded = true;
59 status = rt_mutex_bind(&mutex, mutex_name.c_str(), TM_INFINITE);
60 }
61 if (status != 0) {
62 self->Err("rt_mutex_create error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
63 return;
64 }
65
66#else
67 shm_name = "/" + name;
68 fd = shm_open(shm_name.c_str(), O_RDWR | O_CREAT, 0666);
69 if (fd == -1) {
70 self->Err("Error creating shared memory\n");
71 }
72 ftruncate(fd, size);
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");
78 }
79
80 mem_segment =
81 (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
82 if (mem_segment == MAP_FAILED) {
83 self->Err("Failed to map memory\n");
84 }
85#endif
86}
87
88SharedMem_impl::~SharedMem_impl() {
89 int status;
90 char errorMsg[256];
91
92#ifdef __XENO__
93 /* unnecessary because heap is opened in H_SINGLE mode
94 status=rt_heap_free(&heap,mem_segment);
95 if(status!=0)
96 {
97 self->Err("rt_heap_free error (%s)\n",strerror_r(-status, errorMsg, sizeof(errorMsg)));
98 }
99 */
100
101 if (heap_binded == false) {
102 status = rt_heap_delete(&heap);
103 if (status != 0) {
104 self->Err("rt_heap_delete error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
105 }
106 }
107
108 if (mutex_binded == false) {
109 status = rt_mutex_delete(&mutex);
110 if (status != 0) {
111 self->Err("error destroying mutex (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
112 }
113 }
114#else
115 status = munmap(mem_segment, size);
116 if (status != 0) {
117 self->Err("Failed to unmap memory (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
118 }
119
120 status = close(fd);
121 if (status != 0) {
122 self->Err("Failed to close file (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
123 }
124
125 // do not check erros as it can be done by another process
126 status = shm_unlink(shm_name.c_str()); /*
127 if(status!=0)
128 {
129 self->Err("Failed to unlink memory (%s)\n",strerror_r(-status, errorMsg, sizeof(errorMsg)));
130 }
131*/
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 }
143#endif
144}
145
146void SharedMem_impl::Write(const char *buf, size_t size) {
147#ifdef __XENO__
148 int status = rt_mutex_acquire(&mutex, TM_INFINITE);
149 if (status != 0) {
150 char errorMsg[256];
151 self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
152 }
153 memcpy(mem_segment, buf, size);
154 status = rt_mutex_release(&mutex);
155 if (status != 0) {
156 char errorMsg[256];
157 self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
158 }
159#else
160 sem_wait(sem);
161 memcpy(mem_segment, buf, size);
162 sem_post(sem);
163#endif
164}
165
166void SharedMem_impl::Read(char *buf, size_t size) {
167#ifdef __XENO__
168 int status = rt_mutex_acquire(&mutex, TM_INFINITE);
169 if (status != 0) {
170 char errorMsg[256];
171 self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
172 }
173 memcpy(buf, mem_segment, size);
174 status = rt_mutex_release(&mutex);
175 if (status != 0) {
176 char errorMsg[256];
177 self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
178 }
179#else
180 sem_wait(sem);
181 memcpy(buf, mem_segment, size);
182 sem_post(sem);
183#endif
184}
Note: See TracBrowser for help on using the repository browser.