source: flair-src/trunk/lib/FlairCore/src/Mutex_impl.cpp @ 133

Last change on this file since 133 was 133, checked in by Sanahuja Guillaume, 5 years ago

modif sterror

File size: 2.5 KB
Line 
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:    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
22using std::string;
23using namespace flair::core;
24
25Mutex_impl::Mutex_impl(Mutex *self) {
26  this->self = self;
27#ifdef __XENO__
28  int status = rt_mutex_create(&mutex, NULL);
29  if (status != 0) {
30                char errorMsg[256];
31    self->Err("rt_mutex_create error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
32        }
33#else
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  }
48#endif
49}
50
51Mutex_impl::~Mutex_impl() {
52  int status;
53        char errorMsg[256];
54       
55#ifdef __XENO__
56  status = rt_mutex_delete(&mutex);
57#else
58  status = pthread_mutex_destroy(&mutex);
59#endif
60  if (status != 0)
61    self->Err("error destroying mutex (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
62}
63
64void Mutex_impl::GetMutex(void) {
65  int status;
66        char errorMsg[256];
67       
68#ifdef __XENO__
69  status = rt_mutex_acquire(&mutex, TM_INFINITE);
70#else
71  // flag_locked=true;
72  status = pthread_mutex_lock(&mutex);
73#endif
74  if (status != 0)
75    self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
76}
77
78void Mutex_impl::ReleaseMutex(void) {
79  int status;
80       
81#ifdef __XENO__
82  status = rt_mutex_release(&mutex);
83#else
84  status = pthread_mutex_unlock(&mutex);
85// flag_locked=false;
86#endif
87  if (status != 0) {
88                char errorMsg[256];
89    self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
90        }
91}
92
93/*
94bool Mutex_impl::IsLocked(void)
95{
96#ifdef __XENO__
97    RT_MUTEX_INFO info;
98    int status=rt_mutex_inquire(&mutex_rt,&info);
99    if(status!=0) mutex->Err("erreur rt_mutex_inquire (%s)\n",strerror_r(-status, errorMsg, sizeof(errorMsg)));
100    if(info.locked>0) return true;
101    return false;
102#else
103    return flag_locked;
104#endif
105}
106*/
Note: See TracBrowser for help on using the repository browser.