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
11#include "mutex_test_template.hpp"
12#include "sharable_mutex_test_template.hpp"
13#include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
14#include <boost/interprocess/sync/scoped_lock.hpp>
15#include <boost/interprocess/sync/sharable_lock.hpp>
16#include <boost/interprocess/sync/upgradable_lock.hpp>
17#include "util.hpp"
18
19int main ()
20{
21 using namespace boost::interprocess;
22
23 test::test_all_lock<interprocess_upgradable_mutex>();
24 test::test_all_mutex<interprocess_upgradable_mutex>();
25 test::test_all_sharable_mutex<interprocess_upgradable_mutex>();
26
27 //Test lock transition
28 {
29 typedef interprocess_upgradable_mutex Mutex;
30 Mutex mut;
31 Mutex mut2;
32
33 //Conversions to scoped_lock
34 {
35 scoped_lock<Mutex> lock(mut);
36 scoped_lock<Mutex> e_lock(boost::move(t&: lock));
37 lock.swap(other&: e_lock);
38 }
39 {
40 scoped_lock<Mutex> lock(mut);
41 scoped_lock<Mutex> e_lock(mut2);
42 e_lock = boost::move(t&: lock);
43 }
44 {
45 upgradable_lock<Mutex> u_lock(mut);
46 //This calls unlock_upgradable_and_lock()
47 scoped_lock<Mutex> e_lock(boost::move(t&: u_lock));
48 }
49 {
50 upgradable_lock<Mutex> u_lock(mut);
51 //This calls unlock_upgradable_and_lock()
52 scoped_lock<Mutex> e_lock(mut2);
53 scoped_lock<Mutex> moved(boost::move(t&: u_lock));
54 e_lock = boost::move(t&: moved);
55 }
56 {
57 upgradable_lock<Mutex> u_lock(mut);
58 //This calls try_unlock_upgradable_and_lock()
59 scoped_lock<Mutex> e_lock(boost::move(t&: u_lock), try_to_lock);
60 }
61 {
62 upgradable_lock<Mutex> u_lock(mut);
63 //This calls try_unlock_upgradable_and_lock()
64 scoped_lock<Mutex> e_lock(mut2);
65 scoped_lock<Mutex> moved(boost::move(t&: u_lock), try_to_lock);
66 e_lock = boost::move(t&: moved);
67 }
68 {
69 upgradable_lock<Mutex> u_lock(mut);
70 //This calls timed_unlock_upgradable_and_lock()
71 scoped_lock<Mutex> e_lock(boost::move(t&: u_lock), test::boost_systemclock_delay_ms(msecs: 10));
72 }
73 {
74 upgradable_lock<Mutex> u_lock(mut);
75 //This calls timed_unlock_upgradable_and_lock()
76 scoped_lock<Mutex> e_lock(mut2);
77 scoped_lock<Mutex> moved(boost::move(t&: u_lock), test::ptime_delay_ms(msecs: 10));
78 e_lock = boost::move(t&: moved);
79 }
80 {
81 sharable_lock<Mutex> s_lock(mut);
82 //This calls try_unlock_sharable_and_lock()
83 scoped_lock<Mutex> e_lock(boost::move(t&: s_lock), try_to_lock);
84 }
85 {
86 sharable_lock<Mutex> s_lock(mut);
87 //This calls try_unlock_sharable_and_lock()
88 scoped_lock<Mutex> e_lock(mut2);
89 scoped_lock<Mutex> moved(boost::move(t&: s_lock), try_to_lock);
90 e_lock = boost::move(t&: moved);
91 }
92 //Conversions to upgradable_lock
93 {
94 upgradable_lock<Mutex> lock(mut);
95 upgradable_lock<Mutex> u_lock(boost::move(t&: lock));
96 lock.swap(other&: u_lock);
97 }
98 {
99 upgradable_lock<Mutex> lock(mut);
100 upgradable_lock<Mutex> u_lock(mut2);
101 upgradable_lock<Mutex> moved(boost::move(t&: lock));
102 u_lock = boost::move(t&: moved);
103 }
104 {
105 sharable_lock<Mutex> s_lock(mut);
106 //This calls unlock_sharable_and_lock_upgradable()
107 upgradable_lock<Mutex> u_lock(boost::move(t&: s_lock), try_to_lock);
108 }
109 {
110 sharable_lock<Mutex> s_lock(mut);
111 //This calls unlock_sharable_and_lock_upgradable()
112 upgradable_lock<Mutex> u_lock(mut2);
113 upgradable_lock<Mutex> moved(boost::move(t&: s_lock), try_to_lock);
114 u_lock = boost::move(t&: moved);
115 }
116 {
117 scoped_lock<Mutex> e_lock(mut);
118 //This calls unlock_and_lock_upgradable()
119 upgradable_lock<Mutex> u_lock(boost::move(t&: e_lock));
120 }
121 {
122 scoped_lock<Mutex> e_lock(mut);
123 //This calls unlock_and_lock_upgradable()
124 upgradable_lock<Mutex> u_lock(mut2);
125 upgradable_lock<Mutex> moved(boost::move(t&: e_lock));
126 u_lock = boost::move(t&: moved);
127 }
128 //Conversions to sharable_lock
129 {
130 sharable_lock<Mutex> lock(mut);
131 sharable_lock<Mutex> s_lock(boost::move(t&: lock));
132 lock.swap(other&: s_lock);
133 }
134 {
135 sharable_lock<Mutex> lock(mut);
136 sharable_lock<Mutex> s_lock(mut2);
137 sharable_lock<Mutex> moved(boost::move(t&: lock));
138 s_lock = boost::move(t&: moved);
139 }
140 {
141 upgradable_lock<Mutex> u_lock(mut);
142 //This calls unlock_upgradable_and_lock_sharable()
143 sharable_lock<Mutex> s_lock(boost::move(t&: u_lock));
144 }
145 {
146 upgradable_lock<Mutex> u_lock(mut);
147 //This calls unlock_upgradable_and_lock_sharable()
148 sharable_lock<Mutex> s_lock(mut2);
149 sharable_lock<Mutex> moved(boost::move(t&: u_lock));
150 s_lock = boost::move(t&: moved);
151 }
152 {
153 scoped_lock<Mutex> e_lock(mut);
154 //This calls unlock_and_lock_sharable()
155 sharable_lock<Mutex> s_lock(boost::move(t&: e_lock));
156 }
157 {
158 scoped_lock<Mutex> e_lock(mut);
159 //This calls unlock_and_lock_sharable()
160 sharable_lock<Mutex> s_lock(mut2);
161 sharable_lock<Mutex> moved(boost::move(t&: e_lock));
162 s_lock = boost::move(t&: moved);
163 }
164 }
165
166 return 0;
167}
168

source code of boost/libs/interprocess/test/upgradable_mutex_test.cpp