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 |
|
---|
24 | using std::string;
|
---|
25 | using namespace flair::core;
|
---|
26 |
|
---|
27 | Semaphore_impl::Semaphore_impl(Semaphore *self, uint32_t initialValue) {
|
---|
28 | this->self = self;
|
---|
29 | int status;
|
---|
30 |
|
---|
31 | #ifdef __XENO__
|
---|
32 | status = rt_sem_create(&semaphore, NULL, initialValue, S_FIFO);
|
---|
33 | #else
|
---|
34 | status = sem_init(&semaphore, 0, initialValue);
|
---|
35 | #endif
|
---|
36 | if (status != 0) {
|
---|
37 | char errorMsg[256];
|
---|
38 | self->Err("error creating semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | Semaphore_impl::~Semaphore_impl() {
|
---|
43 | int status;
|
---|
44 |
|
---|
45 | #ifdef __XENO__
|
---|
46 | status = rt_sem_delete(&semaphore);
|
---|
47 | #else
|
---|
48 | status = sem_destroy(&semaphore);
|
---|
49 | #endif
|
---|
50 | if (status != 0) {
|
---|
51 | char errorMsg[256];
|
---|
52 | self->Err("error destroying semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | bool Semaphore_impl::TryGetSemaphore() {
|
---|
57 | #ifdef __XENO__
|
---|
58 | return !rt_sem_p(&semaphore, TM_NONBLOCK);
|
---|
59 | #else
|
---|
60 | return !sem_trywait(&semaphore);
|
---|
61 | #endif
|
---|
62 | }
|
---|
63 |
|
---|
64 | bool Semaphore_impl::GetSemaphore(Time timeout) {
|
---|
65 | int status;
|
---|
66 | bool returnValue = true;
|
---|
67 | #ifdef __XENO__
|
---|
68 | status = rt_sem_p(&semaphore, timeout);
|
---|
69 | #else
|
---|
70 | if (timeout != 0) {
|
---|
71 | struct timespec semTimeout;
|
---|
72 | clock_gettime(CLOCK_REALTIME, &semTimeout);
|
---|
73 | semTimeout.tv_sec += timeout / 1000000000ULL;
|
---|
74 | semTimeout.tv_nsec += timeout % 1000000000ULL;
|
---|
75 | if (semTimeout.tv_nsec >= 1000000000ULL) {
|
---|
76 | semTimeout.tv_sec++;
|
---|
77 | semTimeout.tv_nsec -= 1000000000ULL;
|
---|
78 | }
|
---|
79 | status = sem_timedwait(&semaphore, &semTimeout);
|
---|
80 | } else {
|
---|
81 | status = sem_wait(&semaphore);
|
---|
82 | }
|
---|
83 | #endif
|
---|
84 | if (status != 0) {
|
---|
85 | if (errno == ETIMEDOUT) {
|
---|
86 | #ifdef DEBUG
|
---|
87 | self->Warn("warning : semaphore timedout\n");
|
---|
88 | #endif
|
---|
89 | } else {
|
---|
90 | char errorMsg[256];
|
---|
91 | self->Err("error getting the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
92 | }
|
---|
93 | returnValue = false;
|
---|
94 | }
|
---|
95 | return returnValue;
|
---|
96 | }
|
---|
97 |
|
---|
98 | bool Semaphore_impl::ReleaseSemaphore(void) {
|
---|
99 | int status;
|
---|
100 | bool returnValue = true;
|
---|
101 | #ifdef __XENO__
|
---|
102 | status = rt_sem_v(&semaphore);
|
---|
103 | #else
|
---|
104 | status = sem_post(&semaphore);
|
---|
105 | #endif
|
---|
106 | if (status != 0) {
|
---|
107 | char errorMsg[256];
|
---|
108 | self->Err("error releasing the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
109 | returnValue = false;
|
---|
110 | }
|
---|
111 | return returnValue;
|
---|
112 | }
|
---|