Changeset 126 in flair-src for trunk/lib/FlairCore/src


Ignore:
Timestamp:
Jan 17, 2017, 10:47:02 AM (8 years ago)
Author:
Thomas Fuhrmann
Message:

Add timed semaphore and bool return

Location:
trunk/lib/FlairCore/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FlairCore/src/Semaphore.cpp

    r118 r126  
    3131Semaphore::~Semaphore() { delete pimpl_; }
    3232
    33 void Semaphore::GetSemaphore(void) const { pimpl_->GetSemaphore(); }
     33bool Semaphore::GetSemaphore(Time timeout) const { return pimpl_->GetSemaphore(timeout); }
    3434
    35 void Semaphore::ReleaseSemaphore(void) const { pimpl_->ReleaseSemaphore(); }
     35bool Semaphore::ReleaseSemaphore(void) const { return pimpl_->ReleaseSemaphore(); }
    3636
    3737} // end namespace core
  • trunk/lib/FlairCore/src/Semaphore.h

    r118 r126  
    5151  *
    5252  * Lock the semaphore.
    53   *
     53  *
     54  * \param timeout The timeout, in nanoseconds.
    5455  */
    55   void GetSemaphore(void) const;
     56  bool GetSemaphore(Time timeout = TIME_INFINITE) const;
    5657
    5758  /*!
     
    6162  *
    6263  */
    63   void ReleaseSemaphore(void) const;
     64  bool ReleaseSemaphore(void) const;
    6465
    6566private:
  • trunk/lib/FlairCore/src/Semaphore_impl.cpp

    r118 r126  
    2020
    2121#include <string.h>
     22#include <time.h>
    2223
    2324using std::string;
     
    4849}
    4950
    50 void Semaphore_impl::GetSemaphore(void) {
     51bool Semaphore_impl::GetSemaphore(Time timeout) {
    5152  int status;
     53  bool returnValue = true;
    5254#ifdef __XENO__
    53   status = rt_sem_p(&semaphore, TM_INFINITE);
     55  status = rt_sem_p(&semaphore, timeout);
    5456#else
    55   status = sem_wait(&semaphore);
     57  if (timeout != 0) {
     58    struct timespec semTimeout;
     59    clock_gettime(CLOCK_REALTIME, &semTimeout);
     60    semTimeout.tv_sec += timeout / 1000000000ULL;
     61    semTimeout.tv_nsec += timeout % 1000000000ULL;
     62    if (semTimeout.tv_nsec >= 1000000000ULL) {
     63        semTimeout.tv_sec++;
     64        semTimeout.tv_nsec -= 1000000000ULL;
     65    }
     66    status = sem_timedwait(&semaphore, &semTimeout);
     67  } else {
     68    status = sem_wait(&semaphore);
     69  }
    5670#endif
    57   if (status != 0)
    58     self->Err("error getting the semaphore (%s)\n", strerror(-status));
     71        if (status != 0) {
     72                if (errno == ETIMEDOUT) {
     73                        self->Warn("warning : semaphore timedout\n");   
     74                } else {
     75                    self->Err("error getting the semaphore (%s)\n", strerror(-status));
     76                }
     77    returnValue = false;
     78  }
     79  return returnValue;
    5980}
    6081
    61 void Semaphore_impl::ReleaseSemaphore(void) {
     82bool Semaphore_impl::ReleaseSemaphore(void) {
    6283  int status;
     84  bool returnValue = true;
    6385#ifdef __XENO__
    6486  status = rt_sem_v(&semaphore);
     
    6688  status = sem_post(&semaphore);
    6789#endif
    68   if (status != 0)
     90  if (status != 0) {
    6991    self->Err("error releasing the semaphore (%s)\n", strerror(-status));
     92    returnValue = false;
     93  }
     94  return returnValue;
    7095}
Note: See TracChangeset for help on using the changeset viewer.