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

Last change on this file since 213 was 213, checked in by Sanahuja Guillaume, 4 years ago

thread stack size rework
add Matrix class

File size: 3.8 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, size_t size, SharedMem::Type &type):self(self),type(type),size(size),
29 sem(self,1,"/" + name + "_mutex"), sem_producer(self,0,"/" + name + "_producer"),sem_consumer(self,0,"/" + name + "_consumer") {
30
31#ifdef __XENO__
32 heap_binded = false;
33 int status = rt_heap_create(&heap, name.c_str(), size,
34 H_SHARED | H_FIFO | H_NONCACHED);
35 if (status == -EEXIST) {
36 heap_binded = true;
37 status = rt_heap_bind(&heap, name.c_str(), TM_INFINITE);
38 }
39 if (status != 0) {
40 char errorMsg[256];
41 self->Err("rt_heap_create error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
42 return;
43 }
44
45 void *ptr;
46 status = rt_heap_alloc(&heap, 0, TM_NONBLOCK, &ptr);
47 if (status != 0) {
48 char errorMsg[256];
49 self->Err("rt_heap_alloc error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
50 }
51 mem_segment = (char *)ptr;
52
53#else
54 shm_name = "/" + name;
55 fd = shm_open(shm_name.c_str(), O_RDWR | O_CREAT, 0666);
56 if (fd == -1) {
57 self->Err("Error creating shared memory\n");
58 }
59 ftruncate(fd, size);
60
61 mem_segment = (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
62 if (mem_segment == MAP_FAILED) {
63 self->Err("Failed to map memory\n");
64 }
65#endif
66}
67
68SharedMem_impl::~SharedMem_impl() {
69 int status;
70
71#ifdef __XENO__
72
73 if (heap_binded == false) {
74 status = rt_heap_delete(&heap);
75 if (status != 0) {
76 char errorMsg[256];
77 self->Err("rt_heap_delete error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
78 }
79 }
80
81#else
82 status = munmap(mem_segment, size);
83 if (status != 0) {
84 char errorMsg[256];
85 self->Err("Failed to unmap memory (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
86 }
87
88 status = close(fd);
89 if (status != 0) {
90 char errorMsg[256];
91 self->Err("Failed to close file (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
92 }
93
94 // do not check errors as it can be done by another process
95 status = shm_unlink(shm_name.c_str()); /*
96 if(status!=0)
97 {
98 char errorMsg[256];
99 self->Err("Failed to unlink memory (%s)\n",strerror_r(-status, errorMsg, sizeof(errorMsg)));
100 }
101*/
102#endif
103}
104
105void SharedMem_impl::ReaderReady() {
106 sem_consumer.ReleaseSemaphore();
107}
108
109void SharedMem_impl::Write(const char *buf, size_t size) {
110 if (type==SharedMem::Type::mutex) {
111 sem.GetSemaphore();
112 memcpy(mem_segment, buf, size);
113 sem.ReleaseSemaphore();
114 } else if (type==SharedMem::Type::producerConsumer) {
115 //wait until consumer took the data away before writting a new one
116 //but should not block if nobody's there to read
117 if (sem_consumer.TryGetSemaphore()) {
118 memcpy(mem_segment, buf, size);
119 sem_producer.ReleaseSemaphore();
120 }
121 }
122}
123
124bool SharedMem_impl::Read(char *buf, size_t size, Time nsTimeout) {
125 if (type==SharedMem::Type::mutex) {
126 if (sem.GetSemaphore()) {
127 memcpy(buf, mem_segment, size);
128 sem.ReleaseSemaphore();
129 return true;
130 }
131 } else if (type==SharedMem::Type::producerConsumer) {
132 if (sem_producer.GetSemaphore(nsTimeout)) {
133 memcpy(buf, mem_segment, size);
134 sem_consumer.ReleaseSemaphore();
135 return true;
136 }
137 }
138 return false;
139}
Note: See TracBrowser for help on using the repository browser.