Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/thread/test/test_mutex.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

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