Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
src
icl_core
src
ts
icl_core_thread
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
;
28
using
icl_core::TimeStamp
;
29
using
icl_core::thread::Mutex
;
30
using
icl_core::thread::Thread
;
31
32
const
TimeSpan
timeout
(1, 0);
33
34
class
MutexTestThread
:
public
Thread
35
{
36
public
:
37
MutexTestThread
(
Mutex
*mutex) :
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
;
61
Mutex
*
m_mutex
;
62
};
63
64
BOOST_AUTO_TEST_SUITE(ts_Mutex)
65
66
BOOST_AUTO_TEST_CASE
(MutexLock)
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()
MutexTestThread
Definition:
ts_Mutex.cpp:34
icl_core::TimeStamp
Represents absolute times.
Definition:
TimeStamp.h:61
icl_core::thread::Mutex::tryLock
bool tryLock()
Definition:
Mutex.cpp:116
icl_core::thread::Thread
Definition:
icl_core_thread/Thread.h:77
MutexTestThread::~MutexTestThread
virtual ~MutexTestThread()
Definition:
ts_Mutex.cpp:44
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(MutexLock)
Definition:
ts_Mutex.cpp:66
MutexTestThread::hasRun
bool hasRun() const
Definition:
ts_Mutex.cpp:57
MutexTestThread::MutexTestThread
MutexTestThread(Mutex *mutex)
Definition:
ts_Mutex.cpp:37
MutexTestThread::m_mutex
Mutex * m_mutex
Definition:
ts_Mutex.cpp:61
icl_core::thread::Mutex
Definition:
Mutex.h:47
timeout
const TimeSpan timeout(1, 0)
Mutex.h
Contains icl_core::thread::Mutex.
icl_core::TimeSpan
Repesents absolute times.
Definition:
TimeSpan.h:46
MutexTestThread::run
virtual void run()
Definition:
ts_Mutex.cpp:48
Thread.h
Contains icl_core::thread::Thread.
icl_core::thread::Mutex::lock
bool lock()
Definition:
Mutex.cpp:92
icl_core::thread::Mutex::unlock
void unlock()
Definition:
Mutex.cpp:124
MutexTestThread::m_has_run
bool m_has_run
Definition:
ts_Mutex.cpp:60
fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58