MutexImplPosix.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
24 
25 #include <pthread.h>
26 #include <icl_core/os_time.h>
27 
28 #include "icl_core_thread/Common.h"
29 
30 namespace icl_core {
31 namespace thread {
32 
34  : m_mutex(NULL)
35 {
36  m_mutex = new pthread_mutex_t;
37  pthread_mutexattr_t mutex_attr;
38  pthread_mutexattr_init(&mutex_attr);
39  pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
40  pthread_mutex_init(m_mutex, &mutex_attr);
41  pthread_mutexattr_destroy(&mutex_attr);
42 }
43 
45 {
46  if (m_mutex)
47  {
48  pthread_mutex_destroy(m_mutex);
49  delete m_mutex;
50  m_mutex = NULL;
51  }
52 }
53 
55 {
56  return pthread_mutex_lock(m_mutex) == 0;
57 }
58 
59 bool MutexImplPosix::lock(const ::icl_core::TimeStamp& timeout)
60 {
61 #ifdef _SYSTEM_DARWIN_
62  int ret = pthread_mutex_trylock(m_mutex);
63  while ((ret != 0) && ((timeout > icl_core::TimeStamp::now())))
64  {
65  // one microsecond
67  ret = pthread_mutex_trylock(m_mutex);
68  }
69  return ret == 0;
70 #else
71  struct timespec timeout_spec = timeout.timespec();
72  int ret = pthread_mutex_timedlock(m_mutex, &timeout_spec);
73  return (ret == 0);
74 #endif
75 }
76 
77 bool MutexImplPosix::lock(const ::icl_core::TimeSpan& timeout)
78 {
79  return lock(impl::getAbsoluteTimeout(timeout));
80 }
81 
83 {
84  int ret = pthread_mutex_trylock(m_mutex);
85  return (ret == 0);
86 }
87 
89 {
90  pthread_mutex_unlock(m_mutex);
91 }
92 
93 }
94 }
TimeStamp getAbsoluteTimeout(const TimeSpan &timeout_relative)
Definition: Common.h:59
static TimeStamp now()
Definition: TimeStamp.cpp:111
int usleep(unsigned long useconds)
Definition: os_time.h:56
Contains icl_core::thread::tMutex.
const TimeSpan timeout(1, 0)
Contains global functions for time manipulation, encapsulated into the icl_core::os namespace...
Contains icl_core::thread::MutexImplPosix.


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58