1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #include <boost/interprocess/detail/config_begin.hpp>
12 #include <boost/interprocess/sync/named_semaphore.hpp>
13 #include <boost/interprocess/detail/interprocess_tester.hpp>
14 #include <boost/interprocess/exceptions.hpp>
15 #include <boost/date_time/posix_time/posix_time_types.hpp>
16 #include "named_creation_template.hpp"
17 #include "mutex_test_template.hpp"
19 #include "get_process_id_name.hpp"
21 using namespace boost::interprocess
;
23 static const std::size_t RecSemCount
= 100;
24 static const char * SemName
= test::get_process_id_name();
26 //This wrapper is necessary to plug this class
28 class lock_test_wrapper
29 : public named_semaphore
33 lock_test_wrapper(create_only_t
, const char *name
, unsigned int count
= 1)
34 : named_semaphore(create_only
, name
, count
)
37 lock_test_wrapper(open_only_t
, const char *name
)
38 : named_semaphore(open_only
, name
)
41 lock_test_wrapper(open_or_create_t
, const char *name
, unsigned int count
= 1)
42 : named_semaphore(open_or_create
, name
, count
)
52 { return this->try_wait(); }
54 bool timed_lock(const boost::posix_time::ptime
&pt
)
55 { return this->timed_wait(pt
); }
61 //This wrapper is necessary to plug this class
63 class recursive_test_wrapper
64 : public lock_test_wrapper
67 recursive_test_wrapper(create_only_t
, const char *name
)
68 : lock_test_wrapper(create_only
, name
, RecSemCount
)
71 recursive_test_wrapper(open_only_t
, const char *name
)
72 : lock_test_wrapper(open_only
, name
)
75 recursive_test_wrapper(open_or_create_t
, const char *name
)
76 : lock_test_wrapper(open_or_create
, name
, RecSemCount
)
80 bool test_named_semaphore_specific()
84 named_semaphore
sem(create_only
, SemName
, 3);
87 named_semaphore
sem(open_only
, SemName
);
88 BOOST_INTERPROCESS_CHECK(sem
.try_wait() == true);
89 BOOST_INTERPROCESS_CHECK(sem
.try_wait() == true);
90 BOOST_INTERPROCESS_CHECK(sem
.try_wait() == true);
91 BOOST_INTERPROCESS_CHECK(sem
.try_wait() == false);
95 named_semaphore
sem(open_only
, SemName
);
96 BOOST_INTERPROCESS_CHECK(sem
.try_wait() == true);
97 BOOST_INTERPROCESS_CHECK(sem
.try_wait() == false);
100 named_semaphore::remove(SemName
);
107 named_semaphore::remove(SemName
);
108 test::test_named_creation
< test::named_sync_creation_test_wrapper
<lock_test_wrapper
> >();
109 test::test_all_lock
< test::named_sync_wrapper
<lock_test_wrapper
> >();
110 test::test_all_mutex
<test::named_sync_wrapper
<lock_test_wrapper
> >();
111 test::test_all_recursive_lock
<test::named_sync_wrapper
<recursive_test_wrapper
> >();
112 test_named_semaphore_specific();
114 catch(std::exception
&ex
){
115 named_semaphore::remove(SemName
);
116 std::cout
<< ex
.what() << std::endl
;
119 named_semaphore::remove(SemName
);
123 #include <boost/interprocess/detail/config_end.hpp>