[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 | char errorMsg[256];
|
---|
| 31 |
|
---|
[117] | 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) {
|
---|
[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 | char errorMsg[256];
|
---|
| 45 |
|
---|
[117] | 46 | #ifdef __XENO__
|
---|
| 47 | status = rt_sem_delete(&semaphore);
|
---|
| 48 | #else
|
---|
| 49 | status = sem_destroy(&semaphore);
|
---|
| 50 | #endif
|
---|
| 51 | if (status != 0)
|
---|
[133] | 52 | self->Err("error destroying semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[117] | 53 | }
|
---|
| 54 |
|
---|
[126] | 55 | bool Semaphore_impl::GetSemaphore(Time timeout) {
|
---|
[117] | 56 | int status;
|
---|
[126] | 57 | bool returnValue = true;
|
---|
[117] | 58 | #ifdef __XENO__
|
---|
[126] | 59 | status = rt_sem_p(&semaphore, timeout);
|
---|
[117] | 60 | #else
|
---|
[126] | 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 | }
|
---|
[117] | 74 | #endif
|
---|
[126] | 75 | if (status != 0) {
|
---|
| 76 | if (errno == ETIMEDOUT) {
|
---|
| 77 | self->Warn("warning : semaphore timedout\n");
|
---|
| 78 | } else {
|
---|
[133] | 79 | char errorMsg[256];
|
---|
| 80 | self->Err("error getting the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[126] | 81 | }
|
---|
| 82 | returnValue = false;
|
---|
| 83 | }
|
---|
| 84 | return returnValue;
|
---|
[117] | 85 | }
|
---|
| 86 |
|
---|
[126] | 87 | bool Semaphore_impl::ReleaseSemaphore(void) {
|
---|
[117] | 88 | int status;
|
---|
[126] | 89 | bool returnValue = true;
|
---|
[117] | 90 | #ifdef __XENO__
|
---|
| 91 | status = rt_sem_v(&semaphore);
|
---|
| 92 | #else
|
---|
| 93 | status = sem_post(&semaphore);
|
---|
| 94 | #endif
|
---|
[126] | 95 | if (status != 0) {
|
---|
[133] | 96 | char errorMsg[256];
|
---|
| 97 | self->Err("error releasing the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[126] | 98 | returnValue = false;
|
---|
| 99 | }
|
---|
| 100 | return returnValue;
|
---|
[117] | 101 | }
|
---|