[117] | 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>
|
---|
[126] | 22 | #include <time.h>
|
---|
[117] | 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;
|
---|
[133] | 30 |
|
---|
[117] | 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) {
|
---|
[213] | 37 | char errorMsg[256];
|
---|
[133] | 38 | self->Err("error creating semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[117] | 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | Semaphore_impl::~Semaphore_impl() {
|
---|
| 43 | int status;
|
---|
[133] | 44 |
|
---|
[117] | 45 | #ifdef __XENO__
|
---|
| 46 | status = rt_sem_delete(&semaphore);
|
---|
| 47 | #else
|
---|
| 48 | status = sem_destroy(&semaphore);
|
---|
| 49 | #endif
|
---|
[213] | 50 | if (status != 0) {
|
---|
| 51 | char errorMsg[256];
|
---|
[133] | 52 | self->Err("error destroying semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[213] | 53 | }
|
---|
[117] | 54 | }
|
---|
| 55 |
|
---|
[203] | 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 |
|
---|
[126] | 64 | bool Semaphore_impl::GetSemaphore(Time timeout) {
|
---|
[117] | 65 | int status;
|
---|
[126] | 66 | bool returnValue = true;
|
---|
[117] | 67 | #ifdef __XENO__
|
---|
[126] | 68 | status = rt_sem_p(&semaphore, timeout);
|
---|
[117] | 69 | #else
|
---|
[126] | 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 | }
|
---|
[117] | 83 | #endif
|
---|
[126] | 84 | if (status != 0) {
|
---|
| 85 | if (errno == ETIMEDOUT) {
|
---|
[203] | 86 | #ifdef DEBUG
|
---|
| 87 | self->Warn("warning : semaphore timedout\n");
|
---|
| 88 | #endif
|
---|
[126] | 89 | } else {
|
---|
[133] | 90 | char errorMsg[256];
|
---|
| 91 | self->Err("error getting the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[126] | 92 | }
|
---|
| 93 | returnValue = false;
|
---|
| 94 | }
|
---|
| 95 | return returnValue;
|
---|
[117] | 96 | }
|
---|
| 97 |
|
---|
[126] | 98 | bool Semaphore_impl::ReleaseSemaphore(void) {
|
---|
[117] | 99 | int status;
|
---|
[126] | 100 | bool returnValue = true;
|
---|
[117] | 101 | #ifdef __XENO__
|
---|
| 102 | status = rt_sem_v(&semaphore);
|
---|
| 103 | #else
|
---|
| 104 | status = sem_post(&semaphore);
|
---|
| 105 | #endif
|
---|
[126] | 106 | if (status != 0) {
|
---|
[133] | 107 | char errorMsg[256];
|
---|
| 108 | self->Err("error releasing the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[126] | 109 | returnValue = false;
|
---|
| 110 | }
|
---|
| 111 | return returnValue;
|
---|
[117] | 112 | }
|
---|