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

Last change on this file since 203 was 203, checked in by Bayard Gildas, 7 years ago

Added timeout on SharedMem Read

File size: 3.7 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 char errorMsg[256];
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_r(-status, errorMsg, sizeof(errorMsg)));
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_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 char errorMsg[256];
71
72#ifdef __XENO__
73
74 if (heap_binded == false) {
75 status = rt_heap_delete(&heap);
76 if (status != 0) {
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 self->Err("Failed to unmap memory (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
85 }
86
87 status = close(fd);
88 if (status != 0) {
89 self->Err("Failed to close file (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
90 }
91
92 // do not check errors as it can be done by another process
93 status = shm_unlink(shm_name.c_str()); /*
94 if(status!=0)
95 {
96 self->Err("Failed to unlink memory (%s)\n",strerror_r(-status, errorMsg, sizeof(errorMsg)));
97 }
98*/
99#endif
100}
101
102void SharedMem_impl::ReaderReady() {
103 sem_consumer.ReleaseSemaphore();
104}
105
106void SharedMem_impl::Write(const char *buf, size_t size) {
107 if (type==SharedMem::Type::mutex) {
108 sem.GetSemaphore();
109 memcpy(mem_segment, buf, size);
110 sem.ReleaseSemaphore();
111 } else if (type==SharedMem::Type::producerConsumer) {
112 //wait until consumer took the data away before writting a new one
113 //but should not block if nobody's there to read
114 if (sem_consumer.TryGetSemaphore()) {
115 memcpy(mem_segment, buf, size);
116 sem_producer.ReleaseSemaphore();
117 }
118 }
119}
120
121bool SharedMem_impl::Read(char *buf, size_t size, Time nsTimeout) {
122 if (type==SharedMem::Type::mutex) {
123 if (sem.GetSemaphore()) {
124 memcpy(buf, mem_segment, size);
125 sem.ReleaseSemaphore();
126 return true;
127 }
128 } else if (type==SharedMem::Type::producerConsumer) {
129 if (sem_producer.GetSemaphore(nsTimeout)) {
130 memcpy(buf, mem_segment, size);
131 sem_consumer.ReleaseSemaphore();
132 return true;
133 }
134 }
135 return false;
136}
Note: See TracBrowser for help on using the repository browser.