ChimeraTK-cppext
01.05.02
Loading...
Searching...
No Matches
testSemaphore.cc
Go to the documentation of this file.
1
// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2
// SPDX-License-Identifier: LGPL-3.0-or-later
3
#define BOOST_TEST_MODULE test_semaphore
4
#include <boost/test/included/unit_test.hpp>
5
using namespace
boost::unit_test_framework;
6
7
#include "
semaphore.hpp
"
8
#include "
threadsafe_unit_test.hpp
"
9
10
#include <thread>
11
12
BOOST_AUTO_TEST_SUITE(TestLatch)
13
14
/*********************************************************************************************************************/
15
16
BOOST_AUTO_TEST_CASE
(singleThreaded) {
17
std::cout <<
"singleThreaded"
<< std::endl;
18
19
cppext::semaphore
sem;
20
21
// unlock the semaphore
22
BOOST_CHECK(sem.
is_ready
() ==
false
);
23
sem.
unlock
();
24
// now semaphore should be ready and wait() should not block
25
BOOST_CHECK(sem.
is_ready
() ==
true
);
26
sem.
wait_and_reset
();
27
BOOST_CHECK(sem.
is_ready
() ==
false
);
28
// test reusing the semaphore
29
sem.
unlock
();
30
BOOST_CHECK(sem.
is_ready
() ==
true
);
31
sem.
wait_and_reset
();
32
BOOST_CHECK(sem.
is_ready
() ==
false
);
33
sem.
unlock
();
34
BOOST_CHECK(sem.
is_ready
() ==
true
);
35
}
36
37
/*********************************************************************************************************************/
38
39
// one thread waiting and another counting
40
BOOST_AUTO_TEST_CASE
(multiThreaded) {
41
std::cout <<
"multiThreaded"
<< std::endl;
42
43
cppext::semaphore
sem;
44
45
std::thread waiting([&sem] {
46
BOOST_CHECK_TS
(sem.
is_ready
() ==
false
);
47
sem.
wait_and_reset
();
48
});
// end waiting thread
49
50
std::thread down_counting([&sem] {
51
usleep(100000);
52
sem.
unlock
();
53
});
// end down_counting thread
54
55
down_counting.join();
56
waiting.join();
57
BOOST_CHECK_TS
(sem.
is_ready
() ==
false
);
58
}
59
60
/*********************************************************************************************************************/
61
62
BOOST_AUTO_TEST_SUITE_END()
cppext::semaphore
Definition
semaphore.hpp:103
cppext::semaphore::is_ready
bool is_ready()
Definition
semaphore.hpp:114
cppext::semaphore::unlock
void unlock()
Definition
semaphore.hpp:107
cppext::semaphore::wait_and_reset
void wait_and_reset()
Definition
semaphore.hpp:119
semaphore.hpp
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(singleThreaded)
Definition
testSemaphore.cc:16
threadsafe_unit_test.hpp
BOOST_CHECK_TS
#define BOOST_CHECK_TS(condition)
Definition
threadsafe_unit_test.hpp:10
sources
ChimeraTK-cppext
tests
testSemaphore.cc
Generated by
1.9.8