[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 4 | // %flair:license}
|
---|
| 5 | // created: 2012/03/14
|
---|
| 6 | // filename: Mutex.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: Class defining a mutex
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include "Mutex_impl.h"
|
---|
| 19 | #include "Mutex.h"
|
---|
| 20 | #include <string.h>
|
---|
| 21 |
|
---|
| 22 | using std::string;
|
---|
| 23 | using namespace flair::core;
|
---|
| 24 |
|
---|
[15] | 25 | Mutex_impl::Mutex_impl(Mutex *self) {
|
---|
| 26 | this->self = self;
|
---|
[2] | 27 | #ifdef __XENO__
|
---|
[15] | 28 | int status = rt_mutex_create(&mutex, NULL);
|
---|
| 29 | if (status != 0)
|
---|
| 30 | self->Err("rt_mutex_create error (%s)\n", strerror(-status));
|
---|
[2] | 31 | #else
|
---|
[15] | 32 | // flag_locked=false;//revoir l'implementation nrt du is_locked
|
---|
| 33 | pthread_mutexattr_t attr;
|
---|
| 34 | if (pthread_mutexattr_init(&attr) != 0) {
|
---|
| 35 | self->Err("pthread_mutexattr_init error\n");
|
---|
| 36 | }
|
---|
| 37 | if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) {
|
---|
| 38 | self->Err("pthread_mutexattr_settype error\n");
|
---|
| 39 | }
|
---|
| 40 | if (pthread_mutex_init(&mutex, &attr) != 0) {
|
---|
| 41 | self->Err("pthread_mutex_init error\n");
|
---|
| 42 | }
|
---|
| 43 | if (pthread_mutexattr_destroy(&attr) != 0) {
|
---|
| 44 | self->Err("pthread_mutexattr_destroy error\n");
|
---|
| 45 | }
|
---|
[2] | 46 | #endif
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[15] | 49 | Mutex_impl::~Mutex_impl() {
|
---|
| 50 | int status;
|
---|
[2] | 51 | #ifdef __XENO__
|
---|
[15] | 52 | status = rt_mutex_delete(&mutex);
|
---|
[2] | 53 | #else
|
---|
[15] | 54 | status = pthread_mutex_destroy(&mutex);
|
---|
[2] | 55 | #endif
|
---|
[15] | 56 | if (status != 0)
|
---|
| 57 | self->Err("error destroying mutex (%s)\n", strerror(-status));
|
---|
[2] | 58 | }
|
---|
| 59 |
|
---|
[15] | 60 | void Mutex_impl::GetMutex(void) {
|
---|
| 61 | int status;
|
---|
[2] | 62 | #ifdef __XENO__
|
---|
[15] | 63 | status = rt_mutex_acquire(&mutex, TM_INFINITE);
|
---|
[2] | 64 | #else
|
---|
[15] | 65 | // flag_locked=true;
|
---|
| 66 | status = pthread_mutex_lock(&mutex);
|
---|
[2] | 67 | #endif
|
---|
[15] | 68 | if (status != 0)
|
---|
| 69 | self->Err("error (%s)\n", strerror(-status));
|
---|
[2] | 70 | }
|
---|
| 71 |
|
---|
[15] | 72 | void Mutex_impl::ReleaseMutex(void) {
|
---|
| 73 | int status;
|
---|
[2] | 74 | #ifdef __XENO__
|
---|
[15] | 75 | status = rt_mutex_release(&mutex);
|
---|
[2] | 76 | #else
|
---|
[15] | 77 | status = pthread_mutex_unlock(&mutex);
|
---|
| 78 | // flag_locked=false;
|
---|
[2] | 79 | #endif
|
---|
[15] | 80 | if (status != 0)
|
---|
| 81 | self->Err("error (%s)\n", strerror(-status));
|
---|
[2] | 82 | }
|
---|
| 83 |
|
---|
| 84 | /*
|
---|
| 85 | bool Mutex_impl::IsLocked(void)
|
---|
| 86 | {
|
---|
| 87 | #ifdef __XENO__
|
---|
| 88 | RT_MUTEX_INFO info;
|
---|
| 89 | int status=rt_mutex_inquire(&mutex_rt,&info);
|
---|
[15] | 90 | if(status!=0) mutex->Err("erreur rt_mutex_inquire
|
---|
| 91 | (%s)\n",strerror(-status));
|
---|
[2] | 92 | if(info.locked>0) return true;
|
---|
| 93 | return false;
|
---|
| 94 | #else
|
---|
| 95 | return flag_locked;
|
---|
| 96 | #endif
|
---|
| 97 | }
|
---|
| 98 | */
|
---|