[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 |
|
---|
[238] | 27 | Semaphore_impl::Semaphore_impl(Semaphore *self, string name, uint32_t initialValue, Semaphore::Type &type):type(type) {
|
---|
[117] | 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
|
---|
[238] | 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 | }
|
---|
[117] | 42 | #endif
|
---|
| 43 | if (status != 0) {
|
---|
[213] | 44 | char errorMsg[256];
|
---|
[133] | 45 | self->Err("error creating semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[117] | 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | Semaphore_impl::~Semaphore_impl() {
|
---|
| 50 | int status;
|
---|
[133] | 51 |
|
---|
[117] | 52 | #ifdef __XENO__
|
---|
| 53 | status = rt_sem_delete(&semaphore);
|
---|
| 54 | #else
|
---|
[238] | 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 | }
|
---|
[117] | 61 | #endif
|
---|
[213] | 62 | if (status != 0) {
|
---|
| 63 | char errorMsg[256];
|
---|
[133] | 64 | self->Err("error destroying semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[213] | 65 | }
|
---|
[117] | 66 | }
|
---|
| 67 |
|
---|
[203] | 68 | bool Semaphore_impl::TryGetSemaphore() {
|
---|
| 69 | #ifdef __XENO__
|
---|
| 70 | return !rt_sem_p(&semaphore, TM_NONBLOCK);
|
---|
| 71 | #else
|
---|
[238] | 72 | return !sem_trywait(semaphore);
|
---|
[203] | 73 | #endif
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[126] | 76 | bool Semaphore_impl::GetSemaphore(Time timeout) {
|
---|
[117] | 77 | int status;
|
---|
[126] | 78 | bool returnValue = true;
|
---|
[117] | 79 | #ifdef __XENO__
|
---|
[126] | 80 | status = rt_sem_p(&semaphore, timeout);
|
---|
[117] | 81 | #else
|
---|
[126] | 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 | }
|
---|
[238] | 91 | status = sem_timedwait(semaphore, &semTimeout);
|
---|
[126] | 92 | } else {
|
---|
[238] | 93 | status = sem_wait(semaphore);
|
---|
[126] | 94 | }
|
---|
[117] | 95 | #endif
|
---|
[126] | 96 | if (status != 0) {
|
---|
| 97 | if (errno == ETIMEDOUT) {
|
---|
[203] | 98 | #ifdef DEBUG
|
---|
| 99 | self->Warn("warning : semaphore timedout\n");
|
---|
| 100 | #endif
|
---|
[126] | 101 | } else {
|
---|
[133] | 102 | char errorMsg[256];
|
---|
| 103 | self->Err("error getting the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[126] | 104 | }
|
---|
| 105 | returnValue = false;
|
---|
| 106 | }
|
---|
| 107 | return returnValue;
|
---|
[117] | 108 | }
|
---|
| 109 |
|
---|
[126] | 110 | bool Semaphore_impl::ReleaseSemaphore(void) {
|
---|
[117] | 111 | int status;
|
---|
[126] | 112 | bool returnValue = true;
|
---|
[117] | 113 | #ifdef __XENO__
|
---|
| 114 | status = rt_sem_v(&semaphore);
|
---|
| 115 | #else
|
---|
[238] | 116 | status = sem_post(semaphore);
|
---|
[117] | 117 | #endif
|
---|
[126] | 118 | if (status != 0) {
|
---|
[133] | 119 | char errorMsg[256];
|
---|
| 120 | self->Err("error releasing the semaphore (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[126] | 121 | returnValue = false;
|
---|
| 122 | }
|
---|
| 123 | return returnValue;
|
---|
[117] | 124 | }
|
---|