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

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

Added timeout on SharedMem Read

File size: 2.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: 2016/11/16
6// filename: Semaphore_impl.cpp
7//
8// author: Thomas Fuhrmann
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Class defining a semaphore implementation
14//
15//
16/*********************************************************************/
17
18#include "Semaphore.h"
19#include "Semaphore_impl.h"
20
21#include <string.h>
22#include <time.h>
23
24using std::string;
25using namespace flair::core;
26
27Semaphore_impl::Semaphore_impl(Semaphore *self, uint32_t initialValue) {
28 this->self = self;
29 int status;
30 char errorMsg[256];
31
32#ifdef __XENO__
33 status = rt_sem_create(&semaphore, NULL, initialValue, S_FIFO);
34#else
35 status = sem_init(&semaphore, 0, initialValue);
36#endif
37 if (status != 0) {
38 self->Err("error creating semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
39 }
40}
41
42Semaphore_impl::~Semaphore_impl() {
43 int status;
44 char errorMsg[256];
45
46#ifdef __XENO__
47 status = rt_sem_delete(&semaphore);
48#else
49 status = sem_destroy(&semaphore);
50#endif
51 if (status != 0)
52 self->Err("error destroying semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
53}
54
55bool Semaphore_impl::TryGetSemaphore() {
56#ifdef __XENO__
57 return !rt_sem_p(&semaphore, TM_NONBLOCK);
58#else
59 return !sem_trywait(&semaphore);
60#endif
61}
62
63bool Semaphore_impl::GetSemaphore(Time timeout) {
64 int status;
65 bool returnValue = true;
66#ifdef __XENO__
67 status = rt_sem_p(&semaphore, timeout);
68#else
69 if (timeout != 0) {
70 struct timespec semTimeout;
71 clock_gettime(CLOCK_REALTIME, &semTimeout);
72 semTimeout.tv_sec += timeout / 1000000000ULL;
73 semTimeout.tv_nsec += timeout % 1000000000ULL;
74 if (semTimeout.tv_nsec >= 1000000000ULL) {
75 semTimeout.tv_sec++;
76 semTimeout.tv_nsec -= 1000000000ULL;
77 }
78 status = sem_timedwait(&semaphore, &semTimeout);
79 } else {
80 status = sem_wait(&semaphore);
81 }
82#endif
83 if (status != 0) {
84 if (errno == ETIMEDOUT) {
85#ifdef DEBUG
86 self->Warn("warning : semaphore timedout\n");
87#endif
88 } else {
89 char errorMsg[256];
90 self->Err("error getting the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
91 }
92 returnValue = false;
93 }
94 return returnValue;
95}
96
97bool Semaphore_impl::ReleaseSemaphore(void) {
98 int status;
99 bool returnValue = true;
100#ifdef __XENO__
101 status = rt_sem_v(&semaphore);
102#else
103 status = sem_post(&semaphore);
104#endif
105 if (status != 0) {
106 char errorMsg[256];
107 self->Err("error releasing the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
108 returnValue = false;
109 }
110 return returnValue;
111}
Note: See TracBrowser for help on using the repository browser.