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