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

Last change on this file since 202 was 133, checked in by Sanahuja Guillaume, 7 years ago

modif sterror

File size: 2.5 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::GetSemaphore(Time timeout) {
56 int status;
57 bool returnValue = true;
58#ifdef __XENO__
59 status = rt_sem_p(&semaphore, timeout);
60#else
61 if (timeout != 0) {
62 struct timespec semTimeout;
63 clock_gettime(CLOCK_REALTIME, &semTimeout);
64 semTimeout.tv_sec += timeout / 1000000000ULL;
65 semTimeout.tv_nsec += timeout % 1000000000ULL;
66 if (semTimeout.tv_nsec >= 1000000000ULL) {
67 semTimeout.tv_sec++;
68 semTimeout.tv_nsec -= 1000000000ULL;
69 }
70 status = sem_timedwait(&semaphore, &semTimeout);
71 } else {
72 status = sem_wait(&semaphore);
73 }
74#endif
75 if (status != 0) {
76 if (errno == ETIMEDOUT) {
77 self->Warn("warning : semaphore timedout\n");
78 } else {
79 char errorMsg[256];
80 self->Err("error getting the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
81 }
82 returnValue = false;
83 }
84 return returnValue;
85}
86
87bool Semaphore_impl::ReleaseSemaphore(void) {
88 int status;
89 bool returnValue = true;
90#ifdef __XENO__
91 status = rt_sem_v(&semaphore);
92#else
93 status = sem_post(&semaphore);
94#endif
95 if (status != 0) {
96 char errorMsg[256];
97 self->Err("error releasing the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
98 returnValue = false;
99 }
100 return returnValue;
101}
Note: See TracBrowser for help on using the repository browser.