RWLockImplPosix.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_rwlock(NULL)
35 {
36  m_rwlock = new pthread_rwlock_t;
37  pthread_rwlock_init(m_rwlock, NULL);
38 }
39 
41 {
42  if (m_rwlock)
43  {
44  pthread_rwlock_destroy(m_rwlock);
45  delete m_rwlock;
46  m_rwlock = NULL;
47  }
48 }
49 
51 {
52  return pthread_rwlock_rdlock(m_rwlock) == 0;
53 }
54 
55 // ReadLock with absolute timeout
56 bool RWLockImplPosix::readLock(const ::icl_core::TimeStamp& timeout)
57 {
58 #ifdef _SYSTEM_DARWIN_
59  int ret = pthread_rwlock_tryrdlock(m_rwlock);
60  while ((ret != 0) && ((timeout > icl_core::TimeStamp::now())))
61  {
62  // one microsecond
64  ret = pthread_rwlock_tryrdlock(m_rwlock);
65  }
66  return (ret == 0);
67 #else
68  struct timespec timeout_timespec = timeout.timespec();
69  int ret = pthread_rwlock_timedrdlock(m_rwlock, &timeout_timespec);
70  return (ret == 0);
71 #endif
72 }
73 
74 // ReadLock with relative timeout
75 bool RWLockImplPosix::readLock(const ::icl_core::TimeSpan& timeout)
76 {
77  return readLock(impl::getAbsoluteTimeout(timeout));
78 }
79 
81 {
82  bool ret = pthread_rwlock_tryrdlock(m_rwlock);
83  return (ret == 0);
84 }
85 
87 {
88  return pthread_rwlock_wrlock(m_rwlock) == 0;
89 }
90 
91 // WriteLock with absolute timeout
92 bool RWLockImplPosix::writeLock(const ::icl_core::TimeStamp& timeout)
93 {
94 #ifdef _SYSTEM_DARWIN_
95  int ret = pthread_rwlock_trywrlock(m_rwlock);
96  while ((ret != 0) && ((timeout > icl_core::TimeStamp::now())))
97  {
98  // one microsecond
100  ret = pthread_rwlock_trywrlock(m_rwlock);
101  }
102  return (ret == 0);
103 #else
104  struct timespec timeout_timespec = timeout.timespec();
105  bool ret = pthread_rwlock_timedwrlock(m_rwlock, &timeout_timespec);
106  return (ret == 0);
107 #endif
108 }
109 
110 // WriteLock with relative timeout
111 bool RWLockImplPosix::writeLock(const ::icl_core::TimeSpan& timeout)
112 {
113  return writeLock(impl::getAbsoluteTimeout(timeout));
114 }
115 
117 {
118  bool ret = pthread_rwlock_trywrlock(m_rwlock);
119  return (ret == 0);
120 }
121 
123 {
124  pthread_rwlock_unlock(m_rwlock);
125 }
126 
127 }
128 }
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::RWLockImplPosix.
Contains icl_core::thread::tMutex.
const TimeSpan timeout(1, 0)
Contains global functions for time manipulation, encapsulated into the icl_core::os namespace...


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