annotate DEPENDENCIES/generic/include/boost/thread/lock_algorithms.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 2 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 3 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 4 // (C) Copyright 2007 Anthony Williams
Chris@16 5 // (C) Copyright 2011-2012 Vicente J. Botet Escriba
Chris@16 6
Chris@16 7 #ifndef BOOST_THREAD_LOCK_ALGORITHMS_HPP
Chris@16 8 #define BOOST_THREAD_LOCK_ALGORITHMS_HPP
Chris@16 9
Chris@16 10 #include <boost/thread/detail/config.hpp>
Chris@16 11 #include <boost/thread/lock_types.hpp>
Chris@16 12 #include <boost/thread/lockable_traits.hpp>
Chris@16 13
Chris@16 14 #include <algorithm>
Chris@16 15 #include <iterator>
Chris@16 16
Chris@16 17 #include <boost/config/abi_prefix.hpp>
Chris@16 18
Chris@16 19 namespace boost
Chris@16 20 {
Chris@16 21 namespace detail
Chris@16 22 {
Chris@16 23 template <typename MutexType1, typename MutexType2>
Chris@16 24 unsigned try_lock_internal(MutexType1& m1, MutexType2& m2)
Chris@16 25 {
Chris@16 26 boost::unique_lock<MutexType1> l1(m1, boost::try_to_lock);
Chris@16 27 if (!l1)
Chris@16 28 {
Chris@16 29 return 1;
Chris@16 30 }
Chris@16 31 if (!m2.try_lock())
Chris@16 32 {
Chris@16 33 return 2;
Chris@16 34 }
Chris@16 35 l1.release();
Chris@16 36 return 0;
Chris@16 37 }
Chris@16 38
Chris@16 39 template <typename MutexType1, typename MutexType2, typename MutexType3>
Chris@16 40 unsigned try_lock_internal(MutexType1& m1, MutexType2& m2, MutexType3& m3)
Chris@16 41 {
Chris@16 42 boost::unique_lock<MutexType1> l1(m1, boost::try_to_lock);
Chris@16 43 if (!l1)
Chris@16 44 {
Chris@16 45 return 1;
Chris@16 46 }
Chris@16 47 if (unsigned const failed_lock=try_lock_internal(m2,m3))
Chris@16 48 {
Chris@16 49 return failed_lock + 1;
Chris@16 50 }
Chris@16 51 l1.release();
Chris@16 52 return 0;
Chris@16 53 }
Chris@16 54
Chris@16 55 template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4>
Chris@16 56 unsigned try_lock_internal(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4)
Chris@16 57 {
Chris@16 58 boost::unique_lock<MutexType1> l1(m1, boost::try_to_lock);
Chris@16 59 if (!l1)
Chris@16 60 {
Chris@16 61 return 1;
Chris@16 62 }
Chris@16 63 if (unsigned const failed_lock=try_lock_internal(m2,m3,m4))
Chris@16 64 {
Chris@16 65 return failed_lock + 1;
Chris@16 66 }
Chris@16 67 l1.release();
Chris@16 68 return 0;
Chris@16 69 }
Chris@16 70
Chris@16 71 template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4, typename MutexType5>
Chris@16 72 unsigned try_lock_internal(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4, MutexType5& m5)
Chris@16 73 {
Chris@16 74 boost::unique_lock<MutexType1> l1(m1, boost::try_to_lock);
Chris@16 75 if (!l1)
Chris@16 76 {
Chris@16 77 return 1;
Chris@16 78 }
Chris@16 79 if (unsigned const failed_lock=try_lock_internal(m2,m3,m4,m5))
Chris@16 80 {
Chris@16 81 return failed_lock + 1;
Chris@16 82 }
Chris@16 83 l1.release();
Chris@16 84 return 0;
Chris@16 85 }
Chris@16 86
Chris@16 87 template <typename MutexType1, typename MutexType2>
Chris@16 88 unsigned lock_helper(MutexType1& m1, MutexType2& m2)
Chris@16 89 {
Chris@16 90 boost::unique_lock<MutexType1> l1(m1);
Chris@16 91 if (!m2.try_lock())
Chris@16 92 {
Chris@16 93 return 1;
Chris@16 94 }
Chris@16 95 l1.release();
Chris@16 96 return 0;
Chris@16 97 }
Chris@16 98
Chris@16 99 template <typename MutexType1, typename MutexType2, typename MutexType3>
Chris@16 100 unsigned lock_helper(MutexType1& m1, MutexType2& m2, MutexType3& m3)
Chris@16 101 {
Chris@16 102 boost::unique_lock<MutexType1> l1(m1);
Chris@16 103 if (unsigned const failed_lock=try_lock_internal(m2,m3))
Chris@16 104 {
Chris@16 105 return failed_lock;
Chris@16 106 }
Chris@16 107 l1.release();
Chris@16 108 return 0;
Chris@16 109 }
Chris@16 110
Chris@16 111 template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4>
Chris@16 112 unsigned lock_helper(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4)
Chris@16 113 {
Chris@16 114 boost::unique_lock<MutexType1> l1(m1);
Chris@16 115 if (unsigned const failed_lock=try_lock_internal(m2,m3,m4))
Chris@16 116 {
Chris@16 117 return failed_lock;
Chris@16 118 }
Chris@16 119 l1.release();
Chris@16 120 return 0;
Chris@16 121 }
Chris@16 122
Chris@16 123 template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4, typename MutexType5>
Chris@16 124 unsigned lock_helper(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4, MutexType5& m5)
Chris@16 125 {
Chris@16 126 boost::unique_lock<MutexType1> l1(m1);
Chris@16 127 if (unsigned const failed_lock=try_lock_internal(m2,m3,m4,m5))
Chris@16 128 {
Chris@16 129 return failed_lock;
Chris@16 130 }
Chris@16 131 l1.release();
Chris@16 132 return 0;
Chris@16 133 }
Chris@16 134 }
Chris@16 135
Chris@16 136 namespace detail
Chris@16 137 {
Chris@16 138 template <bool x>
Chris@16 139 struct is_mutex_type_wrapper
Chris@16 140 {
Chris@16 141 };
Chris@16 142
Chris@16 143 template <typename MutexType1, typename MutexType2>
Chris@16 144 void lock_impl(MutexType1& m1, MutexType2& m2, is_mutex_type_wrapper<true> )
Chris@16 145 {
Chris@16 146 unsigned const lock_count = 2;
Chris@16 147 unsigned lock_first = 0;
Chris@16 148 for (;;)
Chris@16 149 {
Chris@16 150 switch (lock_first)
Chris@16 151 {
Chris@16 152 case 0:
Chris@16 153 lock_first = detail::lock_helper(m1, m2);
Chris@16 154 if (!lock_first) return;
Chris@16 155 break;
Chris@16 156 case 1:
Chris@16 157 lock_first = detail::lock_helper(m2, m1);
Chris@16 158 if (!lock_first) return;
Chris@16 159 lock_first = (lock_first + 1) % lock_count;
Chris@16 160 break;
Chris@16 161 }
Chris@16 162 }
Chris@16 163 }
Chris@16 164
Chris@16 165 template <typename Iterator>
Chris@16 166 void lock_impl(Iterator begin, Iterator end, is_mutex_type_wrapper<false> );
Chris@16 167 }
Chris@16 168
Chris@16 169 template <typename MutexType1, typename MutexType2>
Chris@16 170 void lock(MutexType1& m1, MutexType2& m2)
Chris@16 171 {
Chris@16 172 detail::lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
Chris@16 173 }
Chris@16 174
Chris@16 175 template <typename MutexType1, typename MutexType2>
Chris@16 176 void lock(const MutexType1& m1, MutexType2& m2)
Chris@16 177 {
Chris@16 178 detail::lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
Chris@16 179 }
Chris@16 180
Chris@16 181 template <typename MutexType1, typename MutexType2>
Chris@16 182 void lock(MutexType1& m1, const MutexType2& m2)
Chris@16 183 {
Chris@16 184 detail::lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
Chris@16 185 }
Chris@16 186
Chris@16 187 template <typename MutexType1, typename MutexType2>
Chris@16 188 void lock(const MutexType1& m1, const MutexType2& m2)
Chris@16 189 {
Chris@16 190 detail::lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
Chris@16 191 }
Chris@16 192
Chris@16 193 template <typename MutexType1, typename MutexType2, typename MutexType3>
Chris@16 194 void lock(MutexType1& m1, MutexType2& m2, MutexType3& m3)
Chris@16 195 {
Chris@16 196 unsigned const lock_count = 3;
Chris@16 197 unsigned lock_first = 0;
Chris@16 198 for (;;)
Chris@16 199 {
Chris@16 200 switch (lock_first)
Chris@16 201 {
Chris@16 202 case 0:
Chris@16 203 lock_first = detail::lock_helper(m1, m2, m3);
Chris@16 204 if (!lock_first) return;
Chris@16 205 break;
Chris@16 206 case 1:
Chris@16 207 lock_first = detail::lock_helper(m2, m3, m1);
Chris@16 208 if (!lock_first) return;
Chris@16 209 lock_first = (lock_first + 1) % lock_count;
Chris@16 210 break;
Chris@16 211 case 2:
Chris@16 212 lock_first = detail::lock_helper(m3, m1, m2);
Chris@16 213 if (!lock_first) return;
Chris@16 214 lock_first = (lock_first + 2) % lock_count;
Chris@16 215 break;
Chris@16 216 }
Chris@16 217 }
Chris@16 218 }
Chris@16 219
Chris@16 220 template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4>
Chris@16 221 void lock(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4)
Chris@16 222 {
Chris@16 223 unsigned const lock_count = 4;
Chris@16 224 unsigned lock_first = 0;
Chris@16 225 for (;;)
Chris@16 226 {
Chris@16 227 switch (lock_first)
Chris@16 228 {
Chris@16 229 case 0:
Chris@16 230 lock_first = detail::lock_helper(m1, m2, m3, m4);
Chris@16 231 if (!lock_first) return;
Chris@16 232 break;
Chris@16 233 case 1:
Chris@16 234 lock_first = detail::lock_helper(m2, m3, m4, m1);
Chris@16 235 if (!lock_first) return;
Chris@16 236 lock_first = (lock_first + 1) % lock_count;
Chris@16 237 break;
Chris@16 238 case 2:
Chris@16 239 lock_first = detail::lock_helper(m3, m4, m1, m2);
Chris@16 240 if (!lock_first) return;
Chris@16 241 lock_first = (lock_first + 2) % lock_count;
Chris@16 242 break;
Chris@16 243 case 3:
Chris@16 244 lock_first = detail::lock_helper(m4, m1, m2, m3);
Chris@16 245 if (!lock_first) return;
Chris@16 246 lock_first = (lock_first + 3) % lock_count;
Chris@16 247 break;
Chris@16 248 }
Chris@16 249 }
Chris@16 250 }
Chris@16 251
Chris@16 252 template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4, typename MutexType5>
Chris@16 253 void lock(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4, MutexType5& m5)
Chris@16 254 {
Chris@16 255 unsigned const lock_count = 5;
Chris@16 256 unsigned lock_first = 0;
Chris@16 257 for (;;)
Chris@16 258 {
Chris@16 259 switch (lock_first)
Chris@16 260 {
Chris@16 261 case 0:
Chris@16 262 lock_first = detail::lock_helper(m1, m2, m3, m4, m5);
Chris@16 263 if (!lock_first) return;
Chris@16 264 break;
Chris@16 265 case 1:
Chris@16 266 lock_first = detail::lock_helper(m2, m3, m4, m5, m1);
Chris@16 267 if (!lock_first) return;
Chris@16 268 lock_first = (lock_first + 1) % lock_count;
Chris@16 269 break;
Chris@16 270 case 2:
Chris@16 271 lock_first = detail::lock_helper(m3, m4, m5, m1, m2);
Chris@16 272 if (!lock_first) return;
Chris@16 273 lock_first = (lock_first + 2) % lock_count;
Chris@16 274 break;
Chris@16 275 case 3:
Chris@16 276 lock_first = detail::lock_helper(m4, m5, m1, m2, m3);
Chris@16 277 if (!lock_first) return;
Chris@16 278 lock_first = (lock_first + 3) % lock_count;
Chris@16 279 break;
Chris@16 280 case 4:
Chris@16 281 lock_first = detail::lock_helper(m5, m1, m2, m3, m4);
Chris@16 282 if (!lock_first) return;
Chris@16 283 lock_first = (lock_first + 4) % lock_count;
Chris@16 284 break;
Chris@16 285 }
Chris@16 286 }
Chris@16 287 }
Chris@16 288
Chris@16 289 namespace detail
Chris@16 290 {
Chris@16 291 template <typename Mutex, bool x = is_mutex_type<Mutex>::value>
Chris@16 292 struct try_lock_impl_return
Chris@16 293 {
Chris@16 294 typedef int type;
Chris@16 295 };
Chris@16 296
Chris@16 297 template <typename Iterator>
Chris@16 298 struct try_lock_impl_return<Iterator, false>
Chris@16 299 {
Chris@16 300 typedef Iterator type;
Chris@16 301 };
Chris@16 302
Chris@16 303 template <typename MutexType1, typename MutexType2>
Chris@16 304 int try_lock_impl(MutexType1& m1, MutexType2& m2, is_mutex_type_wrapper<true> )
Chris@16 305 {
Chris@16 306 return ((int) detail::try_lock_internal(m1, m2)) - 1;
Chris@16 307 }
Chris@16 308
Chris@16 309 template <typename Iterator>
Chris@16 310 Iterator try_lock_impl(Iterator begin, Iterator end, is_mutex_type_wrapper<false> );
Chris@16 311 }
Chris@16 312
Chris@16 313 template <typename MutexType1, typename MutexType2>
Chris@16 314 typename detail::try_lock_impl_return<MutexType1>::type try_lock(MutexType1& m1, MutexType2& m2)
Chris@16 315 {
Chris@16 316 return detail::try_lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
Chris@16 317 }
Chris@16 318
Chris@16 319 template <typename MutexType1, typename MutexType2>
Chris@16 320 typename detail::try_lock_impl_return<MutexType1>::type try_lock(const MutexType1& m1, MutexType2& m2)
Chris@16 321 {
Chris@16 322 return detail::try_lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
Chris@16 323 }
Chris@16 324
Chris@16 325 template <typename MutexType1, typename MutexType2>
Chris@16 326 typename detail::try_lock_impl_return<MutexType1>::type try_lock(MutexType1& m1, const MutexType2& m2)
Chris@16 327 {
Chris@16 328 return detail::try_lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
Chris@16 329 }
Chris@16 330
Chris@16 331 template <typename MutexType1, typename MutexType2>
Chris@16 332 typename detail::try_lock_impl_return<MutexType1>::type try_lock(const MutexType1& m1, const MutexType2& m2)
Chris@16 333 {
Chris@16 334 return detail::try_lock_impl(m1, m2, detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>());
Chris@16 335 }
Chris@16 336
Chris@16 337 template <typename MutexType1, typename MutexType2, typename MutexType3>
Chris@16 338 int try_lock(MutexType1& m1, MutexType2& m2, MutexType3& m3)
Chris@16 339 {
Chris@16 340 return ((int) detail::try_lock_internal(m1, m2, m3)) - 1;
Chris@16 341 }
Chris@16 342
Chris@16 343 template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4>
Chris@16 344 int try_lock(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4)
Chris@16 345 {
Chris@16 346 return ((int) detail::try_lock_internal(m1, m2, m3, m4)) - 1;
Chris@16 347 }
Chris@16 348
Chris@16 349 template <typename MutexType1, typename MutexType2, typename MutexType3, typename MutexType4, typename MutexType5>
Chris@16 350 int try_lock(MutexType1& m1, MutexType2& m2, MutexType3& m3, MutexType4& m4, MutexType5& m5)
Chris@16 351 {
Chris@16 352 return ((int) detail::try_lock_internal(m1, m2, m3, m4, m5)) - 1;
Chris@16 353 }
Chris@16 354
Chris@16 355 namespace detail
Chris@16 356 {
Chris@16 357 template <typename Iterator>
Chris@16 358 struct range_lock_guard
Chris@16 359 {
Chris@16 360 Iterator begin;
Chris@16 361 Iterator end;
Chris@16 362
Chris@16 363 range_lock_guard(Iterator begin_, Iterator end_) :
Chris@16 364 begin(begin_), end(end_)
Chris@16 365 {
Chris@16 366 boost::lock(begin, end);
Chris@16 367 }
Chris@16 368
Chris@16 369 void release()
Chris@16 370 {
Chris@16 371 begin = end;
Chris@16 372 }
Chris@16 373
Chris@16 374 ~range_lock_guard()
Chris@16 375 {
Chris@16 376 for (; begin != end; ++begin)
Chris@16 377 {
Chris@16 378 begin->unlock();
Chris@16 379 }
Chris@16 380 }
Chris@16 381 };
Chris@16 382
Chris@16 383 template <typename Iterator>
Chris@16 384 Iterator try_lock_impl(Iterator begin, Iterator end, is_mutex_type_wrapper<false> )
Chris@16 385
Chris@16 386 {
Chris@16 387 if (begin == end)
Chris@16 388 {
Chris@16 389 return end;
Chris@16 390 }
Chris@16 391 typedef typename std::iterator_traits<Iterator>::value_type lock_type;
Chris@16 392 unique_lock<lock_type> guard(*begin, try_to_lock);
Chris@16 393
Chris@16 394 if (!guard.owns_lock())
Chris@16 395 {
Chris@16 396 return begin;
Chris@16 397 }
Chris@16 398 Iterator const failed = boost::try_lock(++begin, end);
Chris@16 399 if (failed == end)
Chris@16 400 {
Chris@16 401 guard.release();
Chris@16 402 }
Chris@16 403
Chris@16 404 return failed;
Chris@16 405 }
Chris@16 406 }
Chris@16 407
Chris@16 408 namespace detail
Chris@16 409 {
Chris@16 410 template <typename Iterator>
Chris@16 411 void lock_impl(Iterator begin, Iterator end, is_mutex_type_wrapper<false> )
Chris@16 412 {
Chris@16 413 typedef typename std::iterator_traits<Iterator>::value_type lock_type;
Chris@16 414
Chris@16 415 if (begin == end)
Chris@16 416 {
Chris@16 417 return;
Chris@16 418 }
Chris@16 419 bool start_with_begin = true;
Chris@16 420 Iterator second = begin;
Chris@16 421 ++second;
Chris@16 422 Iterator next = second;
Chris@16 423
Chris@16 424 for (;;)
Chris@16 425 {
Chris@16 426 unique_lock<lock_type> begin_lock(*begin, defer_lock);
Chris@16 427 if (start_with_begin)
Chris@16 428 {
Chris@16 429 begin_lock.lock();
Chris@16 430 Iterator const failed_lock = boost::try_lock(next, end);
Chris@16 431 if (failed_lock == end)
Chris@16 432 {
Chris@16 433 begin_lock.release();
Chris@16 434 return;
Chris@16 435 }
Chris@16 436 start_with_begin = false;
Chris@16 437 next = failed_lock;
Chris@16 438 }
Chris@16 439 else
Chris@16 440 {
Chris@16 441 detail::range_lock_guard<Iterator> guard(next, end);
Chris@16 442 if (begin_lock.try_lock())
Chris@16 443 {
Chris@16 444 Iterator const failed_lock = boost::try_lock(second, next);
Chris@16 445 if (failed_lock == next)
Chris@16 446 {
Chris@16 447 begin_lock.release();
Chris@16 448 guard.release();
Chris@16 449 return;
Chris@16 450 }
Chris@16 451 start_with_begin = false;
Chris@16 452 next = failed_lock;
Chris@16 453 }
Chris@16 454 else
Chris@16 455 {
Chris@16 456 start_with_begin = true;
Chris@16 457 next = second;
Chris@16 458 }
Chris@16 459 }
Chris@16 460 }
Chris@16 461 }
Chris@16 462
Chris@16 463 }
Chris@16 464
Chris@16 465 }
Chris@16 466 #include <boost/config/abi_suffix.hpp>
Chris@16 467
Chris@16 468 #endif