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 |
|
---|
25 | using std::string;
|
---|
26 | using namespace flair::core;
|
---|
27 |
|
---|
28 | SharedMem_impl::SharedMem_impl(const SharedMem* self,string name,size_t size)
|
---|
29 | {
|
---|
30 | this->size=size;
|
---|
31 | this->self=self;
|
---|
32 |
|
---|
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 | }
|
---|
46 |
|
---|
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;
|
---|
54 |
|
---|
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 | }
|
---|
68 |
|
---|
69 | #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);
|
---|
77 |
|
---|
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 | }
|
---|
84 |
|
---|
85 | mem_segment =(char*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
---|
86 | if (mem_segment == MAP_FAILED)
|
---|
87 | {
|
---|
88 | self->Err("Failed to map memory\n");
|
---|
89 | }
|
---|
90 | #endif
|
---|
91 | }
|
---|
92 |
|
---|
93 | SharedMem_impl::~SharedMem_impl()
|
---|
94 | {
|
---|
95 | int status;
|
---|
96 | #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));
|
---|
102 | }
|
---|
103 | */
|
---|
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 | }
|
---|
112 | }
|
---|
113 |
|
---|
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 | }
|
---|
128 |
|
---|
129 | status = close(fd);
|
---|
130 | if(status!=0)
|
---|
131 | {
|
---|
132 | self->Err("Failed to close file (%s)\n",strerror(-status));
|
---|
133 | }
|
---|
134 |
|
---|
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 | }
|
---|
154 | #endif
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | void SharedMem_impl::Write(const char* buf,size_t size)
|
---|
159 | {
|
---|
160 | #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));
|
---|
166 | #else
|
---|
167 | sem_wait(sem);
|
---|
168 | memcpy(mem_segment,buf,size);
|
---|
169 | sem_post(sem);
|
---|
170 | #endif
|
---|
171 | }
|
---|
172 |
|
---|
173 | void SharedMem_impl::Read(char* buf,size_t size)
|
---|
174 | {
|
---|
175 | #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));
|
---|
181 | #else
|
---|
182 | sem_wait(sem);
|
---|
183 | memcpy(buf,mem_segment,size);
|
---|
184 | sem_post(sem);
|
---|
185 | #endif
|
---|
186 | }
|
---|