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 |
|
---|
23 | using std::string;
|
---|
24 | using namespace flair::core;
|
---|
25 |
|
---|
26 | Semaphore_impl::Semaphore_impl(Semaphore *self, uint32_t initialValue) {
|
---|
27 | this->self = self;
|
---|
28 | int status;
|
---|
29 | #ifdef __XENO__
|
---|
30 | status = rt_sem_create(&semaphore, NULL, initialValue, S_FIFO);
|
---|
31 | #else
|
---|
32 | status = sem_init(&semaphore, 0, initialValue);
|
---|
33 | #endif
|
---|
34 | if (status != 0) {
|
---|
35 | self->Err("error creating semaphore (%s)\n", strerror(-status));
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | Semaphore_impl::~Semaphore_impl() {
|
---|
40 | int status;
|
---|
41 | #ifdef __XENO__
|
---|
42 | status = rt_sem_delete(&semaphore);
|
---|
43 | #else
|
---|
44 | status = sem_destroy(&semaphore);
|
---|
45 | #endif
|
---|
46 | if (status != 0)
|
---|
47 | self->Err("error destroying semaphore (%s)\n", strerror(-status));
|
---|
48 | }
|
---|
49 |
|
---|
50 | void Semaphore_impl::GetSemaphore(void) {
|
---|
51 | int status;
|
---|
52 | #ifdef __XENO__
|
---|
53 | status = rt_sem_p(&semaphore, TM_INFINITE);
|
---|
54 | #else
|
---|
55 | status = sem_wait(&semaphore);
|
---|
56 | #endif
|
---|
57 | if (status != 0)
|
---|
58 | self->Err("error getting the semaphore (%s)\n", strerror(-status));
|
---|
59 | }
|
---|
60 |
|
---|
61 | void Semaphore_impl::ReleaseSemaphore(void) {
|
---|
62 | int status;
|
---|
63 | #ifdef __XENO__
|
---|
64 | status = rt_sem_v(&semaphore);
|
---|
65 | #else
|
---|
66 | status = sem_post(&semaphore);
|
---|
67 | #endif
|
---|
68 | if (status != 0)
|
---|
69 | self->Err("error releasing the semaphore (%s)\n", strerror(-status));
|
---|
70 | }
|
---|