[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);
|
---|
[133] | 29 | if (status != 0) {
|
---|
| 30 | char errorMsg[256];
|
---|
| 31 | self->Err("rt_mutex_create error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
| 32 | }
|
---|
[2] | 33 | #else
|
---|
[15] | 34 | // flag_locked=false;//revoir l'implementation nrt du is_locked
|
---|
| 35 | pthread_mutexattr_t attr;
|
---|
| 36 | if (pthread_mutexattr_init(&attr) != 0) {
|
---|
| 37 | self->Err("pthread_mutexattr_init error\n");
|
---|
| 38 | }
|
---|
| 39 | if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) {
|
---|
| 40 | self->Err("pthread_mutexattr_settype error\n");
|
---|
| 41 | }
|
---|
| 42 | if (pthread_mutex_init(&mutex, &attr) != 0) {
|
---|
| 43 | self->Err("pthread_mutex_init error\n");
|
---|
| 44 | }
|
---|
| 45 | if (pthread_mutexattr_destroy(&attr) != 0) {
|
---|
| 46 | self->Err("pthread_mutexattr_destroy error\n");
|
---|
| 47 | }
|
---|
[2] | 48 | #endif
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[15] | 51 | Mutex_impl::~Mutex_impl() {
|
---|
| 52 | int status;
|
---|
[133] | 53 |
|
---|
[2] | 54 | #ifdef __XENO__
|
---|
[15] | 55 | status = rt_mutex_delete(&mutex);
|
---|
[2] | 56 | #else
|
---|
[15] | 57 | status = pthread_mutex_destroy(&mutex);
|
---|
[2] | 58 | #endif
|
---|
[213] | 59 | if (status != 0) {
|
---|
| 60 | char errorMsg[256];
|
---|
[133] | 61 | self->Err("error destroying mutex (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[213] | 62 | }
|
---|
[2] | 63 | }
|
---|
| 64 |
|
---|
[15] | 65 | void Mutex_impl::GetMutex(void) {
|
---|
| 66 | int status;
|
---|
[133] | 67 |
|
---|
[2] | 68 | #ifdef __XENO__
|
---|
[15] | 69 | status = rt_mutex_acquire(&mutex, TM_INFINITE);
|
---|
[2] | 70 | #else
|
---|
[15] | 71 | // flag_locked=true;
|
---|
| 72 | status = pthread_mutex_lock(&mutex);
|
---|
[2] | 73 | #endif
|
---|
[213] | 74 | if (status != 0) {
|
---|
| 75 | char errorMsg[256];
|
---|
[133] | 76 | self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[213] | 77 | }
|
---|
[2] | 78 | }
|
---|
| 79 |
|
---|
[15] | 80 | void Mutex_impl::ReleaseMutex(void) {
|
---|
| 81 | int status;
|
---|
[133] | 82 |
|
---|
[2] | 83 | #ifdef __XENO__
|
---|
[15] | 84 | status = rt_mutex_release(&mutex);
|
---|
[2] | 85 | #else
|
---|
[15] | 86 | status = pthread_mutex_unlock(&mutex);
|
---|
| 87 | // flag_locked=false;
|
---|
[2] | 88 | #endif
|
---|
[133] | 89 | if (status != 0) {
|
---|
| 90 | char errorMsg[256];
|
---|
| 91 | self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
| 92 | }
|
---|
[2] | 93 | }
|
---|
| 94 |
|
---|
| 95 | /*
|
---|
| 96 | bool Mutex_impl::IsLocked(void)
|
---|
| 97 | {
|
---|
| 98 | #ifdef __XENO__
|
---|
| 99 | RT_MUTEX_INFO info;
|
---|
| 100 | int status=rt_mutex_inquire(&mutex_rt,&info);
|
---|
[133] | 101 | if(status!=0) mutex->Err("erreur rt_mutex_inquire (%s)\n",strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
[2] | 102 | if(info.locked>0) return true;
|
---|
| 103 | return false;
|
---|
| 104 | #else
|
---|
| 105 | return flag_locked;
|
---|
| 106 | #endif
|
---|
| 107 | }
|
---|
| 108 | */
|
---|