source: flair-src/branches/sanscv/lib/FlairCore/src/Semaphore_impl.cpp @ 326

Last change on this file since 326 was 324, checked in by Sanahuja Guillaume, 2 years ago

removing opencv dependency

File size: 3.1 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, string name, uint32_t initialValue, Semaphore::Type &type):type(type) {
28  this->self = self;
29  int status;
30       
31#ifdef __XENO__
32  status = rt_sem_create(&semaphore, NULL, initialValue, S_FIFO);
33#else
34  if (type==Semaphore::Type::named) {
35    sem_name = "/" + name;
36    semaphore=sem_open(sem_name.c_str(),O_CREAT, 0666, initialValue);
37    if (semaphore==SEM_FAILED) status=errno; else status=0;
38  } else { //anonymous
39    semaphore=new sem_t;
40    status = sem_init(semaphore, 0, initialValue);
41  }
42#endif
43  if (status != 0) {
44    char errorMsg[256];
45    self->Err("error creating semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
46  }
47}
48
49Semaphore_impl::~Semaphore_impl() {
50  int status;
51       
52#ifdef __XENO__
53  status = rt_sem_delete(&semaphore);
54#else
55  if (type==Semaphore::Type::named) {
56    status=sem_close(semaphore);
57    sem_unlink(sem_name.c_str());
58  } else { //anonymous
59    status = sem_destroy(semaphore);
60  }
61#endif
62  if (status != 0) {
63    char errorMsg[256];
64    self->Err("error destroying semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
65  }
66}
67
68bool Semaphore_impl::TryGetSemaphore() {
69#ifdef __XENO__
70  return !rt_sem_p(&semaphore, TM_NONBLOCK);
71#else
72  return !sem_trywait(semaphore);
73#endif
74}
75
76bool Semaphore_impl::GetSemaphore(Time timeout) {
77  int status;
78  bool returnValue = true;
79#ifdef __XENO__
80  status = rt_sem_p(&semaphore, timeout);
81#else
82  if (timeout != 0) {
83    struct timespec semTimeout;
84    clock_gettime(CLOCK_REALTIME, &semTimeout);
85    semTimeout.tv_sec += timeout / 1000000000ULL;
86    semTimeout.tv_nsec += timeout % 1000000000ULL;
87    if (semTimeout.tv_nsec >= 1000000000ULL) {
88        semTimeout.tv_sec++;
89        semTimeout.tv_nsec -= 1000000000ULL;
90    }
91    status = sem_timedwait(semaphore, &semTimeout);
92  } else {
93    status = sem_wait(semaphore);
94  }
95#endif
96        if (status != 0) {
97                if (errno == ETIMEDOUT) {
98#ifdef DEBUG
99                        self->Warn("warning : semaphore timedout\n");
100#endif
101                } else {
102                                        char errorMsg[256];
103                    self->Err("error getting the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
104                }
105    returnValue = false;
106  }
107  return returnValue;
108}
109
110bool Semaphore_impl::ReleaseSemaphore(void) {
111  int status;
112  bool returnValue = true;
113#ifdef __XENO__
114  status = rt_sem_v(&semaphore);
115#else
116  status = sem_post(semaphore);
117#endif
118  if (status != 0) {
119                char errorMsg[256];
120    self->Err("error releasing the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
121    returnValue = false;
122  }
123  return returnValue;
124}
Note: See TracBrowser for help on using the repository browser.