ts_RWLock.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 //----------------------------------------------------------------------
23 #include <boost/test/unit_test.hpp>
24 #include "icl_core_thread/Thread.h"
25 #include "icl_core_thread/RWLock.h"
26 #include "icl_core/os_time.h"
27 
28 using icl_core::TimeSpan;
33 
34 
35 class RWLockTestThread : public Thread
36 {
37 public:
38  RWLockTestThread(RWLock *rwlock, bool read_lock)
39  : Thread("Test Thread"),
40  m_has_run(false),
41  m_read_lock(read_lock),
42  m_rwlock(rwlock)
43  { }
44 
46  { }
47 
48  virtual void run()
49  {
50  if (m_read_lock)
51  {
52  BOOST_CHECK(m_rwlock->tryReadLock());
53  BOOST_CHECK(m_rwlock->readLock(icl_core::TimeSpan(1, 0)));
55  }
56  else
57  {
58  BOOST_CHECK(!m_rwlock->tryReadLock());
59  BOOST_CHECK(!m_rwlock->readLock(icl_core::TimeSpan(1, 0)));
61  }
62 
63  BOOST_CHECK(!m_rwlock->tryWriteLock());
64  BOOST_CHECK(!m_rwlock->writeLock(icl_core::TimeSpan(1, 0)));
66 
67  m_has_run = true;
68  }
69 
70  bool hasRun() const { return m_has_run; }
71 
72 private:
73  bool m_has_run;
76 };
77 
78 
79 BOOST_AUTO_TEST_SUITE(ts_RWLock)
80 
82 {
83  RWLock rwlock;
84  BOOST_CHECK(rwlock.readLock());
85  rwlock.unlock();
86 }
87 
89 {
90  RWLock rwlock;
91  BOOST_CHECK(rwlock.tryReadLock());
92  rwlock.unlock();
93 }
94 
95 BOOST_AUTO_TEST_CASE(ReadLockAbsoluteTimeout)
96 {
97  RWLock rwlock;
98  BOOST_CHECK(rwlock.readLock(TimeStamp::now() + TimeSpan(1, 0)));
99  rwlock.unlock();
100 }
101 
102 BOOST_AUTO_TEST_CASE(ReadLockRelativeTimeout)
103 {
104  RWLock rwlock;
105  BOOST_CHECK(rwlock.readLock(TimeSpan(1, 0)));
106  rwlock.unlock();
107 }
108 
110 {
111  RWLock rwlock;
112  BOOST_CHECK(rwlock.writeLock());
113  rwlock.unlock();
114 }
115 
116 BOOST_AUTO_TEST_CASE(TryWriteLock)
117 {
118  RWLock rwlock;
119  BOOST_CHECK(rwlock.tryWriteLock());
120  rwlock.unlock();
121 }
122 
123 BOOST_AUTO_TEST_CASE(WriteLockAbsoluteTimeout)
124 {
125  RWLock rwlock;
126  BOOST_CHECK(rwlock.writeLock(TimeStamp::now() + TimeSpan(1, 0)));
127  rwlock.unlock();
128 }
129 
130 BOOST_AUTO_TEST_CASE(WriteLockRelativeTimeout)
131 {
132  RWLock rwlock;
133  BOOST_CHECK(rwlock.writeLock(TimeSpan(1, 0)));
134  rwlock.unlock();
135 }
136 
137 BOOST_AUTO_TEST_CASE(ReadAndWriteLockWhileReadLock)
138 {
139  RWLock rwlock;
140 
141  BOOST_CHECK(rwlock.readLock());
142 
143  RWLockTestThread *testthread = new RWLockTestThread(&rwlock, true);
144 
145  testthread->start();
146  testthread->join();
147  BOOST_CHECK(testthread->hasRun());
148 
149  delete testthread;
150 
151  rwlock.unlock();
152 }
153 
154 BOOST_AUTO_TEST_CASE(ReadAndWriteLockWhileWriteLock)
155 {
156  RWLock rwlock;
157 
158  BOOST_CHECK(rwlock.writeLock());
159 
160  RWLockTestThread *testthread = new RWLockTestThread(&rwlock, false);
161 
162  testthread->start();
163  testthread->join();
164  BOOST_CHECK(testthread->hasRun());
165 
166  delete testthread;
167 
168  rwlock.unlock();
169 }
170 
171 BOOST_AUTO_TEST_SUITE_END()
virtual void run()
Definition: ts_RWLock.cpp:48
Represents absolute times.
Definition: TimeStamp.h:61
RWLockTestThread(RWLock *rwlock, bool read_lock)
Definition: ts_RWLock.cpp:38
static TimeStamp now()
Definition: TimeStamp.cpp:111
BOOST_AUTO_TEST_CASE(ReadLock)
Definition: ts_RWLock.cpp:81
virtual ~RWLockTestThread()
Definition: ts_RWLock.cpp:45
Contains icl_core::thread::RWLock.
RWLock * m_rwlock
Definition: ts_RWLock.cpp:75
bool hasRun() const
Definition: ts_RWLock.cpp:70
Repesents absolute times.
Definition: TimeSpan.h:46
Contains icl_core::thread::Thread.
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