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

Last change on this file since 42 was 15, checked in by Bayard Gildas, 5 years ago

sources reformatted with flair-format-dir script

File size: 4.3 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
33#ifdef __XENO__
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  }
45
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;
52
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  }
64
65#else
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);
72
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  }
78
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  }
84#endif
85}
86
87SharedMem_impl::~SharedMem_impl() {
88  int status;
89#ifdef __XENO__
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    }
103  }
104
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));
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  }
116
117  status = close(fd);
118  if (status != 0) {
119    self->Err("Failed to close file (%s)\n", strerror(-status));
120  }
121
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   }*/
135
136  status = sem_close(sem);
137  if (status != 0) {
138    self->Err("Failed to close semaphore (%s)\n", strerror(-status));
139  }
140#endif
141}
142
143void SharedMem_impl::Write(const char *buf, size_t size) {
144#ifdef __XENO__
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));
152#else
153  sem_wait(sem);
154  memcpy(mem_segment, buf, size);
155  sem_post(sem);
156#endif
157}
158
159void SharedMem_impl::Read(char *buf, size_t size) {
160#ifdef __XENO__
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));
168#else
169  sem_wait(sem);
170  memcpy(buf, mem_segment, size);
171  sem_post(sem);
172#endif
173}
Note: See TracBrowser for help on using the repository browser.