source: flair-src/trunk/lib/FlairCore/src/ConditionVariable_impl.cpp @ 330

Last change on this file since 330 was 330, checked in by Sanahuja Guillaume, 2 years ago

use less bandwidth in vprnlite

File size: 2.7 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:    2014/02/07
6//  filename:   ConditionVariable_impl.cpp
7//
8//  author:     Guillaume Sanahuja
9//              Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11//  version:    $Id: $
12//
13//  purpose:    Class defining a condition variable
14//
15//
16/*********************************************************************/
17
18#include "ConditionVariable_impl.h"
19#include "ConditionVariable.h"
20#include "Mutex_impl.h"
21#include <string.h>
22
23using std::string;
24using namespace flair::core;
25
26ConditionVariable_impl::ConditionVariable_impl(ConditionVariable *self) {
27  this->self = self;
28  int status;
29
30#ifdef __XENO__
31  status = rt_cond_create(&m_ResumeCond, NULL);
32#else
33  status = pthread_cond_init(&m_ResumeCond, 0);
34#endif
35  if (status != 0) {
36                char errorMsg[256];
37    self->Err("error creating condition variable (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
38        }
39}
40
41ConditionVariable_impl::~ConditionVariable_impl() {
42  int status;
43
44#ifdef __XENO__
45  status = rt_cond_delete(&m_ResumeCond);
46#else
47  status = pthread_cond_destroy(&m_ResumeCond);
48#endif
49  if (status != 0) {
50                char errorMsg[256];
51    self->Err("error destroying condition variable (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
52        }
53}
54
55bool ConditionVariable_impl::CondWait(Time timeout) {
56  int status;
57  if(timeout==TIME_INFINITE) {
58#ifdef __XENO__
59    status=rt_cond_wait(&m_ResumeCond, &self->Mutex::pimpl_->mutex, TM_INFINITE);
60#else
61    status=pthread_cond_wait(&m_ResumeCond, &self->Mutex::pimpl_->mutex);
62#endif
63    if(status != 0) {
64      char errorMsg[256];
65      self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
66      return false;
67    }
68    return true;
69  } else {
70    return CondWaitUntil(GetTime()+timeout);
71  }
72}
73
74bool ConditionVariable_impl::CondWaitUntil(Time date) {
75  int status;
76#ifdef __XENO__
77  status = rt_cond_wait_until(&m_ResumeCond, &self->Mutex::pimpl_->mutex, date);
78#else
79  struct timespec abstime;
80  abstime.tv_sec = date / 1000000000;
81  abstime.tv_nsec = date % 1000000000;
82  status = pthread_cond_timedwait(&m_ResumeCond, &self->Mutex::pimpl_->mutex,
83                                  &abstime);
84#endif
85  if (status == 0) return true;
86  if (status != ETIMEDOUT) {
87                char errorMsg[256];
88    self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
89        }
90  return false;
91}
92
93void ConditionVariable_impl::CondSignal(void) {
94  int status;
95#ifdef __XENO__
96  status = rt_cond_signal(&m_ResumeCond);
97#else
98  status = pthread_cond_signal(&m_ResumeCond);
99#endif
100        if (status != 0) {
101                char errorMsg[256];
102    self->Err("error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
103        }
104}
Note: See TracBrowser for help on using the repository browser.