ts_Mutex.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/Mutex.h>
25 #include <icl_core_thread/Thread.h>
26 
27 using icl_core::TimeSpan;
31 
32 const TimeSpan timeout(1, 0);
33 
34 class MutexTestThread : public Thread
35 {
36 public:
38  Thread("Mutex Test Thread"),
39  m_has_run(false),
40  m_mutex(mutex)
41  {
42  }
43 
44  virtual ~MutexTestThread()
45  {
46  }
47 
48  virtual void run()
49  {
50  BOOST_CHECK(!m_mutex->tryLock());
51  BOOST_CHECK(!m_mutex->lock(timeout));
52  BOOST_CHECK(!m_mutex->lock(TimeStamp::now() + timeout));
53 
54  m_has_run = true;
55  }
56 
57  bool hasRun() const { return m_has_run; }
58 
59 private:
60  bool m_has_run;
62 };
63 
64 BOOST_AUTO_TEST_SUITE(ts_Mutex)
65 
67 {
68  Mutex mutex;
69  BOOST_CHECK(mutex.lock());
70  mutex.unlock();
71 }
72 
73 BOOST_AUTO_TEST_CASE(MutexTryLock)
74 {
75  Mutex mutex;
76  BOOST_CHECK(mutex.tryLock());
77  mutex.unlock();
78 }
79 
80 BOOST_AUTO_TEST_CASE(MutexLockAbsoluteTimeout)
81 {
82  Mutex mutex;
83  BOOST_CHECK(mutex.lock(TimeStamp::now() + TimeSpan(1, 0)));
84  mutex.unlock();
85 }
86 
87 BOOST_AUTO_TEST_CASE(MutexLockRelativeTimeout)
88 {
89  Mutex mutex;
90  BOOST_CHECK(mutex.lock(TimeSpan(1, 0)));
91  mutex.unlock();
92 }
93 
94 BOOST_AUTO_TEST_CASE(MultiThreadMutexTest)
95 {
96  Mutex mutex;
97 
98  BOOST_CHECK(mutex.lock());
99 
100  MutexTestThread *thread = new MutexTestThread(&mutex);
101 
102  thread->start();
103  thread->join();
104  BOOST_CHECK(thread->hasRun());
105 
106  delete thread;
107 
108  mutex.unlock();
109 }
110 
111 BOOST_AUTO_TEST_SUITE_END()
Represents absolute times.
Definition: TimeStamp.h:61
virtual ~MutexTestThread()
Definition: ts_Mutex.cpp:44
BOOST_AUTO_TEST_CASE(MutexLock)
Definition: ts_Mutex.cpp:66
bool hasRun() const
Definition: ts_Mutex.cpp:57
MutexTestThread(Mutex *mutex)
Definition: ts_Mutex.cpp:37
Mutex * m_mutex
Definition: ts_Mutex.cpp:61
const TimeSpan timeout(1, 0)
Contains icl_core::thread::Mutex.
Repesents absolute times.
Definition: TimeSpan.h:46
virtual void run()
Definition: ts_Mutex.cpp:48
Contains icl_core::thread::Thread.


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