1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // |
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) |
6 | // |
7 | // See http://www.boost.org/libs/interprocess for documentation. |
8 | // |
9 | ////////////////////////////////////////////////////////////////////////////// |
10 | // Copyright (C) 2001-2003 |
11 | // William E. Kempf |
12 | // |
13 | // Permission to use, copy, modify, distribute and sell this software |
14 | // and its documentation for any purpose is hereby granted without fee, |
15 | // provided that the above copyright notice appear in all copies and |
16 | // that both that copyright notice and this permission notice appear |
17 | // in supporting documentation. William E. Kempf makes no representations |
18 | // about the suitability of this software for any purpose. |
19 | // It is provided "as is" without express or implied warranty. |
20 | |
21 | #ifndef BOOST_INTERPROCESS_TEST_SEMAPHORE_TEST_TEMPLATE_HEADER |
22 | #define |
23 | |
24 | #include <boost/interprocess/detail/config_begin.hpp> |
25 | #include <boost/interprocess/exceptions.hpp> |
26 | #include <boost/interprocess/timed_utils.hpp> |
27 | #include "named_creation_template.hpp" |
28 | #include "mutex_test_template.hpp" |
29 | |
30 | #include <boost/interprocess/exceptions.hpp> |
31 | #include "named_creation_template.hpp" |
32 | #include "mutex_test_template.hpp" |
33 | |
34 | namespace boost { namespace interprocess { namespace test { |
35 | |
36 | static const std::size_t SemCount = 1; |
37 | static const std::size_t RecSemCount = 100; |
38 | |
39 | //This wrapper is necessary to plug this class |
40 | //in named creation tests and interprocess_mutex tests |
41 | template<class Semaphore> |
42 | class semaphore_test_wrapper |
43 | : public Semaphore |
44 | { |
45 | public: |
46 | semaphore_test_wrapper() |
47 | : Semaphore(SemCount) |
48 | {} |
49 | |
50 | void lock() |
51 | { this->wait(); } |
52 | |
53 | bool try_lock() |
54 | { return this->try_wait(); } |
55 | |
56 | template<class TimePoint> |
57 | bool timed_lock(const TimePoint &pt) |
58 | { return this->timed_wait(pt); } |
59 | |
60 | template<class TimePoint> bool try_lock_until(const TimePoint &abs_time) |
61 | { return this->timed_lock(abs_time); } |
62 | |
63 | template<class Duration> bool try_lock_for(const Duration &dur) |
64 | { return this->timed_lock(boost::interprocess::ipcdetail::duration_to_ustime(dur)); } |
65 | |
66 | void unlock() |
67 | { this->post(); } |
68 | |
69 | protected: |
70 | semaphore_test_wrapper(std::size_t initial_count) |
71 | : Semaphore(unsigned(initial_count)) |
72 | {} |
73 | }; |
74 | |
75 | //This wrapper is necessary to plug this class |
76 | //in recursive tests |
77 | template<class Semaphore> |
78 | class recursive_semaphore_test_wrapper |
79 | : public semaphore_test_wrapper<Semaphore> |
80 | { |
81 | public: |
82 | recursive_semaphore_test_wrapper() |
83 | : semaphore_test_wrapper<Semaphore>(RecSemCount) |
84 | {} |
85 | }; |
86 | |
87 | template <class Semaphore> |
88 | inline int test_all_semaphore() |
89 | { |
90 | using namespace boost::interprocess; |
91 | |
92 | test::test_all_lock<semaphore_test_wrapper<Semaphore> >(); |
93 | test::test_all_recursive_lock<recursive_semaphore_test_wrapper<Semaphore> >(); |
94 | test::test_all_mutex<semaphore_test_wrapper<Semaphore> >(); |
95 | return 0; |
96 | } |
97 | |
98 | }}} //namespace boost { namespace interprocess { namespace test { |
99 | |
100 | #include <boost/interprocess/detail/config_end.hpp> |
101 | |
102 | #endif //BOOST_INTERPROCESS_TEST_SEMAPHORE_TEST_TEMPLATE_HEADER |
103 | |