Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/thread/test/test_mutex.cpp @ 20

Last change on this file since 20 was 12, checked in by landauf, 18 years ago

added boost

File size: 6.4 KB
Line 
1// Copyright (C) 2001-2003
2// William E. Kempf
3//
4// Permission to use, copy, modify, distribute and sell this software
5// and its documentation for any purpose is hereby granted without fee,
6// provided that the above copyright notice appear in all copies and
7// that both that copyright notice and this permission notice appear
8// in supporting documentation.  William E. Kempf makes no representations
9// about the suitability of this software for any purpose.
10// It is provided "as is" without express or implied warranty.
11
12#include <boost/thread/detail/config.hpp>
13
14#include <boost/thread/mutex.hpp>
15#include <boost/thread/recursive_mutex.hpp>
16#include <boost/thread/xtime.hpp>
17#include <boost/thread/condition.hpp>
18
19#include <boost/test/unit_test.hpp>
20
21#define DEFAULT_EXECUTION_MONITOR_TYPE execution_monitor::use_sleep_only
22#include <libs/thread/test/util.inl>
23
24template <typename M>
25struct test_lock
26{
27    typedef M mutex_type;
28    typedef typename M::scoped_lock lock_type;
29
30    void operator()()
31    {
32        mutex_type mutex;
33        boost::condition condition;
34
35        // Test the lock's constructors.
36        {
37            lock_type lock(mutex, false);
38            BOOST_CHECK(!lock);
39        }
40        lock_type lock(mutex);
41        BOOST_CHECK(lock ? true : false);
42
43        // Construct and initialize an xtime for a fast time out.
44        boost::xtime xt = delay(0, 100);
45
46        // Test the lock and the mutex with condition variables.
47        // No one is going to notify this condition variable.  We expect to
48        // time out.
49        BOOST_CHECK(!condition.timed_wait(lock, xt));
50        BOOST_CHECK(lock ? true : false);
51
52        // Test the lock and unlock methods.
53        lock.unlock();
54        BOOST_CHECK(!lock);
55        lock.lock();
56        BOOST_CHECK(lock ? true : false);
57    }
58};
59
60template <typename M>
61struct test_trylock
62{
63    typedef M mutex_type;
64    typedef typename M::scoped_try_lock try_lock_type;
65
66    void operator()()
67    {
68        mutex_type mutex;
69        boost::condition condition;
70
71        // Test the lock's constructors.
72        {
73            try_lock_type lock(mutex);
74            BOOST_CHECK(lock ? true : false);
75        }
76        {
77            try_lock_type lock(mutex, false);
78            BOOST_CHECK(!lock);
79        }
80        try_lock_type lock(mutex, true);
81        BOOST_CHECK(lock ? true : false);
82
83        // Construct and initialize an xtime for a fast time out.
84        boost::xtime xt = delay(0, 100);
85
86        // Test the lock and the mutex with condition variables.
87        // No one is going to notify this condition variable.  We expect to
88        // time out.
89        BOOST_CHECK(!condition.timed_wait(lock, xt));
90        BOOST_CHECK(lock ? true : false);
91
92        // Test the lock, unlock and trylock methods.
93        lock.unlock();
94        BOOST_CHECK(!lock);
95        lock.lock();
96        BOOST_CHECK(lock ? true : false);
97        lock.unlock();
98        BOOST_CHECK(!lock);
99        BOOST_CHECK(lock.try_lock());
100        BOOST_CHECK(lock ? true : false);
101    }
102};
103
104template <typename M>
105struct test_timedlock
106{
107    typedef M mutex_type;
108    typedef typename M::scoped_timed_lock timed_lock_type;
109
110    void operator()()
111    {
112        mutex_type mutex;
113        boost::condition condition;
114
115        // Test the lock's constructors.
116        {
117            // Construct and initialize an xtime for a fast time out.
118            boost::xtime xt = delay(0, 100);
119
120            timed_lock_type lock(mutex, xt);
121            BOOST_CHECK(lock ? true : false);
122        }
123        {
124            timed_lock_type lock(mutex, false);
125            BOOST_CHECK(!lock);
126        }
127        timed_lock_type lock(mutex, true);
128        BOOST_CHECK(lock ? true : false);
129
130        // Construct and initialize an xtime for a fast time out.
131        boost::xtime xt = delay(0, 100);
132
133        // Test the lock and the mutex with condition variables.
134        // No one is going to notify this condition variable.  We expect to
135        // time out.
136        BOOST_CHECK(!condition.timed_wait(lock, xt));
137        BOOST_CHECK(lock ? true : false);
138        BOOST_CHECK(in_range(xt));
139
140        // Test the lock, unlock and timedlock methods.
141        lock.unlock();
142        BOOST_CHECK(!lock);
143        lock.lock();
144        BOOST_CHECK(lock ? true : false);
145        lock.unlock();
146        BOOST_CHECK(!lock);
147        xt = delay(0, 100);
148        BOOST_CHECK(lock.timed_lock(xt));
149        BOOST_CHECK(lock ? true : false);
150    }
151};
152
153template <typename M>
154struct test_recursive_lock
155{
156    typedef M mutex_type;
157    typedef typename M::scoped_lock lock_type;
158
159    void operator()()
160    {
161        mutex_type mx;
162        lock_type lock1(mx);
163        lock_type lock2(mx);
164    }
165};
166
167void do_test_mutex()
168{
169    test_lock<boost::mutex>()();
170}
171
172void test_mutex()
173{
174    timed_test(&do_test_mutex, 3);
175}
176
177void do_test_try_mutex()
178{
179    test_lock<boost::try_mutex>()();
180    test_trylock<boost::try_mutex>()();
181}
182
183void test_try_mutex()
184{
185    timed_test(&do_test_try_mutex, 3);
186}
187
188void do_test_timed_mutex()
189{
190    test_lock<boost::timed_mutex>()();
191    test_trylock<boost::timed_mutex>()();
192    test_timedlock<boost::timed_mutex>()();
193}
194
195void test_timed_mutex()
196{
197    timed_test(&do_test_timed_mutex, 3);
198}
199
200void do_test_recursive_mutex()
201{
202    test_lock<boost::recursive_mutex>()();
203    test_recursive_lock<boost::recursive_mutex>()();
204}
205
206void test_recursive_mutex()
207{
208    timed_test(&do_test_recursive_mutex, 3);
209}
210
211void do_test_recursive_try_mutex()
212{
213    test_lock<boost::recursive_try_mutex>()();
214    test_trylock<boost::recursive_try_mutex>()();
215    test_recursive_lock<boost::recursive_try_mutex>()();
216}
217
218void test_recursive_try_mutex()
219{
220    timed_test(&do_test_recursive_try_mutex, 3);
221}
222
223void do_test_recursive_timed_mutex()
224{
225    test_lock<boost::recursive_timed_mutex>()();
226    test_trylock<boost::recursive_timed_mutex>()();
227    test_timedlock<boost::recursive_timed_mutex>()();
228    test_recursive_lock<boost::recursive_timed_mutex>()();
229}
230
231void test_recursive_timed_mutex()
232{
233    timed_test(&do_test_recursive_timed_mutex, 3);
234}
235
236boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
237{
238    boost::unit_test_framework::test_suite* test =
239        BOOST_TEST_SUITE("Boost.Threads: mutex test suite");
240
241    test->add(BOOST_TEST_CASE(&test_mutex));
242    test->add(BOOST_TEST_CASE(&test_try_mutex));
243    test->add(BOOST_TEST_CASE(&test_timed_mutex));
244    test->add(BOOST_TEST_CASE(&test_recursive_mutex));
245    test->add(BOOST_TEST_CASE(&test_recursive_try_mutex));
246    test->add(BOOST_TEST_CASE(&test_recursive_timed_mutex));
247
248    return test;
249}
Note: See TracBrowser for help on using the repository browser.